diff options
-rw-r--r-- | decls_cgen.c | 1 | ||||
-rw-r--r-- | main.c | 10 | ||||
-rw-r--r-- | parse.c | 18 | ||||
-rw-r--r-- | std/io.toc | 2 | ||||
-rw-r--r-- | test.toc | 16 | ||||
-rw-r--r-- | tests/arr/arr.toc | 8 | ||||
-rw-r--r-- | tests/arr2/arr2.toc | 2 | ||||
-rw-r--r-- | tests/bf/bf.toc | 2 |
8 files changed, 41 insertions, 18 deletions
diff --git a/decls_cgen.c b/decls_cgen.c index 3c761ac..762a8cb 100644 --- a/decls_cgen.c +++ b/decls_cgen.c @@ -228,6 +228,7 @@ static bool cgen_decls_decl(CGenerator *g, Declaration *d) { d->val.fn->c.name = d->idents[0]; return true; } else { + /* foreign non-function */ const char *foreign_name = d->foreign.name_str; if (!cgen_type_pre(g, &d->type, d->where)) return false; @@ -18,12 +18,18 @@ /* TODO: -get tests to compile without warnings -foreign non-functions no foreign parameter declarations +see NOTE in test.toc variadic fns #include constants in structs +#builtin values - accessed via, e.g. #builtin("sizeof(int)") +- sizeof(int) (size of C int type), sizeof(long), sizeof(size_t) etc. +- compiling - true if @ compile time, false otherwise +- stdout, stderr, stdin - pointers to C FILEs + +#if + --- X ::= newtype(int); or something don't allow while {3; 5} (once break is added) @@ -710,18 +710,18 @@ static bool parser_is_definitely_type(Parser *p, Token **end) { if (token_is_kw(t->token, KW_LBRACE)) goto end; /* void fn expr */ if (is_decl(t)) /* has return declaration */ goto end; - Type return_type; - /* TODO: think of a better way of determining if it's a void fn type. (maybe followed by ;/,/)?)*/ - bool *enabled = &t->token->pos.ctx->enabled; - bool prev_enabled = *enabled; - *enabled = false; - if (!parse_type(p, &return_type)) { - /* couldn't parse a return type. void fn type */ - *enabled = prev_enabled; + + if (t->token->kind == TOKEN_KW && + (t->token->kw == KW_SEMICOLON || t->token->kw == KW_COMMA || t->token->kw == KW_RPAREN || t->token->kw == KW_LBRACE)) { + /* void fn type */ ret = true; goto end; } - *enabled = prev_enabled; + Type return_type; + if (!parse_type(p, &return_type)) { + /* couldn't parse a return type. this shouldn't happen in theory. */ + assert(0); + } if (token_is_kw(t->token, KW_LBRACE)) { /* non-void fn expr */ goto end; @@ -1,6 +1,6 @@ pkg "io"; -c_putchar :: fn(i32) = #foreign "putchar", "libc.so.6"; +c_putchar :: fn(i32) i32 = #foreign "putchar", "libc.so.6"; #export puts ::= fn(x: []char) { each c := x { @@ -1,4 +1,16 @@ -io ::= pkg "std/io"; +// io ::= pkg "std/io"; +// main ::= fn() { + // io.puts("Hello, world!"); +// }; + +stdout :: &u8 = #foreign "stdout"; +fwrite :: fn(&u8, u64, u64, &u8) = #foreign "fwrite"; + +puts ::= fn(x : []char) { +// NOTE: removing brackets here causes error! this shouldn't happen! + fwrite((&x[0]) as (&u8), 1, x.len as u64, stdout); +}; + main ::= fn() { - io.puts("Hello, world!"); + puts("Hello, world!\n"); };
\ No newline at end of file diff --git a/tests/arr/arr.toc b/tests/arr/arr.toc index 4cabd9e..d18e8ea 100644 --- a/tests/arr/arr.toc +++ b/tests/arr/arr.toc @@ -1,10 +1,10 @@ puti ::= fn(x: int) { - #C("printf(\"%ld\\n\", (long)x); -"); + #C("extern int printf(const char *fmt, ...)"); + #C("printf(\"%ld\\n\", (long)x)"); }; putf ::= fn(x: float) { - #C("printf(\"%f\\n\", (double)x); -"); + #C("extern int printf(const char *fmt, ...)"); + #C("printf(\"%f\\n\", (double)x)"); }; // it would be nice if Arr.data.len == Arr.len (: but this will require some C code... diff --git a/tests/arr2/arr2.toc b/tests/arr2/arr2.toc index ea9ea79..7056416 100644 --- a/tests/arr2/arr2.toc +++ b/tests/arr2/arr2.toc @@ -1,8 +1,10 @@ puti ::= fn(x: int) { + #C("extern int printf(const char *fmt, ...)"); #C("printf(\"%ld\\n\", (long)x); "); }; putf ::= fn(x: float) { + #C("extern int printf(const char *fmt, ...)"); #C("printf(\"%f\\n\", (double)x); "); }; diff --git a/tests/bf/bf.toc b/tests/bf/bf.toc index b624101..c2f9970 100644 --- a/tests/bf/bf.toc +++ b/tests/bf/bf.toc @@ -32,10 +32,12 @@ getstdin ::= fn() []char { }; puti ::= fn(x: int) { + #C("extern int printf(const char *fmt, ...)"); #C("printf(\"%ld\\n\", x)"); }; main ::= fn() { + #C("extern int putchar(int c)"); code := getstdin(); tape_sz := 3; tape := new(int, tape_sz); |