change load/run style, hunt for memory leaks

master
Clyne 7 years ago
parent 8a25cb84e7
commit 31458dd042

12
calc

@ -0,0 +1,12 @@
go = 1
while (go == 1) {
print("> ")
input = gets()
answer = solve(input)
if (answer == 42) {
go = 0
}
print("\n")
print(answer)
print("\n")
}

@ -193,7 +193,8 @@ OP_DEF(eq)
else if (a->type == STRING && b->type == STRING) else if (a->type == STRING && b->type == STRING)
r->value.f = !strcmp((const char *)a->value.p, (const char *)b->value.p); r->value.f = !strcmp((const char *)a->value.p, (const char *)b->value.p);
else else
return seterror(EBADPARAM); r->value.f = 0.0f; // *sshhh*
//return seterror(EBADPARAM);
return 0; return 0;
} }

@ -84,9 +84,12 @@ void idelinstance(instance *it)
} }
free(it->lines); free(it->lines);
free(it->vars); for (uint32_t i = 0; i < MAX_VARS; i++) {
for (uint32_t i = 0; i < MAX_VARS; i++) if (it->vars[i].type == STRING)
free((void *)it->vars[i].value.p);
free(it->names[i]); free(it->names[i]);
}
free(it->vars);
free(it->names); free(it->names);
free(it->stack); free(it->stack);
@ -177,14 +180,20 @@ void inew_string(instance *it, const char *name, const char *s)
v->value.p = (uint32_t)strclone(s); v->value.p = (uint32_t)strclone(s);
} }
int idoline(instance *it, const char *s) int iaddline(instance *it, const char *s)
{ {
variable **ops = iparse(it, s); variable **ops = iparse(it, s);
if (ops == 0) if (ops == 0)
return 0; return 0;
it->lines[it->lnidx] = ops; it->lines[it->lnidx] = ops;
it->lnidx++;
return 0;
}
int irun(instance *it)
{
variable **copy; variable **copy;
it->lnidx = 0;
loop: loop:
//if (it->ret != 0) //if (it->ret != 0)
// itryfree(it->ret); // itryfree(it->ret);
@ -222,6 +231,7 @@ loop:
variable *ret = igetvar(it, "ANS"); variable *ret = igetvar(it, "ANS");
ret->type = it->ret->type; ret->type = it->ret->type;
ret->value.p = it->ret->value.p; ret->value.p = it->ret->value.p;
itryfree(it->ret);
it->ret = ret; it->ret = ret;
} }

@ -63,12 +63,20 @@ instance *inewinstance(void);
void idelinstance(instance *it); void idelinstance(instance *it);
/** /**
* Adds and runs the line through the parser instance. * Parses the given line of script, and adds it to the end of the instance's
* This parses and runs the line, while storing it at the current line index. * stored script.
* @param it the current instance * @param it the instance to use
* @param s the line to parse/run * @param s the line to load
* @return zero for success, otherwise error
*/
int iaddline(instance *it, const char *s);
/**
* Runs the instance with its loaded script until there's nothing more to run.
* @param the instance to use
* @return an error code, or zero if success
*/ */
int idoline(instance *it, const char *s); int irun(instance *it);
/** /**
* Makes a C function visible to the script. * Makes a C function visible to the script.

@ -19,6 +19,7 @@
*/ */
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
#include <string.h> #include <string.h>
#include "parser.h" #include "parser.h"
@ -37,6 +38,18 @@ int print(instance *it)
return 0; return 0;
} }
int gets(instance *it)
{
char *line = 0;
size_t size;
getline(&line, &size, stdin);
*strchr(line, '\n') = '\0';
variable *v = make_vars(0, line);
free(line);
ipush(it, (uint32_t)v);
return 0;
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
if (argc != 2) { if (argc != 2) {
@ -52,20 +65,21 @@ int main(int argc, char **argv)
instance *it = inewinstance(); instance *it = inewinstance();
inew_cfunc(it, "print", print); inew_cfunc(it, "print", print);
inew_cfunc(it, "gets", gets);
char *line = 0; char *line = 0;
size_t size; size_t size;
int result; int result;
while (getline(&line, &size, fp) != -1) { while (getline(&line, &size, fp) != -1) {
*strchr(line, '\n') = '\0'; *strchr(line, '\n') = '\0';
result = idoline(it, line); result = iaddline(it, line);
if (result != 0) if (result != 0)
printf("Error: %d\n", result); printf("Error: %d\n", result);
//if (it->ret != 0)
// printf("%s = %f\n", line, it->ret->value.f);
} }
free(line);
fclose(fp); fclose(fp);
irun(it);
idelinstance(it); idelinstance(it);
return 0; return 0;
} }

@ -11,11 +11,11 @@ func(checka) {
} }
} }
checka checka()
print("Increment a...") print("Increment a...")
a = a + 1 a = a + 1
checka checka()
d = 0 d = 0
while (d < 20) { while (d < 20) {

Loading…
Cancel
Save