testing new features, imp. mult & negatives

master
Clyne Sullivan 7 years ago
parent 3fc57ae22f
commit d90a58eea6

@ -91,14 +91,33 @@ instance *inewinstance(void)
void idelinstance(instance *it) 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); free(it->vars);
for (uint32_t i = 0; i < MAX_VARS; i++) for (uint32_t i = 0; i < MAX_VARS; i++)
free(it->names[i]); free(it->names[i]);
free(it->names); free(it->names);
free(it->stack); 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); itryfree(it->ret);
free(it); free(it);
} }
@ -251,9 +270,27 @@ loop:
itryfree(it->ret); itryfree(it->ret);
it->ret = 0; it->ret = 0;
variable **copy = malloc(32 * sizeof(variable *)); variable **copy = (variable **)malloc(32 * sizeof(variable *));
for (int i = 0; i < 32; i++) for (int i = 0; i < 32; i++) {
copy[i] = it->lines[it->lnidx][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); it->ret = isolve(it, copy, 0);
free(copy); free(copy);
@ -340,7 +377,7 @@ variable *isolve_(instance *it, variable **ops, uint32_t count)
ops[start + 1] = 0; ops[start + 1] = 0;
for (uint32_t j = start + 2; j < i; j++) { for (uint32_t j = start + 2; j < i; j++) {
//itryfree(ops[j]); itryfree(ops[j]);
ops[j] = 0; ops[j] = 0;
} }
} }
@ -368,24 +405,30 @@ variable *isolve_(instance *it, variable **ops, uint32_t count)
if (bidx == count) if (bidx == count)
return 0; return 0;
if (it->sindent & SKIP) { if (!(it->sindent & SKIP)) {
//itryfree(ops[aidx]); variable *v = !ops[aidx]->tmp ? varclone(ops[aidx]) : ops[aidx];
//itryfree(ops[bidx]);
ops[aidx] = 0;
} else {
variable *v = varclone(ops[aidx]);
if (func(v, ops[aidx], ops[bidx]) != 0) if (func(v, ops[aidx], ops[bidx]) != 0)
return 0; return 0;
//itryfree(ops[aidx]);
ops[aidx] = v; ops[aidx] = v;
//itryfree(ops[bidx]); } else {
itryfree(ops[aidx]);
ops[aidx] = 0;
} }
ops[i] = 0; itryfree(ops[bidx]);
ops[bidx] = 0; 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]; return ops[0];
} }
@ -446,7 +489,7 @@ variable **iparse(instance *it, const char *s)
ops[ooffset++] = (variable *)1; ops[ooffset++] = (variable *)1;
} }
offset = end; offset = end;
} else if (isdigit(s[offset])) { } else if (isdigit(s[offset])) {// || (s[offset] == '-' && isdigit(s[offset + 1]))) {
size_t end = offset + 1; size_t end = offset + 1;
while (isdigit(s[end]) || s[end] == '.') while (isdigit(s[end]) || s[end] == '.')
end++; end++;

@ -1,52 +0,0 @@
#include <stdio.h>
#include <string.h>
#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;
}

21
test1

@ -3,11 +3,16 @@
# looking for proper basic function, respect for order of ops, # looking for proper basic function, respect for order of ops,
# and respect for parentheses # and respect for parentheses
2 + 5 print(2 + 5)
14 - 9 print(14 - 9)
3 * 8 + 3 print(3 * 8 + 3)
9 - 3 / 2 print(9 - 3 / 2)
3 * (8 + 3) print(3 * (8 + 3))
(9 - 3) / 2 print((9 - 3) / 2)
(4 + 5) * ((9 - 1) + 3) print((4 + 5) * ((9 - 1) + 3))
5 - 3 + 4 print(5 - 3 + 4)
print("")
print(-4)
print(-4 + -3)
print(-8+13)
print(4- -9)

@ -18,7 +18,7 @@ a = a + 1
checka checka
d = 0 d = 0
while (d < 10) { while (d < 20) {
print(d) print(d)
d = d + 1 d = d + 1
} }

@ -4,3 +4,7 @@
x = 4 x = 4
y = solve("x-2") y = solve("x-2")
print(y) print(y)
print("")
y = solve("3*x")
print(y)

Loading…
Cancel
Save