diff options
-rwxr-xr-x | a.out | bin | 16800 -> 0 bytes | |||
-rw-r--r-- | eval.c | 6 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | parse.c | 38 | ||||
-rw-r--r-- | test.toc | 4 | ||||
-rw-r--r-- | tokenizer.c | 6 | ||||
-rw-r--r-- | types.c | 18 |
7 files changed, 36 insertions, 37 deletions
Binary files differ @@ -14,17 +14,17 @@ static bool eval_expr_as_int(Expression *e, Integer *i) { /* OPTIM: cache eval'd expression values? (probably only for declarations) */ switch (e->kind) { - case EXPR_FLOAT_LITERAL: + case EXPR_LITERAL_FLOAT: err_print(e->where, "Expected integer, but found floating-point literal."); return false; - case EXPR_INT_LITERAL: + case EXPR_LITERAL_INT: if (e->intl > (UInteger)INTEGER_MAX) { /* TODO: FIXME */ err_print(e->where, "Overflow when evaluating integer."); return false; } *i = (Integer)e->intl; return true; - case EXPR_STR_LITERAL: + case EXPR_LITERAL_STR: err_print(e->where, "Expected integer, but found string literal."); return false; case EXPR_UNARY_OP: @@ -60,6 +60,7 @@ int main(int argc, char **argv) { return EXIT_FAILURE; } + block_enter(NULL, &f.stmts); /* enter global scope */ if (!types_file(&f)) { err_fprint(TEXT_IMPORTANT("Errors occured while determining types.\n")); @@ -17,8 +17,8 @@ typedef enum { BUILTIN_U16, BUILTIN_U32, BUILTIN_U64, - BUILTIN_FLOAT, - BUILTIN_DOUBLE, + BUILTIN_F32, + BUILTIN_F64, BUILTIN_TYPE_COUNT } BuiltinType; @@ -51,9 +51,9 @@ typedef struct Block { } Block; typedef enum { - EXPR_INT_LITERAL, - EXPR_FLOAT_LITERAL, - EXPR_STR_LITERAL, + EXPR_LITERAL_FLOAT, + EXPR_LITERAL_INT, + EXPR_LITERAL_STR, EXPR_IDENT, /* variable or constant */ EXPR_BINARY_OP, EXPR_UNARY_OP, @@ -194,8 +194,8 @@ static bool type_builtin_is_integer(BuiltinType b) { static bool type_builtin_is_floating(BuiltinType b) { switch (b) { - case BUILTIN_FLOAT: - case BUILTIN_DOUBLE: + case BUILTIN_F32: + case BUILTIN_F64: return true; default: return false; } @@ -218,8 +218,8 @@ static BuiltinType kw_to_builtin_type(Keyword kw) { case KW_U16: return BUILTIN_U16; case KW_U32: return BUILTIN_U32; case KW_U64: return BUILTIN_U64; - case KW_FLOAT: return BUILTIN_FLOAT; - case KW_DOUBLE: return BUILTIN_DOUBLE; + case KW_F32: return BUILTIN_F32; + case KW_F64: return BUILTIN_F64; default: return BUILTIN_TYPE_COUNT; } } @@ -234,8 +234,8 @@ static Keyword builtin_type_to_kw(BuiltinType t) { case BUILTIN_U16: return KW_U16; case BUILTIN_U32: return KW_U32; case BUILTIN_U64: return KW_U64; - case BUILTIN_FLOAT: return KW_FLOAT; - case BUILTIN_DOUBLE: return KW_DOUBLE; + case BUILTIN_F32: return KW_F32; + case BUILTIN_F64: return KW_F64; case BUILTIN_TYPE_COUNT: break; } assert(0); @@ -622,15 +622,11 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { NumLiteral *num = &t->token->num; switch (num->kind) { case NUM_LITERAL_FLOAT: - e->kind = EXPR_FLOAT_LITERAL; - e->type.kind = TYPE_BUILTIN; - e->type.builtin = BUILTIN_FLOAT; + e->kind = EXPR_LITERAL_FLOAT; e->floatl = num->floatval; break; case NUM_LITERAL_INT: - e->kind = EXPR_INT_LITERAL; - e->type.kind = TYPE_BUILTIN; - e->type.builtin = BUILTIN_I64; /* TODO: if it's too big, use a u64 instead. */ + e->kind = EXPR_LITERAL_INT; e->intl = num->intval; break; } @@ -640,7 +636,7 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { e->ident = t->token->ident; break; case TOKEN_STR_LITERAL: - e->kind = EXPR_STR_LITERAL; + e->kind = EXPR_LITERAL_STR; e->strl = t->token->str; break; default: @@ -1225,13 +1221,13 @@ static void fprint_args(FILE *out, Array *args) { static void fprint_expr(FILE *out, Expression *e) { PARSE_PRINT_LOCATION(e->where); switch (e->kind) { - case EXPR_INT_LITERAL: + case EXPR_LITERAL_INT: fprintf(out, "%lld", (long long)e->intl); break; - case EXPR_FLOAT_LITERAL: + case EXPR_LITERAL_FLOAT: fprintf(out, "%f", (double)e->floatl); break; - case EXPR_STR_LITERAL: + case EXPR_LITERAL_STR: fprintf(out, "\"%s\"", e->strl.str); break; case EXPR_IDENT: @@ -1,6 +1,6 @@ #C("#include <stdio.h>\n"); -print_int @= fn(x: int) { +print_int @= fn(y: int) { #C("printf(\"%ld\\n\", (long)x);\n"); }; @@ -8,6 +8,6 @@ times2 @= fn(x: int) int { x + x }; main @= fn() { print_int(times2(5)); - foo := fn (x, y, z,w : float, A:double) {}; + foo := fn (x, y, z,w : f32, A:f64) {}; foo(foo,4,5,3,5); }; diff --git a/tokenizer.c b/tokenizer.c index 032085a..b821820 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -41,14 +41,14 @@ typedef enum { KW_U16, KW_U32, KW_U64, - KW_FLOAT, - KW_DOUBLE, + KW_F32, + KW_F64, KW_COUNT } Keyword; static const char *keywords[KW_COUNT] = {";", "=", ":", "@", ",", "(", ")", "{", "}", "[", "]", "==", "<", "<=", "-", "+", "fn", - "int", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "float", "double"}; + "int", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "f32", "f64"}; static const char *directives[DIRECT_COUNT] = {"C"}; @@ -63,7 +63,7 @@ static bool type_eq(Type *a, Type *b) { if (b->flags & TYPE_FLAG_FLEXIBLE) return true; assert(a->kind == TYPE_BUILTIN); - if (a->builtin == BUILTIN_FLOAT) { + if (type_builtin_is_floating(a->builtin)) { return type_builtin_is_floating(b->builtin); } assert(a->builtin == BUILTIN_I64); @@ -245,17 +245,17 @@ static bool type_of_expr(Expression *e, Type *t) { *param_type = param_types[i]; } } break; - case EXPR_INT_LITERAL: + case EXPR_LITERAL_INT: t->kind = TYPE_BUILTIN; t->builtin = BUILTIN_I64; t->flags |= TYPE_FLAG_FLEXIBLE; break; - case EXPR_STR_LITERAL: + case EXPR_LITERAL_STR: t->kind = TYPE_UNKNOWN; /* TODO */ break; - case EXPR_FLOAT_LITERAL: + case EXPR_LITERAL_FLOAT: t->kind = TYPE_BUILTIN; - t->builtin = BUILTIN_FLOAT; + t->builtin = BUILTIN_F32; t->flags |= TYPE_FLAG_FLEXIBLE; break; case EXPR_IDENT: { @@ -356,9 +356,9 @@ static bool type_of_expr(Expression *e, Type *t) { int rhs_is_flexible = rhs_type->flags & TYPE_FLAG_FLEXIBLE; if (lhs_is_flexible && rhs_is_flexible) { *t = *lhs_type; - if (rhs_type->builtin == BUILTIN_FLOAT) { + if (rhs_type->builtin == BUILTIN_F32) { /* promote to float */ - t->builtin = BUILTIN_FLOAT; + t->builtin = BUILTIN_F32; } } else if (type_eq(lhs_type, rhs_type)) { if (!lhs_is_flexible) @@ -425,7 +425,9 @@ static bool types_block(Block *b) { arr_foreach(&b->stmts, Statement, s) { if (!types_stmt(s)) ret = false; } - if (b->ret_expr) types_expr(b->ret_expr); + if (b->ret_expr) + if (!types_expr(b->ret_expr)) + ret = false; block_exit(b, &b->stmts); return ret; } |