pebblisp/src/pebblisp.c

400 lines
9.3 KiB
C
Raw Normal View History

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-04 10:03:35 -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-04 10:03:35 -04:00
// printf("parse() END\n");
2016-04-18 04:25:36 -04:00
}
Result readSeq(struct Slice *tokens)
{
2020-05-04 10:03:35 -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-04 10:03:35 -04:00
// printf("readSeq() before malloc\n");
2020-05-03 21:24:36 -04:00
march->forward = malloc(sizeof(struct Object));
2020-05-04 10:03:35 -04:00
// 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-04 10:03:35 -04:00
// printf("readSeq() END\n");
2016-04-18 04:25:36 -04:00
}
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 10:03:35 -04:00
Object evalDefArgs(const Object *first, const Object *rest,
struct Environment *env)
{
printf("evalDefArgs()\n");
Object o;
return o;
}
Object evalIfArgs(const Object *arg_forms, struct Environment *env)
{
printf("evalIfArgs()\n");
Object o;
printf("test_form:\n");
const Object *test_form = arg_forms;
printObj(test_form);
printObj(test_form->list);
//printObj(test_form->list->forward);
printf("test_eval:\n");
Object test_eval = eval(test_form, env);
printObj(&test_eval);
printf("test_form END\n");
o.type = TYPE_NUMBER;
o.number = 222;
return o;
}
Object evalBuiltIns(const Object *first, const Object *rest, int *found,
struct Environment *env)
{
if(strcmp(first->name, "def") == 0) {
*found = 0;
return evalDefArgs(first, rest, env);
}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-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:
2020-05-04 10:03:35 -04:00
case TYPE_BOOL:
2016-04-18 04:25:36 -04:00
return *obj;
2020-05-04 10:03:35 -04:00
case TYPE_SYMBOL:
return fetchFromEnvironment(obj->name, env);
2016-04-18 04:25:36 -04:00
case TYPE_LIST:
{
Object first_form = *obj->forward;
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-03 21:24:36 -04:00
return first_eval.func(arg1, arg2);
2016-04-18 04:25:36 -04:00
}
default:
;
}
return o;
}
2020-05-04 10:03:35 -04:00
char* stringObj(char *dest, const Object *obj)
2016-04-18 04:25:36 -04:00
{
if(obj->type == TYPE_NUMBER) {
snprintf(dest, MAX_TOK_LEN, "%d", obj->number);
} else if(obj->type == TYPE_SYMBOL) {
2020-05-04 10:03:35 -04:00
snprintf(dest, MAX_TOK_LEN, "%s", obj->name);
} else if(obj->type == TYPE_BOOL) {
snprintf(dest, MAX_TOK_LEN, "%s", obj->number ? "T" : "F");
2016-04-18 04:25:36 -04:00
}
return dest;
}
2020-05-04 10:03:35 -04:00
void printObj(const Object *obj)
2020-05-03 21:24:36 -04:00
{
if(obj->type == TYPE_NUMBER) {
printf("TYPE_NUMBER");
} else if(obj->type == TYPE_LIST) {
2020-05-04 10:03:35 -04:00
printf("TYPE_LIST");
2020-05-03 21:24:36 -04:00
} else if(obj->type == TYPE_SYMBOL) {
2020-05-04 10:03:35 -04:00
printf("TYPE_SYMBOL");
} else if(obj->type == TYPE_BOOL) {
printf("TYPE_BOOL");
2020-05-03 21:24:36 -04:00
} else {
printf("TYPE_OTHER");
}
2020-05-04 10:03:35 -04:00
char temp[20] = "";
2020-05-03 21:24:36 -04:00
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;
2020-05-04 10:03:35 -04:00
2016-04-18 04:25:36 -04:00
o.type = TYPE_NUMBER;
switch(op){
case '+':
o.number = obj1->number + obj2->number;
2020-05-04 10:03:35 -04:00
return o;
2016-04-18 04:25:36 -04:00
case '-':
o.number = obj1->number - obj2->number;
2020-05-04 10:03:35 -04:00
return o;
2016-04-18 04:25:36 -04:00
case '*':
o.number = obj1->number * obj2->number;
2020-05-04 10:03:35 -04:00
return o;
2016-04-18 04:25:36 -04:00
case '/':
o.number = obj1->number / obj2->number;
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 '=':
o.number = obj1->number == obj2->number;
return o;
case '>':
o.number = obj1->number > obj2->number;
return o;
case '<':
o.number = obj1->number < obj2->number;
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, '=');
2016-04-18 04:25:36 -04:00
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);
2020-05-04 10:03:35 -04:00
addFunc("/", &dvi, &e);
addFunc("=", &equ, &e);
2016-04-18 04:25:36 -04:00
/*
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);
2020-05-04 10:03:35 -04:00
printObj(&obj);
//printf("eval: %d\n", obj.number);
2016-04-18 04:25:36 -04:00
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