git @ Cat's Eye Technologies SixtyPical / dd86015
Add another test for `for`. There's another one I want to add too. Chris Pressey 4 years ago
1 changed file(s) with 53 addition(s) and 35 deletion(s). Raw diff Collapse all Expand all
17711771
17721772 Basic "open-faced for" loop. We'll start with the "upto" variant.
17731773
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.
17751778
17761779 | byte table[16] tab
17771780 |
18031806 | }
18041807 | }
18051808 ? 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
18061822
18071823 You cannot modify the loop variable in a "for" loop.
18081824
18741890 | }
18751891 ? RangeExceededError
18761892
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
18771929 In a "for" loop (downward-counting variant), we know the exact range the loop variable takes on.
18781930
18791931 | byte table[16] tab
19301982 | }
19311983 | }
19321984 ? 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
19671985
19681986 ### save ###
19691987