2016-04-18 04:25:36 -04:00
|
|
|
#include "pebblisp.h"
|
|
|
|
#include "tokens.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#ifndef STANDALONE
|
|
|
|
#define printf(...) copySlice(NULL, NULL)
|
|
|
|
#else
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void copySlice(char * dest, struct Slice *src)
|
|
|
|
{
|
|
|
|
if(!dest || !src)
|
|
|
|
return;
|
|
|
|
strncpy(dest, src->text, src->length);
|
|
|
|
dest[src->length] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void debugSlice(struct Slice *s)
|
|
|
|
{
|
|
|
|
//printf("Debug Slice\n text:'");
|
|
|
|
for(int i = 0; i < s->length; i++) {
|
|
|
|
//printf("%c", s->text[i]);
|
|
|
|
}
|
|
|
|
// printf("'\n");
|
|
|
|
// printf(" length: %d\n", s->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object fetchFromEnvironment(const char *name, struct Environment *env)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
const char *next = env->strings[i];
|
|
|
|
while(next != NULL) {
|
|
|
|
// printf("fetching '%s' against '%s'\n", name, next);
|
|
|
|
if(strcmp(name, next) == 0) {
|
|
|
|
return env->objects[i];
|
|
|
|
}
|
|
|
|
next = env->strings[++i];
|
|
|
|
}
|
|
|
|
printf("DID NOT FIND SYMBOL\n");
|
|
|
|
Object o;
|
|
|
|
o.type = TYPE_ERROR;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result parse(struct Slice *slices)
|
|
|
|
{
|
2020-05-03 21:24:36 -04:00
|
|
|
printf("parse() START\n");
|
2016-04-18 04:25:36 -04:00
|
|
|
struct Slice *token = slices;
|
|
|
|
struct Slice *rest;
|
|
|
|
if(token->text != NULL) {
|
|
|
|
rest = &slices[1];
|
|
|
|
} else {
|
|
|
|
// printf("Assigning null...\n");
|
|
|
|
rest = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(token->text[0] == '(') {
|
|
|
|
// todo check for null rest
|
|
|
|
return readSeq(rest);
|
|
|
|
} else { // todo error on closed paren
|
|
|
|
return R(parseAtom(token), rest);
|
|
|
|
}
|
2020-05-03 21:24:36 -04:00
|
|
|
printf("parse() END\n");
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Result readSeq(struct Slice *tokens)
|
|
|
|
{
|
2020-05-03 21:24:36 -04:00
|
|
|
printf("readSeq() START\n");
|
2016-04-18 04:25:36 -04:00
|
|
|
Object res;
|
|
|
|
res.forward = NULL;
|
|
|
|
res.type = TYPE_LIST;
|
|
|
|
//res.list = malloc(sizeof(Object));
|
|
|
|
|
|
|
|
Object *march = &res;
|
|
|
|
for(;;) {
|
|
|
|
struct Slice *next = &tokens[0];
|
|
|
|
struct Slice *rest = next->text? &next[1] : NULL;
|
|
|
|
if(next->text[0] == ')') {
|
|
|
|
return R(res, rest);
|
|
|
|
}
|
|
|
|
Result r = parse(tokens);
|
2020-05-03 21:24:36 -04:00
|
|
|
printf("readSeq() before malloc\n");
|
|
|
|
march->forward = malloc(sizeof(struct Object));
|
|
|
|
printf("readSeq() after malloc\n");
|
2016-04-18 04:25:36 -04:00
|
|
|
*march->forward = r.obj;
|
|
|
|
// char out[MAX_TOK_LEN];
|
|
|
|
// printf("stringObj: %s\n", stringObj(out, &r.obj));
|
|
|
|
tokens = r.slices;
|
|
|
|
march = march->forward;
|
|
|
|
}
|
2020-05-03 21:24:36 -04:00
|
|
|
printf("readSeq() END\n");
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Object parseAtom(struct Slice *s)
|
|
|
|
{
|
|
|
|
// printf("parseAtom()\n");
|
|
|
|
Object o;
|
|
|
|
o.forward = NULL;
|
|
|
|
if(isDigit(s->text[0])) {
|
|
|
|
o.type = TYPE_NUMBER;
|
|
|
|
o.number = 0;
|
|
|
|
for(int i = 0; i < s->length; i++) {
|
|
|
|
o.number *= 10;
|
|
|
|
o.number += s->text[i] - '0';
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
o.type = TYPE_SYMBOL;
|
|
|
|
copySlice(o.name, s);
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object eval(Object *obj, struct Environment *env)
|
|
|
|
{
|
2020-05-03 21:24:36 -04:00
|
|
|
printf("eval(): ");
|
|
|
|
printObj(obj);
|
2016-04-18 04:25:36 -04:00
|
|
|
Object o = *obj;
|
|
|
|
switch(obj->type) {
|
|
|
|
case TYPE_NUMBER:
|
|
|
|
{
|
|
|
|
return *obj;
|
|
|
|
}
|
|
|
|
case TYPE_LIST:
|
|
|
|
{
|
|
|
|
// printf("TYPE_LIST\n");
|
|
|
|
Object first_form = *obj->forward;
|
|
|
|
Object first_eval = eval(&first_form, env);
|
2020-05-03 21:24:36 -04:00
|
|
|
Object arg1 = eval(first_form.forward, env);
|
|
|
|
Object arg2 = eval(first_form.forward->forward, env);
|
|
|
|
return first_eval.func(arg1, arg2);
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
case TYPE_SYMBOL:
|
|
|
|
{
|
|
|
|
return fetchFromEnvironment(obj->name, env);
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* stringObj(char *dest, Object *obj)
|
|
|
|
{
|
|
|
|
if(obj->type == TYPE_NUMBER) {
|
|
|
|
snprintf(dest, MAX_TOK_LEN, "%d", obj->number);
|
|
|
|
} else if(obj->type == TYPE_SYMBOL) {
|
|
|
|
return obj->name;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dest;
|
|
|
|
}
|
|
|
|
|
2020-05-03 21:24:36 -04:00
|
|
|
void printObj(Object *obj)
|
|
|
|
{
|
|
|
|
if(obj->type == TYPE_NUMBER) {
|
|
|
|
printf("TYPE_NUMBER");
|
|
|
|
} else if(obj->type == TYPE_LIST) {
|
|
|
|
printf("TYPE_NUMBER");
|
|
|
|
} else if(obj->type == TYPE_SYMBOL) {
|
|
|
|
printf("TYPE_NUMBER");
|
|
|
|
} else {
|
|
|
|
printf("TYPE_OTHER");
|
|
|
|
}
|
|
|
|
char temp[20];
|
|
|
|
stringObj(temp, obj);
|
|
|
|
printf(": %s\n", temp);
|
|
|
|
}
|
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
Result resultFromObjAndSlices(Object obj, struct Slice *slices)
|
|
|
|
{
|
|
|
|
Result r;
|
|
|
|
r.obj = obj;
|
|
|
|
r.slices = slices;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object basicOp(Object *obj1, Object *obj2, const char op)
|
|
|
|
{
|
|
|
|
Object o;
|
|
|
|
o.forward = NULL;
|
|
|
|
o.type = TYPE_NUMBER;
|
|
|
|
switch(op){
|
|
|
|
case '+':
|
|
|
|
o.number = obj1->number + obj2->number;
|
|
|
|
break;
|
|
|
|
case '-':
|
|
|
|
o.number = obj1->number - obj2->number;
|
|
|
|
break;
|
|
|
|
case '*':
|
|
|
|
o.number = obj1->number * obj2->number;
|
|
|
|
break;
|
|
|
|
case '/':
|
|
|
|
o.number = obj1->number / obj2->number;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
o = *obj1;
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object add(Object obj1, Object obj2)
|
|
|
|
{ return basicOp(&obj1, &obj2, '+'); }
|
|
|
|
|
|
|
|
Object sub(Object obj1, Object obj2)
|
|
|
|
{ return basicOp(&obj1, &obj2, '-'); }
|
|
|
|
|
|
|
|
Object mul(Object obj1, Object obj2)
|
|
|
|
{ return basicOp(&obj1, &obj2, '*'); }
|
|
|
|
|
|
|
|
Object divi(Object obj1, Object obj2)
|
|
|
|
{ return basicOp(&obj1, &obj2, '/'); }
|
|
|
|
|
|
|
|
void addFunc(const char *name, Object (*func)(Object, Object),
|
|
|
|
struct Environment *env)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < MAX_ENV_ELM; i++) {
|
|
|
|
if(env->strings[i] == NULL) {
|
|
|
|
//printf("Adding at %d\n", i);
|
|
|
|
env->strings[i] = malloc(sizeof(name));
|
|
|
|
strncpy(env->strings[i], name, MAX_TOK_LEN);
|
|
|
|
Object o;
|
|
|
|
o.type = TYPE_FUNC;
|
|
|
|
o.forward = NULL;
|
|
|
|
o.func = func;
|
|
|
|
env->objects[i] = o;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Environment defaultEnv() {
|
|
|
|
struct Environment e;
|
|
|
|
e.strings = malloc(sizeof(char*) * MAX_ENV_ELM);
|
2020-05-03 21:24:36 -04:00
|
|
|
for(int i = 0; i < MAX_ENV_ELM; i++) {
|
|
|
|
e.strings[i] = NULL;
|
|
|
|
}
|
2016-04-18 04:25:36 -04:00
|
|
|
e.objects = malloc(sizeof(Object) * MAX_ENV_ELM);
|
|
|
|
addFunc("+", &add, &e);
|
|
|
|
addFunc("-", &sub, &e);
|
|
|
|
addFunc("*", &mul, &e);
|
|
|
|
addFunc("/", &divi, &e);
|
|
|
|
|
|
|
|
/*
|
|
|
|
e.strings[0] = malloc(sizeof("+"));
|
|
|
|
strncpy(e.strings[0], "+", MAX_TOK_LEN);
|
|
|
|
Object o;
|
|
|
|
o.type = TYPE_FUNC;
|
|
|
|
o.forward = NULL;
|
|
|
|
o.func = &add;
|
|
|
|
e.objects[0] = o;
|
|
|
|
*/
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object parseEval(const char *input, struct Environment *env)
|
|
|
|
{
|
|
|
|
struct Slice *tokens = tokenize(input);
|
|
|
|
struct Slice *debug = tokens;
|
2020-05-03 17:00:45 -04:00
|
|
|
#ifdef STANDALONE
|
2016-04-18 04:25:36 -04:00
|
|
|
if(debug) {
|
|
|
|
while(debug->text) {
|
2020-05-03 17:00:45 -04:00
|
|
|
char tok[MAX_TOK_LEN];
|
2016-04-18 04:25:36 -04:00
|
|
|
copySlice(tok, debug);
|
|
|
|
// printf("'%s', ", tok);
|
|
|
|
debug++;
|
|
|
|
}
|
|
|
|
// printf("\n");
|
|
|
|
} else {
|
|
|
|
// printf("parse error\n");
|
|
|
|
}
|
|
|
|
// printf("parseEval() parse()\n");
|
2020-05-03 17:00:45 -04:00
|
|
|
#endif
|
2016-04-18 04:25:36 -04:00
|
|
|
Object parsed = parse(tokens).obj;
|
2020-05-03 21:24:36 -04:00
|
|
|
free(tokens);
|
2016-04-18 04:25:36 -04:00
|
|
|
return eval(&parsed, env);
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef STANDALONE
|
|
|
|
int repl(struct Environment *env)
|
|
|
|
{
|
|
|
|
char input[100] = "";
|
|
|
|
while(input[0] != 'q') {
|
|
|
|
printf("pebblisp>> ");
|
|
|
|
fgets(input, 100, stdin);
|
|
|
|
Object obj = parseEval(input, env);
|
|
|
|
printf("eval: %d\n", obj.number);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
struct Environment env = defaultEnv();
|
|
|
|
if(1) {
|
|
|
|
repl(&env);
|
|
|
|
} else {
|
|
|
|
struct Slice *tokens = tokenize("(+ 10 5)");
|
|
|
|
struct Slice *debug = tokens;
|
|
|
|
|
|
|
|
if(debug) {
|
|
|
|
while(debug->text) {
|
|
|
|
char tok[10];
|
|
|
|
copySlice(tok, debug);
|
|
|
|
printf("'%s', ", tok);
|
|
|
|
debug++;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
} else {
|
|
|
|
printf("parse error\n");
|
|
|
|
}
|
|
|
|
parse(tokens);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|