diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-05-23 19:27:06 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-05-23 19:27:06 -0400 |
commit | 1194d23d65d424e760fa972aa2decc1650497a1b (patch) | |
tree | a3f143c9020b3f4f4de096930e30b4e798cb6194 /tests | |
parent | 72e2940d78fc923f3d9af4ef333a40a0b397b065 (diff) |
started fixing tests; found bug
Diffstat (limited to 'tests')
-rw-r--r-- | tests/bf.toc | 148 | ||||
-rw-r--r-- | tests/control_flow.toc | 2 | ||||
-rw-r--r-- | tests/std/io.toc | 8 | ||||
-rw-r--r-- | tests/std/mem.toc | 4 | ||||
-rw-r--r-- | tests/types.toc | 6 |
5 files changed, 91 insertions, 77 deletions
diff --git a/tests/bf.toc b/tests/bf.toc index 83fb291..3d4ed36 100644 --- a/tests/bf.toc +++ b/tests/bf.toc @@ -16,51 +16,51 @@ extern void exit(int); contents_len : int; buffer : [1024]char; while #C("fgets(buffer, 1024, fp)") { - buffer_len : int; - while buffer[buffer_len] { - buffer_len = buffer_len + 1; - } - if contents_sz < contents_len + buffer_len { - old_contents := contents; - contents_sz = 2*contents_sz + 1024; - contents = news(char, contents_sz); - i := 0; - while i < contents_len { - contents[i] = old_contents[i]; - i = i + 1; - } - dels(old_contents); - } - i := 0; - while i < buffer_len { - contents[contents_len] = buffer[i]; - contents_len = contents_len + 1; - i = i + 1; - } + buffer_len : int; + while buffer[buffer_len] { + buffer_len = buffer_len + 1; + } + if contents_sz < contents_len + buffer_len { + old_contents := contents; + contents_sz = 2*contents_sz + 1024; + contents = news(char, contents_sz); + i := 0; + while i < contents_len { + contents[i] = old_contents[i]; + i = i + 1; + } + dels(old_contents); + } + i := 0; + while i < buffer_len { + contents[contents_len] = buffer[i]; + contents_len = contents_len + 1; + i = i + 1; + } } contents[contents_len] = 0 as char; - contents + return contents; }; runfile ::= fn(filename: []char) { - #C("extern int putchar(int c);"); - code := readfile(filename); - tape_sz := 3; - tape := news(int, tape_sz); - ptr := tape_sz / 2; - i := 0; - while code[i] { - // puti(ptr); - // puti(tape_sz); - if code[i] == '+' { - tape[ptr] = tape[ptr]+1; - } elif code[i] == '-' { - tape[ptr] = tape[ptr]-1; - } elif code[i] == '>' { - ptr = ptr + 1; - if ptr >= tape_sz { - // extend to the right - newtape := news(int, 2*tape_sz); + #C("extern int putchar(int c);"); + code := readfile(filename); + tape_sz := 3; + tape := news(int, tape_sz); + ptr := tape_sz / 2; + i := 0; + while code[i] { + // puti(ptr); + // puti(tape_sz); + if code[i] == '+' { + tape[ptr] = tape[ptr]+1; + } elif code[i] == '-' { + tape[ptr] = tape[ptr]-1; + } elif code[i] == '>' { + ptr = ptr + 1; + if ptr >= tape_sz { + // extend to the right + newtape := news(int, 2*tape_sz); j := 0; while j < tape_sz { newtape[j] = tape[j]; @@ -69,12 +69,12 @@ runfile ::= fn(filename: []char) { tape_sz = tape_sz * 2; dels(tape); tape = newtape; - } - } elif code[i] == '<' { - ptr = ptr - 1; - if ptr < 0 { - // extend to the left - newtape := news(int, 2*tape_sz); + } + } elif code[i] == '<' { + ptr = ptr - 1; + if ptr < 0 { + // extend to the left + newtape := news(int, 2*tape_sz); j := 0; while j < tape_sz { newtape[j+tape_sz] = tape[j]; @@ -84,36 +84,44 @@ runfile ::= fn(filename: []char) { dels(tape); tape = newtape; ptr = ptr + tape_sz; - } - } elif code[i] == '[' { - if !tape[ptr] { - // jump to matching ] + } + } elif code[i] == '[' { + if !tape[ptr] { + // jump to matching ] level := 0; while level >= 0 { - i = i + 1; - level = level + (if code[i] == '[' { 1 } elif code[i] == ']' { -1 } else {0}); + i = i + 1; + if code[i] == '[' { + level += 1; + } elif code[i] == ']' { + level -= 1; + } } - } - } elif code[i] == ']' { - if tape[ptr] { - // jump to matching [ + } + } elif code[i] == ']' { + if tape[ptr] { + // jump to matching [ level := 0; while level <= 0 { - i = i - 1; - level = level + (if code[i] == '[' { 1 } elif code[i] == ']' { -1 } else {0}); + i = i - 1; + if code[i] == '[' { + level += 1; + } elif code[i] == ']' { + level -= 1; + } } - } - } elif code[i] == '.' { - c := tape[ptr] as char; - #C("putchar(c);"); - } elif code[i] == ',' { - // Input doesn't really work, because you - // need to send an EOF to end the program. - } - i = i + 1; - } - dels(tape); - dels(code); + } + } elif code[i] == '.' { + c := tape[ptr] as char; + #C("putchar(c);"); + } elif code[i] == ',' { + // Input doesn't really work, because you + // need to send an EOF to end the program. + } + i = i + 1; + } + dels(tape); + dels(code); }; main ::= fn() { diff --git a/tests/control_flow.toc b/tests/control_flow.toc index f54a8bc..012978d 100644 --- a/tests/control_flow.toc +++ b/tests/control_flow.toc @@ -21,7 +21,7 @@ f ::= fn() int { return total; } } - 0 + return 0; } main ::= fn() { diff --git a/tests/std/io.toc b/tests/std/io.toc index 2b9e999..c274631 100644 --- a/tests/std/io.toc +++ b/tests/std/io.toc @@ -32,7 +32,13 @@ writei ::= fn(x: int) { if x == 0 { toc_putchar('0'); } else { - abs ::= fn(x: int) int { if x < 0 { -x } else { x } }; + abs ::= fn(x: int) int { + if x < 0 { + return -x; + } else { + return x; + } + }; scan_digit := 1000000000000000000; started := false; while scan_digit > 0 { diff --git a/tests/std/mem.toc b/tests/std/mem.toc index ef4e638..9ea9b12 100644 --- a/tests/std/mem.toc +++ b/tests/std/mem.toc @@ -5,14 +5,14 @@ calloc ::= #foreign("calloc", base.libc) fn(#C size_t, #C size_t) #C &"void"; free ::= #foreign("free", base.libc) fn(#C &"void"); new ::= fn(t :: Type) &t { - calloc(1, (sizeof t) as #C size_t) + return calloc(1, (sizeof t) as #C size_t); } news ::= fn(t :: Type, n : int) []t { s: []t; s.data = calloc(n as #C size_t, (sizeof t) as #C size_t); s.len = n; - s + return s; } // TODO(eventually): use type information to make this just one function diff --git a/tests/types.toc b/tests/types.toc index 7e56f91..25c38d5 100644 --- a/tests/types.toc +++ b/tests/types.toc @@ -2,13 +2,13 @@ #include "std/mem.toc"; z ::= nms { - Foo ::= struct(f ::= fn() int { 7 }) { + Foo ::= struct(f ::= fn() int { return 7; }) { x: int; } - Bar ::= fn() &(struct { x, y: int; f ::= fn() int { 13 } } ) { + Bar ::= fn() &(struct { x, y: int; f ::= fn() int { return 13; } } ) { x : u64; - &x as &void + return &x as &void; } |