stdlib independence, fixes for calculator

master
Clyne Sullivan 7 years ago
parent 5d85baa270
commit f414175e0c

@ -12,6 +12,7 @@ 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 shell -L. -l interp

@ -2,7 +2,7 @@
#include "stack.h" #include "stack.h"
#include "shelpers.h" #include "shelpers.h"
#include <stdlib.h> #include <memory.h>
extern char *str_func; extern char *str_func;
extern char *str_undef; extern char *str_undef;

@ -8,6 +8,8 @@
void iload_core(interpreter *it); void iload_core(interpreter *it);
int ifunc_ret(interpreter *it);
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];

@ -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 <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <memory.h>
#include <string.h> #include <string.h>
#define MAX_VARS 48 #define MAX_VARS 48
#define MAX_STACK 16 #define MAX_STACK 16
#define MAX_LINES 1000 #define MAX_LINES 1000
extern int atoi(const char *);
extern float strtof(const char *, char **);
char *str_func = "(func)"; char *str_func = "(func)";
char *str_undef = "(undefined)"; char *str_undef = "(undefined)";
variable *idoexpr(interpreter *interp, const char *line);
void iinit(interpreter *interp) void iinit(interpreter *interp)
{ {
interp->vars = (variable *)malloc(MAX_VARS * sizeof(variable)); interp->vars = (variable *)malloc(MAX_VARS * sizeof(variable));
@ -222,7 +223,8 @@ int idoline(interpreter *interp, const char *line)
skipblank(line, eol, &offset); skipblank(line, eol, &offset);
continue; continue;
} }
ops[ooffset] = make_var(interp, line + offset, &next); variable *v = make_var(interp, line + offset, &next);
ops[ooffset] = v;
if (ops[ooffset] == 0) { if (ops[ooffset] == 0) {
fret = -4; fret = -4;
goto fail; goto fail;
@ -280,7 +282,8 @@ cont:
goto norun; goto norun;
ops = (variable **)malloc(8 * sizeof(variable *)); 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; uint32_t oldLnidx = interp->lnidx;
// eval expressions // eval expressions

@ -32,5 +32,6 @@ variable *inew_float(interpreter *, const char *, float);
void inew_cfunc(interpreter *, const char *, func_t); void inew_cfunc(interpreter *, const char *, func_t);
int idoline(interpreter *, const char *); int idoline(interpreter *, const char *);
variable *idoexpr(interpreter *interp, const char *line);
#endif // PARSER_H_ #endif // PARSER_H_

@ -1,18 +1,15 @@
func divisible set a "Het\n"
if (arg1 == 0) set b a
ret 0 print b
end
ret (arg0 % arg1)
end
set a 0 #set a 0
set b 4 #do
do # print "> "
set a (a + 1) # gets text
divisible a b > i # set input "("
if (i == 0) # concat input text
print a # concat input ")"
end # expr input a
while (a < 100) # print a
# print "\n"
print "All done!" #while (1)

@ -1,4 +1,5 @@
#include <parser.h> #include <parser.h>
#include <builtins.h>
#include "stack.h" #include "stack.h"
@ -9,7 +10,7 @@
int s_put(interpreter *it) int s_put(interpreter *it)
{ {
char *s = igetarg_string(it, 0); char *s = igetarg_string(it, 0);
printf("%s\n", s); printf("%s", s);
return 0; return 0;
} }
@ -36,6 +37,29 @@ int s_type(interpreter *it)
return 0; 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) int quit(interpreter *it)
{ {
(void)it; (void)it;
@ -43,6 +67,21 @@ int quit(interpreter *it)
return 0; 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) int main(int argc, char **argv)
{ {
interpreter interp; interpreter interp;
@ -62,6 +101,9 @@ int main(int argc, char **argv)
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, "q", quit);
inew_cfunc(&interp, "gets", input);
inew_cfunc(&interp, "concat", concat);
inew_cfunc(&interp, "expr", expr);
char *line = 0; char *line = 0;

@ -1,6 +1,6 @@
#include "shelpers.h" #include "shelpers.h"
#include <stdlib.h> #include <memory.h>
#include <string.h> #include <string.h>
char *strclone(const char *s) char *strclone(const char *s)

@ -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;
}

@ -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 "variable.h"
#include "parser.h" #include "parser.h"
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <memory.h>
#include <string.h>
extern int atoi(const char *);
extern char *str_undef; extern char *str_undef;
extern char *str_func; 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 *vmake(uint8_t fromc, uint8_t valtype, void *value)
{ {
variable *v = (variable *)malloc(sizeof(variable)); variable *v = (variable *)malloc(sizeof(variable));
@ -18,7 +39,8 @@ variable *vmake(uint8_t fromc, uint8_t valtype, void *value)
switch (valtype) { switch (valtype) {
case STRING: case STRING:
v->value = 0; v->value = 0;
v->svalue = value; v->svalue = fixstring(value);
free(value);
break; break;
case INTEGER: case INTEGER:
INT(v) = (int32_t)value; INT(v) = (int32_t)value;

Loading…
Cancel
Save