From 2a361278cbcde8917dfe3e0ca6c0ef767e9beb53 Mon Sep 17 00:00:00 2001 From: Gabriel Pariat Date: Sun, 20 Feb 2022 11:33:51 -0500 Subject: [PATCH] refactoring --- implementations/c/src/cons.c | 12 +++-- implementations/c/src/evaluator.c | 2 +- implementations/c/src/lisp.c | 43 ++++++++++++------ implementations/c/src/lisp.h | 38 ++++++++-------- implementations/c/src/reader.c | 14 +++--- implementations/c/tests/check_ptlisp.log | Bin 9173 -> 277 bytes implementations/c/tests/cons-test.c | 2 +- implementations/c/tests/lisp-test.c | 54 +++++++++++------------ implementations/c/tests/reader-test.c | 2 +- 9 files changed, 92 insertions(+), 75 deletions(-) diff --git a/implementations/c/src/cons.c b/implementations/c/src/cons.c index f7fa251..db98031 100644 --- a/implementations/c/src/cons.c +++ b/implementations/c/src/cons.c @@ -17,18 +17,22 @@ Pointer reduce_fn(Pointer args, Pointer env) { return previous; } -Pointer cons_fn(Pointer args) { +Pointer cons_fn(Pointer args, Pointer env) { + (void) env; return cons(CAR(args), CAR(CDR(args))); } -Pointer car_fn(Pointer args) { +Pointer car_fn(Pointer args, Pointer env) { + (void) env; return CAR(CAR(args)); } -Pointer cdr_fn(Pointer args) { +Pointer cdr_fn(Pointer args, Pointer env) { + (void) env; return CDR(CAR(args)); } -Pointer list_fn(Pointer args) { +Pointer list_fn(Pointer args, Pointer env) { + (void) env; return args; } diff --git a/implementations/c/src/evaluator.c b/implementations/c/src/evaluator.c index f146b35..e3c9e05 100644 --- a/implementations/c/src/evaluator.c +++ b/implementations/c/src/evaluator.c @@ -43,7 +43,7 @@ Pointer eval(Pointer data, Pointer env) { } if (type == NATIVE_FUNC) { - return NATIVE_FUNC(op)(params); + return NATIVE_FUNC(op)(params, env); } if (type == FUNC) { diff --git a/implementations/c/src/lisp.c b/implementations/c/src/lisp.c index 69ee377..2d12ccc 100644 --- a/implementations/c/src/lisp.c +++ b/implementations/c/src/lisp.c @@ -31,15 +31,17 @@ Pointer func(Pointer code, Pointer env) { return fn; } -/** OPERATIONS **/ +/** FUNCTIONS **/ -Pointer add_fn(Pointer args) { +Pointer add_fn(Pointer args, Pointer env) { + (void) env; Pointer result = number(0); REDUCE(args, result; NUMBER(result) += NUMBER(CAR(args)), result); return result; } -Pointer sub_fn(Pointer args) { +Pointer sub_fn(Pointer args, Pointer env) { + (void) env; Pointer first = CAR(args); Pointer rest = CDR(args); if (first == NIL) return UNDEFINED; // Empty args @@ -49,13 +51,15 @@ Pointer sub_fn(Pointer args) { return result; } -Pointer mul_fn(Pointer args) { +Pointer mul_fn(Pointer args, Pointer env) { + (void) env; Pointer result = number(1); REDUCE(args, result; NUMBER(result) *= NUMBER(CAR(args)), result); return result; } -Pointer div_fn(Pointer args) { +Pointer div_fn(Pointer args, Pointer env) { + (void) env; Pointer first = CAR(args); Pointer rest = CDR(args); if (first == NIL) return UNDEFINED; // Empty args @@ -65,20 +69,23 @@ Pointer div_fn(Pointer args) { return result; } -Pointer pow_fn(Pointer args) { +Pointer pow_fn(Pointer args, Pointer env) { + (void) env; Pointer a = CAR(args); Pointer b = CAR(CDR(args)); if (a == NIL || b == NIL) return UNDEFINED; // Arguments missing. return number(pow(NUMBER(a), NUMBER(b))); } -Pointer sqrt_fn(Pointer args) { +Pointer sqrt_fn(Pointer args, Pointer env) { + (void) env; Pointer a = CAR(args); if (a == NIL) return UNDEFINED; // Arguments missing. return number(sqrt(NUMBER(a))); } -Pointer logand_fn(Pointer args) { +Pointer logand_fn(Pointer args, Pointer env) { + (void) env; Pointer result = number(-1l); REDUCE(args, result; NUMBER(result) = (long) NUMBER(result) & (long) NUMBER(CAR(args)), @@ -86,7 +93,8 @@ Pointer logand_fn(Pointer args) { return result; } -Pointer logor_fn(Pointer args) { +Pointer logor_fn(Pointer args, Pointer env) { + (void) env; Pointer result = number(0); REDUCE(args, result; NUMBER(result) = (long) NUMBER(result) | (long) NUMBER(CAR(args)), @@ -94,7 +102,8 @@ Pointer logor_fn(Pointer args) { return result; } -Pointer logxor_fn(Pointer args) { +Pointer logxor_fn(Pointer args, Pointer env) { + (void) env; Pointer result = number(0); REDUCE(args, result; NUMBER(result) = (long) NUMBER(result) ^ (long) NUMBER(CAR(args)), @@ -102,7 +111,8 @@ Pointer logxor_fn(Pointer args) { return result; } -Pointer lognot_fn(Pointer args) { +Pointer lognot_fn(Pointer args, Pointer env) { + (void) env; Pointer a = CAR(args); if (a == NIL) return UNDEFINED; // Arguments missing. return number(~(long) NUMBER(a)); @@ -164,7 +174,8 @@ Pointer or_fn(Pointer args, Pointer env) { return NIL; } -Pointer not_fn(Pointer args) { +Pointer not_fn(Pointer args, Pointer env) { + (void) env; return CAR(args) == NIL ? T : NIL; } @@ -187,6 +198,9 @@ Pointer fn_fn(Pointer args, Pointer env) { return func(args, env); } +#define SET_FUNC(s, fn) environment_set(NIL, symbol1(s), native_func(fn)) +#define SET_FORM(s, fn) environment_set(NIL, symbol1(s), special_form(fn)) + void init(void) { memory_init(16); symbol_init(); @@ -194,8 +208,9 @@ void init(void) { environment_init(); environment_set(NIL, STANDARD_INPUT, stream(stdin)); environment_set(NIL, STANDARD_OUTPUT, stream(stdout)); - Pointer add = symbol1("+"); - environment_set(NIL, add, native_func(add_fn)); + SET_FUNC("+", add_fn); + SET_FUNC("-", sub_fn); + SET_FUNC("*", mul_fn); } void repl(void) { diff --git a/implementations/c/src/lisp.h b/implementations/c/src/lisp.h index 8412fad..9a488f9 100644 --- a/implementations/c/src/lisp.h +++ b/implementations/c/src/lisp.h @@ -74,11 +74,9 @@ typedef struct { } Table; /* CHANGER POUR TABLEAU DE FONCTIONS */ -typedef Pointer (*NativeFunc)(Pointer params); +typedef Pointer (*NativeFunc)(Pointer params, Pointer env); typedef Pointer (*SpecialForm)(Pointer params, Pointer env); -/* FIN A RETRAVAILLER! */ - typedef FILE* Stream; typedef union { @@ -172,36 +170,36 @@ Pointer special_form(SpecialForm func); Pointer character(Char c); Pointer stream(FILE* s); -/* OPERATIONS */ +/* FUNCTIONS */ Pointer eval_fn(Pointer args, Pointer env); -Pointer cons_fn(Pointer args); -Pointer car_fn(Pointer args); -Pointer cdr_fn(Pointer args); +Pointer cons_fn(Pointer args, Pointer env); +Pointer car_fn(Pointer args, Pointer env); +Pointer cdr_fn(Pointer args, Pointer env); Pointer reduce_fn(Pointer args, Pointer env); -Pointer add_fn(Pointer args); -Pointer list_fn(Pointer args); -Pointer sub_fn(Pointer args); -Pointer mul_fn(Pointer args); -Pointer div_fn(Pointer args); -Pointer pow_fn(Pointer args); -Pointer sqrt_fn(Pointer args); -Pointer logand_fn(Pointer args); -Pointer logor_fn(Pointer args); -Pointer logxor_fn(Pointer args); -Pointer lognot_fn(Pointer args); +Pointer add_fn(Pointer args, Pointer env); +Pointer list_fn(Pointer args, Pointer env); +Pointer sub_fn(Pointer args, Pointer env); +Pointer mul_fn(Pointer args, Pointer env); +Pointer div_fn(Pointer args, Pointer env); +Pointer pow_fn(Pointer args, Pointer env); +Pointer sqrt_fn(Pointer args, Pointer env); +Pointer logand_fn(Pointer args, Pointer env); +Pointer logor_fn(Pointer args, Pointer env); +Pointer logxor_fn(Pointer args, Pointer env); +Pointer lognot_fn(Pointer args, Pointer env); Pointer if_fn(Pointer args, Pointer env); Pointer let_fn(Pointer args, Pointer env); Pointer quote_fn(Pointer args, Pointer env); Pointer and_fn(Pointer args, Pointer env); Pointer or_fn(Pointer args, Pointer env); -Pointer not_fn(Pointer args); +Pointer not_fn(Pointer args, Pointer env); Pointer def_fn(Pointer args, Pointer env); Pointer set_fn(Pointer args, Pointer env); Pointer fn_fn(Pointer args, Pointer env); Pointer peek_char_fn(Pointer args, Pointer env); Pointer read_char_fn(Pointer args, Pointer env); Pointer read_fn(Pointer args, Pointer env); -Pointer set_reader_macro_fn(Pointer args); +Pointer set_reader_macro_fn(Pointer args, Pointer env); Pointer read_char_macro_fn(Pointer args, Pointer env); Pointer read_list_macro_fn(Pointer args, Pointer env); Pointer read_right_paren_macro_fn(Pointer args, Pointer env); diff --git a/implementations/c/src/reader.c b/implementations/c/src/reader.c index 9937ee0..2c11ff2 100644 --- a/implementations/c/src/reader.c +++ b/implementations/c/src/reader.c @@ -107,15 +107,14 @@ Pointer read_fn(Pointer args, Pointer env) { : STREAM(streamPtr); double num = 0; Pointer macro; - int pos; Char c = peek_char(T, stream); if (feof(stream)) return NIL; macro = table_get(READTABLE, c); if (macro != UNDEFINED) { switch(memory_get(macro)->type) { - case SPECIAL_FORM: - return SPECIAL_FORM(macro)(LIST(streamPtr, character(get_utf8(stream))), env); + case NATIVE_FUNC: + return NATIVE_FUNC(macro)(LIST(streamPtr, character(get_utf8(stream))), env); case FUNC: return eval_fn(LIST(macro, streamPtr, character(get_utf8(stream))), env); default: break; @@ -144,7 +143,8 @@ Pointer set_reader_macro(Pointer c, Pointer fn) { return T; } -Pointer set_reader_macro_fn(Pointer args) { +Pointer set_reader_macro_fn(Pointer args, Pointer env) { + (void) env; return set_reader_macro(CAR(args), CAR(CDR (args))); } @@ -254,9 +254,9 @@ Pointer read_right_paren_macro_fn(Pointer args, Pointer env) { void reader_init(void) { buffer = string(NULL, 0); READTABLE = table(1); - READTABLE = table_set(READTABLE, '(', special_form(read_list_macro_fn)); - READTABLE = table_set(READTABLE, ')', special_form(read_list_macro_fn)); - READTABLE = table_set(READTABLE, '\\', special_form(read_char_macro_fn)); + READTABLE = table_set(READTABLE, '(', native_func(read_list_macro_fn)); + READTABLE = table_set(READTABLE, ')', native_func(read_list_macro_fn)); + READTABLE = table_set(READTABLE, '\\', native_func(read_char_macro_fn)); /* GLOBALS = table_set(GLOBALS, */ /* symbol("*standard-input*", sizeof("*standard-input*")), */ /* stream(stdin)); */ diff --git a/implementations/c/tests/check_ptlisp.log b/implementations/c/tests/check_ptlisp.log index d4f6b29338123627e5affeb0f50a41d2e2ba0b34..933bc4d40d9c41cce1a03efcc39a4b3d0cd88ef9 100644 GIT binary patch delta 112 zcmccWK9y;LzOh1ZNl|8AI+ucTeqJ$`f@@w`W>J1#Zfaf$kW-eJQ<_+kU&N*0lUZEA zr4W>wn37sFxkpxevXY#Trfx}Uafx2Cm4b-@R}okWA!25wV8o>m05rP6(DygC6!L{IxkrgKbLMNOmZumnb1z$6+ z5T!>*_GGzv$m=#&LSkmz%FOgb;Ju#oS)ZzoR2ESN#KoL-o=$zAKkmB~4)x%3)ocvy z6BWiZ1atx?GV`+-F@cysOdMM@Vs&73V0B=1?Dw&bZ!RHEAWtAqAWuBS6ZWz)w-%bk zV+&1cubXLN%K|4C)5wmSxw8gs=O)e#F#Jwp=E zzmgsA1d9yryVFld!;p|TjYtyEh|H$#$Y_)C7@;2Hjj^Y>A9GcyyAtwKyBPQc`I*YxdgFgLF{orN#hlup%H#PoKC-?`Q*kR;! zHy8HhJOFhB^dyK$GAb3__NRp$lb#L+{SnY#q(1`s^YooKc7R)btw6ash2~b_7nWpY zNlGp833->n^*vktY%)mV8GOjAx|K|~G!Sm26tes2-Cix1u