summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLeo Tenenbaum <pommicket@gmail.com>2019-10-21 19:27:47 -0400
committerLeo Tenenbaum <pommicket@gmail.com>2019-10-21 19:27:47 -0400
commitd28b38f18575ae9fe65936fc71ddcb7ea91131cd (patch)
treed97bc4473ad2d26d556f5e7de1ff215654729ff9
parent52fa150ba836c0ddd7b02623541fb307478a9088 (diff)
finished slice notation
-rw-r--r--cgen.c17
-rw-r--r--eval.c2
-rw-r--r--main.c2
-rw-r--r--out.c16
-rw-r--r--parse.c2
-rw-r--r--test.toc11
6 files changed, 33 insertions, 17 deletions
diff --git a/cgen.c b/cgen.c
index 1fb0166..c635d9c 100644
--- a/cgen.c
+++ b/cgen.c
@@ -674,7 +674,12 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) {
return false;
if (s->to && !cgen_expr_pre(g, s->to))
return false;
- cgen_write(g, "u64 ");
+ cgen_write(g, "slice_ ");
+ cgen_ident_id(g, s_id);
+ cgen_write(g, "; { slice_ of__ = ");
+ if (!cgen_expr(g, s->of))
+ return false;
+ cgen_write(g, "; u64 ");
cgen_ident_id(g, from_id);
cgen_write(g, " = ");
if (s->from) {
@@ -683,8 +688,6 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) {
} else {
cgen_write(g, "0");
}
- cgen_write(g, "; slice_ ");
- cgen_ident_id(g, s_id);
cgen_write(g, "; ");
cgen_ident_id(g, s_id);
cgen_write(g, ".data = (");
@@ -693,9 +696,7 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) {
cgen_write(g, "(*)");
if (!cgen_type_post(g, e->type.slice, e->where))
return false;
- cgen_write(g, ")(");
- if (!cgen_expr(g, s->of))
- return false;
+ cgen_write(g, ")(of__");
if (s->of->type.kind == TYPE_SLICE) {
cgen_write(g, ".data");
}
@@ -708,11 +709,11 @@ static bool cgen_expr_pre(CGenerator *g, Expression *e) {
if (!cgen_expr(g, s->to))
return false;
} else {
- /* TODO */
+ cgen_write(g, "of__.n - 1");
}
cgen_write(g, " - ");
cgen_ident_id(g, from_id);
- cgen_write(g, ";");
+ cgen_write(g, "; }");
cgen_nl(g);
} break;
case EXPR_LITERAL_INT:
diff --git a/eval.c b/eval.c
index aa17e5b..0a21c10 100644
--- a/eval.c
+++ b/eval.c
@@ -871,7 +871,7 @@ static bool eval_expr(Evaluator *ev, Expression *e, Value *v) {
return false;
}
void *ptr1, *ptr2;
- if (to < from) {
+ if (from < to) {
if (!eval_val_ptr_at_index(ev, e->where, &ofv, from, of_type, &s->from->type, &ptr1, NULL))
return false;
if (!eval_val_ptr_at_index(ev, e->where, &ofv, to, of_type, &s->to->type, &ptr2, NULL))
diff --git a/main.c b/main.c
index 041eaf7..2f335e1 100644
--- a/main.c
+++ b/main.c
@@ -1,12 +1,12 @@
/*
TODO:
-slice notation (x[a:b])
fix constants
bf interpreter (& other tests)
error on failed calloc in output
unicode variable names
make sure initializers for global variables are compile-time constants
structs
+length of slice/arr with .len
don't allow while {3; 5} (once break is added)
any odd number of "s for a string
modifiable strings:
diff --git a/out.c b/out.c
index a4dc39f..9994c0d 100644
--- a/out.c
+++ b/out.c
@@ -18,6 +18,7 @@ typedef struct { void *data; u64 n; } slice_;
/* declarations */
void puti(i64 x);
+i64 foo(void);
void main__(void);
/* code */
int main() {
@@ -33,7 +34,7 @@ void puti(i64 x) {
}
-void main__(void) {
+i64 foo(void) {
i64 N; {
i64 expr__; expr__ = 10;N = expr__;}
@@ -45,8 +46,17 @@ void main__(void) {
(((i64(*))(numbers.data))[i]) = i;;
i = (i+1);;
};
- u64 a3_ = 0; slice_ a2_; a2_.data = (i64(*))(numbers.data) + a3_; a2_.n = 8 - a3_;
- (puti((((i64(*))(a2_.data))[0])));
+ slice_ a2_; { slice_ of__ = numbers; u64 a3_ = 5; a2_.data = (i64(*))(of__.data) + a3_; a2_.n = 7 - a3_; }
+ slice_ a4_; { slice_ of__ = numbers; u64 a5_ = 2; a4_.data = (i64(*))(of__.data) + a5_; a4_.n = of__.n - 1 - a5_; }
+ slice_ a6_; { slice_ of__ = numbers; u64 a7_ = 0; a6_.data = (i64(*))(of__.data) + a7_; a6_.n = 6 - a7_; }
+ return (((((i64(*))(a2_.data))[1])+(((i64(*))(a4_.data))[0]))+(((i64(*))(a6_.data))[3]));
+}
+
+
+void main__(void) {
+
+ i64( x[11]) = {0};
+ (puti((foo())));
}
diff --git a/parse.c b/parse.c
index 22473bc..a8ffa93 100644
--- a/parse.c
+++ b/parse.c
@@ -1660,7 +1660,7 @@ static void fprint_expr(FILE *out, Expression *e) {
fprintf(out, "[");
if (s->from) fprint_expr(out, s->from);
fprintf(out, ":");
- if (s->from) fprint_expr(out, s->to);
+ if (s->to) fprint_expr(out, s->to);
fprintf(out, "]");
} break;
}
diff --git a/test.toc b/test.toc
index 404b85f..bf98789 100644
--- a/test.toc
+++ b/test.toc
@@ -5,13 +5,18 @@ puti @= fn(x: int) {
#C("printf(\"%ld\\n\", (long)x)");
};
-main @= fn() {
+foo @= fn() int {
N := 10;
numbers := new(int, N);
i := 0;
while i < N {
numbers[i] = i;
- i = i + 1;
+ i = i + 1;
}
- puti(numbers[:8][0]);
+ numbers[5:7][1] + numbers[2:][0] + numbers[:6][3]
+};
+
+main @= fn() {
+ x : [foo()]int;
+ puti(foo());
};