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)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void copySlice(char * dest, struct Slice *src)
|
|
|
|
{
|
|
|
|
if(!dest || !src)
|
|
|
|
return;
|
|
|
|
strncpy(dest, src->text, src->length);
|
2020-05-04 18:14:41 -04:00
|
|
|
dest[(int)src->length] = '\0';
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void debugSlice(struct Slice *s)
|
|
|
|
{
|
2020-05-04 18:14:41 -04:00
|
|
|
printf("Debug Slice\n text:'");
|
2016-04-18 04:25:36 -04:00
|
|
|
for(int i = 0; i < s->length; i++) {
|
2020-05-04 18:14:41 -04:00
|
|
|
printf("%c", s->text[i]);
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
2020-05-04 18:14:41 -04:00
|
|
|
printf("'\n");
|
|
|
|
printf(" length: %d\n", s->length);
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Object fetchFromEnvironment(const char *name, struct Environment *env)
|
|
|
|
{
|
2020-05-04 18:14:41 -04:00
|
|
|
printf("Fetching '%s' from env\n", name);
|
2016-04-18 04:25:36 -04:00
|
|
|
int i = 0;
|
|
|
|
const char *next = env->strings[i];
|
|
|
|
while(next != NULL) {
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
struct Slice *token = slices;
|
|
|
|
struct Slice *rest;
|
|
|
|
if(token->text != NULL) {
|
|
|
|
rest = &slices[1];
|
|
|
|
} else {
|
2020-05-04 18:14:41 -04:00
|
|
|
printf("Assigning null...\n");
|
2016-04-18 04:25:36 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Result readSeq(struct Slice *tokens)
|
|
|
|
{
|
2020-05-04 18:14:41 -04:00
|
|
|
Object res = listObject();
|
2016-04-18 04:25:36 -04:00
|
|
|
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-04 18:14:41 -04:00
|
|
|
addToList(&res, r.obj);
|
2016-04-18 04:25:36 -04:00
|
|
|
tokens = r.slices;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object parseAtom(struct Slice *s)
|
|
|
|
{
|
|
|
|
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';
|
|
|
|
}
|
2020-05-04 10:03:35 -04:00
|
|
|
} else if (s->text[0] == 'T' && s->text[1] == '\0') {
|
|
|
|
o.type = TYPE_BOOL;
|
|
|
|
o.number = 1;
|
|
|
|
} else if (s->text[0] == 'F' && s->text[1] == '\0') {
|
|
|
|
o.type = TYPE_BOOL;
|
|
|
|
o.number = 0;
|
2016-04-18 04:25:36 -04:00
|
|
|
} else {
|
|
|
|
o.type = TYPE_SYMBOL;
|
|
|
|
copySlice(o.name, s);
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2020-05-04 18:14:41 -04:00
|
|
|
Object evalDefArgs(const Object *arg_forms, struct Environment *env)
|
2020-05-04 10:03:35 -04:00
|
|
|
{
|
|
|
|
printf("evalDefArgs()\n");
|
|
|
|
|
2020-05-04 18:14:41 -04:00
|
|
|
const Object *first_form = &arg_forms[0];
|
|
|
|
const char *name = first_form->name;
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2020-05-04 18:14:41 -04:00
|
|
|
Object second_eval = eval(first_form->forward, env);
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2020-05-04 18:14:41 -04:00
|
|
|
addToEnv(env, name, second_eval);
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2020-05-04 18:14:41 -04:00
|
|
|
return *first_form;
|
|
|
|
}
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2020-05-04 18:14:41 -04:00
|
|
|
Object evalIfArgs(const Object *arg_forms, struct Environment *env)
|
|
|
|
{
|
|
|
|
return eval(arg_forms, env).number?
|
|
|
|
eval(arg_forms->forward, env) :
|
|
|
|
eval(arg_forms->forward->forward, env);
|
2020-05-04 10:03:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Object evalBuiltIns(const Object *first, const Object *rest, int *found,
|
|
|
|
struct Environment *env)
|
|
|
|
{
|
|
|
|
if(strcmp(first->name, "def") == 0) {
|
|
|
|
*found = 0;
|
2020-05-04 18:14:41 -04:00
|
|
|
return evalDefArgs(rest, env);
|
2020-05-04 10:03:35 -04:00
|
|
|
}else if(strcmp(first->name, "if") == 0) {
|
|
|
|
*found = 0;
|
|
|
|
return evalIfArgs(rest, env);
|
|
|
|
}
|
|
|
|
|
|
|
|
Object o;
|
|
|
|
*found = -1;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object eval(const Object *obj, struct Environment *env)
|
2016-04-18 04:25:36 -04:00
|
|
|
{
|
2020-05-04 18:14:41 -04:00
|
|
|
// printf("eval(): ");
|
|
|
|
// printObj(obj);
|
2016-04-18 04:25:36 -04:00
|
|
|
Object o = *obj;
|
|
|
|
switch(obj->type) {
|
|
|
|
case TYPE_NUMBER:
|
2020-05-04 10:03:35 -04:00
|
|
|
case TYPE_BOOL:
|
2020-05-04 18:14:41 -04:00
|
|
|
return o;
|
2020-05-04 10:03:35 -04:00
|
|
|
|
|
|
|
case TYPE_SYMBOL:
|
2020-05-04 18:14:41 -04:00
|
|
|
o = fetchFromEnvironment(obj->name, env);
|
|
|
|
printf("fetched object '%s':\n", obj->name);
|
|
|
|
printObj(&o);
|
|
|
|
return o;
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
case TYPE_LIST:
|
|
|
|
{
|
2020-05-04 18:14:41 -04:00
|
|
|
if(listLength(obj) == 1) {
|
|
|
|
o = *obj->list;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
Object first_form = *obj->list;
|
2020-05-04 10:03:35 -04:00
|
|
|
|
|
|
|
{ // Try to eval built-ins
|
|
|
|
int i = -1;
|
|
|
|
Object built_in =
|
|
|
|
evalBuiltIns(&first_form, first_form.forward, &i, env);
|
|
|
|
|
|
|
|
if(i == 0)
|
|
|
|
return built_in;
|
|
|
|
}
|
|
|
|
printf("\nNo built-ins found\n");
|
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
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);
|
2020-05-04 10:03:35 -04:00
|
|
|
|
|
|
|
printf("Evaluating func\n");
|
2020-05-04 18:14:41 -04:00
|
|
|
Object func_eval = first_eval.func(arg1, arg2);
|
|
|
|
deleteList(obj);
|
|
|
|
return func_eval;
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
|
|
|
Result resultFromObjAndSlices(Object obj, struct Slice *slices)
|
|
|
|
{
|
|
|
|
Result r;
|
|
|
|
r.obj = obj;
|
|
|
|
r.slices = slices;
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2020-05-04 18:14:41 -04:00
|
|
|
// todo could include and return a starting index for faster multi-adds
|
|
|
|
void addToEnv(struct Environment *env, const char *name, const Object obj)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < MAX_ENV_ELM; i++) {
|
|
|
|
if(env->strings[i] == NULL) {
|
|
|
|
env->strings[i] = malloc(sizeof(name));
|
|
|
|
strncpy(env->strings[i], name, MAX_TOK_LEN);
|
|
|
|
env->objects[i] = obj;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
Object basicOp(Object *obj1, Object *obj2, const char op)
|
|
|
|
{
|
|
|
|
Object o;
|
|
|
|
o.forward = NULL;
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
o.type = TYPE_NUMBER;
|
2020-05-04 18:14:41 -04:00
|
|
|
const int n1 = obj1->number;
|
|
|
|
const int n2 = obj2->number;
|
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
switch(op){
|
|
|
|
case '+':
|
2020-05-04 18:14:41 -04:00
|
|
|
o.number = n1 + n2;
|
2020-05-04 10:03:35 -04:00
|
|
|
return o;
|
2016-04-18 04:25:36 -04:00
|
|
|
case '-':
|
2020-05-04 18:14:41 -04:00
|
|
|
o.number = n1 - n2;
|
2020-05-04 10:03:35 -04:00
|
|
|
return o;
|
2016-04-18 04:25:36 -04:00
|
|
|
case '*':
|
2020-05-04 18:14:41 -04:00
|
|
|
o.number = n1 * n2;
|
2020-05-04 10:03:35 -04:00
|
|
|
return o;
|
2016-04-18 04:25:36 -04:00
|
|
|
case '/':
|
2020-05-04 18:14:41 -04:00
|
|
|
o.number = n1 / n2;
|
2020-05-04 10:03:35 -04:00
|
|
|
return o;
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
|
2020-05-04 10:03:35 -04:00
|
|
|
o.type = TYPE_BOOL;
|
|
|
|
switch(op) {
|
|
|
|
case '=':
|
2020-05-04 18:14:41 -04:00
|
|
|
o.number = n1 == n2;
|
2020-05-04 10:03:35 -04:00
|
|
|
return o;
|
|
|
|
case '>':
|
2020-05-04 18:14:41 -04:00
|
|
|
o.number = n1 > n2;
|
2020-05-04 10:03:35 -04:00
|
|
|
return o;
|
|
|
|
case '<':
|
2020-05-04 18:14:41 -04:00
|
|
|
o.number = n1 < n2;
|
2020-05-04 10:03:35 -04:00
|
|
|
return o;
|
|
|
|
}
|
2016-04-18 04:25:36 -04:00
|
|
|
|
2020-05-04 10:03:35 -04:00
|
|
|
o = *obj1;
|
|
|
|
return o;
|
|
|
|
}
|
2016-04-18 04:25:36 -04:00
|
|
|
|
2020-05-04 10:03:35 -04:00
|
|
|
#define bopf(_name, _char) \
|
|
|
|
Object _name(Object obj1, Object obj2) \
|
|
|
|
{ \
|
|
|
|
return basicOp(&obj1, &obj2, _char); \
|
|
|
|
}
|
2016-04-18 04:25:36 -04:00
|
|
|
|
2020-05-04 10:03:35 -04:00
|
|
|
bopf(add, '+');
|
|
|
|
bopf(sub, '-');
|
|
|
|
bopf(mul, '*');
|
|
|
|
bopf(dvi, '/');
|
|
|
|
bopf(equ, '=');
|
2020-05-04 18:14:41 -04:00
|
|
|
bopf(gth, '>');
|
|
|
|
bopf(lth, '<');
|
2016-04-18 04:25:36 -04:00
|
|
|
|
|
|
|
void addFunc(const char *name, Object (*func)(Object, Object),
|
|
|
|
struct Environment *env)
|
|
|
|
{
|
2020-05-04 18:14:41 -04:00
|
|
|
Object o;
|
|
|
|
o.type = TYPE_FUNC;
|
|
|
|
o.forward = NULL;
|
|
|
|
o.func = func;
|
|
|
|
addToEnv(env, name, o);
|
|
|
|
}
|
|
|
|
|
|
|
|
void deleteEnv(struct Environment *e)
|
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
while(e->strings[i]) {
|
|
|
|
free(e->strings[i]);
|
|
|
|
i++;
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
2020-05-04 18:14:41 -04:00
|
|
|
free(e->strings);
|
|
|
|
e->strings = NULL;
|
|
|
|
|
|
|
|
free(e->objects);
|
|
|
|
e->objects = NULL;
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2020-05-04 18:14:41 -04:00
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
e.objects = malloc(sizeof(Object) * MAX_ENV_ELM);
|
2020-05-04 18:14:41 -04:00
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
addFunc("+", &add, &e);
|
|
|
|
addFunc("-", &sub, &e);
|
|
|
|
addFunc("*", &mul, &e);
|
2020-05-04 10:03:35 -04:00
|
|
|
addFunc("/", &dvi, &e);
|
|
|
|
addFunc("=", &equ, &e);
|
2020-05-04 18:14:41 -04:00
|
|
|
addFunc(">", >h, &e);
|
|
|
|
addFunc("<", <h, &e);
|
2016-04-18 04:25:36 -04:00
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object parseEval(const char *input, struct Environment *env)
|
|
|
|
{
|
2020-05-04 18:14:41 -04:00
|
|
|
struct Slice *tokens = nf_tokenize(input);
|
2020-05-03 17:00:45 -04:00
|
|
|
#ifdef STANDALONE
|
2020-05-04 18:14:41 -04:00
|
|
|
struct Slice *debug = tokens;
|
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);
|
|
|
|
debug++;
|
|
|
|
}
|
|
|
|
}
|
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);
|
2020-05-04 10:03:35 -04:00
|
|
|
printObj(&obj);
|
2020-05-04 18:14:41 -04:00
|
|
|
//break;
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void)
|
|
|
|
{
|
|
|
|
struct Environment env = defaultEnv();
|
2020-05-04 18:14:41 -04:00
|
|
|
#ifndef NO_REPL
|
|
|
|
repl(&env);
|
|
|
|
#else
|
|
|
|
struct Slice *tokens = nf_tokenize("(+ 10 5)");
|
|
|
|
struct Slice *debug = tokens;
|
|
|
|
|
|
|
|
if(debug) {
|
|
|
|
while(debug->text) {
|
|
|
|
char tok[10];
|
|
|
|
copySlice(tok, debug);
|
|
|
|
printf("'%s', ", tok);
|
|
|
|
debug++;
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
2020-05-04 18:14:41 -04:00
|
|
|
printf("\n");
|
|
|
|
} else {
|
|
|
|
printf("parse error\n");
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
2020-05-04 18:14:41 -04:00
|
|
|
parse(tokens);
|
|
|
|
free(tokens);
|
|
|
|
#endif
|
|
|
|
deleteEnv(&env);
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
#endif
|