From 63e18bde87876ac6f3217500a1e51e1e957fc9c7 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Sat, 28 Sep 2019 10:37:45 -0400 Subject: chars, string literals --- eval.c | 8 +++----- main.c | 2 -- parse.c | 13 ++++++++++++- test.toc | 12 +++++------- tokenizer.c | 11 ++++++----- types.c | 13 ++++++++++++- 6 files changed, 38 insertions(+), 21 deletions(-) diff --git a/eval.c b/eval.c index 6ab901a..d0b5fa6 100644 --- a/eval.c +++ b/eval.c @@ -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: diff --git a/main.c b/main.c index a072ade..f0df649 100644 --- a/main.c +++ b/main.c @@ -1,7 +1,5 @@ /* TODO: -char type -string constants are []char new/delete evaluator (simplify compile time constant expressions) re-do cgen diff --git a/parse.c b/parse.c index 872c0ae..6e91f11 100644 --- a/parse.c +++ b/parse.c @@ -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; diff --git a/test.toc b/test.toc index 155b62b..4d9ab40 100644 --- a/test.toc +++ b/test.toc @@ -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]; } diff --git a/types.c b/types.c index 8f54b8e..2596596 100644 --- a/types.c +++ b/types.c @@ -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; } -- cgit v1.2.3