summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cgen.c8
-rw-r--r--main.c1
-rw-r--r--out.c8
-rw-r--r--test.toc5
4 files changed, 8 insertions, 14 deletions
diff --git a/cgen.c b/cgen.c
index 2180e6a..bea7482 100644
--- a/cgen.c
+++ b/cgen.c
@@ -519,7 +519,7 @@ static bool cgen_new_slice(CGenerator *g, Type *t, IdentID id, Location where) {
cgen_ident_id(g, id);
cgen_write(g, ";");
cgen_ident_id(g, id);
- cgen_write(g, ".data = calloc(s");
+ cgen_write(g, ".data = e__calloc(s");
cgen_ident_id(g, id);
cgen_write(g, ", sizeof(");
if (t->arr.of->kind == TYPE_ARR) {
@@ -844,7 +844,7 @@ static bool cgen_expr(CGenerator *g, Expression *e) {
} break;
case EXPR_NEW: {
if (e->new.n) {
- cgen_write(g, "mkslice_(calloc(");
+ cgen_write(g, "mkslice_(e__calloc(");
if (!cgen_expr(g, e->new.n)) return false;
cgen_write(g, ", sizeof(");
if (!cgen_type_pre(g, &e->new.type, e->where)) return false;
@@ -859,7 +859,7 @@ static bool cgen_expr(CGenerator *g, Expression *e) {
return false;
if (!cgen_type_post(g, &e->type, e->where))
return false;
- cgen_write(g, ")calloc(1, sizeof(");
+ cgen_write(g, ")e__calloc(1, sizeof(");
if (!cgen_type_pre(g, t, e->where))
return false;
if (!cgen_type_post(g, t, e->where))
@@ -1207,6 +1207,7 @@ static bool cgen_file(CGenerator *g, ParsedFile *f) {
g->file = f;
cgen_write(g, "#include <stdint.h>\n"
"#include <stdlib.h>\n"
+ "#include <stdio.h>\n"
"typedef int8_t i8;\n"
"typedef int16_t i16;\n"
"typedef int32_t i32;\n"
@@ -1220,6 +1221,7 @@ static bool cgen_file(CGenerator *g, ParsedFile *f) {
"typedef unsigned char bool;\n"
"typedef struct { void *data; u64 n; } slice_;\n"
"static slice_ mkslice_(void *data, u64 n) { slice_ ret; ret.data = data; ret.n = n; return ret; }\n"
+ "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; }\n"
"#define false ((bool)0)\n"
"#define true ((bool)1)\n\n\n");
if (!cgen_decls_file(g, f))
diff --git a/main.c b/main.c
index 11fdeb4..306e681 100644
--- a/main.c
+++ b/main.c
@@ -1,6 +1,5 @@
/*
TODO:
-error on failed calloc in output
bf interpreter (& other tests)
unicode variable names
make sure initializers for global variables are compile-time constants
diff --git a/out.c b/out.c
index a1950de..a9db4d3 100644
--- a/out.c
+++ b/out.c
@@ -1,5 +1,6 @@
#include <stdint.h>
#include <stdlib.h>
+#include <stdio.h>
typedef int8_t i8;
typedef int16_t i16;
typedef int32_t i32;
@@ -13,6 +14,7 @@ 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)
@@ -28,8 +30,6 @@ int main() {
return 0;
}
-#include <stdio.h>
-#define kasfdhkjasdfhjk ;
void puti(i64 x) {
printf("%ld\n", (long)x);
@@ -47,7 +47,7 @@ i64 foo(void) {
i64 N; {
i64 expr__; expr__ = 10;N = expr__;}
slice_ numbers; {
- slice_ expr__; expr__ = mkslice_(calloc(N, sizeof(i64)), N);numbers = expr__;}
+ slice_ expr__; expr__ = mkslice_(e__calloc(N, sizeof(i64)), N);numbers = expr__;}
i64 i; {
i64 expr__; expr__ = 0;i = expr__;}
while ((i<N)) {
@@ -65,8 +65,6 @@ void main__(void) {
i64 N = 5;
(puti(N));
- f32 M = 1.4320000410079956;
- (putf(M));
i64( x[11]) = {0};
(puti((foo())));
}
diff --git a/test.toc b/test.toc
index 40ddbe5..eea04f7 100644
--- a/test.toc
+++ b/test.toc
@@ -1,6 +1,3 @@
-#C("#include <stdio.h>
-#define kasfdhkjasdfhjk ");
-
puti @= fn(x: int) {
#C("printf(\"%ld\\n\", (long)x)");
};
@@ -23,8 +20,6 @@ foo @= fn() int {
main @= fn() {
N @= 5;
puti(N);
- M @= 1.432;
- putf(M);
x : [foo()]int;
puti(foo());