From 16ac802f5a8cfa961c92669f8e1fc66ff769fbdf Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Wed, 18 Sep 2019 21:09:23 -0400 Subject: idealistic float, double => f32, f64; literals were also being typed in parsing for some reason? trying to fix --- a.out | Bin 16800 -> 0 bytes main.c | 1 + parse.c | 22 ++++++++-------------- test.toc | 4 ++-- tokenizer.c | 6 +++--- types.c | 12 +++++++----- 6 files changed, 21 insertions(+), 24 deletions(-) delete mode 100755 a.out diff --git a/a.out b/a.out deleted file mode 100755 index c8b4185..0000000 Binary files a/a.out and /dev/null differ diff --git a/main.c b/main.c index 50df138..9b0e864 100644 --- a/main.c +++ b/main.c @@ -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")); diff --git a/parse.c b/parse.c index effe5f7..f3e7efa 100644 --- a/parse.c +++ b/parse.c @@ -17,8 +17,8 @@ typedef enum { BUILTIN_U16, BUILTIN_U32, BUILTIN_U64, - BUILTIN_FLOAT, - BUILTIN_DOUBLE, + BUILTIN_F32, + BUILTIN_F64, BUILTIN_TYPE_COUNT } BuiltinType; @@ -192,8 +192,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; } @@ -216,8 +216,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; } } @@ -232,8 +232,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); @@ -620,15 +620,9 @@ 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->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->intl = num->intval; break; } diff --git a/test.toc b/test.toc index 5c5af41..d8a82af 100644 --- a/test.toc +++ b/test.toc @@ -1,6 +1,6 @@ #C("#include \n"); -print_int @= fn(x: int) { +print_int @= fn(y: int) { #C("printf(\"%ld\\n\", (long)x);\n"); }; @@ -8,5 +8,5 @@ times2 @= fn(x: int) int { x + x }; main @= fn() { print_int(times2(5)); - foo := fn (x, y, z : float, A:double) {}; + foo := fn (x, y, z : f32, A:f64) {}; }; 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"}; diff --git a/types.c b/types.c index 9c43c13..8ce7b4d 100644 --- a/types.c +++ b/types.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); @@ -254,7 +254,7 @@ static bool type_of_expr(Expression *e, Type *t) { break; case EXPR_FLOAT_LITERAL: t->kind = TYPE_BUILTIN; - t->builtin = BUILTIN_FLOAT; + t->builtin = BUILTIN_F32; t->flags |= TYPE_FLAG_FLEXIBLE; break; case EXPR_IDENT: { @@ -331,9 +331,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) @@ -400,7 +400,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; } -- cgit v1.2.3