diff options
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | builtins.c | 2 | ||||
-rw-r--r-- | builtins.h | 2 | ||||
-rw-r--r-- | memory.h | 8 | ||||
-rw-r--r-- | parser.c | 13 | ||||
-rw-r--r-- | parser.h | 1 | ||||
-rw-r--r-- | script | 31 | ||||
-rw-r--r-- | shell.c | 44 | ||||
-rw-r--r-- | shelpers.c | 2 | ||||
-rw-r--r-- | stdlib.c | 21 | ||||
-rw-r--r-- | stdlib.h | 9 | ||||
-rw-r--r-- | variable.c | 26 |
12 files changed, 133 insertions, 27 deletions
@@ -12,6 +12,7 @@ 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 @@ -2,7 +2,7 @@ #include "stack.h" #include "shelpers.h" -#include <stdlib.h> +#include <memory.h> extern char *str_func; extern char *str_undef; @@ -8,6 +8,8 @@ void iload_core(interpreter *it); +int ifunc_ret(interpreter *it); + const func_t indent_up[IUP_COUNT]; const func_t indent_down[IDOWN_COUNT]; diff --git a/memory.h b/memory.h new file mode 100644 index 0000000..546aa2d --- /dev/null +++ b/memory.h @@ -0,0 +1,8 @@ +#ifndef MEMORY_H_ +#define MEMORY_H_ + +void *malloc(unsigned int); +void *calloc(unsigned int, unsigned int); +void free(void *); + +#endif // MEMORY_H_ @@ -7,18 +7,19 @@ #include <ctype.h> #include <stdlib.h> -#include <stdio.h> +#include <memory.h> #include <string.h> #define MAX_VARS 48 #define MAX_STACK 16 #define MAX_LINES 1000 +extern int atoi(const char *); +extern float strtof(const char *, char **); + char *str_func = "(func)"; char *str_undef = "(undefined)"; -variable *idoexpr(interpreter *interp, const char *line); - void iinit(interpreter *interp) { interp->vars = (variable *)malloc(MAX_VARS * sizeof(variable)); @@ -222,7 +223,8 @@ int idoline(interpreter *interp, const char *line) skipblank(line, eol, &offset); continue; } - ops[ooffset] = make_var(interp, line + offset, &next); + variable *v = make_var(interp, line + offset, &next); + ops[ooffset] = v; if (ops[ooffset] == 0) { fret = -4; goto fail; @@ -280,7 +282,8 @@ cont: goto norun; ops = (variable **)malloc(8 * sizeof(variable *)); - memcpy(ops, interp->lines[interp->lnidx], 8 * sizeof(variable *)); + for (uint8_t i = 0; i < 8; i++) + ops[i] = interp->lines[interp->lnidx][i]; uint32_t oldLnidx = interp->lnidx; // eval expressions @@ -32,5 +32,6 @@ variable *inew_float(interpreter *, const char *, float); void inew_cfunc(interpreter *, const char *, func_t); int idoline(interpreter *, const char *); +variable *idoexpr(interpreter *interp, const char *line); #endif // PARSER_H_ @@ -1,18 +1,15 @@ -func divisible - if (arg1 == 0) - ret 0 - end - ret (arg0 % arg1) -end +set a "Het\n" +set b a +print b -set a 0 -set b 4 -do - set a (a + 1) - divisible a b > i - if (i == 0) - print a - end -while (a < 100) - -print "All done!" +#set a 0 +#do +# print "> " +# gets text +# set input "(" +# concat input text +# concat input ")" +# expr input a +# print a +# print "\n" +#while (1) @@ -1,4 +1,5 @@ #include <parser.h> +#include <builtins.h> #include "stack.h" @@ -9,7 +10,7 @@ int s_put(interpreter *it) { char *s = igetarg_string(it, 0); - printf("%s\n", s); + printf("%s", s); return 0; } @@ -36,6 +37,29 @@ int s_type(interpreter *it) return 0; } +int input(interpreter *it) +{ + variable *v = igetarg(it, 0); + v->valtype = STRING; + v->svalue = malloc(128); + unsigned int unused; + getline(&v->svalue, &unused, stdin); + *strchr(v->svalue, '\n') = '\0'; + return 0; +} + +int concat(interpreter *it) +{ + variable *v = igetarg(it, 0); + char *s = igetarg_string(it, 1); + char *new = malloc(strlen(v->svalue) + strlen(s) + 1); + strcpy(new, v->svalue); + strcpy(new + strlen(v->svalue), s); + new[strlen(v->svalue) + strlen(s)] = 0; + v->svalue = new; + return 0; +} + int quit(interpreter *it) { (void)it; @@ -43,6 +67,21 @@ int quit(interpreter *it) return 0; } +int expr(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; + return 0; +} + int main(int argc, char **argv) { interpreter interp; @@ -62,6 +101,9 @@ int main(int argc, char **argv) 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); char *line = 0; @@ -1,6 +1,6 @@ #include "shelpers.h" -#include <stdlib.h> +#include <memory.h> #include <string.h> char *strclone(const char *s) diff --git a/stdlib.c b/stdlib.c new file mode 100644 index 0000000..3299d0b --- /dev/null +++ b/stdlib.c @@ -0,0 +1,21 @@ +#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; +} diff --git a/stdlib.h b/stdlib.h new file mode 100644 index 0000000..0001edc --- /dev/null +++ b/stdlib.h @@ -0,0 +1,9 @@ +#ifndef STDLIB_H_ +#define STDLIB_H_ + +char *snprintf(char *buf, unsigned int max, const char *format, ...); +float strtof(const char *s, char **endptr); + +extern int atoi(const char *); + +#endif // STDLIB_H_ @@ -1,12 +1,33 @@ #include "variable.h" #include "parser.h" -#include <stdio.h> #include <stdlib.h> +#include <memory.h> +#include <string.h> + +extern int atoi(const char *); extern char *str_undef; extern char *str_func; +char *fixstring(char *s) +{ + int len = strlen(s); + char *n = malloc(len + 1); + int i, j; + for (i = 0, j = 0; s[i] != '\0'; i++, j++) { + if (s[i] == '\\') { + if (s[i + 1] == 'n') + n[j] = '\n'; + i++; + } else { + n[j] = s[i]; + } + } + n[j] = '\0'; + return n; +} + variable *vmake(uint8_t fromc, uint8_t valtype, void *value) { variable *v = (variable *)malloc(sizeof(variable)); @@ -18,7 +39,8 @@ variable *vmake(uint8_t fromc, uint8_t valtype, void *value) switch (valtype) { case STRING: v->value = 0; - v->svalue = value; + v->svalue = fixstring(value); + free(value); break; case INTEGER: INT(v) = (int32_t)value; |