memory leak fixes, stuff

master
Clyne Sullivan 7 years ago
parent f414175e0c
commit 609ff56884

@ -105,9 +105,8 @@ int ifunc_while(interpreter *it)
return 0; return 0;
} }
int ifunc_ret(interpreter *it) void iret(interpreter *it, variable *v)
{ {
variable *v = igetarg(it, 0);
switch (v->valtype) { switch (v->valtype) {
case INTEGER: case INTEGER:
inew_integer(it, "RET", INT(v)); inew_integer(it, "RET", INT(v));
@ -119,7 +118,7 @@ int ifunc_ret(interpreter *it)
inew_string(it, "RET", v->svalue); inew_string(it, "RET", v->svalue);
break; break;
default: default:
return -1; return;
break; break;
} }
if (it->ret != 0) { if (it->ret != 0) {
@ -128,6 +127,12 @@ int ifunc_ret(interpreter *it)
it->ret->svalue = v->svalue; it->ret->svalue = v->svalue;
it->ret = 0; it->ret = 0;
} }
}
int ifunc_ret(interpreter *it)
{
variable *v = igetarg(it, 0);
iret(it, v);
return 0; return 0;
} }

@ -8,7 +8,7 @@
void iload_core(interpreter *it); void iload_core(interpreter *it);
int ifunc_ret(interpreter *it); void iret(interpreter *it, variable *v);
const func_t indent_up[IUP_COUNT]; const func_t indent_up[IUP_COUNT];
const func_t indent_down[IDOWN_COUNT]; const func_t indent_down[IDOWN_COUNT];

@ -22,21 +22,22 @@ char *str_undef = "(undefined)";
void iinit(interpreter *interp) void iinit(interpreter *interp)
{ {
interp->vars = (variable *)malloc(MAX_VARS * sizeof(variable)); interp->vars = (variable *)calloc(MAX_VARS, sizeof(variable));
interp->vnames = (char **)malloc(MAX_VARS * sizeof(char *)); interp->vnames = (char **)calloc(MAX_VARS, sizeof(char *));
interp->stack = (stack_t *)malloc(MAX_STACK * sizeof(stack_t)); interp->stack = (stack_t *)calloc(MAX_STACK, sizeof(stack_t));
interp->stidx = 0; interp->stidx = 0;
interp->lines = (variable ***)malloc(MAX_LINES * sizeof(variable **)); interp->lines = (variable ***)calloc(MAX_LINES, sizeof(variable **));
interp->lnidx = 0; interp->lnidx = 0;
interp->indent = 0; interp->indent = 0;
interp->sindent = 0; interp->sindent = 0;
interp->ret = 0;
for (unsigned int i = 0; i < MAX_VARS; i++) { //for (unsigned int i = 0; i < MAX_VARS; i++) {
interp->vars[i].used = 0; // interp->vars[i].used = 0;
interp->vnames[i] = 0; // interp->vnames[i] = 0;
} //}
for (unsigned int i = 0; i < MAX_LINES; i++) //for (unsigned int i = 0; i < MAX_LINES; i++)
interp->lines[i] = 0; // interp->lines[i] = 0;
iload_core(interp); iload_core(interp);
} }
@ -75,7 +76,8 @@ variable *interpreter_get_variable(interpreter *interp, const char *name)
v->valtype = STRING; v->valtype = STRING;
v->value = 0; v->value = 0;
v->svalue = str_undef; v->svalue = str_undef;
interp->vnames[i] = strclone(name); char *s = strclone(name);
interp->vnames[i] = s;
return v; return v;
} else if (interp->vnames[i] != 0 && !strcmp(interp->vnames[i], name)) { } else if (interp->vnames[i] != 0 && !strcmp(interp->vnames[i], name)) {
return &interp->vars[i]; return &interp->vars[i];
@ -187,9 +189,9 @@ variable *make_var(interpreter *interp, const char *line, uint32_t *next)
} }
} }
variable *v; variable *v;
if (dec) { if (dec)
v = vmakef(strtof(line, 0)); v = vmakef(strtof(line, 0));
} else else
v = vmake(0, INTEGER, (void *)atoi(line)); v = vmake(0, INTEGER, (void *)atoi(line));
*next = end; *next = end;
return v; return v;
@ -208,7 +210,8 @@ int idoline(interpreter *interp, const char *line)
if (line[offset] == '#') if (line[offset] == '#')
return 0; return 0;
interp->lines[interp->lnidx] = (variable **)calloc(8, sizeof(variable *)); variable **linebuf = (variable **)calloc(8, sizeof(variable *));
interp->lines[interp->lnidx] = linebuf;
variable **ops = interp->lines[interp->lnidx]; variable **ops = interp->lines[interp->lnidx];
// step 1 - convert to tokens // step 1 - convert to tokens
@ -355,7 +358,7 @@ fail:
variable *idoexpr(interpreter *interp, const char *line) variable *idoexpr(interpreter *interp, const char *line)
{ {
variable *result = (variable *)malloc(sizeof(variable)); variable *result = (variable *)calloc(1, sizeof(variable));
char *mline = line; char *mline = line;
void *ops[16]; void *ops[16];
uint32_t ooffset = 0; uint32_t ooffset = 0;
@ -430,9 +433,8 @@ variable *idoexpr(interpreter *interp, const char *line)
// step 2 - do operations // step 2 - do operations
result->valtype = ((variable *)ops[0])->valtype; result->valtype = ((variable *)ops[0])->valtype;
result->value = ((variable *)ops[0])->value; result->value = ((variable *)ops[0])->value;
for (uint32_t i = 1; i < ooffset; i += 2) { for (uint32_t i = 1; i < ooffset; i += 2)
iopfuncs[(uint32_t)ops[i] - 1](result, result, ops[i + 1]); iopfuncs[(uint32_t)ops[i] - 1](result, result, ops[i + 1]);
}
for (uint32_t i = 0; i < ooffset; i += 2) { for (uint32_t i = 0; i < ooffset; i += 2) {
if (!((variable *)ops[i])->used) { if (!((variable *)ops[i])->used) {

@ -12,10 +12,9 @@ extern char *str_func;
char *fixstring(char *s) char *fixstring(char *s)
{ {
int len = strlen(s); char *n = malloc(strlen(s) + 1);
char *n = malloc(len + 1); int j = 0;
int i, j; for (int i = 0; s[i] != '\0'; i++, j++) {
for (i = 0, j = 0; s[i] != '\0'; i++, j++) {
if (s[i] == '\\') { if (s[i] == '\\') {
if (s[i + 1] == 'n') if (s[i + 1] == 'n')
n[j] = '\n'; n[j] = '\n';

@ -3,8 +3,8 @@
#include <stdint.h> #include <stdint.h>
#define INT(v) (*((int32_t *)&v->value)) #define INT(v) (*((int32_t *)&(v)->value))
#define FLOAT(v) (*((float *)&v->value)) #define FLOAT(v) (*((float *)&(v)->value))
typedef struct { typedef struct {
uint8_t used :1; uint8_t used :1;

Loading…
Cancel
Save