diff options
author | Leo Tenenbaum <pommicket@gmail.com> | 2019-09-28 10:37:45 -0400 |
---|---|---|
committer | Leo Tenenbaum <pommicket@gmail.com> | 2019-09-28 10:37:45 -0400 |
commit | 63e18bde87876ac6f3217500a1e51e1e957fc9c7 (patch) | |
tree | 6dfd127438dd1422a5bf4f4c1d7015f1e158d2c6 | |
parent | f6efc17f61ebf5c57093fdba2963898602f9281e (diff) |
chars, string literals
-rw-r--r-- | eval.c | 8 | ||||
-rw-r--r-- | main.c | 2 | ||||
-rw-r--r-- | parse.c | 13 | ||||
-rw-r--r-- | test.toc | 12 | ||||
-rw-r--r-- | tokenizer.c | 11 | ||||
-rw-r--r-- | types.c | 13 |
6 files changed, 38 insertions, 21 deletions
@@ -28,9 +28,6 @@ static bool eval_expr(Expression *e, Value *v) { } v->intv = (Integer)e->intl; return true; - case EXPR_LITERAL_STR: - err_print(e->where, "not implemented yet"); /* TODO */ - return false; case EXPR_LITERAL_BOOL: v->boolv = e->booll; return true; @@ -183,7 +180,6 @@ static bool eval_expr(Expression *e, Value *v) { info_print(d->where, "Declaration was here."); return false; } - /* TODO: tuples */ if (!d->val) { d->val = err_malloc(sizeof *d->val); /* OPTIM */ if (!eval_expr(&d->expr, d->val)) @@ -199,7 +195,9 @@ static bool eval_expr(Expression *e, Value *v) { case EXPR_IF: case EXPR_WHILE: case EXPR_CALL: - case EXPR_BLOCK: { + case EXPR_BLOCK: + case EXPR_LITERAL_STR: + case EXPR_LITERAL_CHAR: { err_print(e->where, "operation not supported at compile time yet."); /* TODO */ } break; case EXPR_DIRECT: @@ -1,7 +1,5 @@ /* TODO: -char type -string constants are []char new/delete evaluator (simplify compile time constant expressions) re-do cgen @@ -20,6 +20,7 @@ typedef enum { BUILTIN_U64, BUILTIN_F32, BUILTIN_F64, + BUILTIN_CHAR, BUILTIN_BOOL, BUILTIN_TYPE_COUNT } BuiltinType; @@ -30,7 +31,7 @@ typedef enum { typedef struct Type { Location where; TypeKind kind; - unsigned short flags; + uint16_t flags; union { BuiltinType builtin; struct { @@ -66,6 +67,7 @@ typedef enum { EXPR_LITERAL_INT, EXPR_LITERAL_STR, EXPR_LITERAL_BOOL, + EXPR_LITERAL_CHAR, EXPR_IDENT, /* variable or constant */ EXPR_BINARY_OP, EXPR_UNARY_OP, @@ -147,6 +149,7 @@ typedef struct Expression { UInteger intl; StrLiteral strl; bool booll; + char charl; struct { UnaryOp op; struct Expression *of; @@ -327,6 +330,7 @@ static Keyword builtin_type_to_kw(BuiltinType t) { case BUILTIN_F32: return KW_F32; case BUILTIN_F64: return KW_F64; case BUILTIN_BOOL: return KW_BOOL; + case BUILTIN_CHAR: return KW_CHAR; case BUILTIN_TYPE_COUNT: break; } assert(0); @@ -842,6 +846,10 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { e->kind = EXPR_LITERAL_STR; e->strl = t->token->str; break; + case TOKEN_CHAR_LITERAL: + e->kind = EXPR_LITERAL_CHAR; + e->charl = t->token->chr; + break; case TOKEN_KW: switch (t->token->kw) { case KW_TRUE: @@ -1589,6 +1597,9 @@ static void fprint_expr(FILE *out, Expression *e) { case EXPR_LITERAL_BOOL: fprintf(out, "%s", e->booll ? "true" : "false"); break; + case EXPR_LITERAL_CHAR: + fprintf(out, "'%c'", e->charl); + break; case EXPR_IDENT: fprint_ident(out, e->ident); break; @@ -1,10 +1,8 @@ main @= fn() { - foo @= fn() i64 { return 3; }; - test @= fn(x : i64, y : i32, z,w : f32 = 18, foo := false) ret1 : i64, ret2 : i64 { - ret1 = x + (z + w as i64); - }; - a,b : i64; - a, b = test(3,7,2, foo = true); - a, b = test(x = 3, y = 30); + foo := 'a'; + bar := foo as u8 as f32; + x := "hello"; + x[4] = 's'; + x[3] = foo; }; diff --git a/tokenizer.c b/tokenizer.c index 0ed7f5a..2e8127e 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -45,9 +45,6 @@ typedef enum { KW_RETURN, KW_FN, KW_AS, - KW_BOOL, - KW_TRUE, - KW_FALSE, KW_INT, KW_I8, KW_I16, @@ -60,6 +57,10 @@ typedef enum { KW_FLOAT, KW_F32, KW_F64, + KW_CHAR, + KW_BOOL, + KW_TRUE, + KW_FALSE, KW_COUNT } Keyword; @@ -68,9 +69,9 @@ static const char *keywords[KW_COUNT] = "+", "-", "*", "!", "&", "/", "=", "if", "elif", "else", "while", "return", "fn", "as", - "bool", "true", "false", "int", "i8", "i16", "i32", "i64", - "u8", "u16", "u32", "u64", "float", "f32", "f64"}; + "u8", "u16", "u32", "u64", "float", "f32", "f64", + "char", "bool", "true", "false"}; static inline const char *kw_to_str(Keyword k) { return keywords[k]; } @@ -394,7 +394,13 @@ static bool types_expr(Typer *tr, Expression *e) { t->flags |= TYPE_FLAG_FLEXIBLE; break; case EXPR_LITERAL_STR: - t->kind = TYPE_UNKNOWN; /* TODO */ + t->kind = TYPE_ARR; + t->arr.n = e->strl.len; + t->arr.of = malloc(sizeof *t->arr.of); + t->arr.of->flags = TYPE_FLAG_RESOLVED; + t->arr.of->kind = TYPE_BUILTIN; + t->arr.of->builtin = BUILTIN_CHAR; + t->flags |= TYPE_FLAG_RESOLVED; break; case EXPR_LITERAL_FLOAT: t->kind = TYPE_BUILTIN; @@ -405,6 +411,10 @@ static bool types_expr(Typer *tr, Expression *e) { t->kind = TYPE_BUILTIN; t->builtin = BUILTIN_BOOL; break; + case EXPR_LITERAL_CHAR: + t->kind = TYPE_BUILTIN; + t->builtin = BUILTIN_CHAR; + break; case EXPR_IDENT: { if (!type_of_ident(tr, e->where, e->ident, t)) return false; } break; @@ -781,6 +791,7 @@ static bool types_expr(Typer *tr, Expression *e) { } } break; } + return true; } |