From 21b42cd9099d25b89f20789bf75f0833f2a5a980 Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Thu, 31 Oct 2019 22:23:49 -0400 Subject: fixed the bug (but i'm not happy about the solution) --- eval.c | 8 +++----- parse.c | 12 ++++++------ test.toc | 7 ------- tokenizer.c | 22 +++++++++++----------- types.c | 5 +---- types.h | 9 +++++---- 6 files changed, 26 insertions(+), 37 deletions(-) diff --git a/eval.c b/eval.c index b4b747e..a6ad31c 100644 --- a/eval.c +++ b/eval.c @@ -11,6 +11,7 @@ static void evalr_create(Evaluator *ev, Typer *tr) { ev->returning = NULL; ev->to_free = NULL; ev->typer = tr; + ev->enabled = true; } static void evalr_free(Evaluator *ev) { @@ -709,11 +710,8 @@ static bool eval_set(Evaluator *ev, Expression *set, Value *to) { return true; } -static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { - if (e->type.kind == TYPE_UNKNOWN) { - err_print(e->where, "Cannot determine type of expression."); - return false; - } +static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { + if (!ev->enabled) return false; /* silently fail */ /* WARNING: macros ahead */ #define eval_unary_op_one(low, up, op) \ case BUILTIN_##up: \ diff --git a/parse.c b/parse.c index cb11412..618336a 100644 --- a/parse.c +++ b/parse.c @@ -653,9 +653,9 @@ static bool parser_is_definitely_type(Parser *p, Token **end) { } } break; case TOKEN_DIRECT: - case TOKEN_NUM_LITERAL: - case TOKEN_CHAR_LITERAL: - case TOKEN_STR_LITERAL: + case TOKEN_LITERAL_NUM: + case TOKEN_LITERAL_CHAR: + case TOKEN_LITERAL_STR: case TOKEN_EOF: case TOKEN_IDENT: ret = false; @@ -895,7 +895,7 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { if (end - t->token == 1) { /* 1-token expression */ switch (t->token->kind) { - case TOKEN_NUM_LITERAL: { + case TOKEN_LITERAL_NUM: { NumLiteral *num = &t->token->num; switch (num->kind) { case NUM_LITERAL_FLOAT: @@ -912,11 +912,11 @@ static bool parse_expr(Parser *p, Expression *e, Token *end) { e->kind = EXPR_IDENT; e->ident = t->token->ident; break; - case TOKEN_STR_LITERAL: + case TOKEN_LITERAL_STR: e->kind = EXPR_LITERAL_STR; e->strl = t->token->str; break; - case TOKEN_CHAR_LITERAL: + case TOKEN_LITERAL_CHAR: e->kind = EXPR_LITERAL_CHAR; e->charl = t->token->chr; break; diff --git a/test.toc b/test.toc index c7e4997..7991086 100644 --- a/test.toc +++ b/test.toc @@ -6,23 +6,16 @@ puti @= fn(x: int) { Point @= struct { x, y : int; }; - - - sum @= fn(p: &Point) int { p.x + p.y }; - - somesum @= fn() int { - p : Point; p.x = 12389; p.y = 29404; total := sum(&p); total }; - main @= fn() { // puti(somesum()); foo @= somesum(); diff --git a/tokenizer.c b/tokenizer.c index 7ec5f1a..aa9dd83 100644 --- a/tokenizer.c +++ b/tokenizer.c @@ -61,9 +61,9 @@ static const char *token_kind_to_str(TokenKind t) { case TOKEN_KW: return "keyword"; case TOKEN_IDENT: return "identifier"; case TOKEN_DIRECT: return "directive"; - case TOKEN_NUM_LITERAL: return "numerical literal"; - case TOKEN_CHAR_LITERAL: return "character literal"; - case TOKEN_STR_LITERAL: return "string literal"; + case TOKEN_LITERAL_NUM: return "numerical literal"; + case TOKEN_LITERAL_CHAR: return "character literal"; + case TOKEN_LITERAL_STR: return "string literal"; case TOKEN_EOF: return "end of file"; } assert(0); @@ -80,7 +80,7 @@ static void fprint_token(FILE *out, Token *t) { fprintf(out, "identifier: %p: ", (void*)t->ident); fprint_ident(out, t->ident); break; - case TOKEN_NUM_LITERAL: + case TOKEN_LITERAL_NUM: fprintf(out, "number: "); switch (t->num.kind) { case NUM_LITERAL_INT: @@ -91,10 +91,10 @@ static void fprint_token(FILE *out, Token *t) { break; } break; - case TOKEN_CHAR_LITERAL: + case TOKEN_LITERAL_CHAR: fprintf(out, "char: '%c' (%d)", t->chr, t->chr); break; - case TOKEN_STR_LITERAL: + case TOKEN_LITERAL_STR: fprintf(out, "str: \"%s\"", t->str.str); break; case TOKEN_DIRECT: @@ -208,8 +208,8 @@ static bool tokenize_string(Tokenizer *t, char *str) { switch (t->s[1]) { case '/': /* single line comment */ tokr_nextchar(t); - for (t->s++; *t->s != '\n' && *t->s; t->s++); - t->line++; + for (t->s++; *t->s && *t->s != '\n'; t->s++); + if (t->s) tokr_nextchar(t); /* skip newline */ break; case '*': { /* multi line comment */ tokr_nextchar(t); @@ -390,7 +390,7 @@ static bool tokenize_string(Tokenizer *t, char *str) { } tokr_nextchar(t); } - token->kind = TOKEN_NUM_LITERAL; + token->kind = TOKEN_LITERAL_NUM; token->num = n; continue; } @@ -418,7 +418,7 @@ static bool tokenize_string(Tokenizer *t, char *str) { goto err; } tokr_nextchar(t); - token->kind = TOKEN_CHAR_LITERAL; + token->kind = TOKEN_LITERAL_CHAR; token->chr = c; continue; } @@ -464,7 +464,7 @@ static bool tokenize_string(Tokenizer *t, char *str) { } } *strptr = 0; - token->kind = TOKEN_STR_LITERAL; + token->kind = TOKEN_LITERAL_STR; token->str.len = (size_t)(strptr - strlit); token->str.str = strlit; tokr_nextchar(t); /* move past closing " */ diff --git a/types.c b/types.c index 641b922..ba82ed0 100644 --- a/types.c +++ b/types.c @@ -1200,10 +1200,6 @@ static bool types_decl(Typer *tr, Declaration *d) { success = false; goto ret; } - } else { - /* if we can't find the type, default to unknown */ - d->type.flags = 0; - d->type.kind = TYPE_UNKNOWN; } if (d->flags & DECL_FLAG_HAS_EXPR) { if (!types_expr(tr, &d->expr)) { @@ -1272,6 +1268,7 @@ static bool types_decl(Typer *tr, Declaration *d) { /* use unknown type if we didn't get the type */ d->type.flags = 0; d->type.kind = TYPE_UNKNOWN; + tr->evalr->enabled = false; /* disable evaluator completely so that it doesn't accidentally try to access this declaration */ } arr_remove_last(&tr->in_decls); return success; diff --git a/types.h b/types.h index 2e9383f..388727c 100644 --- a/types.h +++ b/types.h @@ -130,9 +130,9 @@ typedef enum { TOKEN_KW, TOKEN_IDENT, TOKEN_DIRECT, - TOKEN_NUM_LITERAL, - TOKEN_CHAR_LITERAL, - TOKEN_STR_LITERAL, + TOKEN_LITERAL_NUM, + TOKEN_LITERAL_CHAR, + TOKEN_LITERAL_STR, TOKEN_EOF } TokenKind; @@ -495,7 +495,7 @@ typedef struct Declaration { Location where; Identifier *idents; Type type; - uint16_t flags; + U16 flags; Expression expr; Value val; /* only for constant decls. */ @@ -550,6 +550,7 @@ typedef struct { bool returning; Value ret_val; void **to_free; /* an array of data to free for this scope. */ + bool enabled; } Evaluator; typedef struct Typer { -- cgit v1.2.3