stable for calculator

master
Clyne Sullivan 7 years ago
parent 609ff56884
commit 7b08dbbbe9

@ -1,9 +1,11 @@
#CC = gcc -m32 #CC = gcc -m32
#AR = ar #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 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: all:
$(CC) $(CFLAGS) -c shelpers.c $(CC) $(CFLAGS) -c shelpers.c
@ -12,7 +14,6 @@ all:
$(CC) $(CFLAGS) -c stack.c $(CC) $(CFLAGS) -c stack.c
$(CC) $(CFLAGS) -c ops.c $(CC) $(CFLAGS) -c ops.c
$(CC) $(CFLAGS) -c variable.c $(CC) $(CFLAGS) -c variable.c
$(CC) $(CFLAGS) -c stdlib.c
$(AR) r libinterp.a *.o $(AR) r libinterp.a *.o
@rm -f *.o @rm -f *.o
#$(CC) $(CFLAGS) shell.c -o shell -L. -l interp #$(CC) $(CFLAGS) shell.c *.o -o shell #-L. -l interp

@ -94,13 +94,15 @@ int ifunc_do(interpreter *it)
int ifunc_while(interpreter *it) int ifunc_while(interpreter *it)
{ {
int c = igetarg_integer(it, 0); //int c = igetarg_integer(it, 0);
ipop(it); ipop(it);
int nidx = (int)ipop(it); int nidx = (int)ipop(it);
if (c != 0) { //if (c != 0) {
//ipush(it, (void *)nidx); //ipush(it, (void *)nidx);
it->lnidx = nidx - 1; it->lnidx = nidx - 1;
} //} else {
// c++;
//}
ipush(it, 0); ipush(it, 0);
return 0; return 0;
} }

@ -10,8 +10,8 @@
#include <memory.h> #include <memory.h>
#include <string.h> #include <string.h>
#define MAX_VARS 48 #define MAX_VARS 64
#define MAX_STACK 16 #define MAX_STACK 32
#define MAX_LINES 1000 #define MAX_LINES 1000
extern int atoi(const char *); extern int atoi(const char *);
@ -32,13 +32,6 @@ void iinit(interpreter *interp)
interp->sindent = 0; interp->sindent = 0;
interp->ret = 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); 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; variable *v;
if (dec) if (dec)
v = vmakef(strtof(line, 0)); v = vmakef(strtof(copy, 0));
else else
v = vmake(0, INTEGER, (void *)atoi(line)); v = vmake(0, INTEGER, (void *)atoi(copy));
free(copy);
*next = end; *next = end;
return v; return v;
} }
@ -207,7 +204,7 @@ int idoline(interpreter *interp, const char *line)
if (line[0] == '\0') if (line[0] == '\0')
return 0; return 0;
skipblank(line, eol, &offset); skipblank(line, eol, &offset);
if (line[offset] == '#') if (line[offset] == '#' || eol(line[offset]))
return 0; return 0;
variable **linebuf = (variable **)calloc(8, sizeof(variable *)); variable **linebuf = (variable **)calloc(8, sizeof(variable *));
@ -292,10 +289,17 @@ cont:
// eval expressions // eval expressions
ooffset = 1; ooffset = 1;
for (; ops[ooffset] != 0 && ops[ooffset] != (void *)-1; ooffset++) { for (; ops[ooffset] != 0 && ops[ooffset] != (void *)-1; ooffset++) {
if (ops[ooffset]->valtype == EXPR) if (ops[ooffset]->valtype == EXPR) {
ops[ooffset] = idoexpr(interp, ops[ooffset]->svalue); 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) { if (ops[0]->fromc) {
for (uint32_t i = ooffset; --i > 0;) for (uint32_t i = ooffset; --i > 0;)
ipush(interp, ops[i]); ipush(interp, ops[i]);
@ -328,9 +332,6 @@ cont:
interp->indent++; interp->indent++;
} }
if (ops[ooffset] == (void *)-1)
interp->ret = ops[ooffset + 1];
if ((int32_t)interp->stidx < 0) { if ((int32_t)interp->stidx < 0) {
interp->stidx = 0; interp->stidx = 0;
return -5; return -5;
@ -358,12 +359,10 @@ fail:
variable *idoexpr(interpreter *interp, const char *line) variable *idoexpr(interpreter *interp, const char *line)
{ {
variable *result = (variable *)calloc(1, sizeof(variable));
char *mline = line;
void *ops[16]; void *ops[16];
uint32_t ooffset = 0; uint32_t ooffset = 0;
uint32_t offset = 0; uint32_t offset = 0;
uint32_t next; uint32_t next = 0;
// step 1 - break apart line // step 1 - break apart line
@ -389,16 +388,15 @@ variable *idoexpr(interpreter *interp, const char *line)
ops[ooffset] = idoexpr(interp, line + offset + 1); ops[ooffset] = idoexpr(interp, line + offset + 1);
offset = i + 1; offset = i + 1;
} else { } else {
uint32_t end = offset; uint32_t len = offset;
char cend; for (; isalnum(line[len]) || line[len] == '.'; len++);
if (line[offset] != '\"') { len -= offset;
for (; isalnum(line[end]) || line[end] == '.'; end++); char *copy = (char *)malloc(len + 1);
cend = line[end]; strncpy(copy, line + offset, len);
mline[end] = ' '; copy[len] = '\0';
} variable *v = make_var(interp, copy, &next);
ops[ooffset] = make_var(interp, line + offset, &next); free(copy);
if (end != 0) ops[ooffset] = v;
mline[end] = cend;
offset += next; offset += next;
} }
if (ops[ooffset] == 0) if (ops[ooffset] == 0)
@ -431,10 +429,16 @@ variable *idoexpr(interpreter *interp, const char *line)
return 0; return 0;
// step 2 - do operations // step 2 - do operations
variable *result = (variable *)calloc(1, sizeof(variable));
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]);
if (result->valtype == INTEGER)
isetstr(result);
else
fsetstr(result);
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) {
@ -444,13 +448,6 @@ variable *idoexpr(interpreter *interp, const char *line)
free(ops[i]); free(ops[i]);
} }
} }
result->used = 0;
result->svalue = 0;
if (result->valtype == INTEGER)
isetstr(result);
else
fsetstr(result);
return result; return result;
} }

