diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-02-04 18:16:46 -0500 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-02-04 18:16:46 -0500 |
commit | 6b8e777c98b95e6952337c0671e40ff8f962f7dc (patch) | |
tree | 6460856493e40e0e6b4d844673fc5e0fdd601e61 /tests/nms | |
parent | fa91b896505f2c35e35ddcb7b1fb61bf0eb6f815 (diff) |
new test: nms
Diffstat (limited to 'tests/nms')
-rw-r--r-- | tests/nms/expected | 4 | ||||
-rw-r--r-- | tests/nms/got | 4 | ||||
-rw-r--r-- | tests/nms/io.toc | 53 | ||||
-rw-r--r-- | tests/nms/nms.toc | 20 | ||||
-rwxr-xr-x | tests/nms/test.sh | 3 |
5 files changed, 84 insertions, 0 deletions
diff --git a/tests/nms/expected b/tests/nms/expected new file mode 100644 index 0000000..d26c123 --- /dev/null +++ b/tests/nms/expected @@ -0,0 +1,4 @@ +Hello! +2 +3 +5 diff --git a/tests/nms/got b/tests/nms/got new file mode 100644 index 0000000..d26c123 --- /dev/null +++ b/tests/nms/got @@ -0,0 +1,4 @@ +Hello! +2 +3 +5 diff --git a/tests/nms/io.toc b/tests/nms/io.toc new file mode 100644 index 0000000..51cfd76 --- /dev/null +++ b/tests/nms/io.toc @@ -0,0 +1,53 @@ +get_type_with_size ::= fn(size :: i64) Type { + if size == 1 { i8 } + elif size == 2 { i16 } + elif size == 4 { i32 } + elif size == 8 { i64 } + else { f32 } +}; + +get_utype_with_size ::= fn(size :: i64) Type { + if size == 1 { u8 } + elif size == 2 { u16 } + elif size == 4 { u32 } + elif size == 8 { u64 } + else { f32 } +}; + +c_int ::= get_type_with_size(#builtin("sizeof int")); +c_size_t ::= get_utype_with_size(#builtin("sizeof size_t")); + +c_putchar :: fn(c_int) c_int = #foreign "putchar", "libc.so.6"; +toc_putchar ::= fn(x: char) { + c_putchar(x as c_int); +}; + +c_fwrite :: fn(&u8, c_size_t, c_size_t, &u8) = #foreign "fwrite", "libc.so.6"; + +stdout_fwrite ::= fn(data: &u8, size: u64, nmemb: u64) { + c_fwrite(data, size as c_size_t, nmemb as c_size_t, #builtin("stdout")); +}; + +puts ::= fn(x: []char) { + stdout_fwrite(&x[0] as &u8, 1, x.len as u64); + toc_putchar('\n'); +}; + +puti ::= fn(x: int) { + if x < 0 { + toc_putchar('-'); + // NOTE: don't do x = -x; here to make sure I64_MIN works + } + abs ::= fn(x: int) int { if x < 0 { -x } else { x } }; + scan_digit := 1000000000000000000; + started := false; + while scan_digit > 0 { + digit := abs((x / scan_digit) % 10); + if digit > 0 { started = true; } + if started { + toc_putchar((('0' as int) + digit) as char); + } + scan_digit /= 10; + } + toc_putchar('\n'); +};
\ No newline at end of file diff --git a/tests/nms/nms.toc b/tests/nms/nms.toc new file mode 100644 index 0000000..413da8d --- /dev/null +++ b/tests/nms/nms.toc @@ -0,0 +1,20 @@ +io ::= nms { + #include "io.toc"; +}; + +n ::= nms { + x := 1; + counter ::= fn() int { x += 1; x }; +}; + + +main ::= fn() { + a := n.counter(); + b := n.counter(); + n.counter(); + c := n.counter(); + io.puts("Hello!"); + io.puti(a); + io.puti(b); + io.puti(c); +}; diff --git a/tests/nms/test.sh b/tests/nms/test.sh new file mode 100755 index 0000000..8f1a481 --- /dev/null +++ b/tests/nms/test.sh @@ -0,0 +1,3 @@ +#!/bin/sh +./nms.bin > got || exit 1 +diff got expected || exit 1 |