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