Added PC makefile, and defines for debug on/off
This commit is contained in:
parent
63be5f9435
commit
42c9f24f85
|
@ -0,0 +1,5 @@
|
|||
all:
|
||||
gcc -g -O0 -D STANDALONE pebblisp.c tokens.c object.c
|
||||
|
||||
debug:
|
||||
gcc -g -O0 -D STANDALONE -D DEBUG pebblisp.c tokens.c object.c
|
|
@ -105,6 +105,14 @@ char* stringObj(char *dest, const Object *obj)
|
|||
|
||||
int depth = 0;
|
||||
|
||||
void debugObj(const Object *obj)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
printObj(obj);
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
|
||||
// Prints the given object
|
||||
void printObj(const Object *obj)
|
||||
{
|
||||
|
|
|
@ -12,7 +12,8 @@ enum errorCode {
|
|||
BUILT_IN_NOT_FOUND,
|
||||
NULL_PARSE,
|
||||
NULL_LAMBDA_LIST,
|
||||
LAMBDA_ARGS_NOT_LIST
|
||||
LAMBDA_ARGS_NOT_LIST,
|
||||
DID_NOT_FIND_SYMBOL
|
||||
};
|
||||
|
||||
#ifdef STANDALONE
|
||||
|
@ -23,7 +24,8 @@ enum errorCode {
|
|||
"BUILT_IN_NOT_FOUND",
|
||||
"NULL_PARSE",
|
||||
"NULL_LAMBDA_LIST",
|
||||
"LAMBDA_ARGS_NOT_LIST"
|
||||
"LAMBDA_ARGS_NOT_LIST",
|
||||
"DID_NOT_FIND_SYMBOL"
|
||||
};
|
||||
#endif
|
||||
|
||||
|
@ -62,6 +64,7 @@ struct Lambda {
|
|||
char* stringObj(char *dest, const Object *obj);
|
||||
void printList(const Object *list);
|
||||
void printObj(const Object *obj);
|
||||
void debugObj(const Object *obj);
|
||||
|
||||
Object *tail(const Object *listObj);
|
||||
void addToList(Object *dest, Object src);
|
||||
|
|
|
@ -4,10 +4,6 @@
|
|||
#include <string.h>
|
||||
#include <stdio.h>
|
||||
|
||||
#ifndef STANDALONE
|
||||
#define printf(...) copySlice(NULL, NULL)
|
||||
#endif
|
||||
|
||||
void copySlice(char * dest, struct Slice *src)
|
||||
{
|
||||
if(!dest || !src)
|
||||
|
@ -18,12 +14,12 @@ void copySlice(char * dest, struct Slice *src)
|
|||
|
||||
void debugSlice(struct Slice *s)
|
||||
{
|
||||
printf("Debug Slice\n text:'");
|
||||
printd("Debug Slice\n text:'");
|
||||
for(int i = 0; i < s->length; i++) {
|
||||
printf("%c", s->text[i]);
|
||||
printd("%c", s->text[i]);
|
||||
}
|
||||
printf("'\n");
|
||||
printf(" length: %d\n", s->length);
|
||||
printd("'\n");
|
||||
printd(" length: %d\n", s->length);
|
||||
}
|
||||
|
||||
Object fetchFromEnvironment(const char *name, struct Environment *env)
|
||||
|
@ -31,7 +27,7 @@ Object fetchFromEnvironment(const char *name, struct Environment *env)
|
|||
if(!env)
|
||||
return errorObject(NULL_ENV);
|
||||
|
||||
printf("Fetching '%s' from env\n", name);
|
||||
printd("Fetching '%s' from env\n", name);
|
||||
printEnv(env);
|
||||
int i = 0;
|
||||
const char *next = env->strings[i];
|
||||
|
@ -41,14 +37,12 @@ Object fetchFromEnvironment(const char *name, struct Environment *env)
|
|||
}
|
||||
next = env->strings[++i];
|
||||
}
|
||||
printf("Trying outer\n");
|
||||
printd("Trying outer\n");
|
||||
if(env->outer) {
|
||||
return fetchFromEnvironment(name, env->outer);
|
||||
}
|
||||
printf("DID NOT FIND SYMBOL\n");
|
||||
Object o;
|
||||
o.type = TYPE_ERROR;
|
||||
return o;
|
||||
|
||||
return errorObject(DID_NOT_FIND_SYMBOL);
|
||||
}
|
||||
|
||||
Result parse(struct Slice *slices)
|
||||
|
@ -136,8 +130,8 @@ Object evalLambdaArgs(const Object *arg_forms)
|
|||
struct Environment envForLambda(const Object *params, const Object *arg_forms,
|
||||
struct Environment *outer)
|
||||
{
|
||||
printf("\n#####################\nenvForLambda()\n");
|
||||
printObj(arg_forms);
|
||||
printd("\n#####################\nenvForLambda()\n");
|
||||
debugObj(arg_forms);
|
||||
struct Environment env;
|
||||
env.outer = outer;
|
||||
env.strings = NULL;
|
||||
|
@ -160,10 +154,10 @@ struct Environment envForLambda(const Object *params, const Object *arg_forms,
|
|||
} // Something is segfaulting, anyway
|
||||
env.strings[length] = NULL;
|
||||
|
||||
printf("envForLambda env IT BROKE HERE()\n");
|
||||
printf("envForLambda length=%d\n", length);
|
||||
printd("envForLambda env IT BROKE HERE()\n");
|
||||
printd("envForLambda length=%d\n", length);
|
||||
printEnv(&env);
|
||||
printf("END envForLambda()\n\n");
|
||||
printd("END envForLambda()\n\n");
|
||||
return env;
|
||||
}
|
||||
|
||||
|
@ -175,10 +169,10 @@ Object evalBuiltIns(const Object *first, const Object *rest,
|
|||
}else if(strcmp(first->name, "if") == 0) {
|
||||
return evalIfArgs(rest, env);
|
||||
}else if(strcmp(first->name, "fn") == 0) {
|
||||
// printf("EVALBUILTINS first:\n");
|
||||
// printObj(first);
|
||||
// printf("\nEVALBUILTINS rest:\n");
|
||||
// printObj(rest);
|
||||
// printd("EVALBUILTINS first:\n");
|
||||
// debugObj(first);
|
||||
// printd("\nEVALBUILTINS rest:\n");
|
||||
// debugObj(rest);
|
||||
return evalLambdaArgs(rest);
|
||||
}
|
||||
|
||||
|
@ -195,8 +189,8 @@ void eval_forms(Object *destList, const Object *src, struct Environment *env)
|
|||
|
||||
Object eval(const Object *obj, struct Environment *env)
|
||||
{
|
||||
printf("eval():\n");
|
||||
printObj(obj);
|
||||
printd("eval():\n");
|
||||
debugObj(obj);
|
||||
switch(obj->type) {
|
||||
case TYPE_NUMBER:
|
||||
case TYPE_BOOL:
|
||||
|
@ -230,15 +224,15 @@ Object eval(const Object *obj, struct Environment *env)
|
|||
Object rest[length];
|
||||
eval_forms(rest, obj, env);
|
||||
|
||||
printf("Evaluating func\n");
|
||||
Object func_eval = first_eval.func(rest[0], rest[1]);
|
||||
// deleteList(obj);
|
||||
return func_eval;
|
||||
|
||||
} else if (first_eval.type == TYPE_LAMBDA) {
|
||||
printf("lammy\n");
|
||||
struct Environment newEnv =
|
||||
envForLambda(&first_eval.lambda->params, first_form->forward, env);
|
||||
return eval(&first_eval.lambda->body, &newEnv);
|
||||
|
||||
} else {
|
||||
return errorObject(TYPE_LIST_NOT_CAUGHT);
|
||||
}
|
||||
|
@ -287,8 +281,8 @@ void printEnv(struct Environment *env)
|
|||
for(int i = 0; i < MAX_ENV_ELM; i++) {
|
||||
if(env->strings[i] == NULL)
|
||||
return;
|
||||
printf("env[%d]: '%s'\n ", i, env->strings[i]);
|
||||
printObj(&env->objects[i]);
|
||||
printd("env[%d]: '%s'\n ", i, env->strings[i]);
|
||||
debugObj(&env->objects[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -395,20 +389,20 @@ struct Environment defaultEnv() {
|
|||
Object parseEval(const char *input, struct Environment *env)
|
||||
{
|
||||
struct Slice *tokens = nf_tokenize(input);
|
||||
#ifdef STANDALONE
|
||||
#ifdef DEBUG
|
||||
struct Slice *debug = tokens;
|
||||
if(debug) {
|
||||
while(debug->text) {
|
||||
char tok[MAX_TOK_LEN];
|
||||
copySlice(tok, debug);
|
||||
//printf("slice: '%s'\n", tok);
|
||||
//printd("slice: '%s'\n", tok);
|
||||
debug++;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
Object parsed = parse(tokens).obj;
|
||||
// printf("PARSEEVAL() PRINTLIST():\n");
|
||||
// printObj(&parsed);
|
||||
// debugObject(&parsed);
|
||||
// printf("end PARSEEVAL() PRINTLIST():\n\n");
|
||||
free(tokens);
|
||||
return eval(&parsed, env);
|
||||
|
|
|
@ -1,11 +1,20 @@
|
|||
#ifndef PEBBLISP_H
|
||||
#define PEBBLISP_H
|
||||
|
||||
// #define STANDALONE
|
||||
// #define NO_REPL
|
||||
|
||||
#include "object.h"
|
||||
|
||||
#ifndef STANDALONE
|
||||
#include <pebble.h>
|
||||
// #define printf(...) copySlice(NULL, NULL)
|
||||
#define printd(...) copySlice(NULL, NULL)
|
||||
#endif
|
||||
|
||||
#ifdef DEBUG
|
||||
#define printd(...) printf(__VA_ARGS__)
|
||||
#else
|
||||
#define printd(...) copySlice(NULL, NULL)
|
||||
#endif
|
||||
|
||||
struct Slice {
|
||||
const char *text;
|
||||
char length;
|
||||
|
|
|
@ -4,7 +4,8 @@
|
|||
#ifdef STANDALONE
|
||||
#include <stdio.h>
|
||||
#else
|
||||
#define printf(...) nf_tokenize(NULL)
|
||||
#include <pebble.h>
|
||||
// #define printf(...) nf_tokenize(NULL)
|
||||
#endif
|
||||
|
||||
/*
|
||||
|
|
Loading…
Reference in New Issue