From 1184d39f79a341bfa6c1642dc1ae246113186155 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Wed, 22 Jan 2020 17:02:14 -0500 Subject: new std/io --- pkg.sh | 2 +- std/io.toc | 14 +++++--------- test.toc | 46 ++-------------------------------------------- 3 files changed, 8 insertions(+), 54 deletions(-) diff --git a/pkg.sh b/pkg.sh index 1a397b5..d0fc503 100755 --- a/pkg.sh +++ b/pkg.sh @@ -15,5 +15,5 @@ for thing in $std_things; do done cd .. $VALGRIND ./toc test.toc || exit 1 -$CC out.c std/*.c || exit 1 +$CC out.c std/*.c -Wno-builtin-declaration-mismatch || exit 1 ./a.out diff --git a/std/io.toc b/std/io.toc index 68e9744..ea45ce9 100644 --- a/std/io.toc +++ b/std/io.toc @@ -1,14 +1,10 @@ pkg "io"; -#export puti ::= fn(x: int) { - #C("printf(\"%ld\\n\", (long)x)"); -}; - -#export putf ::= fn(x: float) { - #C("printf(\"%f\\n\", (double)x)"); -}; +c_putchar :: fn(i32) = #foreign "putchar", "libc.so.6"; #export puts ::= fn(x: []char) { - #C("fwrite(x.data, 1, x.n, stdout)"); - #C("printf(\"\\n\")"); + each c := x { + c_putchar(c as i32); + }; + c_putchar('\n' as i32); }; \ No newline at end of file diff --git a/test.toc b/test.toc index 4b21d20..eb602ca 100644 --- a/test.toc +++ b/test.toc @@ -1,46 +1,4 @@ -import ::= fn(x :: []char) &Package { - p ::= pkg x; - p_ptr ::= new(Package); - *p_ptr = p; - p_ptr -}; - -cputs :: fn(&char) i32 = #foreign "puts", "libc.so.6"; - -tocputs ::= fn(s: []char) i32 { - cstr := new(char, s.len + 1); - each i := 0..s.len-1 { - cstr[i] = s[i]; - } - cstr[s.len] = '\0'; - ret := cputs(&cstr[0]); - del(cstr); - ret -}; - -foo ::= fn() i32 { - tocputs("Hi"); - tocputs("Hello"); - tocputs("Hey") -}; - -malloc :: fn(u64) &u8 = #foreign "malloc", "libc.so.6"; - -sqrt :: fn(f64) f64 = #foreign "sqrt", "libm.so.6"; -cos :: fn(f64) f64 = #foreign "cos", "libm.so.6"; -sin :: fn(f64) f64 = #foreign "sin", "libm.so.6"; - -stderr :: &u8 = #foreign "stderr"; -fprintf :: fn(&u8, &char) = #foreign "fprintf"; - - +io ::= pkg "std/io"; main ::= fn() { - x ::= foo(); - // y ::= malloc(10); - sq2 ::= sqrt(2); - cospi ::= cos(3.14159); - sinpi ::= sin(3.14159); - foo(); - s := "Hello!\n\0"; - fprintf(stderr, &s[0]); + io.puts("Hello, world!"); }; \ No newline at end of file -- cgit v1.2.3