test functions
This commit is contained in:
+82
-72
@@ -1,92 +1,102 @@
|
||||
#include "../include/data.h"
|
||||
#include "../include/define.h"
|
||||
#include <ctype.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#define GLOBAL_ENV_
|
||||
|
||||
void advance_char(lexer_t *lexer) {
|
||||
if (lexer->current_char == '\n') {
|
||||
lexer->line++;
|
||||
lexer->column = 1;
|
||||
} else {
|
||||
lexer->column++;
|
||||
}
|
||||
lexer->pos++;
|
||||
lexer->current_char = lexer->input[lexer->pos];
|
||||
}
|
||||
#include "../include/data.h"
|
||||
|
||||
void skip_whitespace(lexer_t *lexer) {
|
||||
while (isspace(lexer->current_char) && lexer->current_char != '\n') {
|
||||
advance_char(lexer);
|
||||
}
|
||||
}
|
||||
|
||||
void skip_comment(lexer_t *lexer) {
|
||||
if (lexer->current_char == '/' && lexer->input[lexer->pos + 1] == '/') {
|
||||
while (lexer->current_char != '\n' && lexer->current_char != '\0') {
|
||||
advance_char(lexer);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
node_t *create_list_node(void) {
|
||||
node_t *node = malloc(sizeof(node_t));
|
||||
if (!node) {
|
||||
// Memory management
|
||||
Value *make_value(ValueType type) {
|
||||
/*
|
||||
** Create a Value struct of type -type-
|
||||
*/
|
||||
Value *v = (Value *)malloc(sizeof(Value));
|
||||
if (!v) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
node->type = NODE_LIST;
|
||||
node->data.list.child_count = 0;
|
||||
node->data.list.children = NULL;
|
||||
|
||||
return node;
|
||||
v->type = type;
|
||||
return v;
|
||||
}
|
||||
|
||||
void add_arg_to_call(node_t *call, node_t *arg) {
|
||||
if (call->type == NODE_FUNCTION_CALL &&
|
||||
call->data.call.arg_count < MAX_ARGS) {
|
||||
call->data.call.args =
|
||||
(node_t **)realloc(call->data.call.args,
|
||||
(call->data.call.arg_count + 1) * sizeof(node_t *));
|
||||
call->data.call.args[call->data.call.arg_count++] = arg;
|
||||
Value *make_nil(void) { return make_value(VAL_NIL); }
|
||||
|
||||
Value *make_number(double n) {
|
||||
Value *v = make_value(VAL_NUMBER);
|
||||
if (!v) {
|
||||
fprintf(stderr, "ERROR : not assigned\n");
|
||||
}
|
||||
v->data.number = n;
|
||||
fprintf(stderr, "DEBUG : value %lf\n", n);
|
||||
return v;
|
||||
}
|
||||
|
||||
void add_to_list(node_t *list, node_t *element) {
|
||||
node_t **new_children = NULL;
|
||||
if (!list || !element) {
|
||||
return;
|
||||
Value *make_string(const char *s) {
|
||||
Value *v = make_value(VAL_STRING);
|
||||
v->data.string = strdup(s);
|
||||
return v;
|
||||
}
|
||||
|
||||
Value *make_symbol(const char *s) {
|
||||
Value *v = make_value(VAL_SYMBOL);
|
||||
v->data.symbol = strdup(s);
|
||||
return v;
|
||||
}
|
||||
|
||||
Value *make_list(Value *car, Value *cdr) {
|
||||
fprintf(stderr, "DEBUG : list done\n");
|
||||
Value *v = make_value(VAL_LIST);
|
||||
if (!v) {
|
||||
fprintf(stderr, "ERROR : value\n");
|
||||
}
|
||||
v->data.list.cdr = car;
|
||||
v->data.list.cdr = cdr;
|
||||
return v;
|
||||
}
|
||||
|
||||
// Ensure this is actually a list node
|
||||
if (list->type != NODE_LIST) {
|
||||
return;
|
||||
}
|
||||
Value *make_builtin(BuiltinFunc func) {
|
||||
Value *v = make_value(VAL_BUILTIN);
|
||||
v->data.builtin = func;
|
||||
return v;
|
||||
}
|
||||
|
||||
// If this is the first element
|
||||
if (list->data.list.children == NULL) {
|
||||
// Environment functions
|
||||
Env *make_env(Env *parent) {
|
||||
Env *env = malloc(sizeof(Env));
|
||||
env->names = malloc(sizeof(char *) * 16);
|
||||
env->values = malloc(sizeof(Value *) * 16);
|
||||
env->count = 0;
|
||||
env->capacity = 16;
|
||||
env->parent = parent;
|
||||
return env;
|
||||
}
|
||||
|
||||
fprintf(stderr, "DEBUG : child number 0 added\n");
|
||||
list->data.list.children = malloc(sizeof(node_t *));
|
||||
if (!list->data.list.children) {
|
||||
return; // Memory allocation failed
|
||||
void env_set(Env *env, const char *name, Value *value) {
|
||||
// Check if variable already exists
|
||||
for (int i = 0; i < env->count; i++) {
|
||||
if (strcmp(env->names[i], name) == 0) {
|
||||
env->values[i] = value;
|
||||
return;
|
||||
}
|
||||
list->data.list.children[0] = element;
|
||||
list->data.list.child_count = 1;
|
||||
return;
|
||||
}
|
||||
|
||||
// Reallocate to add new element
|
||||
new_children = realloc(list->data.list.children,
|
||||
sizeof(node_t *) * (list->data.list.child_count + 1));
|
||||
if (!new_children) {
|
||||
return; // Memory allocation failed
|
||||
// Add new variable
|
||||
if (env->count >= env->capacity) {
|
||||
env->capacity *= 2;
|
||||
env->names = realloc(env->names, sizeof(char *) * env->capacity);
|
||||
env->values = realloc(env->values, sizeof(Value *) * env->capacity);
|
||||
}
|
||||
|
||||
list->data.list.children = new_children;
|
||||
list->data.list.children[list->data.list.child_count] = element;
|
||||
list->data.list.child_count++;
|
||||
fprintf(stderr, "DEBUG : child number %d added\n",
|
||||
list->data.list.child_count);
|
||||
env->names[env->count] = strdup(name);
|
||||
env->values[env->count] = value;
|
||||
env->count++;
|
||||
}
|
||||
|
||||
Value *env_get(Env *env, const char *name) {
|
||||
while (env) {
|
||||
for (int i = 0; i < env->count; i++) {
|
||||
if (strcmp(env->names[i], name) == 0) {
|
||||
return env->values[i];
|
||||
}
|
||||
}
|
||||
env = env->parent;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user