2016-04-18 04:25:36 -04:00
|
|
|
#include "pebblisp.h"
|
2022-01-07 16:55:03 -05:00
|
|
|
|
|
|
|
#include <stdio.h>
|
2016-04-18 04:25:36 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-03-15 16:49:12 -04:00
|
|
|
#include <readline/readline.h>
|
|
|
|
#include <readline/history.h>
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
#include "tokens.h"
|
2022-03-14 23:46:20 -04:00
|
|
|
#ifdef STANDALONE
|
|
|
|
#include "web.h"
|
|
|
|
#endif
|
2020-05-07 20:32:01 -04:00
|
|
|
|
2020-11-03 09:46:34 -05:00
|
|
|
/**
|
|
|
|
* Inserts a variable into the environment with a given name and value.
|
|
|
|
*
|
|
|
|
* If `argForms` (symbol) and `argForms->forward` (value) are lists of the same
|
|
|
|
* length, define each symbol element with the corresponding value element.
|
|
|
|
* I.e. `(def (a b) (5 20))` would store `a` as `5` and `b` as `20`.
|
|
|
|
*
|
|
|
|
* @param argForms The symbol(s) and value(s) to define in the environment
|
|
|
|
* @param env The environment to add the new definition to
|
|
|
|
* @return The symbol(s) defined
|
|
|
|
*/
|
2022-01-07 16:55:03 -05:00
|
|
|
Object evalDefArgs(const Object* symbol, const Object* value,
|
|
|
|
struct Environment* env)
|
|
|
|
{
|
|
|
|
const char* name = symbol->string;
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2020-11-03 09:46:34 -05:00
|
|
|
// Handles multi-definitions
|
2022-01-07 16:55:03 -05:00
|
|
|
if (bothAre(TYPE_LIST, symbol, value) &&
|
|
|
|
listLength(symbol) == listLength(value)) {
|
2020-11-06 15:07:12 -05:00
|
|
|
FOR_POINTERS_IN_LISTS(symbol, value) {
|
2020-11-03 09:46:34 -05:00
|
|
|
Object finalValue = eval(P2, env);
|
|
|
|
addToEnv(env, P1->string, finalValue);
|
|
|
|
cleanObject(&finalValue);
|
2020-11-02 13:42:37 -05:00
|
|
|
}
|
2020-11-06 15:07:12 -05:00
|
|
|
return cloneObject(*symbol);
|
2020-11-02 13:42:37 -05:00
|
|
|
}
|
|
|
|
|
2020-11-06 15:07:12 -05:00
|
|
|
Object finalValue = eval(value, env);
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2020-11-03 09:46:34 -05:00
|
|
|
addToEnv(env, name, finalValue);
|
|
|
|
cleanObject(&finalValue);
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2020-11-06 15:07:12 -05:00
|
|
|
return cloneObject(*symbol);
|
2020-05-04 18:14:41 -04:00
|
|
|
}
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2022-03-14 16:51:45 -04:00
|
|
|
void printStructDef(const struct StructDef *def)
|
|
|
|
{
|
|
|
|
printf("%s: {\n", def->name);
|
|
|
|
for (int i = 0; i < def->fieldCount; i++) {
|
|
|
|
printf(" %s,\n", def->names[i]);
|
|
|
|
}
|
|
|
|
printf("}\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a struct to the environment with a given name and fields.
|
|
|
|
*
|
|
|
|
* (struct point (x y))
|
|
|
|
*/
|
|
|
|
Object evalStructArgs(const Object* symbol, const Object* fields, struct Environment* env)
|
|
|
|
{
|
|
|
|
const char* name = symbol->string;
|
|
|
|
|
|
|
|
if (!isListy(*fields)) {
|
|
|
|
return errorObject(NOT_A_LIST);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StructDef def;
|
|
|
|
//def.name = name;
|
|
|
|
def.name = malloc(sizeof(char) * (strlen(name) + 1));
|
|
|
|
strcpy(def.name, name);
|
|
|
|
def.fieldCount = listLength(fields);
|
|
|
|
def.names = malloc(sizeof(char*) * def.fieldCount);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
FOR_POINTER_IN_LIST(fields) {
|
2022-03-15 15:26:01 -04:00
|
|
|
def.names[i] = malloc(sizeof(char) * (strlen(POINTER->string) + 1));
|
|
|
|
strcpy(def.names[i], POINTER->string);
|
2022-03-14 16:51:45 -04:00
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (env->outer) {
|
|
|
|
env = env->outer;
|
|
|
|
}
|
2022-03-15 15:26:01 -04:00
|
|
|
|
|
|
|
env->structDefs[env->structCount] = def;
|
|
|
|
//printStructDef(&env->structDefs[env->structCount]);
|
|
|
|
env->structCount += 1;
|
|
|
|
if (env->structCount == env->structCapacity) {
|
|
|
|
struct StructDef* prev = env->structDefs;
|
|
|
|
int prevCapacity = env->structCapacity;
|
|
|
|
env->structCapacity *= 2;
|
|
|
|
env->structDefs = malloc(sizeof(struct StructDef) * env->structCapacity);
|
|
|
|
printf("Can malloc\n");
|
|
|
|
//memcpy(env->structDefs, prev, sizeof(struct StructDef) * prevCapacity);
|
|
|
|
//printf("Can memcpy\n");
|
|
|
|
for (int i = 0; i < prevCapacity; i++) {
|
|
|
|
env->structDefs[i] = prev[i];
|
|
|
|
}
|
|
|
|
free(prev);
|
|
|
|
}
|
2022-03-14 16:51:45 -04:00
|
|
|
|
|
|
|
return boolObject(1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object evalIfArgs(const Object* argForms, struct Environment* env)
|
2020-05-04 18:14:41 -04:00
|
|
|
{
|
2021-07-05 05:08:37 -04:00
|
|
|
Object condition = eval(argForms, env);
|
2022-01-07 16:55:03 -05:00
|
|
|
Object result = condition.number ? eval(argForms->forward, env)
|
|
|
|
: eval(argForms->forward->forward, env);
|
2021-07-05 05:08:37 -04:00
|
|
|
cleanObject(&condition);
|
|
|
|
return result;
|
2020-05-04 10:03:35 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object evalLambdaArgs(const Object* argForms)
|
2020-05-05 13:42:28 -04:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
return constructLambda(argForms, // Params
|
|
|
|
argForms ? argForms->forward : NULL // Body
|
2020-08-02 16:16:26 -04:00
|
|
|
);
|
2020-05-05 13:42:28 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object evalMapArgs(const Object* argForms, struct Environment* env)
|
2020-05-06 11:58:09 -04:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
if (!argForms) {
|
2020-05-07 22:37:54 -04:00
|
|
|
return errorObject(NULL_MAP_ARGS);
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-28 10:28:28 -04:00
|
|
|
Object lambda = eval(argForms, env);
|
2022-01-07 16:55:03 -05:00
|
|
|
const Object* inputList = argForms->forward;
|
2020-08-02 16:16:26 -04:00
|
|
|
Object outputList = listObject();
|
2020-05-07 20:32:01 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
if (lambda.type != TYPE_LAMBDA) {
|
2020-05-07 22:37:54 -04:00
|
|
|
return errorObject(BAD_TYPE);
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2020-05-07 20:32:01 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
if (inputList) {
|
|
|
|
FOR_POINTER_IN_LIST(inputList) {
|
|
|
|
// Create a new list for each element,
|
|
|
|
// since lambda evaluation looks for a list
|
|
|
|
Object tempList = startList(cloneObject(*POINTER));
|
2020-05-16 10:46:19 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object* params = &lambda.lambda->params;
|
|
|
|
struct Environment newEnv = envForLambda(params, &tempList, env);
|
2020-05-07 20:32:01 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
// Add the lambda evaluation to the list
|
|
|
|
Object lambda_output = eval(&lambda.lambda->body, &newEnv);
|
|
|
|
Object delisted = cloneObject(*lambda_output.list);
|
|
|
|
nf_addToList(&outputList, delisted);
|
|
|
|
deleteEnv(&newEnv);
|
|
|
|
cleanObject(&tempList);
|
|
|
|
cleanObject(&lambda_output);
|
|
|
|
}
|
2020-05-07 20:32:01 -04:00
|
|
|
}
|
2020-05-28 10:28:28 -04:00
|
|
|
cleanObject(&lambda);
|
2020-05-07 20:32:01 -04:00
|
|
|
|
2020-08-02 16:16:26 -04:00
|
|
|
return outputList;
|
2020-05-06 11:58:09 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object evalBuiltIns(const Object* first, const Object* rest,
|
|
|
|
struct Environment* env)
|
2020-05-04 10:03:35 -04:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
if (first->type != TYPE_SYMBOL) {
|
2020-05-22 01:16:45 -04:00
|
|
|
return errorObject(NOT_A_SYMBOL);
|
|
|
|
}
|
2020-08-02 16:16:26 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
if (strcmp(first->string, "def") == 0) {
|
2020-11-06 15:07:12 -05:00
|
|
|
return evalDefArgs(rest, rest->forward, env);
|
2022-01-07 16:55:03 -05:00
|
|
|
#ifndef LOW_MEM
|
|
|
|
} else if (strcmp(first->string, "defe") == 0) {
|
2020-11-06 15:07:12 -05:00
|
|
|
Object symbol = eval(rest, env);
|
|
|
|
Object e = evalDefArgs(&symbol, rest->forward, env);
|
|
|
|
cleanObject(&symbol);
|
|
|
|
return e;
|
2022-01-07 16:55:03 -05:00
|
|
|
#endif
|
|
|
|
} else if (strcmp(first->string, "if") == 0) {
|
2020-05-04 10:03:35 -04:00
|
|
|
return evalIfArgs(rest, env);
|
2022-01-07 16:55:03 -05:00
|
|
|
} else if (strcmp(first->string, "fn") == 0) {
|
2020-05-05 13:42:28 -04:00
|
|
|
return evalLambdaArgs(rest);
|
2022-01-07 16:55:03 -05:00
|
|
|
} else if (strcmp(first->string, "map") == 0) {
|
2020-05-08 00:32:08 -04:00
|
|
|
return evalMapArgs(rest, env);
|
2022-03-14 16:51:45 -04:00
|
|
|
} else if (strcmp(first->string, "struct") == 0) {
|
|
|
|
return evalStructArgs(rest, rest->forward, env);
|
2020-05-04 10:03:35 -04:00
|
|
|
}
|
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
return errorObject(BUILT_IN_NOT_FOUND);
|
|
|
|
}
|
|
|
|
|
2020-11-03 14:35:47 -05:00
|
|
|
/**
|
|
|
|
* Bulk evaluator of Objects in a given list. Puts the results in a given array
|
|
|
|
*
|
|
|
|
* Note that `destArr` is a raw array, not an Object list, and that `start` is
|
|
|
|
* not itself a list, but the first element *in* a list to be evaluated. This
|
|
|
|
* allows more speed and flexibility in what should be evaluated.
|
|
|
|
*
|
|
|
|
* @param destArr The raw array to put the results into. Not an Object list!
|
|
|
|
* @param start The Object element to start with
|
|
|
|
* @param env The environment to use while evaluating
|
|
|
|
*/
|
2022-01-07 16:55:03 -05:00
|
|
|
void evalForms(Object* destArr, const Object* start, struct Environment* env)
|
2020-11-03 14:35:47 -05:00
|
|
|
{
|
|
|
|
int i = 0;
|
2022-01-07 16:55:03 -05:00
|
|
|
while (start) {
|
2020-11-03 14:35:47 -05:00
|
|
|
destArr[i] = eval(start, env);
|
|
|
|
start = start->forward;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates a list whose first element is a function, applying that function
|
|
|
|
*
|
|
|
|
* Tries to either apply the function to its parameters, or create a partial
|
|
|
|
* function, if not enough parameters were passed.
|
|
|
|
*
|
|
|
|
* @param list The list being evaluated
|
|
|
|
* @param function First element of the list, already evaluated to be a function
|
|
|
|
* @param length Length of `list` - 1, to exclude the already-evaluated element
|
|
|
|
* @param env The environment to evaluate in
|
|
|
|
*/
|
2022-01-07 16:55:03 -05:00
|
|
|
Object listEvalFunc(const Object* list, const Object* function,
|
|
|
|
const int length, struct Environment* env)
|
2020-11-03 14:35:47 -05:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
if (length == 0) {
|
2021-07-13 16:46:15 -04:00
|
|
|
return function->func(boolObject(0), boolObject(0), env);
|
|
|
|
}
|
|
|
|
|
2020-11-03 14:35:47 -05:00
|
|
|
Object rest[length];
|
|
|
|
evalForms(rest, list->list->forward, env);
|
|
|
|
|
|
|
|
Object func_result = rest[0];
|
2022-01-07 16:55:03 -05:00
|
|
|
if (length == 1) {
|
2021-07-21 11:26:04 -04:00
|
|
|
Object oneArg = errorObject(ONLY_ONE_ARGUMENT);
|
2022-01-07 16:55:03 -05:00
|
|
|
func_result = function->func(func_result, oneArg, env);
|
2020-11-03 14:35:47 -05:00
|
|
|
// Return a partial function if more parameters are required
|
|
|
|
// Otherwise, return the function result
|
2020-11-08 15:26:28 -05:00
|
|
|
cleanObject(&rest[0]);
|
2021-07-21 11:26:04 -04:00
|
|
|
if (isError(func_result, ONLY_ONE_ARGUMENT)) {
|
|
|
|
// These functions modify their second argument,
|
|
|
|
// so we don't clean oneArg here
|
|
|
|
cleanObject(&func_result);
|
|
|
|
return cloneObject(*list);
|
|
|
|
} else {
|
|
|
|
cleanObject(&oneArg);
|
|
|
|
return func_result;
|
|
|
|
}
|
2020-11-03 14:35:47 -05:00
|
|
|
} else {
|
|
|
|
// With two args, will apply function once
|
|
|
|
// With more than two args, apply the function repeatedly
|
2022-01-07 16:55:03 -05:00
|
|
|
for (int i = 1; i < length; i++) {
|
2020-11-03 14:35:47 -05:00
|
|
|
Object toClean = func_result;
|
|
|
|
func_result = function->func(func_result, rest[i], env);
|
|
|
|
cleanObject(&toClean);
|
|
|
|
cleanObject(&rest[i]);
|
|
|
|
}
|
|
|
|
return func_result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Evaluates a list whose first element is a lambda, applying that lambda
|
|
|
|
*
|
|
|
|
* Tries to apply the lambda to its parameters. Doesn't attempt partial
|
|
|
|
* application.
|
|
|
|
*
|
|
|
|
* @param lambda First element of the list, already evaluated to be a lambda
|
|
|
|
* @param remaining The first element after `lambda`
|
|
|
|
* @param env The environment to evaluate in
|
|
|
|
*/
|
2022-01-07 16:55:03 -05:00
|
|
|
Object listEvalLambda(Object* lambda, const Object* remaining,
|
|
|
|
struct Environment* env)
|
|
|
|
{
|
|
|
|
struct Environment newEnv =
|
|
|
|
envForLambda(&lambda->lambda->params, remaining, env);
|
2020-11-03 14:35:47 -05:00
|
|
|
Object ret = eval(&lambda->lambda->body, &newEnv);
|
2020-11-04 15:19:08 -05:00
|
|
|
|
2020-11-03 14:35:47 -05:00
|
|
|
deleteEnv(&newEnv);
|
|
|
|
cleanObject(lambda);
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object* t = tail(&ret);
|
|
|
|
if (t) {
|
2020-11-04 15:19:08 -05:00
|
|
|
Object o = cloneObject(*t);
|
|
|
|
cleanObject(&ret);
|
|
|
|
return o;
|
2020-05-05 13:42:28 -04:00
|
|
|
}
|
2020-11-03 14:35:47 -05:00
|
|
|
return ret;
|
2020-05-04 10:03:35 -04:00
|
|
|
}
|
|
|
|
|
2020-11-03 14:35:47 -05:00
|
|
|
/**
|
|
|
|
* Evaluates a given list, including the application of functions and lambdas
|
|
|
|
*
|
|
|
|
* Engages in several behaviors, depending on list contents:
|
|
|
|
* - () => ()
|
2020-11-03 14:44:37 -05:00
|
|
|
* - (x y z) => (eval_x eval_y eval_z)
|
2020-11-03 14:35:47 -05:00
|
|
|
* - (function x y) => evaluated function(x, y)
|
|
|
|
* - (function ...) => evaluated function(...) applied to each arg
|
|
|
|
* - (function x) => functionx() partial function
|
|
|
|
* - (lambda x) => evaluated lambda(x)
|
|
|
|
*
|
|
|
|
* @param obj The list to be evaluated
|
|
|
|
* @param env The environment to evaluate in
|
|
|
|
*/
|
2022-01-07 16:55:03 -05:00
|
|
|
Object evalList(const Object* obj, struct Environment* env)
|
2020-08-02 16:16:26 -04:00
|
|
|
{
|
|
|
|
const int evalLength = listLength(obj);
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
if (evalLength == 0) {
|
2020-08-02 16:16:26 -04:00
|
|
|
return cloneObject(*obj);
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2020-08-02 16:16:26 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object* first_form = obj->list;
|
|
|
|
{ // Try to eval built-ins
|
2021-07-21 11:26:04 -04:00
|
|
|
Object builtIn = evalBuiltIns(first_form, first_form->forward, env);
|
2020-08-02 16:16:26 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
if (!isError(builtIn, BUILT_IN_NOT_FOUND) &&
|
2020-08-02 16:16:26 -04:00
|
|
|
!isError(builtIn, NOT_A_SYMBOL)) {
|
|
|
|
return builtIn;
|
|
|
|
}
|
2021-07-21 11:26:04 -04:00
|
|
|
cleanObject(&builtIn);
|
2020-08-02 16:16:26 -04:00
|
|
|
}
|
|
|
|
|
2022-03-15 15:26:01 -04:00
|
|
|
//struct StructDef *def = NULL;
|
|
|
|
int def = -1;
|
2022-03-14 16:51:45 -04:00
|
|
|
if (first_form->type == TYPE_SYMBOL) {
|
|
|
|
struct Environment* outerEnv = env;
|
|
|
|
while (outerEnv->outer) {
|
|
|
|
outerEnv = outerEnv->outer;
|
|
|
|
}
|
|
|
|
//printf("evalList firstElementName: %s\n", first_form->string);
|
|
|
|
//printf("checking for struct `%s`\n", first_form->string);
|
|
|
|
//printf("%d structs available\n", outerEnv->structCount);
|
|
|
|
for (int i = 0; i < outerEnv->structCount; i++) {
|
|
|
|
//printf("struct[%d] - `%s`\n", i, outerEnv->structDefs[i].name);
|
|
|
|
if (strcmp(first_form->string, outerEnv->structDefs[i].name) == 0) {
|
2022-03-15 15:26:01 -04:00
|
|
|
def = i;
|
|
|
|
//printf("Found struct definition for %s!\n", first_form->string);
|
2022-03-14 16:51:45 -04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-03-15 15:26:01 -04:00
|
|
|
if (def >= 0) {
|
2022-03-14 16:51:45 -04:00
|
|
|
Object structo = structObject(def);
|
|
|
|
int i = 0;
|
|
|
|
FOR_POINTER_IN_LIST(obj) {
|
|
|
|
if (i != 0) {
|
|
|
|
structo.structObject->fields[i - 1] = eval(POINTER, env);
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
return structo;
|
|
|
|
}
|
|
|
|
|
2020-11-03 14:35:47 -05:00
|
|
|
// Evaluate the list based on the first element's type
|
2020-08-02 16:16:26 -04:00
|
|
|
Object first_eval = eval(first_form, env);
|
2022-01-07 16:55:03 -05:00
|
|
|
switch (first_eval.type) {
|
2020-11-03 14:35:47 -05:00
|
|
|
case TYPE_FUNC:
|
|
|
|
// Passes evalLength - 1, because we skip the first form
|
|
|
|
return listEvalFunc(obj, &first_eval, evalLength - 1, env);
|
2020-08-02 16:16:26 -04:00
|
|
|
|
2020-11-03 14:35:47 -05:00
|
|
|
case TYPE_LAMBDA:
|
|
|
|
return listEvalLambda(&first_eval, first_form->forward, env);
|
2020-08-02 16:16:26 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
default: { // Return list with each element evaluated
|
2020-11-03 14:35:47 -05:00
|
|
|
Object list = listObject();
|
2020-11-04 15:19:08 -05:00
|
|
|
int i = 0;
|
|
|
|
nf_addToList(&list, first_eval);
|
2020-11-03 14:35:47 -05:00
|
|
|
FOR_POINTER_IN_LIST(obj) {
|
2022-01-07 16:55:03 -05:00
|
|
|
if (i != 0) {
|
2020-11-04 15:19:08 -05:00
|
|
|
nf_addToList(&list, eval(POINTER, env));
|
|
|
|
}
|
|
|
|
i++;
|
2020-11-03 14:35:47 -05:00
|
|
|
}
|
|
|
|
return list;
|
2020-08-02 16:16:26 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object eval(const Object* obj, struct Environment* env)
|
2016-04-18 04:25:36 -04:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
switch (obj->type) {
|
2020-08-02 16:16:26 -04:00
|
|
|
case TYPE_FUNC:
|
2021-07-21 11:26:04 -04:00
|
|
|
case TYPE_ERROR:
|
2020-08-03 11:21:04 -04:00
|
|
|
case TYPE_OTHER:
|
2016-04-18 04:25:36 -04:00
|
|
|
case TYPE_NUMBER:
|
2020-05-04 10:03:35 -04:00
|
|
|
case TYPE_BOOL:
|
2020-05-15 16:28:16 -04:00
|
|
|
case TYPE_STRING:
|
2022-03-14 16:51:45 -04:00
|
|
|
case TYPE_STRUCT:
|
2020-05-22 01:16:45 -04:00
|
|
|
return cloneObject(*obj);
|
2020-05-04 10:03:35 -04:00
|
|
|
|
|
|
|
case TYPE_SYMBOL:
|
2020-05-22 01:16:45 -04:00
|
|
|
return fetchFromEnvironment(obj->string, env);
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2021-12-15 14:30:16 -05:00
|
|
|
case TYPE_SLIST: {
|
|
|
|
Object o = cloneObject(*obj);
|
|
|
|
o.type = TYPE_LIST;
|
|
|
|
return o;
|
|
|
|
}
|
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
case TYPE_LIST:
|
2020-08-02 16:16:26 -04:00
|
|
|
return evalList(obj, env);
|
2020-05-22 01:16:45 -04:00
|
|
|
|
2020-05-05 13:42:28 -04:00
|
|
|
case TYPE_LAMBDA:
|
2020-08-09 15:03:02 -04:00
|
|
|
return eval(&obj->lambda->body, env);
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
|
2020-08-02 16:16:26 -04:00
|
|
|
return errorObject(BAD_TYPE);
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
|
2022-03-15 21:43:53 -04:00
|
|
|
#define CAT_MAX 1024
|
2022-01-07 16:55:03 -05:00
|
|
|
Object catObjects(const Object obj1, const Object obj2, struct Environment* env)
|
2020-05-10 02:27:59 -04:00
|
|
|
{
|
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
|
|
|
|
2022-03-15 16:03:13 -04:00
|
|
|
char str1[CAT_MAX] = "";
|
|
|
|
char str2[CAT_MAX] = "";
|
2020-05-10 02:27:59 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object listEquality(const Object* list1, const Object* list2)
|
2020-08-02 16:16:26 -04:00
|
|
|
{
|
|
|
|
FOR_POINTERS_IN_LISTS(list1, list2) {
|
2022-01-07 16:55:03 -05:00
|
|
|
if (P1->type != P2->type || P1->number != P2->number) {
|
2020-08-02 16:16:26 -04:00
|
|
|
return boolObject(0);
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2020-08-02 16:16:26 -04:00
|
|
|
}
|
|
|
|
return boolObject(1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05: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;
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
switch (op) {
|
2016-04-18 04:25:36 -04:00
|
|
|
case '+':
|
2022-01-07 16:55:03 -05:00
|
|
|
if (eitherIs(TYPE_STRING, obj1, obj2)) {
|
2020-05-15 00:43:08 -04:00
|
|
|
return catObjects(*obj1, *obj2, env);
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
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
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
case '&':
|
|
|
|
return boolObject(n1 != 0 && n2 != 0);
|
|
|
|
case '|':
|
|
|
|
return boolObject(n1 != 0 || n2 != 0);
|
|
|
|
|
2020-05-04 10:03:35 -04:00
|
|
|
case '=':
|
2022-01-07 16:55:03 -05:00
|
|
|
if (bothAre(TYPE_STRING, obj1, obj2)) {
|
2020-05-22 01:16:45 -04:00
|
|
|
return boolObject(!strcmp(obj1->string, obj2->string));
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2022-01-07 16:55:03 -05:00
|
|
|
if (bothAre(TYPE_LIST, obj1, obj2)) {
|
2020-08-02 16:16:26 -04:00
|
|
|
return listEquality(obj1, obj2);
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
|
|
|
return boolObject(n1 == n2 && areSameType(obj1, obj2));
|
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
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object basicOp(const Object* obj1, const Object* obj2, const char op,
|
|
|
|
struct Environment* env)
|
2020-05-09 23:51:55 -04:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
if (isError(*obj2, ONLY_ONE_ARGUMENT)) {
|
2020-05-28 10:28:28 -04:00
|
|
|
return *obj2;
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2020-05-28 10:28:28 -04:00
|
|
|
|
2020-05-09 23:51:55 -04:00
|
|
|
int lists = (obj1->type == TYPE_LIST) + (obj2->type == TYPE_LIST);
|
2022-01-07 16:55:03 -05:00
|
|
|
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
|
|
|
|
2022-01-07 16:55:03 -05: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
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
} else { // 2 lists with the op applied to matching indices of both lists
|
|
|
|
if (listLength(obj1) == listLength(obj2)) {
|
2020-05-09 23:51:55 -04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
#define BASIC_OP(_name, _char) \
|
|
|
|
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, '+');
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
BASIC_OP(sub, '-');
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
BASIC_OP(mul, '*');
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
BASIC_OP(dvi, '/');
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
BASIC_OP(mod, '%');
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
BASIC_OP(equ, '=');
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
BASIC_OP(gth, '>');
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
BASIC_OP(lth, '<');
|
2016-04-18 04:25:36 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
BASIC_OP(and, '&');
|
|
|
|
|
|
|
|
BASIC_OP(or, '|');
|
|
|
|
|
2020-05-11 01:15:44 -04:00
|
|
|
#undef BASIC_OP
|
2016-04-18 04:25:36 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object filter(Object obj1, Object obj2, struct Environment* env)
|
2020-05-22 01:16:45 -04:00
|
|
|
{
|
|
|
|
Object filteredList = listObject();
|
2022-01-07 16:55:03 -05:00
|
|
|
Object* filteringList = &obj2;
|
2020-05-22 01:16:45 -04:00
|
|
|
FOR_POINTER_IN_LIST(filteringList) {
|
|
|
|
Object conditional = cloneObject(obj1);
|
2022-01-07 16:55:03 -05:00
|
|
|
nf_addToList(&conditional, *POINTER); // cloneObject()?
|
2020-05-28 10:28:28 -04:00
|
|
|
Object result = eval(&conditional, env);
|
|
|
|
cleanObject(&conditional);
|
2022-01-07 16:55:03 -05:00
|
|
|
if (result.number == 1) {
|
2020-05-22 01:16:45 -04:00
|
|
|
nf_addToList(&filteredList, *POINTER);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return filteredList;
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object append(Object list, Object newElement, struct Environment* env)
|
2020-05-28 10:28:28 -04:00
|
|
|
{
|
|
|
|
Object newList = cloneObject(list);
|
2020-11-03 00:07:08 -05:00
|
|
|
nf_addToList(&newList, cloneObject(newElement));
|
2020-05-28 10:28:28 -04:00
|
|
|
return newList;
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object prepend(Object list, Object newElement, struct Environment* env)
|
2020-10-29 11:23:35 -04:00
|
|
|
{
|
|
|
|
Object newList = listObject();
|
2020-11-03 00:07:08 -05:00
|
|
|
nf_addToList(&newList, cloneObject(newElement));
|
2020-10-29 11:23:35 -04:00
|
|
|
appendList(&newList, &list);
|
|
|
|
return newList;
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object at(Object index, Object list, struct Environment* env)
|
2020-05-28 10:28:28 -04:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
const Object* found = itemAt(&list, index.number);
|
|
|
|
if (found) {
|
2021-07-05 01:13:45 -04:00
|
|
|
return cloneObject(*found);
|
|
|
|
} else {
|
|
|
|
return errorObject(INDEX_PAST_END);
|
|
|
|
}
|
2020-05-28 10:28:28 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object rest(Object list, Object ignore, struct Environment* env)
|
2020-05-28 10:28:28 -04:00
|
|
|
{
|
|
|
|
Object ret = listObject();
|
2022-01-07 16:55:03 -05:00
|
|
|
Object* l = &list;
|
2020-05-28 10:28:28 -04:00
|
|
|
FOR_POINTER_IN_LIST(l) {
|
2022-01-07 16:55:03 -05:00
|
|
|
if (POINTER == l->list) {
|
2020-05-28 10:28:28 -04:00
|
|
|
continue;
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2020-11-03 00:07:08 -05:00
|
|
|
nf_addToList(&ret, cloneObject(*POINTER));
|
2020-05-28 10:28:28 -04:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object reverse(Object _list, Object ignore, struct Environment* ignore2)
|
2020-05-28 10:28:28 -04:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
const Object* list = &_list;
|
2020-05-28 10:28:28 -04:00
|
|
|
Object rev = listObject();
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object* tail = NULL;
|
2020-05-28 10:28:28 -04:00
|
|
|
FOR_POINTER_IN_LIST(list) {
|
2022-01-07 16:55:03 -05:00
|
|
|
Object* oldTail = tail;
|
2020-05-28 10:28:28 -04:00
|
|
|
allocObject(&tail, cloneObject(*POINTER));
|
2022-01-07 16:55:03 -05:00
|
|
|
if (oldTail) {
|
2020-05-28 10:28:28 -04:00
|
|
|
tail->forward = oldTail;
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2020-05-28 10:28:28 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
rev.list = tail;
|
|
|
|
return rev;
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object isNum(Object test, Object ignore, struct Environment* ignore2)
|
2020-10-28 18:08:03 -04:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
return test.type == TYPE_NUMBER ? boolObject(1) : boolObject(0);
|
2020-10-28 18:08:03 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object isString(Object test, Object ignore, struct Environment* ignore2)
|
2020-11-02 15:29:03 -05:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
return test.type == TYPE_STRING ? boolObject(1) : boolObject(0);
|
2020-11-02 15:29:03 -05:00
|
|
|
}
|
|
|
|
|
2021-12-10 16:33:59 -05:00
|
|
|
// Get the int value of a string's first character
|
2022-01-07 16:55:03 -05:00
|
|
|
Object charVal(Object test, Object ignore, struct Environment* ignore2)
|
2021-12-10 16:33:59 -05:00
|
|
|
{
|
|
|
|
return numberObject(test.string[0]);
|
2022-01-07 16:55:03 -05:00
|
|
|
// return test.type == TYPE_STRING && test.string[0] == '\0' ?
|
|
|
|
// boolObject(1) : boolObject(0);
|
2021-12-10 16:33:59 -05:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object isErr(Object test, Object ignore, struct Environment* ignore2)
|
2020-11-06 15:07:12 -05:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
return test.type == TYPE_ERROR ? boolObject(1) : boolObject(0);
|
2020-11-06 15:07:12 -05:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object charAt(Object string, Object at, struct Environment* ignore)
|
2021-12-10 16:33:59 -05:00
|
|
|
{
|
|
|
|
char* c = malloc(sizeof(char) * 2);
|
|
|
|
c[0] = string.string[at.number];
|
|
|
|
c[1] = '\0';
|
|
|
|
string.string = c;
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
|
2021-07-21 14:31:40 -04:00
|
|
|
#ifdef STANDALONE
|
2022-01-07 16:55:03 -05:00
|
|
|
|
|
|
|
Object print(Object p, Object ignore, struct Environment* env)
|
2020-10-30 14:36:44 -04:00
|
|
|
{
|
|
|
|
p = cloneObject(p);
|
|
|
|
p = eval(&p, env);
|
2020-11-02 15:23:04 -05:00
|
|
|
_printObj(&p, 0);
|
2020-11-04 15:19:08 -05:00
|
|
|
return numberObject(0);
|
2020-10-30 14:36:44 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 23:46:20 -04:00
|
|
|
Object addRouteO(Object path, Object textFunc, struct Environment* env)
|
|
|
|
{
|
2022-03-15 21:43:53 -04:00
|
|
|
char* p = malloc(sizeof(char) * strlen(path.string) + 1);
|
|
|
|
strcpy(p, path.string);
|
|
|
|
//const char* t = malloc(sizeof(char) * strlen(textFunc.string) + 1);
|
|
|
|
//strcpy(t, textFunc.string);
|
|
|
|
|
|
|
|
struct Route r;
|
|
|
|
r.path = p;
|
|
|
|
r.routeAction = cloneObject(textFunc);
|
|
|
|
r.env = env;
|
|
|
|
env->refs += 1;
|
|
|
|
//r.text = t;
|
|
|
|
addRoute(r);
|
2022-03-14 23:46:20 -04:00
|
|
|
|
|
|
|
return numberObject(1);
|
|
|
|
}
|
|
|
|
|
2022-03-15 21:43:53 -04:00
|
|
|
Object startServer(Object portObject, Object ignored, struct Environment* env)
|
2022-03-14 23:46:20 -04:00
|
|
|
{
|
2022-03-15 21:43:53 -04:00
|
|
|
int port = 8888;
|
|
|
|
if (portObject.type == TYPE_NUMBER) {
|
|
|
|
port = portObject.number;
|
|
|
|
}
|
|
|
|
return numberObject(start(port));
|
2022-03-14 23:46:20 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object pChar(Object c, Object i1, struct Environment* i2)
|
2020-11-02 16:30:21 -05:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
if (c.type != TYPE_NUMBER) {
|
2020-11-02 16:30:21 -05:00
|
|
|
return errorObject(BAD_NUMBER);
|
|
|
|
}
|
|
|
|
printf("%c", c.number % 256);
|
|
|
|
return numberObject(0);
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object printEnvO(Object i1, Object i2, struct Environment* env)
|
|
|
|
{
|
|
|
|
while (env->outer) {
|
|
|
|
env = env->outer;
|
2020-11-02 12:40:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
printEnv(env);
|
|
|
|
return numberObject(0);
|
|
|
|
}
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2021-07-21 14:31:40 -04:00
|
|
|
#endif
|
2020-11-02 12:40:42 -05:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object parseEvalO(Object text, Object ignore, struct Environment* env)
|
2020-11-02 15:46:17 -05:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
if (text.type == TYPE_SYMBOL) {
|
2020-11-06 15:07:12 -05:00
|
|
|
Object string = eval(&text, env);
|
|
|
|
Object parsed = parseEval(string.string, env);
|
|
|
|
cleanObject(&string);
|
|
|
|
return parsed;
|
2022-01-07 16:55:03 -05:00
|
|
|
} else if (text.type != TYPE_STRING) {
|
2020-11-02 15:46:17 -05:00
|
|
|
return errorObject(CAN_ONLY_EVAL_STRINGS);
|
|
|
|
}
|
|
|
|
return parseEval(text.string, env);
|
|
|
|
}
|
|
|
|
|
2020-11-02 16:30:21 -05:00
|
|
|
#ifdef STANDALONE
|
2022-01-07 16:55:03 -05:00
|
|
|
|
|
|
|
Object takeInput(Object i1, Object i2, struct Environment* i3)
|
2020-11-02 16:30:21 -05:00
|
|
|
{
|
|
|
|
char input[256] = "";
|
|
|
|
fgets(input, 256, stdin);
|
|
|
|
return stringFromSlice(input, strlen(input) - 1);
|
|
|
|
}
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2020-11-02 16:30:21 -05:00
|
|
|
#endif
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
void copySlice(char* dest, struct Slice* src)
|
2020-05-15 00:43:08 -04:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
if (!dest || !src) {
|
2020-05-15 00:43:08 -04:00
|
|
|
return;
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2020-05-15 00:43:08 -04:00
|
|
|
strncpy(dest, src->text, src->length);
|
2022-01-07 16:55:03 -05:00
|
|
|
dest[(int) src->length] = '\0';
|
2020-05-15 00:43:08 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
void debugSlice(struct Slice* s)
|
2020-05-15 00:43:08 -04:00
|
|
|
{
|
2021-07-21 14:31:40 -04:00
|
|
|
#ifdef DEBUG
|
2022-01-07 16:55:03 -05:00
|
|
|
if (!s) {
|
2020-05-15 00:43:08 -04:00
|
|
|
printf("NULL SLICE\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
printf("Debug Slice\n text:'");
|
2022-01-07 16:55:03 -05:00
|
|
|
for (int i = 0; i < s->length; i++) {
|
2020-05-15 00:43:08 -04:00
|
|
|
printf("%c", s->text[i]);
|
2022-01-07 16:55:03 -05:00
|
|
|
if (s->text[i] == '\0') {
|
2020-05-15 00:43:08 -04:00
|
|
|
printf("NULLCHAR\n");
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2020-05-15 00:43:08 -04:00
|
|
|
}
|
|
|
|
printf("'\n");
|
|
|
|
printf(" length: %d\n", s->length);
|
2021-07-21 14:31:40 -04:00
|
|
|
#endif
|
2020-05-15 00:43:08 -04:00
|
|
|
}
|
|
|
|
|
2022-03-15 15:26:01 -04:00
|
|
|
Object possessive(Object structo, Object field, struct Environment* env)
|
|
|
|
{
|
|
|
|
if (structo.type != TYPE_STRUCT) {
|
|
|
|
printf("`'s` must be used on a struct!\n");
|
|
|
|
return errorObject(NULL_PARSE);
|
|
|
|
}
|
|
|
|
if (!isStringy(field)) {
|
|
|
|
printf("`'s` field name must be stringy! Received a ");
|
|
|
|
printType(&field);
|
|
|
|
printObj(&field);
|
|
|
|
printf("\n");
|
|
|
|
return errorObject(NULL_PARSE);
|
|
|
|
}
|
|
|
|
|
2022-03-15 21:43:53 -04:00
|
|
|
struct StructDef structDef = global()->structDefs[structo.structObject->definition];
|
2022-03-15 15:26:01 -04:00
|
|
|
for (int i = 0; i < structDef.fieldCount; i++) {
|
|
|
|
if (strcmp(field.string, structDef.names[i]) == 0) {
|
|
|
|
return cloneObject(structo.structObject->fields[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Could not find field name `%s`\n", field.string);
|
|
|
|
return errorObject(NULL_PARSE);
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Result parse(struct Slice* slices)
|
2020-05-15 00:43:08 -04:00
|
|
|
{
|
2022-01-07 16:55:03 -05:00
|
|
|
struct Slice* token = slices;
|
|
|
|
if (token && token->text) {
|
|
|
|
struct Slice* rest = &slices[1];
|
|
|
|
if (token->text[0] == '\'' && token->text[1] == '(') {
|
2021-12-15 14:30:16 -05:00
|
|
|
Result r = readSeq(&slices[2]);
|
|
|
|
r.obj.type = TYPE_SLIST;
|
|
|
|
return r;
|
2022-01-07 16:55:03 -05:00
|
|
|
} else if (token->text[0] == '(') {
|
2020-05-15 00:43:08 -04:00
|
|
|
// todo check for null rest
|
|
|
|
return readSeq(rest);
|
2022-01-07 16:55:03 -05:00
|
|
|
} else { // todo error on missing close paren
|
2022-03-15 15:26:01 -04:00
|
|
|
Result r = parseAtom(token);
|
|
|
|
r.slices = &r.slices[1];
|
|
|
|
return r;
|
2020-05-15 00:43:08 -04:00
|
|
|
}
|
|
|
|
} else {
|
2022-01-07 16:55:03 -05:00
|
|
|
return (Result) {errorObject(NULL_PARSE), NULL};
|
2020-05-15 00:43:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Result readSeq(struct Slice* tokens)
|
2020-05-15 00:43:08 -04:00
|
|
|
{
|
|
|
|
Object res = listObject();
|
2022-01-07 16:55:03 -05:00
|
|
|
for (;;) {
|
|
|
|
struct Slice* next = tokens;
|
|
|
|
struct Slice* rest = next->text ? &next[1] : NULL;
|
|
|
|
if (next->text[0] == ')') {
|
|
|
|
return (Result) {res, rest};
|
2020-05-15 00:43:08 -04:00
|
|
|
}
|
|
|
|
Result r = parse(tokens);
|
2022-01-07 16:55:03 -05:00
|
|
|
if (r.obj.type == TYPE_ERROR) {
|
2020-05-28 10:28:28 -04:00
|
|
|
return r;
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2020-05-22 01:16:45 -04:00
|
|
|
nf_addToList(&res, cloneObject(r.obj));
|
2020-05-15 00:43:08 -04:00
|
|
|
tokens = r.slices;
|
2020-05-22 01:16:45 -04:00
|
|
|
cleanObject(&r.obj);
|
2020-05-15 00:43:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object parseDecimal(struct Slice* s)
|
2020-05-15 00:43:08 -04:00
|
|
|
{
|
2020-05-22 01:16:45 -04:00
|
|
|
int num = 0;
|
2022-01-07 16:55:03 -05:00
|
|
|
for (int i = 0; i < s->length; i++) {
|
|
|
|
if (!isDigit(s->text[i])) {
|
2020-08-02 16:16:26 -04:00
|
|
|
return errorObject(BAD_NUMBER);
|
2020-05-15 00:43:08 -04:00
|
|
|
}
|
2020-05-22 01:16:45 -04:00
|
|
|
num *= 10;
|
|
|
|
num += s->text[i] - '0';
|
|
|
|
}
|
|
|
|
return numberObject(num);
|
2020-05-16 14:31:14 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object parseHex(struct Slice* s)
|
2020-08-02 16:16:26 -04:00
|
|
|
{
|
|
|
|
int num = 0;
|
2022-01-07 16:55:03 -05:00
|
|
|
for (int i = 2; i < s->length; i++) {
|
2020-08-02 16:16:26 -04:00
|
|
|
const char c = s->text[i];
|
2022-01-07 16:55:03 -05:00
|
|
|
if (!isHex(c)) {
|
2020-08-02 16:16:26 -04:00
|
|
|
return errorObject(BAD_NUMBER);
|
|
|
|
}
|
|
|
|
num *= 16;
|
2022-01-07 16:55:03 -05:00
|
|
|
if (isDigit(c)) {
|
2020-08-02 16:16:26 -04:00
|
|
|
num += c - '0';
|
|
|
|
} else /* is hex */ {
|
|
|
|
num += c - 'a' + 10;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return numberObject(num);
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object parseBin(struct Slice* s)
|
2020-08-02 16:16:26 -04:00
|
|
|
{
|
|
|
|
int num = 0;
|
2022-01-07 16:55:03 -05:00
|
|
|
for (int i = 2; i < s->length; i++) {
|
2020-08-02 16:16:26 -04:00
|
|
|
const char c = s->text[i];
|
2022-01-07 16:55:03 -05:00
|
|
|
if (c != '0' && c != '1') {
|
2020-08-02 16:16:26 -04:00
|
|
|
return errorObject(BAD_NUMBER);
|
|
|
|
}
|
|
|
|
num *= 2;
|
|
|
|
num += c - '0';
|
|
|
|
}
|
|
|
|
return numberObject(num);
|
|
|
|
}
|
|
|
|
|
2022-03-15 15:26:01 -04:00
|
|
|
Result parseAtom(struct Slice* s)
|
2020-05-16 14:31:14 -04:00
|
|
|
{
|
2020-08-02 16:16:26 -04:00
|
|
|
const char c = s->text[0];
|
2022-01-07 16:55:03 -05:00
|
|
|
if (isDigit(c)) {
|
|
|
|
if (c != '0' || s->length == 1) {
|
2022-03-15 15:26:01 -04:00
|
|
|
return (Result) {parseDecimal(s), s};
|
2022-01-07 16:55:03 -05:00
|
|
|
#ifndef LOW_MEM
|
|
|
|
} else if (c == '0' && s->text[1] == 'x') {
|
2022-03-15 15:26:01 -04:00
|
|
|
return (Result) {parseHex(s), s};
|
2022-01-07 16:55:03 -05:00
|
|
|
} else if (c == '0' && s->text[1] == 'b') {
|
2022-03-15 15:26:01 -04:00
|
|
|
return (Result) { parseBin(s), s };
|
2022-01-07 16:55:03 -05:00
|
|
|
#endif
|
2020-08-02 16:16:26 -04:00
|
|
|
} else {
|
2022-03-15 15:26:01 -04:00
|
|
|
return (Result) { errorObject(UNSUPPORTED_NUMBER_TYPE), s};
|
2020-05-16 14:31:14 -04:00
|
|
|
}
|
2020-08-02 16:16:26 -04:00
|
|
|
} else if (s->length == 1 && (c == 'T' || c == 't')) {
|
2022-03-15 15:26:01 -04:00
|
|
|
return (Result) { boolObject(1), s};
|
2020-08-02 16:16:26 -04:00
|
|
|
} else if (s->length == 1 && (c == 'F' || c == 'f')) {
|
2022-03-15 15:26:01 -04:00
|
|
|
return (Result) { boolObject(0), s};
|
|
|
|
} else if (c == '"'/* || c == '\''*/) {
|
|
|
|
return (Result) { objFromSlice(s->text, s->length), s};
|
2020-05-15 00:43:08 -04:00
|
|
|
} else {
|
2022-03-15 15:26:01 -04:00
|
|
|
if (s->text[s->length] == '\'' && s->text[s->length + 1] == 's') {
|
|
|
|
Object possessiveFunc = newObject(TYPE_FUNC);
|
|
|
|
possessiveFunc.func = &possessive;
|
|
|
|
Object list = startList(possessiveFunc);
|
|
|
|
Object possesser = symFromSlice(s->text, s->length);
|
|
|
|
nf_addToList(&list, possesser);
|
|
|
|
struct Slice* next = s + 3;
|
|
|
|
Object possessed = objFromSlice(&next->text[-1], next->length + 1);
|
|
|
|
nf_addToList(&list, possessed);
|
|
|
|
return (Result) { list, next };
|
|
|
|
}
|
|
|
|
return (Result) { symFromSlice(s->text, s->length), s};
|
2020-05-15 00:43:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object parseEval(const char* input, struct Environment* env)
|
2016-04-18 04:25:36 -04:00
|
|
|
{
|
2021-12-13 10:47:35 -05:00
|
|
|
struct Error err = noError();
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
struct Slice* tokens = nf_tokenize(input, &err);
|
|
|
|
if (err.context != NULL) {
|
2021-12-13 10:47:35 -05:00
|
|
|
Object o = errorWithContext(err.code, err.context);
|
|
|
|
free(err.context);
|
2021-07-21 11:26:04 -04:00
|
|
|
return o;
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2022-01-07 16:55:03 -05:00
|
|
|
if (!tokens->text) {
|
2020-05-22 01:16:45 -04:00
|
|
|
return symFromSlice(" ", 1);
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2020-05-11 01:15:44 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
#ifdef DEBUG
|
2020-05-04 18:14:41 -04:00
|
|
|
struct Slice *debug = tokens;
|
2020-05-22 01:16:45 -04:00
|
|
|
printd("start slice\n");
|
2022-01-07 16:55:03 -05:00
|
|
|
if (debug) {
|
|
|
|
while (debug->text) {
|
2020-08-02 16:16:26 -04:00
|
|
|
char tok[100];
|
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++;
|
|
|
|
}
|
|
|
|
}
|
2022-01-07 16:55:03 -05: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);
|
2022-01-07 16:55:03 -05:00
|
|
|
struct Slice* tok = tokens;
|
|
|
|
while (tok[i].text != NULL) {
|
|
|
|
if (tok[i].text[0] == '(') {
|
2020-08-02 16:16:26 -04:00
|
|
|
parens++;
|
2022-01-07 16:55:03 -05:00
|
|
|
} else if (tok[i].text[0] == ')') {
|
2020-08-02 16:16:26 -04:00
|
|
|
parens--;
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
if (parens == 0) {
|
2020-08-02 16:16:26 -04:00
|
|
|
cleanObject(&obj);
|
|
|
|
Object parsed = parse(tok).obj;
|
2022-01-07 16:55:03 -05:00
|
|
|
if (parsed.type == TYPE_ERROR) {
|
|
|
|
obj = parsed; // TODO Check necessity
|
2021-07-05 05:08:37 -04:00
|
|
|
break;
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2022-01-07 16:55:03 -05:00
|
|
|
if (tok[i].text[0] == ')') {
|
2021-07-21 11:26:04 -04:00
|
|
|
// Skip `tok` past end of list that just closed
|
2020-08-02 16:16:26 -04:00
|
|
|
tok = &tok[i + 1];
|
|
|
|
i = -1;
|
2020-05-10 13:51:33 -04:00
|
|
|
}
|
2021-12-15 14:30:16 -05:00
|
|
|
if (parsed.type == TYPE_SLIST) {
|
|
|
|
obj = parsed;
|
|
|
|
} else {
|
|
|
|
obj = eval(&parsed, env);
|
|
|
|
cleanObject(&parsed);
|
|
|
|
}
|
2020-05-10 13:51:33 -04:00
|
|
|
}
|
2020-08-02 16:16:26 -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
|
2022-01-07 16:55:03 -05:00
|
|
|
|
|
|
|
int _readFile(FILE* input, struct Environment* env)
|
|
|
|
{
|
2020-10-29 11:18:06 -04:00
|
|
|
Object r = numberObject(0);
|
2020-10-30 09:52:46 -04:00
|
|
|
char page[4096] = "";
|
2022-01-07 16:55:03 -05:00
|
|
|
const int LINE_MAX = 256;
|
2020-11-02 12:40:42 -05:00
|
|
|
char line[LINE_MAX];
|
2022-01-07 16:55:03 -05:00
|
|
|
if (fgets(line, LINE_MAX, input)) {
|
|
|
|
if (line[0] != '#' || line[1] != '!') {
|
2020-10-30 09:52:46 -04:00
|
|
|
strncat(page, line, strlen(line) - 1);
|
2020-10-29 11:18:06 -04:00
|
|
|
}
|
|
|
|
}
|
2022-03-15 21:43:53 -04:00
|
|
|
int isQuote = 0;
|
2022-01-07 16:55:03 -05:00
|
|
|
while (fgets(line, LINE_MAX, input)) {
|
2020-11-02 07:57:13 -05:00
|
|
|
int i;
|
2022-01-07 16:55:03 -05:00
|
|
|
for (i = 0; i < LINE_MAX; i++) {
|
|
|
|
if (line[i] != ' ') {
|
|
|
|
if (line[i] == ';') {
|
2020-11-02 07:57:13 -05:00
|
|
|
break;
|
|
|
|
} else {
|
2020-11-02 12:40:42 -05:00
|
|
|
int j = 0;
|
2022-01-07 16:55:03 -05:00
|
|
|
for (j = i; j < LINE_MAX; j++) {
|
2022-03-15 21:43:53 -04:00
|
|
|
if (line[j] == '"') {
|
|
|
|
isQuote = !isQuote;
|
|
|
|
} else if (line[j] == '\0' || (!isQuote && line[j] == ';')) {
|
2020-11-02 12:40:42 -05:00
|
|
|
break;
|
2021-07-04 23:48:26 -04:00
|
|
|
}
|
2020-11-02 12:40:42 -05:00
|
|
|
}
|
|
|
|
strncat(page, line, j);
|
|
|
|
strcat(page, " ");
|
2020-11-02 07:57:13 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-10-29 11:18:06 -04:00
|
|
|
}
|
|
|
|
}
|
2020-10-30 09:52:46 -04:00
|
|
|
|
|
|
|
r = parseEval(page, env);
|
2020-11-02 15:23:04 -05:00
|
|
|
cleanObject(&r);
|
2020-10-30 09:52:46 -04:00
|
|
|
|
2020-10-29 11:18:06 -04:00
|
|
|
fclose(input);
|
|
|
|
return 0;
|
|
|
|
}
|
2022-01-07 16:55:03 -05:00
|
|
|
|
|
|
|
int readFile(const char* filename, struct Environment* env)
|
|
|
|
{
|
|
|
|
FILE* input = fopen(filename, "r");
|
|
|
|
if (!input) {
|
2021-07-10 22:27:24 -04:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
_readFile(input, env);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-10-29 11:18:06 -04:00
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object loadFile(Object filename, Object _, struct Environment* env)
|
|
|
|
{
|
2021-07-04 21:49:41 -04:00
|
|
|
if (isStringy(filename)) {
|
|
|
|
readFile(filename.string, env);
|
|
|
|
return numberObject(0);
|
|
|
|
}
|
|
|
|
return numberObject(1);
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
Object systemCall(Object process, Object _, struct Environment* env)
|
|
|
|
{
|
2021-12-10 16:33:59 -05:00
|
|
|
if (isStringy(process)) {
|
|
|
|
return numberObject(system(process.string));
|
|
|
|
}
|
|
|
|
return numberObject(255);
|
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
void repl(struct Environment* env)
|
|
|
|
{
|
2022-03-15 16:49:12 -04:00
|
|
|
char* buf;
|
|
|
|
while ((buf = readline("pebblisp::> ")) != NULL) {
|
|
|
|
if (strcmp("q", buf) == 0) {
|
|
|
|
free(buf);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Object o = parseEval(buf, env);
|
|
|
|
printObj(&o);
|
|
|
|
free(buf);
|
2021-07-04 21:49:41 -04:00
|
|
|
}
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
void loadArgsIntoEnv(int argc, const char* argv[], struct Environment* env)
|
|
|
|
{
|
2021-07-10 22:27:24 -04:00
|
|
|
Object args = listObject();
|
2022-01-07 16:55:03 -05:00
|
|
|
for (int i = 0; i < argc; i++) {
|
2021-07-10 22:27:24 -04:00
|
|
|
nf_addToList(&args, stringFromSlice(argv[i], strlen(argv[i])));
|
|
|
|
}
|
|
|
|
addToEnv(env, "args", args);
|
|
|
|
}
|
|
|
|
|
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();
|
2022-03-15 21:43:53 -04:00
|
|
|
setGlobal(&env);
|
2020-11-07 00:05:52 -05:00
|
|
|
readFile(SCRIPTDIR "/lib.pbl", &env);
|
2022-01-07 16:55:03 -05:00
|
|
|
if (argc >= 2) {
|
|
|
|
FILE* file = fopen(argv[1], "r");
|
|
|
|
if (file) {
|
2021-07-10 22:27:24 -04:00
|
|
|
// Executing a file
|
|
|
|
loadArgsIntoEnv(argc, argv, &env);
|
|
|
|
_readFile(file, &env);
|
|
|
|
} else {
|
|
|
|
// Running arguments directly as pl code
|
2020-10-29 11:18:06 -04:00
|
|
|
Object r = numberObject(0);
|
2022-01-07 16:55:03 -05:00
|
|
|
for (int i = 1; i < argc; i++) {
|
2020-10-29 11:18:06 -04:00
|
|
|
r = parseEval(argv[i], &env);
|
|
|
|
printAndClean(&r);
|
|
|
|
}
|
2020-05-10 14:15:53 -04:00
|
|
|
}
|
2020-05-08 12:08:14 -04:00
|
|
|
} else {
|
2021-07-10 22:27:24 -04:00
|
|
|
// Running a repl
|
|
|
|
loadArgsIntoEnv(argc, argv, &env);
|
2020-05-08 12:08:14 -04:00
|
|
|
repl(&env);
|
|
|
|
}
|
2020-05-15 23:22:08 -04:00
|
|
|
deleteEnv(&env);
|
2016-04-18 04:25:36 -04:00
|
|
|
}
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-03-14 16:51:45 -04:00
|
|
|
#endif
|