diff options
-rw-r--r-- | eval.c | 4 | ||||
-rw-r--r-- | main.c | 1 | ||||
-rw-r--r-- | parse.c | 27 | ||||
-rw-r--r-- | test.toc | 4 | ||||
-rw-r--r-- | tokenizer.c | 12 | ||||
-rw-r--r-- | types.c | 4 |
6 files changed, 47 insertions, 5 deletions
@@ -4,6 +4,7 @@ typedef struct Value { Floating floatv; struct Value *points_to; FnExpr fn; + bool boolv; }; } Value; @@ -29,6 +30,9 @@ static bool eval_expr(Expression *e, Value *v) { case EXPR_LITERAL_STR: err_print(e->where, "not implemented yet"); /* TODO */ return false; + case EXPR_LITERAL_BOOL: + v->boolv = e->booll; + return true; case EXPR_UNARY_OP: { Expression *of_expr = e->unary.of; switch (e->unary.op) { @@ -1,6 +1,5 @@ /* TODO: -while re-do cgen */ #include "toc.c" @@ -20,6 +20,7 @@ typedef enum { BUILTIN_U64, BUILTIN_F32, BUILTIN_F64, + BUILTIN_BOOL, BUILTIN_TYPE_COUNT } BuiltinType; @@ -64,6 +65,7 @@ typedef enum { EXPR_LITERAL_FLOAT, EXPR_LITERAL_INT, EXPR_LITERAL_STR, + EXPR_LITERAL_BOOL, EXPR_IDENT, /* variable or constant */ EXPR_BINARY_OP, EXPR_UNARY_OP, @@ -123,6 +125,7 @@ typedef struct Expression { Floating floatl; UInteger intl; StrLiteral strl; + bool booll; struct { UnaryOp op; struct Expression *of; @@ -276,6 +279,7 @@ static BuiltinType kw_to_builtin_type(Keyword kw) { case KW_FLOAT: return BUILTIN_F32; case KW_F32: return BUILTIN_F32; case KW_F64: return BUILTIN_F64; + case KW_BOOL: return BUILTIN_BOOL; default: return BUILTIN_TYPE_COUNT; } } @@ -292,6 +296,7 @@ static Keyword builtin_type_to_kw(BuiltinType t) { case BUILTIN_U64: return KW_U64; case BUILTIN_F32: return KW_F32; case BUILTIN_F64: return KW_F64; + case BUILTIN_BOOL: return KW_BOOL; case BUILTIN_TYPE_COUNT: break; } assert(0); @@ -733,7 +738,21 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { e->kind = EXPR_LITERAL_STR; e->strl = t->token->str; break; + case TOKEN_KW: + switch (t->token->kw) { + case KW_TRUE: + e->kind = EXPR_LITERAL_BOOL; + e->booll = true; + break; + case KW_FALSE: + e->kind = EXPR_LITERAL_BOOL; + e->booll = false; + break; + default: goto unrecognized; + } + break; default: + unrecognized: tokr_err(t, "Unrecognized expression."); return false; } @@ -1115,7 +1134,10 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { case KW_SLASH: op = BINARY_DIV; break; - + case KW_AMPERSAND: + case KW_EXCLAMATION: + err_print(lowest_precedence_op->where, "Unary operator '%s' being used as a binary operator!", kw_to_str(lowest_precedence_op->kw)); + return false; default: assert(0); return false; } e->binary.op = op; @@ -1451,6 +1473,9 @@ static void fprint_expr(FILE *out, Expression *e) { case EXPR_LITERAL_STR: fprintf(out, "\"%s\"", e->strl.str); break; + case EXPR_LITERAL_BOOL: + fprintf(out, "%s", e->booll ? "true" : "false"); + break; case EXPR_IDENT: fprint_ident(out, e->ident); break; @@ -1,4 +1,8 @@ main @= fn() { foo @= fn(x: int) { return; }; + //b := !(5 > 3 && 4 <= 3 && 3 >= 2 || 0); + //b := !(5 < 3); + x : bool = true; + }; diff --git a/tokenizer.c b/tokenizer.c index 1e095a3..5af4297 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -43,6 +43,9 @@ typedef enum { KW_WHILE, KW_RETURN, KW_FN, + KW_BOOL, + KW_TRUE, + KW_FALSE, KW_INT, KW_I8, KW_I16, @@ -60,10 +63,13 @@ typedef enum { static const char *keywords[KW_COUNT] = {";", "=", ":", "@", ",", "(", ")", "{", "}", "[", "]", "==", "<", "<=", ">", ">=", - "+", "-", "*", "!", "&", "/", "if", "elif", "else", "while", "return", "fn", "int", - "i8", "i16", "i32", "i64", + "+", "-", "*", "!", "&", "/", "if", "elif", "else", "while", "return", "fn", + "bool", "true", "false", + "int", "i8", "i16", "i32", "i64", "u8", "u16", "u32", "u64", "float", "f32", "f64"}; +static inline const char *kw_to_str(Keyword k) { return keywords[k]; } + static const char *directives[DIRECT_COUNT] = {"C"}; @@ -172,7 +178,7 @@ static void fprint_token(FILE *out, Token *t) { fprintf(out, "l%lu-", (unsigned long)t->where.line); switch (t->kind) { case TOKEN_KW: - fprintf(out, "keyword: %s", keywords[t->kw]); + fprintf(out, "keyword: %s", kw_to_str(t->kw)); break; case TOKEN_IDENT: fprintf(out, "identifier: %p: ", (void*)t->ident); @@ -385,6 +385,10 @@ static bool types_expr(Typer *tr, Expression *e) { t->builtin = BUILTIN_F32; t->flags |= TYPE_FLAG_FLEXIBLE; break; + case EXPR_LITERAL_BOOL: + t->kind = TYPE_BUILTIN; + t->builtin = BUILTIN_BOOL; + break; case EXPR_IDENT: { if (!type_of_ident(tr, e->where, e->ident, t)) return false; } break; |