diff --git a/Makefile b/Makefile index 319184f..c588c8f 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,11 @@ #CC = gcc -m32 #AR = ar -CC = arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 -fsigned-char +CC = arm-none-eabi-gcc -mcpu=cortex-m4 -mthumb -mfloat-abi=hard -mfpu=fpv4-sp-d16 AR = arm-none-eabi-ar -CFLAGS = -Wall -Wextra -I. -fno-builtin -ggdb +CFLAGS = -Wall -Wextra -Werror -pedantic \ + -Wno-discarded-qualifiers \ + -I. -fsigned-char -fno-builtin -ggdb all: $(CC) $(CFLAGS) -c shelpers.c @@ -12,7 +14,6 @@ all: $(CC) $(CFLAGS) -c stack.c $(CC) $(CFLAGS) -c ops.c $(CC) $(CFLAGS) -c variable.c - $(CC) $(CFLAGS) -c stdlib.c $(AR) r libinterp.a *.o @rm -f *.o - #$(CC) $(CFLAGS) shell.c -o shell -L. -l interp + #$(CC) $(CFLAGS) shell.c *.o -o shell #-L. -l interp diff --git a/builtins.c b/builtins.c index 9c82b8d..309e1d8 100644 --- a/builtins.c +++ b/builtins.c @@ -94,13 +94,15 @@ int ifunc_do(interpreter *it) int ifunc_while(interpreter *it) { - int c = igetarg_integer(it, 0); + //int c = igetarg_integer(it, 0); ipop(it); int nidx = (int)ipop(it); - if (c != 0) { + //if (c != 0) { //ipush(it, (void *)nidx); it->lnidx = nidx - 1; - } + //} else { + // c++; + //} ipush(it, 0); return 0; } diff --git a/parser.c b/parser.c index b2d27e6..0c88cb0 100644 --- a/parser.c +++ b/parser.c @@ -10,8 +10,8 @@ #include #include -#define MAX_VARS 48 -#define MAX_STACK 16 +#define MAX_VARS 64 +#define MAX_STACK 32 #define MAX_LINES 1000 extern int atoi(const char *); @@ -32,13 +32,6 @@ void iinit(interpreter *interp) interp->sindent = 0; interp->ret = 0; - //for (unsigned int i = 0; i < MAX_VARS; i++) { - // interp->vars[i].used = 0; - // interp->vnames[i] = 0; - //} - //for (unsigned int i = 0; i < MAX_LINES; i++) - // interp->lines[i] = 0; - iload_core(interp); } @@ -188,11 +181,15 @@ variable *make_var(interpreter *interp, const char *line, uint32_t *next) } } } + char *copy = (char *)malloc(end + 1); + strncpy(copy, line, end); + copy[end] = '\0'; variable *v; if (dec) - v = vmakef(strtof(line, 0)); + v = vmakef(strtof(copy, 0)); else - v = vmake(0, INTEGER, (void *)atoi(line)); + v = vmake(0, INTEGER, (void *)atoi(copy)); + free(copy); *next = end; return v; } @@ -207,7 +204,7 @@ int idoline(interpreter *interp, const char *line) if (line[0] == '\0') return 0; skipblank(line, eol, &offset); - if (line[offset] == '#') + if (line[offset] == '#' || eol(line[offset])) return 0; variable **linebuf = (variable **)calloc(8, sizeof(variable *)); @@ -292,10 +289,17 @@ cont: // eval expressions ooffset = 1; for (; ops[ooffset] != 0 && ops[ooffset] != (void *)-1; ooffset++) { - if (ops[ooffset]->valtype == EXPR) - ops[ooffset] = idoexpr(interp, ops[ooffset]->svalue); + if (ops[ooffset]->valtype == EXPR) { + char *expr = strclone(ops[ooffset]->svalue); + variable *r = idoexpr(interp, expr); + ops[ooffset] = r; + free(expr); + } } + if (ops[ooffset] == (void *)-1) + interp->ret = ops[ooffset + 1]; + if (ops[0]->fromc) { for (uint32_t i = ooffset; --i > 0;) ipush(interp, ops[i]); @@ -328,9 +332,6 @@ cont: interp->indent++; } - if (ops[ooffset] == (void *)-1) - interp->ret = ops[ooffset + 1]; - if ((int32_t)interp->stidx < 0) { interp->stidx = 0; return -5; @@ -358,12 +359,10 @@ fail: variable *idoexpr(interpreter *interp, const char *line) { - variable *result = (variable *)calloc(1, sizeof(variable)); - char *mline = line; void *ops[16]; uint32_t ooffset = 0; uint32_t offset = 0; - uint32_t next; + uint32_t next = 0; // step 1 - break apart line @@ -389,16 +388,15 @@ variable *idoexpr(interpreter *interp, const char *line) ops[ooffset] = idoexpr(interp, line + offset + 1); offset = i + 1; } else { - uint32_t end = offset; - char cend; - if (line[offset] != '\"') { - for (; isalnum(line[end]) || line[end] == '.'; end++); - cend = line[end]; - mline[end] = ' '; - } - ops[ooffset] = make_var(interp, line + offset, &next); - if (end != 0) - mline[end] = cend; + uint32_t len = offset; + for (; isalnum(line[len]) || line[len] == '.'; len++); + len -= offset; + char *copy = (char *)malloc(len + 1); + strncpy(copy, line + offset, len); + copy[len] = '\0'; + variable *v = make_var(interp, copy, &next); + free(copy); + ops[ooffset] = v; offset += next; } if (ops[ooffset] == 0) @@ -431,10 +429,16 @@ variable *idoexpr(interpreter *interp, const char *line) return 0; // step 2 - do operations + variable *result = (variable *)calloc(1, sizeof(variable)); result->valtype = ((variable *)ops[0])->valtype; result->value = ((variable *)ops[0])->value; for (uint32_t i = 1; i < ooffset; i += 2) iopfuncs[(uint32_t)ops[i] - 1](result, result, ops[i + 1]); + + if (result->valtype == INTEGER) + isetstr(result); + else + fsetstr(result); for (uint32_t i = 0; i < ooffset; i += 2) { if (!((variable *)ops[i])->used) { @@ -444,13 +448,6 @@ variable *idoexpr(interpreter *interp, const char *line) free(ops[i]); } } - - result->used = 0; - result->svalue = 0; - if (result->valtype == INTEGER) - isetstr(result); - else - fsetstr(result); return result; } diff --git a/shell.c b/shell.c index 682213c..c5460e7 100644 --- a/shell.c +++ b/shell.c @@ -3,10 +3,13 @@ #include "stack.h" +#include #include -#include +//#include #include +extern int rand(void); + int s_put(interpreter *it) { char *s = igetarg_string(it, 0); @@ -60,25 +63,21 @@ int concat(interpreter *it) return 0; } -int quit(interpreter *it) +int script_rand(interpreter *it) { - (void)it; - exit(0); + static variable v; + v.valtype = INTEGER; + unsigned int mod = igetarg_integer(it, 0); + unsigned int val = rand(); + INT(&v) = val % mod; + isetstr(&v); + iret(it, &v); return 0; } -int expr(interpreter *it) +int line(interpreter *it) { - variable *v = igetarg(it, 0); - variable *r = igetarg(it, 1); - int len = strlen(v->svalue); - char *s = malloc(len + 1); - strcpy(s, v->svalue); - s[len] = 0; - variable *q = idoexpr(it, s); - r->valtype = q->valtype; - r->value = q->value; - r->svalue = q->svalue; + (void)it; return 0; } @@ -100,10 +99,10 @@ int main(int argc, char **argv) iinit(&interp); inew_cfunc(&interp, "print", s_put); inew_cfunc(&interp, "tp", s_type); - inew_cfunc(&interp, "q", quit); inew_cfunc(&interp, "gets", input); inew_cfunc(&interp, "concat", concat); - inew_cfunc(&interp, "expr", expr); + inew_cfunc(&interp, "rand", script_rand); + inew_cfunc(&interp, "line", line); char *line = 0; diff --git a/stdlib.c b/stdlib.c deleted file mode 100644 index 3299d0b..0000000 --- a/stdlib.c +++ /dev/null @@ -1,21 +0,0 @@ -#include - -char *snprintf(char *buf, unsigned int max, const char *format, ...) -{ - (void)max; - va_list args; - va_start(args, format); - - buf[0] = '0'; - buf[1] = '\0'; - - va_end(args); - return buf; -} - -float strtof(const char *s, char **endptr) -{ - (void)s; - (void)endptr; - return 0.0f; -} diff --git a/stdlib.h b/stdlib.h index 0001edc..b87a823 100644 --- a/stdlib.h +++ b/stdlib.h @@ -4,6 +4,6 @@ char *snprintf(char *buf, unsigned int max, const char *format, ...); float strtof(const char *s, char **endptr); -extern int atoi(const char *); +int atoi(const char *); #endif // STDLIB_H_ diff --git a/variable.c b/variable.c index bcf96cb..8b7dc12 100644 --- a/variable.c +++ b/variable.c @@ -37,7 +37,6 @@ variable *vmake(uint8_t fromc, uint8_t valtype, void *value) v->svalue = 0; switch (valtype) { case STRING: - v->value = 0; v->svalue = fixstring(value); free(value); break; @@ -50,7 +49,6 @@ variable *vmake(uint8_t fromc, uint8_t valtype, void *value) v->svalue = str_func; break; case EXPR: - v->value = 0; v->svalue = value; break; } @@ -71,15 +69,15 @@ variable *vmakef(float value) void fsetstr(variable *f) { if (f->svalue == 0 || f->svalue == str_undef) - f->svalue = (char *)malloc(16); - snprintf(f->svalue, 16, "%f", FLOAT(f)); + f->svalue = (char *)malloc(32); + snprintf(f->svalue, 32, "%f", FLOAT(f)); } void isetstr(variable *i) { if (i->svalue == 0 || i->svalue == str_undef) - i->svalue = (char *)malloc(12); - snprintf(i->svalue, 12, "%d", (int)INT(i)); + i->svalue = (char *)malloc(32); + snprintf(i->svalue, 32, "%d", (int)INT(i)); } variable *itostring(variable *v)