1
1
mirror of https://github.com/kanaka/mal.git synced 2024-10-27 14:52:16 +03:00
mal/impls/c.2/step2_eval.c
Nicolas Boulenguez 033892777a Merge eval-ast and macro expansion into EVAL, add DEBUG-EVAL
See issue #587.
* Merge eval-ast and eval into a single conditional.
* Expand macros during the apply phase, removing lots of duplicate
  tests, and increasing the overall consistency by allowing the macro
  to be computed instead of referenced by name (`((defmacro! cond
  (...)))` is currently illegal for example).
* Print "EVAL: $ast" at the top of EVAL if DEBUG-EVAL exists in the
  MAL environment.
* Remove macroexpand and quasiquoteexpand special forms.
* Use pattern-matching style in process/step*.txt.

Unresolved issues:
c.2: unable to reproduce with gcc 11.12.0.
elm: the directory is unchanged.
groovy: sometimes fail, but not on each rebuild.
nasm: fails some new soft tests, but the issue is unreproducible when
  running the interpreter manually.
objpascal: unreproducible with fpc 3.2.2.
ocaml: unreproducible with 4.11.1.
perl6: unreproducible with rakudo 2021.09.

Unrelated changes:
Reduce diff betweens steps.
Prevent defmacro! from mutating functions: c forth logo miniMAL vb.
dart: fix recent errors and warnings
ocaml: remove metadata from symbols.

Improve the logo implementation.
Encapsulate all representation in types.lg and env.lg, unwrap numbers.
Replace some manual iterations with logo control structures.
Reduce the diff between steps.
Use native iteration in env_get and env_map
Rewrite the reader with less temporary strings.
Reduce the number of temporary lists (for example, reverse iteration
with butlast requires O(n^2) allocations).
It seems possible to remove a few exceptions: GC settings
(Dockerfile), NO_SELF_HOSTING (IMPLS.yml) and step5_EXCLUDES
(Makefile.impls) .
2024-08-05 11:40:49 -05:00

