Add another test for `for`. There's another one I want to add too.
Chris Pressey
4 years ago
1771 | 1771 | |
1772 | 1772 | Basic "open-faced for" loop. We'll start with the "upto" variant. |
1773 | 1773 | |
1774 | In a "for" loop, we know the exact range the loop variable takes on. | |
1774 | #### upward-counting variant | |
1775 | ||
1776 | Even though we do not give the starting value in the "for" construct, | |
1777 | we know the exact range the loop variable takes on. | |
1775 | 1778 | |
1776 | 1779 | | byte table[16] tab |
1777 | 1780 | | |
1803 | 1806 | | } |
1804 | 1807 | | } |
1805 | 1808 | ? UnmeaningfulReadError |
1809 | ||
1810 | Because routines current do not express range constraints, It may not do to take the loop variable as an input. (?) | |
1811 | ||
1812 | | byte table[16] tab | |
1813 | | | |
1814 | | define foo routine | |
1815 | | inputs tab, x | |
1816 | | trashes a, x, c, z, v, n { | |
1817 | | for x up to 15 { | |
1818 | | ld a, 0 | |
1819 | | } | |
1820 | | } | |
1821 | ? RangeExceededError | |
1806 | 1822 | |
1807 | 1823 | You cannot modify the loop variable in a "for" loop. |
1808 | 1824 | |
1874 | 1890 | | } |
1875 | 1891 | ? RangeExceededError |
1876 | 1892 | |
1893 | You can initialize something inside the loop that was uninitialized outside. | |
1894 | ||
1895 | | define main routine | |
1896 | | outputs x, y, n, z | |
1897 | | trashes c | |
1898 | | { | |
1899 | | ld x, 0 | |
1900 | | for x up to 15 { | |
1901 | | ld y, 15 | |
1902 | | } | |
1903 | | } | |
1904 | = ok | |
1905 | ||
1906 | But you can't UNinitialize something at the end of the loop that you need | |
1907 | initialized at the start of that loop. | |
1908 | ||
1909 | | define foo routine | |
1910 | | trashes y | |
1911 | | { | |
1912 | | } | |
1913 | | | |
1914 | | define main routine | |
1915 | | outputs x, y, n, z | |
1916 | | trashes c | |
1917 | | { | |
1918 | | ld x, 0 | |
1919 | | ld y, 15 | |
1920 | | for x up to 15 { | |
1921 | | inc y | |
1922 | | call foo | |
1923 | | } | |
1924 | | } | |
1925 | ? UnmeaningfulReadError: y | |
1926 | ||
1927 | #### downward-counting variant | |
1928 | ||
1877 | 1929 | In a "for" loop (downward-counting variant), we know the exact range the loop variable takes on. |
1878 | 1930 | |
1879 | 1931 | | byte table[16] tab |
1930 | 1982 | | } |
1931 | 1983 | | } |
1932 | 1984 | ? RangeExceededError |
1933 | ||
1934 | You can initialize something inside the loop that was uninitialized outside. | |
1935 | ||
1936 | | define main routine | |
1937 | | outputs x, y, n, z | |
1938 | | trashes c | |
1939 | | { | |
1940 | | ld x, 0 | |
1941 | | for x up to 15 { | |
1942 | | ld y, 15 | |
1943 | | } | |
1944 | | } | |
1945 | = ok | |
1946 | ||
1947 | But you can't UNinitialize something at the end of the loop that you need | |
1948 | initialized at the start of that loop. | |
1949 | ||
1950 | | define foo routine | |
1951 | | trashes y | |
1952 | | { | |
1953 | | } | |
1954 | | | |
1955 | | define main routine | |
1956 | | outputs x, y, n, z | |
1957 | | trashes c | |
1958 | | { | |
1959 | | ld x, 0 | |
1960 | | ld y, 15 | |
1961 | | for x up to 15 { | |
1962 | | inc y | |
1963 | | call foo | |
1964 | | } | |
1965 | | } | |
1966 | ? UnmeaningfulReadError: y | |
1967 | 1985 | |
1968 | 1986 | ### save ### |
1969 | 1987 |