2016-04-18 04:25:36 -04:00
|
|
|
#include "pebblisp.h"
|
|
|
|
#include "tokens.h"
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
2020-05-07 20:32:01 -04:00
|
|
|
#ifndef STANDALONE
|
|
|
|
#undef printf
|
|
|
|
#define printf(...) APP_LOG(APP_LOG_LEVEL_DEBUG, __VA_ARGS__)
|
|
|
|
#endif
|
|
|
|
|
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
|
|
|
{
|
2020-05-11 01:15:44 -04:00
|
|
|
const Object *newSymbol = arg_forms;
|
|
|
|
const char *name = newSymbol->name;
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
const Object newValue = eval(newSymbol->forward, env);
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
addToEnv(env, name, newValue);
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
return *newSymbol;
|
2020-05-04 18:14:41 -04:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
Object evalLambdaArgs(const Object *arg_forms)
|
|
|
|
{
|
2020-05-08 02:29:06 -04:00
|
|
|
// params // body
|
|
|
|
return constructLambda(arg_forms, arg_forms? arg_forms->forward : NULL);
|
2020-05-05 13:42:28 -04:00
|
|
|
}
|
|
|
|
|
2020-05-08 00:32:08 -04:00
|
|
|
Object evalMapArgs(const Object *arg_forms, struct Environment *env)
|
2020-05-06 11:58:09 -04:00
|
|
|
{
|
2020-05-07 20:32:01 -04:00
|
|
|
if(!arg_forms)
|
2020-05-07 22:37:54 -04:00
|
|
|
return errorObject(NULL_MAP_ARGS);
|
2020-05-07 20:32:01 -04:00
|
|
|
|
2020-05-08 12:00:14 -04:00
|
|
|
const Object lambda = eval(arg_forms, env);
|
2020-05-11 01:15:44 -04:00
|
|
|
const Object *inputList = arg_forms->forward;
|
2020-05-07 20:32:01 -04:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
if(lambda.type != TYPE_LAMBDA || inputList->type != TYPE_LIST)
|
2020-05-07 22:37:54 -04:00
|
|
|
return errorObject(BAD_TYPE);
|
2020-05-07 20:32:01 -04:00
|
|
|
|
2020-05-07 22:37:54 -04:00
|
|
|
Object list = listObject();
|
2020-05-07 20:32:01 -04:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
FOR_POINTER_IN_LIST(inputList) {
|
|
|
|
// Create a new list for each element,
|
2020-05-07 20:32:01 -04:00
|
|
|
// since lambda evaluation looks for a list
|
2020-05-15 23:22:08 -04:00
|
|
|
Object tempList;
|
|
|
|
if(POINTER->type == TYPE_LIST) {
|
|
|
|
Object tl = listObject();
|
|
|
|
copyList(&tl, POINTER);
|
|
|
|
tempList = startList(tl);
|
|
|
|
} else {
|
|
|
|
tempList = startList(*POINTER);
|
|
|
|
}
|
2020-05-07 20:32:01 -04:00
|
|
|
|
|
|
|
struct Environment newEnv =
|
|
|
|
envForLambda(&lambda.lambda->params, &tempList, env);
|
2020-05-11 01:15:44 -04:00
|
|
|
// Add the lambda evaluation to the list
|
|
|
|
nf_addToList(&list, eval(&lambda.lambda->body, &newEnv));
|
2020-05-15 23:22:08 -04:00
|
|
|
deleteEnv(&newEnv);
|
|
|
|
cleanObject(&tempList);
|
2020-05-07 20:32:01 -04:00
|
|
|
}
|
|
|
|
|
2020-05-07 22:37:54 -04:00
|
|
|
return list;
|
2020-05-06 11:58:09 -04:00
|
|
|
}
|
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
Object evalBuiltIns(const Object *first, const Object *rest,
|
2020-05-04 10:03:35 -04:00
|
|
|
struct Environment *env)
|
|
|
|
{
|
|
|
|
if(strcmp(first->name, "def") == 0) {
|
2020-05-04 18:14:41 -04:00
|
|
|
return evalDefArgs(rest, env);
|
2020-05-06 03:09:25 -04:00
|
|
|
} else if(strcmp(first->name, "if") == 0) {
|
2020-05-04 10:03:35 -04:00
|
|
|
return evalIfArgs(rest, env);
|
2020-05-06 03:09:25 -04:00
|
|
|
} else if(strcmp(first->name, "fn") == 0) {
|
2020-05-05 13:42:28 -04:00
|
|
|
return evalLambdaArgs(rest);
|
2020-05-07 20:32:01 -04:00
|
|
|
} else if(strcmp(first->name, "map") == 0) {
|
2020-05-08 00:32:08 -04:00
|
|
|
return evalMapArgs(rest, env);
|
2020-05-04 10:03:35 -04:00
|
|
|
}
|
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
return errorObject(BUILT_IN_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
|
|
|
void eval_forms(Object *destList, const Object *src, struct Environment *env)
|
|
|
|
{
|
|
|
|
int length = listLength(src) - 1; // Not counting first_form
|
|
|
|
for(int i = 0; i < length; i++) { // Evaluates all in list
|
|
|
|
destList[i] = eval(itemAt(src, i + 1), env); // Skip the first
|
|
|
|
}
|
2020-05-04 10:03:35 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
Object eval(const Object *obj, struct Environment *env)
|
2016-04-18 04:25:36 -04:00
|
|
|
{
|
2020-05-10 12:51:46 -04:00
|
|
|
// printf("eval():\n");
|
|
|
|
// printObj(obj);
|
2016-04-18 04:25:36 -04:00
|
|
|
switch(obj->type) {
|
|
|
|
case TYPE_NUMBER:
|
2020-05-04 10:03:35 -04:00
|
|
|
case TYPE_BOOL:
|
2020-05-06 03:09:25 -04:00
|
|
|
return *obj; // Return as is
|
2020-05-15 23:22:08 -04:00
|
|
|
|
2020-05-15 16:28:16 -04:00
|
|
|
case TYPE_STRING:
|
2020-05-15 23:22:08 -04:00
|
|
|
return copyString(*obj);
|
2020-05-04 10:03:35 -04:00
|
|
|
|
|
|
|
case TYPE_SYMBOL:
|
2020-05-09 20:39:01 -04:00
|
|
|
return fetchFromEnvironment(obj->name, env);
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
case TYPE_LIST:
|
|
|
|
{
|
2020-05-06 03:09:25 -04:00
|
|
|
if(listLength(obj) == 0)
|
|
|
|
return *obj;
|
2020-05-05 13:42:28 -04:00
|
|
|
|
|
|
|
if(listLength(obj) == 1)
|
|
|
|
return eval(obj->list, env);
|
|
|
|
|
|
|
|
Object *first_form = obj->list;
|
2020-05-04 10:03:35 -04:00
|
|
|
|
|
|
|
{ // Try to eval built-ins
|
2020-05-09 23:51:55 -04:00
|
|
|
const Object built_in =
|
2020-05-05 13:42:28 -04:00
|
|
|
evalBuiltIns(first_form, first_form->forward, env);
|
2020-05-07 20:32:01 -04:00
|
|
|
|
2020-05-06 03:09:25 -04:00
|
|
|
// deleteList(obj); // Decreases indirectly lost memory, but fails on Pebble
|
2020-05-07 20:32:01 -04:00
|
|
|
if(built_in.type != TYPE_ERROR) {
|
2020-05-04 10:03:35 -04:00
|
|
|
return built_in;
|
2020-05-07 20:32:01 -04:00
|
|
|
}
|
2020-05-04 10:03:35 -04:00
|
|
|
}
|
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
Object first_eval = eval(first_form, env);
|
|
|
|
if(first_eval.type == TYPE_FUNC) {
|
|
|
|
int length = listLength(obj) - 1; // Not counting first_form
|
|
|
|
Object rest[length];
|
|
|
|
eval_forms(rest, obj, env);
|
|
|
|
|
2020-05-06 01:00:40 -04:00
|
|
|
Object func_eval = rest[0];
|
|
|
|
for(int i = 1; i < length; i++) {
|
2020-05-15 23:22:08 -04:00
|
|
|
Object toClean = func_eval;
|
2020-05-09 23:51:55 -04:00
|
|
|
func_eval = first_eval.func(func_eval, rest[i], env);
|
2020-05-15 23:22:08 -04:00
|
|
|
//if(i != length - 1)
|
|
|
|
cleanObject(&toClean);
|
2020-05-15 16:28:16 -04:00
|
|
|
cleanObject(&rest[i]);
|
2020-05-06 01:00:40 -04:00
|
|
|
}
|
2020-05-11 01:15:44 -04:00
|
|
|
|
2020-05-06 03:09:25 -04:00
|
|
|
// deleteList(obj); // Decreases indirectly lost memory, but fails on Pebble
|
2020-05-05 13:42:28 -04:00
|
|
|
return func_eval;
|
2020-05-06 00:47:14 -04:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
} else if (first_eval.type == TYPE_LAMBDA) {
|
|
|
|
struct Environment newEnv =
|
|
|
|
envForLambda(&first_eval.lambda->params, first_form->forward, env);
|
2020-05-07 20:32:01 -04:00
|
|
|
Object ret = eval(&first_eval.lambda->body, &newEnv);
|
|
|
|
deleteEnv(&newEnv);
|
|
|
|
return ret;
|
2020-05-06 00:47:14 -04:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
} else {
|
2020-05-14 23:51:59 -04:00
|
|
|
Object newList = listObject();
|
|
|
|
copyList(&newList, obj);
|
|
|
|
return newList;
|
|
|
|
//return *obj;
|
2020-05-05 13:42:28 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case TYPE_LAMBDA:
|
2020-05-06 02:12:58 -04:00
|
|
|
return errorObject(UNEXPECTED_FORM);
|
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
default:
|
2020-05-11 01:15:44 -04:00
|
|
|
return *obj;
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-07 20:32:01 -04:00
|
|
|
Result result(Object obj, struct Slice *slices)
|
2016-04-18 04:25:36 -04:00
|
|
|
{
|
2020-05-07 20:32:01 -04:00
|
|
|
return (Result) {
|
|
|
|
.obj = obj,
|
|
|
|
.slices = slices
|
|
|
|
};
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
|
2020-05-10 02:27:59 -04:00
|
|
|
Object catObjects(const Object obj1, const Object obj2, struct Environment *env)
|
|
|
|
{
|
2020-05-15 16:28:16 -04:00
|
|
|
Object evalObj1 = eval(&obj1, env);
|
|
|
|
Object evalObj2 = eval(&obj2, env);
|
2020-05-10 02:27:59 -04:00
|
|
|
|
|
|
|
char str1[100] = "";
|
|
|
|
char str2[100] = "";
|
|
|
|
stringObj(str1, &evalObj1);
|
|
|
|
stringObj(str2, &evalObj2);
|
2020-05-15 16:28:16 -04:00
|
|
|
cleanObject(&evalObj1);
|
|
|
|
cleanObject(&evalObj2);
|
2020-05-10 02:27:59 -04:00
|
|
|
int length = strlen(str1) + strlen(str2) + 1;
|
|
|
|
|
|
|
|
Object o = newObject(TYPE_STRING);
|
|
|
|
o.string = calloc(sizeof(char), length);
|
|
|
|
strcat(o.string, str1);
|
|
|
|
strcat(o.string, str2);
|
|
|
|
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2020-05-15 00:43:08 -04:00
|
|
|
Object _basicOp(const Object *obj1, const Object *obj2, const char op,
|
|
|
|
struct Environment *env)
|
2016-04-18 04:25:36 -04:00
|
|
|
{
|
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-15 00:43:08 -04:00
|
|
|
if(obj1->type == TYPE_STRING || obj2->type == TYPE_STRING)
|
|
|
|
return catObjects(*obj1, *obj2, env);
|
2020-05-06 02:12:58 -04:00
|
|
|
return numberObject(n1 + n2);
|
2016-04-18 04:25:36 -04:00
|
|
|
case '-':
|
2020-05-06 02:12:58 -04:00
|
|
|
return numberObject(n1 - n2);
|
2016-04-18 04:25:36 -04:00
|
|
|
case '*':
|
2020-05-06 02:12:58 -04:00
|
|
|
return numberObject(n1 * n2);
|
2016-04-18 04:25:36 -04:00
|
|
|
case '/':
|
2020-05-06 02:12:58 -04:00
|
|
|
return numberObject(n1 / n2);
|
2020-05-10 02:27:59 -04:00
|
|
|
case '%':
|
|
|
|
return numberObject(n1 % n2);
|
2016-04-18 04:25:36 -04:00
|
|
|
|
2020-05-04 10:03:35 -04:00
|
|
|
case '=':
|
2020-05-06 02:12:58 -04:00
|
|
|
return boolObject(n1 == n2);
|
2020-05-04 10:03:35 -04:00
|
|
|
case '>':
|
2020-05-06 02:12:58 -04:00
|
|
|
return boolObject(n1 > n2);
|
2020-05-04 10:03:35 -04:00
|
|
|
case '<':
|
2020-05-06 02:12:58 -04:00
|
|
|
return boolObject(n1 < n2);
|
2020-05-04 10:03:35 -04:00
|
|
|
}
|
2016-04-18 04:25:36 -04:00
|
|
|
|
2020-05-06 02:12:58 -04:00
|
|
|
return *obj1;
|
2020-05-04 10:03:35 -04:00
|
|
|
}
|
2016-04-18 04:25:36 -04:00
|
|
|
|
2020-05-09 23:51:55 -04:00
|
|
|
Object basicOp(const Object *obj1, const Object *obj2, const char op,
|
|
|
|
struct Environment *env)
|
|
|
|
{
|
|
|
|
int lists = (obj1->type == TYPE_LIST) + (obj2->type == TYPE_LIST);
|
|
|
|
if(lists == 0) {
|
2020-05-15 00:43:08 -04:00
|
|
|
return _basicOp(obj1, obj2, op, env);
|
2020-05-11 01:15:44 -04:00
|
|
|
|
2020-05-09 23:51:55 -04:00
|
|
|
} else if(lists == 1) { // Single operand is applied to each element in list
|
|
|
|
const Object *listObj = (obj1->type == TYPE_LIST)? obj1 : obj2;
|
|
|
|
const Object *singleObj = (obj1->type == TYPE_LIST)? obj2 : obj1;
|
2020-05-11 01:15:44 -04:00
|
|
|
|
2020-05-09 23:51:55 -04:00
|
|
|
Object newList = listObject();
|
|
|
|
FOR_POINTER_IN_LIST(listObj) {
|
|
|
|
Object adding = eval(POINTER, env);
|
2020-05-15 00:43:08 -04:00
|
|
|
nf_addToList(&newList, _basicOp(&adding, singleObj, op, env));
|
2020-05-09 23:51:55 -04:00
|
|
|
}
|
|
|
|
return newList;
|
2020-05-11 01:15:44 -04:00
|
|
|
|
2020-05-09 23:51:55 -04:00
|
|
|
} else { // 2 lists
|
|
|
|
if(listLength(obj1) == listLength(obj2)) {
|
|
|
|
Object newList = listObject();
|
|
|
|
FOR_POINTERS_IN_LISTS(obj1, obj2) {
|
2020-05-11 01:15:44 -04:00
|
|
|
const Object ev1 = eval(P1, env);
|
|
|
|
const Object ev2 = eval(P2, env);
|
2020-05-15 00:43:08 -04:00
|
|
|
nf_addToList(&newList, _basicOp(&ev1, &ev2, op, env));
|
2020-05-09 23:51:55 -04:00
|
|
|
}
|
|
|
|
return newList;
|
|
|
|
} else {
|
|
|
|
return errorObject(LISTS_NOT_SAME_SIZE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
#define BASIC_OP(_name, _char) \
|
2020-05-09 23:51:55 -04:00
|
|
|
Object _name(Object obj1, Object obj2, struct Environment *env) \
|
|
|
|
{ return basicOp(&obj1, &obj2, _char, env); }
|
2016-04-18 04:25:36 -04:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
BASIC_OP(add, '+');
|
|
|
|
BASIC_OP(sub, '-');
|
|
|
|
BASIC_OP(mul, '*');
|
|
|
|
BASIC_OP(dvi, '/');
|
|
|
|
BASIC_OP(mod, '%');
|
|
|
|
BASIC_OP(equ, '=');
|
|
|
|
BASIC_OP(gth, '>');
|
|
|
|
BASIC_OP(lth, '<');
|
2016-04-18 04:25:36 -04:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
#undef BASIC_OP
|
2016-04-18 04:25:36 -04:00
|
|
|
|
2020-05-15 00:43:08 -04:00
|
|
|
void copySlice(char * dest, struct Slice *src)
|
|
|
|
{
|
|
|
|
if(!dest || !src)
|
|
|
|
return;
|
|
|
|
strncpy(dest, src->text, src->length);
|
|
|
|
dest[(int)src->length] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
void debugSlice(struct Slice *s)
|
|
|
|
{
|
|
|
|
if(!s) {
|
|
|
|
printf("NULL SLICE\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf("Debug Slice\n text:'");
|
|
|
|
for(int i = 0; i < s->length; i++) {
|
|
|
|
printf("%c", s->text[i]);
|
|
|
|
if(s->text[i] == '\0')
|
|
|
|
printf("NULLCHAR\n");
|
|
|
|
}
|
|
|
|
printf("'\n");
|
|
|
|
printf(" length: %d\n", s->length);
|
|
|
|
}
|
|
|
|
|
|
|
|
Result parse(struct Slice *slices)
|
|
|
|
{
|
|
|
|
struct Slice *token = slices;
|
|
|
|
if(token && token->text) {
|
|
|
|
struct Slice *rest = &slices[1];
|
|
|
|
if(token->text[0] == '(') {
|
|
|
|
// todo check for null rest
|
|
|
|
return readSeq(rest);
|
|
|
|
} else { // todo error on missing close paren
|
|
|
|
return result(parseAtom(token), rest);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return result(errorObject(NULL_PARSE), NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Result readSeq(struct Slice *tokens)
|
|
|
|
{
|
|
|
|
Object res = listObject();
|
|
|
|
for(;;) {
|
|
|
|
struct Slice *next = tokens;
|
|
|
|
struct Slice *rest = next->text? &next[1] : NULL;
|
|
|
|
if(next->text[0] == ')') {
|
|
|
|
return result(res, rest);
|
|
|
|
}
|
|
|
|
Result r = parse(tokens);
|
|
|
|
nf_addToList(&res, r.obj);
|
|
|
|
tokens = r.slices;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Object parseAtom(struct Slice *s)
|
|
|
|
{
|
|
|
|
if(isDigit(s->text[0])) {
|
|
|
|
int num = 0;
|
|
|
|
for(int i = 0; i < s->length; i++) {
|
|
|
|
num *= 10;
|
|
|
|
num += s->text[i] - '0';
|
|
|
|
}
|
|
|
|
return numberObject(num);
|
|
|
|
|
|
|
|
} else if (s->text[0] == 'T' && s->length == 1) {
|
|
|
|
return boolObject(1);
|
|
|
|
|
|
|
|
} else if (s->text[0] == 'F' && s->length == 1) {
|
|
|
|
return boolObject(0);
|
|
|
|
|
|
|
|
} else if (s->text[0] == '"') {
|
|
|
|
return objFromSlice(s->text, s->length);
|
|
|
|
} else {
|
|
|
|
Object o = symbolObject();
|
|
|
|
copySlice(o.name, s);
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
Object parseEval(const char *input, struct Environment *env)
|
|
|
|
{
|
2020-05-04 18:14:41 -04:00
|
|
|
struct Slice *tokens = nf_tokenize(input);
|
2020-05-08 02:29:06 -04:00
|
|
|
if(!tokens)
|
|
|
|
return errorObject(MISMATCHED_PARENS);
|
2020-05-11 01:15:44 -04:00
|
|
|
|
2020-05-06 00:47:14 -04:00
|
|
|
#ifdef DEBUG
|
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);
|
2020-05-11 01:15:44 -04:00
|
|
|
printd("slice: '%s'\n", tok);
|
2016-04-18 04:25:36 -04:00
|
|
|
debug++;
|
|
|
|
}
|
|
|
|
}
|
2020-05-03 17:00:45 -04:00
|
|
|
#endif
|
2020-05-11 01:15:44 -04:00
|
|
|
|
2020-05-10 13:51:33 -04:00
|
|
|
int i = 0;
|
|
|
|
int parens = 0;
|
2020-05-15 23:22:08 -04:00
|
|
|
Object obj = numberObject(0);
|
2020-05-10 13:51:33 -04:00
|
|
|
struct Slice *tok = tokens;
|
2020-05-14 23:51:59 -04:00
|
|
|
if(tok[i].text[0] != '(') {
|
|
|
|
Object parsed = parse(tok).obj;
|
|
|
|
obj = eval(&parsed, env);
|
|
|
|
cleanObject(&parsed);
|
|
|
|
} else {
|
|
|
|
while(tok[i].text != NULL) {
|
|
|
|
if(tok[i].text[0] == '(') {
|
|
|
|
parens++;
|
|
|
|
} else if(tok[i].text[0] == ')') {
|
|
|
|
parens--;
|
|
|
|
if(parens == 0) {
|
2020-05-15 23:22:08 -04:00
|
|
|
cleanObject(&obj);
|
2020-05-14 23:51:59 -04:00
|
|
|
Object parsed = parse(tok).obj;
|
|
|
|
tok = &tok[i + 1];
|
|
|
|
i = -1;
|
|
|
|
obj = eval(&parsed, env);
|
|
|
|
cleanObject(&parsed);
|
|
|
|
}
|
2020-05-10 13:51:33 -04:00
|
|
|
}
|
2020-05-14 23:51:59 -04:00
|
|
|
i++;
|
2020-05-10 13:51:33 -04:00
|
|
|
}
|
|
|
|
}
|
2020-05-03 21:24:36 -04:00
|
|
|
free(tokens);
|
2020-05-10 13:51:33 -04:00
|
|
|
return obj;
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef STANDALONE
|
|
|
|
int repl(struct Environment *env)
|
|
|
|
{
|
2020-05-09 22:32:26 -04:00
|
|
|
char input[200] = "";
|
2016-04-18 04:25:36 -04:00
|
|
|
while(input[0] != 'q') {
|
|
|
|
printf("pebblisp>> ");
|
|
|
|
fgets(input, 100, stdin);
|
|
|
|
Object obj = parseEval(input, env);
|
2020-05-10 12:51:46 -04:00
|
|
|
printAndClean(&obj);
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-08 12:08:14 -04:00
|
|
|
int main(int argc, const char* argv[])
|
2016-04-18 04:25:36 -04:00
|
|
|
{
|
|
|
|
struct Environment env = defaultEnv();
|
2020-05-10 14:15:53 -04:00
|
|
|
if(argc >= 2) {
|
|
|
|
Object r = numberObject(0);
|
|
|
|
for(int i = 1; i < argc; i++) {
|
|
|
|
// cleanObject(&r);
|
|
|
|
r = parseEval(argv[i], &env);
|
2020-05-14 23:51:59 -04:00
|
|
|
printAndClean(&r);
|
2020-05-10 14:15:53 -04:00
|
|
|
}
|
2020-05-14 23:51:59 -04:00
|
|
|
// printObj(&r);
|
|
|
|
//printAndClean(&r);
|
2020-05-08 12:08:14 -04:00
|
|
|
} else {
|
|
|
|
repl(&env);
|
|
|
|
}
|
2020-05-15 23:22:08 -04:00
|
|
|
deleteEnv(&env);
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
#endif
|