280 lines
5.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <gc.h>
#include <editline/readline.h>
#include <editline/history.h>
#include "types.h"
#include "reader.h"
#include "printer.h"
#include "env.h"
#define PROMPT_STRING "user> "
MalType* READ(char* str) {
return read_str(str);
}
MalType* EVAL(MalType* ast, Env* env) {
/* forward references */
list evaluate_list(list lst, Env* env);
list evaluate_vector(list lst, Env* env);
list evaluate_hashmap(list lst, Env* env);
/* printf("EVAL: %s\n", pr_str(ast, READABLY)); */
/* NULL */
if (!ast) { return make_nil(); }
if (is_symbol(ast)) {
MalType* symbol_value = hashmap_get(env->data, ast->value.mal_symbol);
if (symbol_value)
return symbol_value;
else
return make_error_fmt("'%s' not found", ast->value.mal_symbol);
}
if (is_vector(ast)) {
list result = evaluate_vector(ast->value.mal_list, env);
if (result && is_error(result->data))
return result->data;
else
return make_vector(result);
}
if (is_hashmap(ast)) {
list result = evaluate_hashmap(ast->value.mal_list, env);
if (result && is_error(result->data))
return result->data;
else
return make_hashmap(result);
}
/* not a list */
if (!is_list(ast)) { return ast; }
/* empty list */
if (ast->value.mal_list == NULL) { return ast; }
/* list */
/* evaluate the list */
list evlst = evaluate_list(ast->value.mal_list, env);
if (is_error(evlst->data)) return evlst->data;
/* apply the first element of the list to the arguments */
MalType* func = evlst->data;
if (is_function(func)) {
return (*func->value.mal_function)(evlst->next);
}
else {
return make_error_fmt("Error: first item in list is not callable: %s.", \
pr_str(func, UNREADABLY));
}
}
void PRINT(MalType* val) {
char* output = pr_str(val, READABLY);
printf("%s\n", output);
}
void rep(char* str, Env* env) {
PRINT(EVAL(READ(str), env));
}
int main(int argc, char** argv) {
MalType* mal_add(list args);
MalType* mal_sub(list args);
MalType* mal_mul(list args);
MalType* mal_div(list args);
/* Greeting message */
puts("Make-a-lisp version 0.0.2\n");
puts("Press Ctrl+d to exit\n");
MalType* func_add = make_function(&mal_add);
MalType* func_sub = make_function(&mal_sub);
MalType* func_mul = make_function(&mal_mul);
MalType* func_div = make_function(&mal_div);
hashmap g = hashmap_make("+", func_add);
g = hashmap_put(g, "-", func_sub);
g = hashmap_put(g, "*", func_mul);
g = hashmap_put(g, "/", func_div);
Env* repl_env = GC_MALLOC(sizeof(*repl_env));
repl_env->data = g;
while (1) {
/* print prompt and get input*/
/* readline allocates memory for input */
char* input = readline(PROMPT_STRING);
/* Check for EOF (Ctrl-D) */
if (!input) {
printf("\n");
return 0;
}
/* add input to history */
add_history(input);
/* call Read-Eval-Print */
rep(input, repl_env);
/* have to release the memory used by readline */
free(input);
}
return 0;
}
list evaluate_list(list lst, Env* env) {
list evlst = NULL;
while (lst) {
evlst = list_push(evlst, EVAL(lst->data, env));
lst = lst->next;
}
return list_reverse(evlst);
}
list evaluate_vector(list lst, Env* env) {
/* TODO: implement a real vector */
list evlst = NULL;
while (lst) {
evlst = list_push(evlst, EVAL(lst->data, env));
lst = lst->next;
}
return list_reverse(evlst);
}
list evaluate_hashmap(list lst, Env* env) {
/* TODO: implement a real hashmap */
list evlst = NULL;
while (lst) {
/* keys are unevaluated */
evlst = list_push(evlst, lst->data);
lst = lst->next;
/* values are evaluated */
evlst = list_push(evlst, EVAL(lst->data, env));
lst = lst->next;
}
return list_reverse(evlst);
}
MalType* mal_add(list args) {
MalType* result = GC_MALLOC(sizeof(*result));
result->type = MALTYPE_INTEGER;
list arg_list = args;
long sum = 0;
while(arg_list) {
MalType* val = arg_list->data;
/* TODO: check argument type */
sum = sum + val->value.mal_integer;
arg_list = arg_list->next;
}
result->value.mal_integer = sum;
return result;
}
MalType* mal_sub(list args) {
long sum;
MalType* result = GC_MALLOC(sizeof(*result));
result->type = MALTYPE_INTEGER;
list arg_list = args;
if (arg_list) {
MalType* first_val = arg_list->data;
arg_list = arg_list->next;
/* TODO: check argument type */
sum = first_val->value.mal_integer;
while(arg_list) {
MalType* val = arg_list->data;
/* TODO: check argument type */
sum = sum - val->value.mal_integer;
arg_list = arg_list->next;
}
}
else {
sum = 0;
}
result->value.mal_integer = sum;
return result;
}
MalType* mal_mul(list args) {
MalType* result = GC_MALLOC(sizeof(*result));
result->type = MALTYPE_INTEGER;
list arg_list = args;
long product = 1;
while(arg_list) {
MalType* val = arg_list->data;
/* TODO: check argument type */
product *= val->value.mal_integer;
arg_list = arg_list->next;
}
result->value.mal_integer = product;
return result;
}
MalType* mal_div(list args) {
long product;
MalType* result = GC_MALLOC(sizeof(*result));
result->type = MALTYPE_INTEGER;
list arg_list = args;
if (arg_list) {
MalType* first_val = arg_list->data;
/* TODO: check argument type */
product = first_val->value.mal_integer;
arg_list = arg_list->next;
while(arg_list) {
MalType* val = arg_list->data;
/* TODO: check argument type */
product /= (val->value.mal_integer);
arg_list = arg_list->next;
}
} else {
product = 1;
}
result->value.mal_integer = product;
return result;
}