summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.gitignore3
-rw-r--r--cgen.c17
-rw-r--r--eval.c3
-rw-r--r--main.c6
-rw-r--r--out.c72
-rw-r--r--test.toc22
-rw-r--r--types.c11
7 files changed, 41 insertions, 93 deletions
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 <stdint.h>
-#include <stdlib.h>
-#include <stdio.h>
-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 ((i<N)) {
- (((i64(*))(numbers.data))[i]) = i;;
- i = (i+1);;
- };
- slice_ a1_; { slice_ of__ = numbers; u64 a2_ = 5; a1_.data = (i64(*))(of__.data) + a2_; a1_.n = 7 - a2_; }
- slice_ a3_; { slice_ of__ = numbers; u64 a4_ = 2; a3_.data = (i64(*))(of__.data) + a4_; a3_.n = of__.n - 1 - a4_; }
- slice_ a5_; { slice_ of__ = numbers; u64 a6_ = 0; a5_.data = (i64(*))(of__.data) + a6_; a5_.n = 6 - a6_; }
- return (((((i64(*))(a1_.data))[1])+(((i64(*))(a3_.data))[0]))+(((i64(*))(a5_.data))[3]));
-}
-
-
-void main__(void) {
-
- i64 N = 5;
- (puti(N));
- i64( x[11]) = {0};
- (puti((foo())));
-}
-
-
diff --git a/test.toc b/test.toc
index eea04f7..71545c6 100644
--- a/test.toc
+++ b/test.toc
@@ -1,9 +1,16 @@
puti @= fn(x: int) {
- #C("printf(\"%ld\\n\", (long)x)");
+ #C("printf(\"%ld\\n\", (long)x);
+");
+};
+
+putch @= fn(x: char) {
+ #C("printf(\"%c\\n\", x);
+");
};
putf @= fn(x: float) {
- #C("printf(\"%f\\n\", (double)x)");
+ #C("printf(\"%f\\n\", (double)x);
+");
};
foo @= fn() int {
@@ -22,5 +29,12 @@ main @= fn() {
puti(N);
x : [foo()]int;
puti(foo());
-
-};
+ teststr := "Hello!";
+ i := 0;
+ while i < 5 {
+ putch(teststr[i]);
+ i = i + 1;
+ }
+ y:int=5+#C("(1<<3)");
+ puti(y);
+};
diff --git a/types.c b/types.c
index c96f5e6..0804a18 100644
--- a/types.c
+++ b/types.c
@@ -542,12 +542,11 @@ static bool types_expr(Typer *tr, Expression *e) {
t->flags |= 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: