From f97d401c3bade9f053055f411be25ef6d6b8041b Mon Sep 17 00:00:00 2001 From: Leo Tenenbaum Date: Tue, 22 Oct 2019 21:47:33 -0400 Subject: string literals are now slices; minor bug fixes --- .gitignore | 3 ++- cgen.c | 17 ++++++++------- eval.c | 3 ++- main.c | 6 +++++- out.c | 72 -------------------------------------------------------------- test.toc | 22 +++++++++++++++---- types.c | 11 +++++----- 7 files changed, 41 insertions(+), 93 deletions(-) delete mode 100644 out.c diff --git a/.gitignore b/.gitignore index 6c313cf..9da0770 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ toc -a.out \ No newline at end of file +a.out +out.c diff --git a/cgen.c b/cgen.c index bea7482..5f68221 100644 --- a/cgen.c +++ b/cgen.c @@ -174,7 +174,7 @@ static bool cgen_type_pre(CGenerator *g, Type *t, Location where) { break; case TYPE_VOID: cgen_write(g, "void"); break; case TYPE_UNKNOWN: - err_print(t->where, "Can't determine type."); + err_print(where, "Can't determine type."); return false; case TYPE_TUPLE: /* We should never try to generate a tuple */ @@ -727,11 +727,11 @@ static bool cgen_expr(CGenerator *g, Expression *e) { break; case EXPR_LITERAL_STR: { size_t c; - cgen_write(g, "\""); + cgen_write(g, "mkslice_(\""); for (c = 0; c < e->strl.len; c++) { cgen_write(g, "\\x%x", e->strl.str[c]); } - cgen_write(g, "\""); + cgen_write(g, "\", %lu)", (unsigned long)e->strl.len); } break; case EXPR_LITERAL_BOOL: cgen_write(g, e->booll ? "true" : "false"); @@ -897,7 +897,7 @@ static bool cgen_expr(CGenerator *g, Expression *e) { if (!eval_expr(g->evalr, &e->direct.args[0], &val)) return false; cgen_indent(g); - fwrite(val.arr, 1, e->direct.args[0].type.arr.n, cgen_writing_to(g)); + fwrite(val.slice.data, 1, val.slice.n, cgen_writing_to(g)); } break; case DIRECT_COUNT: assert(0); break; } @@ -1066,7 +1066,7 @@ static bool cgen_val(CGenerator *g, Value *v, Type *t, Location where) { case BUILTIN_F32: cgen_write(g, F32_FMT, v->f32); break; case BUILTIN_F64: cgen_write(g, F64_FMT, v->f64); break; case BUILTIN_CHAR: cgen_write(g, "\\x%02x", v->charv); break; - case BUILTIN_BOOL: cgen_write(g, "%s", v->boolv ? true : false); break; + case BUILTIN_BOOL: cgen_write(g, "%s", v->boolv ? "true" : "false"); break; } break; } @@ -1075,7 +1075,8 @@ static bool cgen_val(CGenerator *g, Value *v, Type *t, Location where) { static bool cgen_decl(CGenerator *g, Declaration *d) { if (cgen_fn_is_direct(g, d)) { - cgen_fn(g, &d->expr.fn, d->where); + if (!cgen_fn(g, &d->expr.fn, d->where)) + return false; } else if ((d->flags & DECL_FLAG_CONST) || g->block == NULL) { if (d->type.kind == TYPE_TUPLE) { long idx = 0; @@ -1130,9 +1131,9 @@ static bool cgen_decl(CGenerator *g, Declaration *d) { cgen_write(g, "{"); cgen_nl(g); - if (!cgen_type_pre(g, &d->expr.type, d->expr.where)) return false; + if (!cgen_type_pre(g, &d->type, d->expr.where)) return false; cgen_write(g, " expr__"); - if (!cgen_type_post(g, &d->expr.type, d->expr.where)) return false; + if (!cgen_type_post(g, &d->type, d->expr.where)) return false; cgen_write(g, "; "); if (!cgen_set(g, NULL, "expr__", &d->expr, NULL)) return false; diff --git a/eval.c b/eval.c index 0a21c10..fb79774 100644 --- a/eval.c +++ b/eval.c @@ -732,7 +732,8 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) { v->charv = e->charl; break; case EXPR_LITERAL_STR: - v->arr = e->strl.str; + v->slice.data = e->strl.str; + v->slice.n = e->strl.len; break; case EXPR_CAST: { Value casted; diff --git a/main.c b/main.c index 306e681..3acb724 100644 --- a/main.c +++ b/main.c @@ -1,6 +1,7 @@ /* TODO: bf interpreter (& other tests) +compile time arrays, slices unicode variable names make sure initializers for global variables are compile-time constants structs @@ -94,7 +95,10 @@ int main(int argc, char **argv) { } CGenerator g; cgen_create(&g, out, &file_idents, &ev); - cgen_file(&g, &f); + if (!cgen_file(&g, &f)) { + err_fprint(TEXT_IMPORTANT("Errors occured while generating C code.\n")); + return EXIT_FAILURE; + } block_exit(NULL, f.stmts); /* exit global scope */ diff --git a/out.c b/out.c deleted file mode 100644 index a9db4d3..0000000 --- a/out.c +++ /dev/null @@ -1,72 +0,0 @@ -#include -#include -#include -typedef int8_t i8; -typedef int16_t i16; -typedef int32_t i32; -typedef int64_t i64; -typedef uint8_t u8; -typedef uint16_t u16; -typedef uint32_t u32; -typedef uint64_t u64; -typedef float f32; -typedef double f64; -typedef unsigned char bool; -typedef struct { void *data; u64 n; } slice_; -static slice_ mkslice_(void *data, u64 n) { slice_ ret; ret.data = data; ret.n = n; return ret; } -static void *e__calloc(size_t n, size_t sz) { void *ret = calloc(n, sz); if (!ret) { fprintf(stderr, "Out of memory.\n"); abort(); } return ret; } -#define false ((bool)0) -#define true ((bool)1) - - -/* declarations */ -void puti(i64 x); -void putf(f32 x); -i64 foo(void); -void main__(void); -/* code */ -int main() { - main__(); - return 0; -} - -void puti(i64 x) { - - printf("%ld\n", (long)x); -} - - -void putf(f32 x) { - - printf("%f\n", (double)x); -} - - -i64 foo(void) { - - i64 N; { - i64 expr__; expr__ = 10;N = expr__;} - slice_ numbers; { - slice_ expr__; expr__ = mkslice_(e__calloc(N, sizeof(i64)), N);numbers = expr__;} - i64 i; { - i64 expr__; expr__ = 0;i = expr__;} - while ((iflags |= TYPE_FLAG_FLEXIBLE; break; case EXPR_LITERAL_STR: - t->kind = TYPE_ARR; - t->arr.n = e->strl.len; - t->arr.of = typer_malloc(tr, sizeof *t->arr.of); - t->arr.of->flags = TYPE_FLAG_RESOLVED; - t->arr.of->kind = TYPE_BUILTIN; - t->arr.of->builtin = BUILTIN_CHAR; + t->kind = TYPE_SLICE; + t->slice = typer_malloc(tr, sizeof *t->slice); + t->slice->flags = TYPE_FLAG_RESOLVED; + t->slice->kind = TYPE_BUILTIN; + t->slice->builtin = BUILTIN_CHAR; t->flags |= TYPE_FLAG_RESOLVED; break; case EXPR_LITERAL_FLOAT: -- cgit v1.2.3