diff --git a/parser.c b/parser.c index 446b93e..a6dfbe9 100644 --- a/parser.c +++ b/parser.c @@ -91,14 +91,33 @@ instance *inewinstance(void) void idelinstance(instance *it) { + for (uint32_t i = 0; i < MAX_LINES; i++) {// TODO free vars! + if (it->lines[i] == 0) + continue; + for (int j = 0; j < 32; j++) { + variable *v = it->lines[i][j]; + if (v != 0) { + if (((uint32_t)v & OP_MAGIC) == OP_MAGIC) + continue; + + if (v->type == FUNC || v->type == CFUNC) + j++; // argcount + + if (v->tmp == 1) + itryfree(v); + } + } + + free(it->lines[i]); + } + free(it->lines); + free(it->vars); for (uint32_t i = 0; i < MAX_VARS; i++) free(it->names[i]); free(it->names); + free(it->stack); - for (uint32_t i = 0; i < MAX_LINES; i++) // TODO free vars! - free(it->lines[i]); - free(it->lines); itryfree(it->ret); free(it); } @@ -251,9 +270,27 @@ loop: itryfree(it->ret); it->ret = 0; - variable **copy = malloc(32 * sizeof(variable *)); - for (int i = 0; i < 32; i++) - copy[i] = it->lines[it->lnidx][i]; + variable **copy = (variable **)malloc(32 * sizeof(variable *)); + for (int i = 0; i < 32; i++) { + variable *v = it->lines[it->lnidx][i]; + if (v != 0) { + if (((uint32_t)v & OP_MAGIC) == OP_MAGIC) { + copy[i] = v; + continue; + } + + if (v->tmp == 1) + copy[i] = varclone(v); + else + copy[i] = v; + if (v->type == FUNC || v->type == CFUNC) { + i++; + copy[i] = it->lines[it->lnidx][i]; // argcount + } + } else { + copy[i] = 0; + } + } it->ret = isolve(it, copy, 0); free(copy); @@ -340,7 +377,7 @@ variable *isolve_(instance *it, variable **ops, uint32_t count) ops[start + 1] = 0; for (uint32_t j = start + 2; j < i; j++) { - //itryfree(ops[j]); + itryfree(ops[j]); ops[j] = 0; } } @@ -368,24 +405,30 @@ variable *isolve_(instance *it, variable **ops, uint32_t count) if (bidx == count) return 0; - if (it->sindent & SKIP) { - //itryfree(ops[aidx]); - //itryfree(ops[bidx]); - ops[aidx] = 0; - } else { - variable *v = varclone(ops[aidx]); + if (!(it->sindent & SKIP)) { + variable *v = !ops[aidx]->tmp ? varclone(ops[aidx]) : ops[aidx]; if (func(v, ops[aidx], ops[bidx]) != 0) return 0; - //itryfree(ops[aidx]); ops[aidx] = v; - //itryfree(ops[bidx]); + } else { + itryfree(ops[aidx]); + ops[aidx] = 0; } - ops[i] = 0; + itryfree(ops[bidx]); ops[bidx] = 0; + ops[i] = 0; } } } + // implicit multiply + /*if (ops[0] != 0 && ops[0]->type == NUMBER) { + for (uint32_t i = 1; i < count; i++) { + if (ops[i] != 0 && ops[i]->type == NUMBER) + ops[0]->value.f *= ops[i]->value.f; + } + }*/ + return ops[0]; } @@ -446,7 +489,7 @@ variable **iparse(instance *it, const char *s) ops[ooffset++] = (variable *)1; } offset = end; - } else if (isdigit(s[offset])) { + } else if (isdigit(s[offset])) {// || (s[offset] == '-' && isdigit(s[offset + 1]))) { size_t end = offset + 1; while (isdigit(s[end]) || s[end] == '.') end++; diff --git a/shell.c b/shell.c deleted file mode 100644 index ade66b9..0000000 --- a/shell.c +++ /dev/null @@ -1,52 +0,0 @@ -#include -#include - -#include "parser.h" - -int print(instance *it) -{ - variable *s = igetarg(it, 0); - if (s->type == NUMBER) { - if (s->value.f == (int)s->value.f) - printf("%d\n", (int)s->value.f); - else - printf("%.3f\n", s->value.f); - } else if (s->value.p != 0) { - printf("%s\n", (char *)s->value.p); - } - return 0; -} - -int main(int argc, char **argv) -{ - if (argc != 2) { - printf("Usage: %s file\n", argv[0]); - return -1; - } - - FILE *fp = fopen(argv[1], "r"); - if (fp == 0) { - printf("Could not open file: %s\n", argv[1]); - return -1; - } - - instance *it = inewinstance(); - inew_cfunc(it, "print", print); - - char *line = 0; - size_t size; - int result; - while (getline(&line, &size, fp) != -1) { - *strchr(line, '\n') = '\0'; - result = idoline(it, line); - if (result != 0) - printf("Error: %d\n", result); - //if (it->ret != 0) - // printf("%s = %f\n", line, it->ret->value.f); - } - - fclose(fp); - idelinstance(it); - return 0; -} - diff --git a/test1 b/test1 index 1d8588c..cc04423 100644 --- a/test1 +++ b/test1 @@ -3,11 +3,16 @@ # looking for proper basic function, respect for order of ops, # and respect for parentheses -2 + 5 -14 - 9 -3 * 8 + 3 -9 - 3 / 2 -3 * (8 + 3) -(9 - 3) / 2 -(4 + 5) * ((9 - 1) + 3) -5 - 3 + 4 +print(2 + 5) +print(14 - 9) +print(3 * 8 + 3) +print(9 - 3 / 2) +print(3 * (8 + 3)) +print((9 - 3) / 2) +print((4 + 5) * ((9 - 1) + 3)) +print(5 - 3 + 4) +print("") +print(-4) +print(-4 + -3) +print(-8+13) +print(4- -9) diff --git a/test3 b/test3 index 940ed6f..3c727fa 100644 --- a/test3 +++ b/test3 @@ -18,7 +18,7 @@ a = a + 1 checka d = 0 -while (d < 10) { +while (d < 20) { print(d) d = d + 1 } diff --git a/test4 b/test4 index af8595e..98731da 100644 --- a/test4 +++ b/test4 @@ -4,3 +4,7 @@ x = 4 y = solve("x-2") print(y) +print("") + +y = solve("3*x") +print(y)