2016-04-18 04:25:36 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2022-01-07 16:55:03 -05:00
|
|
|
#include "tokens.h"
|
2022-04-05 20:05:58 -04:00
|
|
|
#include "pebblisp.h"
|
2022-03-19 22:25:20 -04:00
|
|
|
|
2022-03-14 23:46:20 -04:00
|
|
|
#ifdef STANDALONE
|
2022-03-19 22:25:20 -04:00
|
|
|
|
2022-03-14 23:46:20 -04:00
|
|
|
#include "web.h"
|
2022-03-19 22:25:20 -04:00
|
|
|
|
2022-03-14 23:46:20 -04:00
|
|
|
#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-04-01 14:42:38 -04:00
|
|
|
Object def(Object* params, unused int length, unused struct Environment* env)
|
2022-01-07 16:55:03 -05:00
|
|
|
{
|
2022-03-30 16:32:31 -04:00
|
|
|
const char* name = params[0].string;
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2022-03-30 16:32:31 -04:00
|
|
|
Object finalValue = eval(¶ms[1], 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
|
|
|
|
2022-03-30 16:32:31 -04:00
|
|
|
return cloneObject(params[0]);
|
2020-05-04 18:14:41 -04:00
|
|
|
}
|
2020-05-04 10:03:35 -04:00
|
|
|
|
2022-03-14 16:51:45 -04:00
|
|
|
/**
|
|
|
|
* Add a struct to the environment with a given name and fields.
|
|
|
|
*
|
2022-03-31 10:29:06 -04:00
|
|
|
* Not a typical pl function because I don't feel like adding more syntactic sugar right now.
|
|
|
|
*
|
2022-03-14 16:51:45 -04:00
|
|
|
* (struct point (x y))
|
|
|
|
*/
|
2022-04-01 14:42:38 -04:00
|
|
|
Object evalStructArgs(const Object* symbol, const Object* fields, unused struct Environment* env)
|
2022-03-14 16:51:45 -04:00
|
|
|
{
|
|
|
|
const char* name = symbol->string;
|
|
|
|
|
|
|
|
if (!isListy(*fields)) {
|
|
|
|
return errorObject(NOT_A_LIST);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct StructDef def;
|
|
|
|
def.name = malloc(sizeof(char) * (strlen(name) + 1));
|
|
|
|
strcpy(def.name, name);
|
2022-03-16 23:31:14 -04:00
|
|
|
|
2022-03-14 16:51:45 -04:00
|
|
|
def.fieldCount = listLength(fields);
|
|
|
|
def.names = malloc(sizeof(char*) * def.fieldCount);
|
|
|
|
|
2022-03-31 10:29:06 -04:00
|
|
|
int i = 0;
|
|
|
|
FOR_POINTER_IN_LIST(fields) {
|
|
|
|
def.names[i] = malloc(sizeof(char) * (strlen(POINTER->string) + 1));
|
|
|
|
strcpy(def.names[i], POINTER->string);
|
|
|
|
i++;
|
2022-03-14 16:51:45 -04:00
|
|
|
}
|
|
|
|
|
2022-03-27 18:52:56 -04:00
|
|
|
addStructDef(def);
|
2022-03-14 16:51:45 -04:00
|
|
|
|
2022-03-31 10:29:06 -04:00
|
|
|
return trueObject();
|
2022-03-14 16:51:45 -04:00
|
|
|
}
|
|
|
|
|
2022-03-31 10:29:06 -04:00
|
|
|
/**
|
|
|
|
* Not a typical pl function because delayed evaluation is annoying in those right now.
|
|
|
|
*/
|
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-03-31 10:29:06 -04:00
|
|
|
/**
|
|
|
|
* Not a typical pl function because it relies almost exclusively on symbols
|
|
|
|
*/
|
2022-03-16 16:36:04 -04:00
|
|
|
Object evalLambdaArgs(const Object* argForms, struct Environment* env)
|
2020-05-05 13:42:28 -04:00
|
|
|
{
|
2022-03-16 16:36:04 -04:00
|
|
|
return constructLambda(argForms, argForms ? argForms->forward : NULL, env);
|
2020-05-05 13:42:28 -04:00
|
|
|
}
|
|
|
|
|
2022-03-31 10:29:06 -04:00
|
|
|
Object mapO(Object* params, int length, struct Environment* env)
|
2020-05-06 11:58:09 -04:00
|
|
|
{
|
2022-03-31 10:29:06 -04:00
|
|
|
if (length < 2) {
|
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
|
|
|
|
2022-03-31 10:29:06 -04:00
|
|
|
Object lambda = eval(¶ms[0], env);
|
|
|
|
const Object* inputList = ¶ms[1];
|
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-03-28 23:51:05 -04:00
|
|
|
Object outputList = listObject();
|
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
|
2022-03-24 21:21:51 -04:00
|
|
|
Object tempInput = cloneObject(*POINTER);
|
2020-05-16 10:46:19 -04:00
|
|
|
|
2022-03-31 10:29:06 -04:00
|
|
|
Object* lambdaParams = &lambda.lambda->params;
|
|
|
|
struct Environment newEnv = envForLambda(lambdaParams, &tempInput, listLength(lambdaParams), 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);
|
2022-03-24 21:21:51 -04:00
|
|
|
nf_addToList(&outputList, lambda_output);
|
2022-01-07 16:55:03 -05:00
|
|
|
deleteEnv(&newEnv);
|
2022-03-24 21:21:51 -04:00
|
|
|
cleanObject(&tempInput);
|
2022-01-07 16:55:03 -05:00
|
|
|
}
|
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,
|
2022-03-26 19:51:52 -04:00
|
|
|
struct Environment* env, int* found)
|
2020-05-04 10:03:35 -04:00
|
|
|
{
|
2022-03-26 19:51:52 -04:00
|
|
|
*found = 1;
|
2022-03-30 16:32:31 -04:00
|
|
|
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) {
|
2022-03-16 16:36:04 -04:00
|
|
|
return evalLambdaArgs(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
|
|
|
}
|
|
|
|
|
2022-03-26 19:51:52 -04:00
|
|
|
*found = 0;
|
|
|
|
return *first;
|
2020-05-05 13:42:28 -04:00
|
|
|
}
|
|
|
|
|
2020-11-03 14:35:47 -05:00
|
|
|
/**
|
2022-03-31 15:44:18 -04:00
|
|
|
* Evaluates a paramList whose first element is a function, applying that function
|
2020-11-03 14:35:47 -05:00
|
|
|
*
|
|
|
|
* Tries to either apply the function to its parameters, or create a partial
|
|
|
|
* function, if not enough parameters were passed.
|
|
|
|
*
|
2022-03-31 15:44:18 -04:00
|
|
|
* @param function First element of the paramList, already evaluated to be a function
|
|
|
|
* @param paramList The parameters to the function
|
|
|
|
* @param length Length of `paramList` - 1, to exclude the already-evaluated element
|
2020-11-03 14:35:47 -05:00
|
|
|
* @param env The environment to evaluate in
|
|
|
|
*/
|
2022-03-31 15:44:18 -04:00
|
|
|
Object listEvalFunc(const Object* function, const Object* paramList,
|
2022-01-07 16:55:03 -05:00
|
|
|
const int length, struct Environment* env)
|
2020-11-03 14:35:47 -05:00
|
|
|
{
|
|
|
|
Object rest[length];
|
2022-03-27 00:03:34 -04:00
|
|
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
2022-03-31 15:44:18 -04:00
|
|
|
rest[i] = eval(paramList, env);
|
|
|
|
paramList = paramList->forward;
|
2022-03-27 00:03:34 -04:00
|
|
|
}
|
2022-03-24 16:56:52 -04:00
|
|
|
|
|
|
|
Object result = function->func(rest, length, env);
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
cleanObject(&rest[i]);
|
2020-11-03 14:35:47 -05:00
|
|
|
}
|
2022-03-24 16:56:52 -04:00
|
|
|
return result;
|
2020-11-03 14:35:47 -05:00
|
|
|
}
|
|
|
|
|
2022-03-27 00:03:34 -04:00
|
|
|
Object simpleFuncEval(const Object func, Object arg1, Object arg2, struct Environment* env)
|
|
|
|
{
|
2022-03-31 15:44:18 -04:00
|
|
|
arg2 = cloneObject(arg2);
|
|
|
|
arg1.forward = &arg2;
|
|
|
|
Object first_eval = eval(&func, env);
|
|
|
|
Object ret = listEvalFunc(&first_eval, &arg1, 2, env);
|
|
|
|
cleanObject(&first_eval);
|
|
|
|
cleanObject(&arg2);
|
|
|
|
return ret;
|
2022-03-27 00:03:34 -04:00
|
|
|
}
|
|
|
|
|
2020-11-03 14:35:47 -05:00
|
|
|
/**
|
|
|
|
* 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-03-28 23:51:05 -04:00
|
|
|
Object listEvalLambda(Object* lambda, const Object* remaining, int evalLength,
|
2022-01-07 16:55:03 -05:00
|
|
|
struct Environment* env)
|
|
|
|
{
|
|
|
|
struct Environment newEnv =
|
2022-03-28 23:51:05 -04:00
|
|
|
envForLambda(&lambda->lambda->params, remaining, evalLength - 1, env);
|
2020-11-03 14:35:47 -05:00
|
|
|
Object ret = eval(&lambda->lambda->body, &newEnv);
|
|
|
|
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;
|
2022-03-26 19:51:52 -04:00
|
|
|
if (first_form->type == TYPE_SYMBOL) {
|
|
|
|
int found;
|
|
|
|
Object builtIn = evalBuiltIns(first_form, first_form->forward, env, &found);
|
|
|
|
if (found) {
|
2020-08-02 16:16:26 -04:00
|
|
|
return builtIn;
|
|
|
|
}
|
|
|
|
|
2022-03-27 18:52:56 -04:00
|
|
|
int i = getStructIndex(first_form->string);
|
|
|
|
if (i >= 0) {
|
|
|
|
Object structo = structObject(i);
|
|
|
|
int s = 0;
|
|
|
|
FOR_POINTER_IN_LIST(obj) {
|
|
|
|
if (s != 0) { // Skip the field name
|
|
|
|
structo.structObject->fields[s - 1] = eval(POINTER, env);
|
2022-03-26 19:51:52 -04:00
|
|
|
}
|
2022-03-27 18:52:56 -04:00
|
|
|
s++;
|
2022-03-14 16:51:45 -04:00
|
|
|
}
|
2022-03-27 18:52:56 -04:00
|
|
|
return structo;
|
2022-03-14 16:51:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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:
|
2022-03-31 15:44:18 -04:00
|
|
|
// Uses evalLength - 1 because we skip the first form (the function itself)
|
|
|
|
return listEvalFunc(&first_eval, obj->list->forward, evalLength - 1, env);
|
2020-08-02 16:16:26 -04:00
|
|
|
|
2020-11-03 14:35:47 -05:00
|
|
|
case TYPE_LAMBDA:
|
2022-03-28 23:51:05 -04:00
|
|
|
return listEvalLambda(&first_eval, first_form->forward, evalLength, env);
|
2020-08-02 16:16:26 -04:00
|
|
|
|
2022-03-26 19:51:52 -04: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;
|
2022-03-28 23:51:05 -04:00
|
|
|
Object* t = 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) {
|
2022-03-28 23:51:05 -04:00
|
|
|
allocObject(&t->forward, eval(POINTER, env));
|
|
|
|
t = t->forward;
|
2020-11-04 15:19:08 -05:00
|
|
|
}
|
|
|
|
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) {
|
2022-03-24 21:21:51 -04:00
|
|
|
case TYPE_LAMBDA:
|
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:
|
2022-03-23 01:00:11 -04:00
|
|
|
case TYPE_SLIST:
|
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
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
case TYPE_LIST:
|
2020-08-02 16:16:26 -04:00
|
|
|
return evalList(obj, 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-04-01 14:42:38 -04:00
|
|
|
Object structAccess(Object* params, unused int length, unused struct Environment* env)
|
2022-03-15 15:26:01 -04:00
|
|
|
{
|
2022-03-31 12:09:16 -04:00
|
|
|
checkTypes(structAccess)
|
|
|
|
|
2022-03-24 16:56:52 -04:00
|
|
|
Object structo = params[0];
|
|
|
|
Object field = params[1];
|
|
|
|
|
2022-03-27 18:52:56 -04:00
|
|
|
struct StructDef* structDef = getStructAt(structo.structObject->definition);
|
|
|
|
for (int i = 0; i < structDef->fieldCount; i++) {
|
|
|
|
if (strcmp(field.string, structDef->names[i]) == 0) {
|
2022-03-15 15:26:01 -04:00
|
|
|
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]);
|
2022-03-23 01:00:11 -04:00
|
|
|
if (r.obj.type == TYPE_LIST) {
|
|
|
|
r.obj.type = TYPE_SLIST;
|
|
|
|
}
|
2021-12-15 14:30:16 -05:00
|
|
|
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-03-31 10:29:06 -04:00
|
|
|
return (Result) { errorObject(NULL_PARSE), NULL };
|
2020-05-15 00:43:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-25 13:33:26 -04:00
|
|
|
#ifdef SUGAR
|
|
|
|
#define sugar(_desc, _code) ;
|
|
|
|
#else
|
|
|
|
#define sugar(_desc, _code) _code
|
|
|
|
#endif
|
|
|
|
|
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-03-30 16:32:31 -04:00
|
|
|
int forceString = 0;
|
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] == ')') {
|
2022-03-31 10:29:06 -04:00
|
|
|
return (Result) { res, rest };
|
2020-05-15 00:43:08 -04:00
|
|
|
}
|
|
|
|
Result r = parse(tokens);
|
2022-03-30 16:32:31 -04:00
|
|
|
sugar("(? fil) => (? 'fil')" // or,
|
|
|
|
"(def yee 10) => (def 'yee' 10)",
|
|
|
|
if (forceString && r.obj.type == TYPE_SYMBOL) {
|
2022-03-26 12:54:38 -04:00
|
|
|
r.obj.type = TYPE_STRING;
|
|
|
|
}
|
2022-03-25 13:33:26 -04:00
|
|
|
)
|
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);
|
2022-03-30 16:47:43 -04:00
|
|
|
forceString = next->text[0] == '?'
|
|
|
|
|| (strncmp(next->text, "def", 3) == 0);
|
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-31 10:29:06 -04:00
|
|
|
return (Result) { parseDecimal(s), s };
|
2022-01-07 16:55:03 -05:00
|
|
|
#ifndef LOW_MEM
|
2022-03-20 03:50:43 -04:00
|
|
|
} else if (s->text[1] == 'x') {
|
2022-03-31 10:29:06 -04:00
|
|
|
return (Result) { parseHex(s), s };
|
2022-03-20 03:50:43 -04:00
|
|
|
} else if (s->text[1] == 'b') {
|
2022-03-31 10:29:06 -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-31 10:29:06 -04:00
|
|
|
return (Result) { errorWithContext(UNSUPPORTED_NUMBER_TYPE, s->text), s };
|
2020-05-16 14:31:14 -04:00
|
|
|
}
|
2022-03-31 10:29:06 -04:00
|
|
|
} else if (s->length == 1 && c == 'T') {
|
|
|
|
return (Result) { trueObject(), s };
|
|
|
|
} else if (s->length == 1 && c == 'F') {
|
|
|
|
return (Result) { falseObject(), s };
|
2022-03-15 15:26:01 -04:00
|
|
|
} else if (c == '"'/* || c == '\''*/) {
|
2022-03-31 10:29:06 -04:00
|
|
|
return (Result) { objFromSlice(s->text, s->length), s };
|
|
|
|
} else if (s->text[s->length] == '.') {
|
2022-03-31 12:09:16 -04:00
|
|
|
Object structAccessFunc = newObject(TYPE_FUNC);
|
|
|
|
structAccessFunc.func = &structAccess;
|
|
|
|
Object list = startList(structAccessFunc);
|
|
|
|
Object theStruct = symFromSlice(s->text, s->length);
|
|
|
|
nf_addToList(&list, theStruct);
|
2022-03-31 10:29:06 -04:00
|
|
|
struct Slice* next = s + 2;
|
2022-03-31 12:09:16 -04:00
|
|
|
Object structField = objFromSlice(&next->text[-1], next->length + 1);
|
|
|
|
nf_addToList(&list, structField);
|
2022-03-31 10:29:06 -04:00
|
|
|
return (Result) { list, next };
|
2020-05-15 00:43:08 -04:00
|
|
|
}
|
2022-03-31 10:29:06 -04:00
|
|
|
return (Result) { symFromSlice(s->text, s->length), s };
|
2020-05-15 00:43:08 -04:00
|
|
|
}
|
|
|
|
|
2022-03-21 12:33:35 -04:00
|
|
|
struct Slice* lastOpen = NULL;
|
2022-03-23 01:00:11 -04:00
|
|
|
|
2022-04-05 20:05:58 -04:00
|
|
|
struct Slice* getLastOpen()
|
|
|
|
{
|
|
|
|
return lastOpen;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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] == '(') {
|
2022-03-21 12:33:35 -04:00
|
|
|
lastOpen = &tok[i];
|
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) {
|
2022-03-26 19:51:52 -04:00
|
|
|
obj = parsed; // TODO Check necessity
|
2022-04-02 06:51:06 -04:00
|
|
|
#ifdef STANDALONE
|
2022-03-25 13:33:26 -04:00
|
|
|
obj.error->plContext = malloc(sizeof(struct Slice));
|
|
|
|
*obj.error->plContext = *lastOpen;
|
2022-04-02 06:51:06 -04:00
|
|
|
#endif
|
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
|
|
|
}
|
|
|
|
|
2022-03-31 12:09:16 -04:00
|
|
|
Object typeCheck(const char* funcName, Object* params, int length,
|
|
|
|
int (* typeChecks[])(Object), int typeLength, int* failed)
|
|
|
|
{
|
|
|
|
*failed = 1;
|
|
|
|
if ((typeLength - 1) > length) {
|
|
|
|
return errorWithContext(NOT_ENOUGH_ARGUMENTS, funcName);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < typeLength - 1; i++) {
|
|
|
|
if (typeChecks[i] && !typeChecks[i](params[i])) { // TODO: Use pl func name instead of C function name.
|
|
|
|
return /*errorObject(BAD_TYPE); */ errorWithContextLineNo(BAD_PARAMS_ON, funcName, 0, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*failed = 0;
|
|
|
|
return numberObject(0);
|
|
|
|
}
|
|
|
|
|
2016-04-18 04:25:36 -04:00
|
|
|
#ifdef STANDALONE
|
2022-01-07 16:55:03 -05:00
|
|
|
|
2022-04-05 20:05:58 -04:00
|
|
|
/// Returns 1 if the file could not be opened. Otherwise, 0
|
2022-03-20 03:50:43 -04:00
|
|
|
int readFile(const char* filename, struct Environment* env)
|
|
|
|
{
|
|
|
|
FILE* input = fopen(filename, "r");
|
|
|
|
if (!input) {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
_readFile(input, env);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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] != '!') {
|
2022-03-25 13:33:26 -04:00
|
|
|
strcat(page, line);
|
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 {
|
2022-03-26 12:54:38 -04:00
|
|
|
int j;
|
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
|
|
|
|
2022-04-05 20:05:58 -04:00
|
|
|
#endif // STANDALONE
|