diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2020-03-12 15:47:12 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2020-03-12 15:47:12 -0400 |
commit | 2da3e68e563fd5c17223183d6d89d442c873a1cb (patch) | |
tree | 25168ce3014a87dc3d5274cf03eba0bd9475009d | |
parent | 62171110eaf3dce74fe4023f7a1d766f7b6d0745 (diff) |
started #foreign varargs
-rw-r--r-- | decls_cgen.c | 3 | ||||
-rw-r--r-- | parse.c | 63 | ||||
-rw-r--r-- | test.toc | 30 | ||||
-rw-r--r-- | types.h | 8 |
4 files changed, 49 insertions, 55 deletions
diff --git a/decls_cgen.c b/decls_cgen.c index 84bab0a..86c4cf9 100644 --- a/decls_cgen.c +++ b/decls_cgen.c @@ -98,6 +98,9 @@ static void cgen_ctype(CGenerator *g, CType *c) { case CTYPE_SIZE_T: cgen_write(g, "size_t"); break; + case CTYPE_VARARGS: + cgen_write(g, "..."); + break; default: assert(0); break; @@ -1128,6 +1128,9 @@ static Status ctype_to_type(Allocator *a, CType *ctype, Type *type, Location whe case CTYPE_SIZE_T: size = sizeof(size_t); break; + case CTYPE_VARARGS: + type->builtin = BUILTIN_VARARGS; + break; case CTYPE_LONGLONG: case CTYPE_UNSIGNED_LONGLONG: #if LONGLONG_AVAILABLE @@ -1165,34 +1168,42 @@ static Status parse_c_type(Parser *p, CType *ctype, Type *type) { if (token_is_direct(t->token, DIRECT_C)) { ++t->token; ctype->kind = CTYPE_NONE; - if (token_is_kw(t->token, KW_INT)) { - ctype->kind = CTYPE_INT; - ++t->token; - } else if (token_is_kw(t->token, KW_FLOAT)) { - ctype->kind = CTYPE_FLOAT; - ++t->token; - } else if (token_is_kw(t->token, KW_CHAR)) { - ctype->kind = CTYPE_CHAR; - ++t->token; - } else if (token_is_kw(t->token, KW_AMPERSAND)) { - ctype->kind = CTYPE_PTR; - ++t->token; - if (t->token->kind == TOKEN_IDENT) { - size_t n = ident_str_len(t->token->ident); - ctype->points_to = parser_malloc(p, n+1); - memcpy(ctype->points_to, t->token->ident, n); - ctype->points_to[n] = 0; - } else if (t->token->kind == TOKEN_LITERAL_STR) { - size_t n = t->token->str.len; - ctype->points_to = parser_malloc(p, n+1); - memcpy(ctype->points_to, t->token->str.str, n); - ctype->points_to[n] = 0; - } else { - tokr_err(t, "Expected type to follow &"); + if (t->token->kind == TOKEN_KW) { + switch (t->token->kw) { + case KW_INT: + ctype->kind = CTYPE_INT; + ++t->token; + break; + case CTYPE_FLOAT: + ctype->kind = CTYPE_FLOAT; + ++t->token; + break; + case KW_CHAR: + ctype->kind = CTYPE_CHAR; + ++t->token; + break; + case KW_AMPERSAND: + ctype->kind = CTYPE_PTR; + ++t->token; + if (t->token->kind == TOKEN_LITERAL_STR) { + size_t n = t->token->str.len; + ctype->points_to = parser_malloc(p, n+1); + memcpy(ctype->points_to, t->token->str.str, n); + ctype->points_to[n] = 0; + } else { + tokr_err(t, "Expected string literal to follow #C &"); + return false; + } + ++t->token; + break; + case KW_DOTDOT: + ctype->kind = CTYPE_VARARGS; + ++t->token; + break; + default: + tokr_err(t, "Unrecognized C type"); return false; } - - ++t->token; } else if (t->token->kind == TOKEN_IDENT) { char *id = t->token->ident; ctype->kind = 0; @@ -1,27 +1,5 @@ -#include "std/io.toc"; +printf ::= #foreign("printf") fn(#C &"const char", #C ..); -g ::= fn(modulator:: int, x :: ..) int { - total := 0; - for e := x { - total += modulator * (e as int); - } - total -}; - -f ::= fn(x :: ..) int { - g(2, x) -}; - -main ::= fn() { - puti(f(5)); - - puti(f(5,6)); - - puti(f(1,2,3)); - - puti(f(1,1,1,1)); - - puti(f()); -}; - -main();
\ No newline at end of file +main::=fn(){ + printf("Hello %d", 5); +};
\ No newline at end of file @@ -666,10 +666,12 @@ typedef enum { CTYPE_UNSIGNED_INT = CTYPE_UNSIGNED|CTYPE_INT, CTYPE_UNSIGNED_LONG = CTYPE_UNSIGNED|CTYPE_LONG, CTYPE_UNSIGNED_LONGLONG = CTYPE_UNSIGNED|CTYPE_LONGLONG, + /* things that can't be unsigned */ CTYPE_PTR = 0x10, - CTYPE_FLOAT = 0x11, - CTYPE_DOUBLE = 0x12, - CTYPE_SIZE_T = 0x13 + CTYPE_FLOAT, + CTYPE_DOUBLE, + CTYPE_SIZE_T, + CTYPE_VARARGS } CTypeKind; typedef struct { CTypeKind kind; |