@ -3,10 +3,13 @@
#include "stack.h" #include "stack.h"
#include <memory.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> //#include <stdlib.h>
#include <string.h> #include <string.h>
extern int rand(void);
int s_put(interpreter *it) int s_put(interpreter *it)
{ {
char *s = igetarg_string(it, 0); char *s = igetarg_string(it, 0);
@ -60,25 +63,21 @@ int concat(interpreter *it)
return 0; return 0;
} }
int quit(interpreter *it) int script_rand(interpreter *it)
{ {
(void)it; static variable v;
exit(0); 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; return 0;
} }
int expr(interpreter *it) int line(interpreter *it)
{ {
variable *v = igetarg(it, 0); (void)it;
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;
return 0; return 0;
} }
@ -100,10 +99,10 @@ int main(int argc, char **argv)
iinit(&interp); iinit(&interp);
inew_cfunc(&interp, "print", s_put); inew_cfunc(&interp, "print", s_put);
inew_cfunc(&interp, "tp", s_type); inew_cfunc(&interp, "tp", s_type);
inew_cfunc(&interp, "q", quit);
inew_cfunc(&interp, "gets", input); inew_cfunc(&interp, "gets", input);
inew_cfunc(&interp, "concat", concat); inew_cfunc(&interp, "concat", concat);
inew_cfunc(&interp, "expr", expr); inew_cfunc(&interp, "rand", script_rand);
inew_cfunc(&interp, "line", line);
char *line = 0; char *line = 0;

@ -1,21 +0,0 @@
#include <stdarg.h>
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;
}

@ -4,6 +4,6 @@
char *snprintf(char *buf, unsigned int max, const char *format, ...); char *snprintf(char *buf, unsigned int max, const char *format, ...);
float strtof(const char *s, char **endptr); float strtof(const char *s, char **endptr);
extern int atoi(const char *); int atoi(const char *);
#endif // STDLIB_H_ #endif // STDLIB_H_

@ -37,7 +37,6 @@ variable *vmake(uint8_t fromc, uint8_t valtype, void *value)
v->svalue = 0; v->svalue = 0;
switch (valtype) { switch (valtype) {
case STRING: case STRING:
v->value = 0;
v->svalue = fixstring(value); v->svalue = fixstring(value);
free(value); free(value);
break; break;
@ -50,7 +49,6 @@ variable *vmake(uint8_t fromc, uint8_t valtype, void *value)
v->svalue = str_func; v->svalue = str_func;
break; break;
case EXPR: case EXPR:
v->value = 0;
v->svalue = value; v->svalue = value;
break; break;
} }
@ -71,15 +69,15 @@ variable *vmakef(float value)
void fsetstr(variable *f) void fsetstr(variable *f)
{ {
if (f->svalue == 0 || f->svalue == str_undef) if (f->svalue == 0 || f->svalue == str_undef)
f->svalue = (char *)malloc(16); f->svalue = (char *)malloc(32);
snprintf(f->svalue, 16, "%f", FLOAT(f)); snprintf(f->svalue, 32, "%f", FLOAT(f));
} }
void isetstr(variable *i) void isetstr(variable *i)
{ {
if (i->svalue == 0 || i->svalue == str_undef) if (i->svalue == 0 || i->svalue == str_undef)
i->svalue = (char *)malloc(12); i->svalue = (char *)malloc(32);
snprintf(i->svalue, 12, "%d", (int)INT(i)); snprintf(i->svalue, 32, "%d", (int)INT(i));
} }
variable *itostring(variable *v) variable *itostring(variable *v)

Loading…
Cancel
Save