You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

596 lines
12 KiB
C

#include <parser.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_VARS 32
#define MAX_STACK 32
#define INT(v) (*((int32_t *)&v->value))
#define FLOAT(v) (*((float *)&v->value))
#define STR(v) (*((char **)&v->value))
char *strclone(const char *s)
{
char *clone = (char *)malloc(strlen(s));
strcpy(clone, s);
return clone;
}
char *strnclone(const char *s, uint32_t n)
{
char *clone = (char *)malloc(n);
strncpy(clone, s, n);
return clone;
}
void ifunc_set(interpreter *it);
void ifunc_jmp(interpreter *it);
void ifunc_label(interpreter *it);
void ifunc_end(interpreter *it);
variable *idoexpr(interpreter *interp, const char *line);
variable *itostring(variable *v);
variable *itoint(variable *v);
variable *itofloat(variable *v);
void iinit(interpreter *interp)
{
interp->vars = (variable *)calloc(MAX_VARS, sizeof(variable));
interp->vnames = (char **)calloc(MAX_VARS, sizeof(char *));
interp->stack = (stack_t *)calloc(MAX_STACK, sizeof(stack_t));
interp->stidx = 0;
interp->lines = (char **)calloc(20, sizeof(char *));
interp->lnidx = 0;
interp->indent = 0;
inew_cfunc(interp, "set", ifunc_set);
inew_cfunc(interp, "jmp", ifunc_jmp);
inew_cfunc(interp, "func", ifunc_label);
inew_cfunc(interp, "end", ifunc_end);
}
void ipush(interpreter *it, void *v)
{
it->stack[it->stidx++] = v;
}
void *ipop(interpreter *it)
{
return it->stack[--it->stidx];
}
variable *interpreter_get_variable(interpreter *interp, const char *name)
{
for (uint32_t i = 0; i < MAX_VARS; i++) {
if (!interp->vars[i].used) {
interp->vars[i].used = 1;
interp->vars[i].valtype = FUNC;
interp->vars[i].value = 0;
interp->vnames[i] = strclone(name);
return &interp->vars[i];
} else if (interp->vnames[i] != 0 && !strcmp(interp->vnames[i], name)) {
return &interp->vars[i];
}
}
return 0;
}
char *interpreter_get_name(interpreter *interp, variable *v)
{
for (uint32_t i = 0; i < MAX_VARS; i++) {
if (v == &interp->vars[i])
return interp->vnames[i];
}
return "(undefined)";
}
void inew_string(interpreter *interp, const char *name, char *value)
{
variable *v = interpreter_get_variable(interp, name);
if (v != 0) {
v->valtype = STRING;
STR(v) = value;
}
}
void inew_integer(interpreter *interp, const char *name, int32_t value)
{
variable *v = interpreter_get_variable(interp, name);
if (v != 0) {
v->valtype = INTEGER;
INT(v) = value;
}
}
void inew_float(interpreter *interp, const char *name, float value)
{
variable *v = interpreter_get_variable(interp, name);
if (v != 0) {
v->valtype = FLOAT;
FLOAT(v) = value;
}
}
void inew_cfunc(interpreter *interp, const char *name, func_t func)
{
variable *v = interpreter_get_variable(interp, name);
if (v != 0) {
v->fromc = 1;
v->valtype = FUNC;
v->value = (uint32_t)func;
}
}
uint8_t eol(int c)
{
return c == '\n' || c == '\0';
}
uint8_t eot(int c)
{
return eol(c) || c == ' ';
}
uint8_t eoe(int c)
{
return eol(c) || c == ')';
}
variable *make_var(interpreter *interp, const char *line, uint32_t *next)
{
if (line[0] == '\"') { // string literal
uint32_t end = 1;
while (!eol(line[end])) {
if (line[end] == '\"'/* && line[end - 1] != '\\'*/) {
if (!eot(line[end + 1]))
return 0;
// TODO string breakdown
variable *v = (variable *)malloc(sizeof(variable));
v->valtype = STRING;
STR(v) = strnclone(line + 1, end - 1);
*next = end + 1;
return v;
}
end++;
}
return 0;
} else if (line[0] == '(') { // equation literal
uint32_t end = 1;
while (!eol(line[end])) {
if (line[end] == ')' && line[end - 1] != '\\') {
if (!eot(line[end + 1]))
return 0;
// TODO string breakdown
*next = end + 1;
return idoexpr(interp, line + 1);
}
end++;
}
} else if (isalpha(line[0])) { // variable/func
uint32_t end = 1;
for (; isalnum(line[end]); end++);
if (!eot(line[end]))
return 0;
char *name = (char *)malloc(end + 1);
strncpy(name, line, end);
name[end] = '\0';
*next = end;
return interpreter_get_variable(interp, name);
} else if (isdigit(line[0])) { // number
uint32_t end = 1;
uint8_t dec = 0;
for (; !eot(line[end]); end++) {
if (!isdigit(line[end])) {
if (line[end] == '.') {
if (!dec)
dec = 1;
else
return 0;
} else {
return 0;
}
}
}
variable *v = (variable *)malloc(sizeof(variable));
if (dec) {
v->valtype = FLOAT;
FLOAT(v) = strtof(line, 0);
} else {
v->valtype = INTEGER;
INT(v) = atoi(line);
}
*next = end;
return v;
}
return 0;
}
int idoline(interpreter *interp, const char *line)
{
variable *ops[8];
uint32_t ooffset, offset, next;
interp->lines[interp->lnidx] = strclone(line);
loop:
if (line[0] == '`') {
goto norun;
} else if (interp->indent > 0) {
if (!strcmp(line, "end"))
interp->indent--;
goto norun;
}
ooffset = 0;
offset = 0;
// step 1 - convert to tokens
while (!eol(line[offset])) {
ops[ooffset] = make_var(interp, line + offset, &next);
if (ops[ooffset] == 0) {
return -4;
} else {
ooffset++;
offset += next;
}
// skip whitespace
for (; line[offset] == ' ' && !eol(line[offset]); offset++);
}
// step 2 - execute
if (ooffset == 0)
return -1;
if (ops[0]->valtype != FUNC)
return -2;
if (ops[0]->value == 0)
return -3;
for (uint32_t i = ooffset; --i > 0;)
ipush(interp, ops[i]);
if (ops[0]->fromc) {
((func_t)ops[0]->value)(interp);
} else {
ipush(interp, (void *)(interp->lnidx + 1));
interp->lnidx = ops[0]->value;
}
interp->stidx -= ooffset - 1;
if ((int32_t)interp->stidx < 0) {
interp->stidx = 0;
return -5;
}
norun:
interp->lnidx++;
if (interp->lines[interp->lnidx] != 0) {
line = interp->lines[interp->lnidx];
goto loop;
}
return 0;
}
typedef void (*operation_t)(variable *, variable *, variable *);
#define IOPS_COUNT 9
static char *iops[IOPS_COUNT] = {
"+", "-", "*", "/", "&", "|", "^", ">>", "<<"
};
void iop_add(variable *, variable *, variable *);
void iop_sub(variable *, variable *, variable *);
void iop_mult(variable *, variable *, variable *);
void iop_div(variable *, variable *, variable *);
void iop_and(variable *, variable *, variable *);
void iop_or(variable *, variable *, variable *);
void iop_xor(variable *, variable *, variable *);
void iop_shr(variable *, variable *, variable *);
void iop_shl(variable *, variable *, variable *);
static operation_t iopfuncs[IOPS_COUNT] = {
iop_add, iop_sub, iop_mult, iop_div, iop_and,
iop_or, iop_xor, iop_shr, iop_shl
};
variable *idoexpr(interpreter *interp, const char *line)
{
static variable result;
char *mline = line;
void *ops[16];
uint32_t ooffset = 0;
uint32_t offset = 0;
uint32_t next;
// step 1 - break apart line
// skip whitespace
for (; line[offset] == ' ' && !eol(line[offset]); offset++);
while (!eoe(line[offset])) {
uint32_t end = offset;
char cend;
if (line[offset] == '(') {
} else 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;
if (ops[ooffset] == 0)
return 0;
ooffset++;
offset += next;
// skip whitespace
for (; line[offset] == ' ' && !eoe(line[offset]); offset++);
if (eoe(line[offset]))
break;
for (uint32_t i = 0; i < IOPS_COUNT; i++) {
int len = strlen(iops[i]);
if (!strncmp(iops[i], line + offset, len)) {
ops[ooffset] = (void *)(i + 1);
offset += len;
break;
}
}
if (ops[ooffset] == 0)
return 0;
ooffset++;
// skip whitespace
for (; line[offset] == ' ' && !eol(line[offset]); offset++);
}
if (ooffset % 2 == 0)
return 0;
// step 2 - do operations
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]);
}
return &result;
}
variable *igetarg(interpreter *interp, uint32_t index)
{
return interp->stack[interp->stidx - index - 1];
}
char *igetarg_string(interpreter *interp, uint32_t index)
{
if (index >= interp->stidx)
return 0;
variable *v = igetarg(interp, index);
if (v->valtype == FUNC)
return "(func)";
return STR(itostring(v));
}
int igetarg_integer(interpreter *interp, uint32_t index)
{
if (index >= interp->stidx)
return 0;
variable *v = igetarg(interp, index);
return INT(itoint(v));
}
float igetarg_float(interpreter *interp, uint32_t index)
{
if (index >= interp->stidx)
return 0;
variable *v = igetarg(interp, index);
return FLOAT(itofloat(v));
}
variable *itostring(variable *v)
{
char *buf;
switch (v->valtype) {
case STRING:
break;
case INTEGER:
buf = (char *)malloc(12);
sprintf(buf, "%d", *((int32_t *)&v->value));
v->valtype = STRING;
STR(v) = buf;
break;
case FLOAT:
buf = (char *)malloc(24);
sprintf(buf, "%f", *((float *)&v->value));
v->valtype = STRING;
STR(v) = buf;
break;
case FUNC:
// no
break;
}
return v;
}
variable *itoint(variable *v)
{
switch (v->valtype) {
case STRING:
v->valtype = INTEGER;
INT(v) = atoi(STR(v));
break;
case INTEGER:
break;
case FLOAT:
v->valtype = INTEGER;
INT(v) = (int32_t)FLOAT(v);
break;
case FUNC:
break;
}
return v;
}
variable *itofloat(variable *v)
{
switch (v->valtype) {
case STRING:
v->valtype = FLOAT;
FLOAT(v) = strtof(STR(v), 0);
break;
case INTEGER:
v->valtype = FLOAT;
FLOAT(v) = (float)INT(v);
break;
case FLOAT:
break;
case FUNC:
break;
}
return v;
}
/*char *iget_string(interpreter *, const char *)
{
}
int iget_integer(interpreter *, const char *)
{
}
float iget_float(interpreter *, const char *)
{
}*/
/**
* Builtin functions
*/
void ifunc_set(interpreter *it)
{
variable *n = igetarg(it, 0);
variable *v = igetarg(it, 1);
n->valtype = v->valtype;
n->value = v->value;
}
void ifunc_label(interpreter *it)
{
variable *n = igetarg(it, 0);
n->valtype = FUNC;
n->value = it->lnidx;
it->indent++;
}
void ifunc_end(interpreter *it)
{
if (it->stidx > 0) {
uint32_t line = (uint32_t)ipop(it);
it->lnidx = line - 1;
}
}
void ifunc_jmp(interpreter *it)
{
int newidx = igetarg_integer(it, 0);
it->lnidx = newidx - 1;
}
/**
* Builtin operations
*/
void iop_add(variable *r, variable *a, variable *b)
{
if (a->valtype == INTEGER && b->valtype == INTEGER) {
INT(r) = INT(a) + INT(b);
} else {
itofloat(a);
itofloat(b);
FLOAT(r) = FLOAT(a) + FLOAT(b);
}
}
void iop_sub(variable *r, variable *a, variable *b)
{
if (a->valtype == INTEGER && b->valtype == INTEGER) {
INT(r) = INT(a) - INT(b);
} else {
itofloat(a);
itofloat(b);
FLOAT(r) = FLOAT(a) - FLOAT(b);
}
}
void iop_mult(variable *r, variable *a, variable *b)
{
if (a->valtype == INTEGER && b->valtype == INTEGER) {
INT(r) = INT(a) * INT(b);
} else {
itofloat(a);
itofloat(b);
FLOAT(r) = FLOAT(a) * FLOAT(b);
}
}
void iop_div(variable *r, variable *a, variable *b)
{
if (a->valtype == INTEGER && b->valtype == INTEGER) {
INT(r) = INT(a) / INT(b);
} else {
itofloat(a);
itofloat(b);
FLOAT(r) = FLOAT(a) / FLOAT(b);
}
}
void iop_and(variable *r, variable *a, variable *b)
{
if (a->valtype == INTEGER && b->valtype == INTEGER) {
INT(r) = INT(a) & INT(b);
}
}
void iop_or(variable *r, variable *a, variable *b)
{
if (a->valtype == INTEGER && b->valtype == INTEGER) {
INT(r) = INT(a) | INT(b);
}
}
void iop_xor(variable *r, variable *a, variable *b)
{
if (a->valtype == INTEGER && b->valtype == INTEGER) {
INT(r) = INT(a) ^ INT(b);
}
}
void iop_shr(variable *r, variable *a, variable *b)
{
if (a->valtype == INTEGER && b->valtype == INTEGER) {
INT(r) = INT(a) >> INT(b);
}
}
void iop_shl(variable *r, variable *a, variable *b)
{
if (a->valtype == INTEGER && b->valtype == INTEGER) {
INT(r) = INT(a) << INT(b);
}
}