Browse Source

refactoring

master
Gabriel Pariat 3 years ago
parent
commit
6b2dc86318
  1. 124
      implementations/c/src/lisp.c
  2. 23
      implementations/c/src/lisp.h
  3. 14
      implementations/c/src/reader.c
  4. 25
      implementations/c/tests/reader-test.c

124
implementations/c/src/lisp.c

@ -208,9 +208,33 @@ void init(void) { @@ -208,9 +208,33 @@ void init(void) {
environment_init();
environment_set(NIL, STANDARD_INPUT, stream(stdin));
environment_set(NIL, STANDARD_OUTPUT, stream(stdout));
SET_FUNC("eval", eval_fn);
SET_FUNC("cons", cons_fn);
SET_FUNC("car", car_fn);
SET_FUNC("cdr", cdr_fn);
SET_FUNC("reduce", reduce_fn);
SET_FUNC("list", list_fn);
SET_FUNC("+", add_fn);
SET_FUNC("-", sub_fn);
SET_FUNC("*", mul_fn);
SET_FUNC("/", div_fn);
SET_FUNC("pow", pow_fn);
SET_FUNC("sqrt", sqrt_fn);
SET_FUNC("&", logand_fn);
SET_FUNC("|", logor_fn);
SET_FUNC("^", logxor_fn);
SET_FUNC("~", lognot_fn);
SET_FUNC("peek-char", peek_char_fn);
SET_FUNC("read-char", read_char_fn);
SET_FORM("if", if_fn);
SET_FORM("let", let_fn);
SET_FORM("quote", quote_fn);
SET_FORM("and", and_fn);
SET_FORM("or", or_fn);
SET_FORM("def", def_fn);
SET_FORM("set", set_fn);
SET_FORM("fn", fn_fn);
}
void repl(void) {
@ -218,103 +242,3 @@ void repl(void) { @@ -218,103 +242,3 @@ void repl(void) {
print(eval(readed, NIL), stdout);
printf("\n");
}
/* Pointer op_readChar(Pointer params) { */
/* return readerPop(params == NIL ? STANDARD_INPUT : GET_CONS(params).car); */
/* } */
/* Pointer op_peekChar(Pointer params) { */
/* return readerPop(params == NIL ? STANDARD_INPUT : GET_CONS(params).car); */
/* } */
/* Pointer set_reader_macro(Pointer params) { */
/* Cons cons = CONS(params); */
/* return rdr_setReaderMacro(cons.car, GET_CONS(cons.cdr).car); */
/* } */
/* Pointer op_read(Pointer params) { */
/* return rdr_read(CAR(params)); */
/* } */
/* Pointer op_map(Pointer params) { */
/* Cons cons = GET_CONS(params); */
/* Pointer list = cons.car; */
/* Pointer fn = GET_CONS(cons.cdr).car; */
/* while (list != NIL) { */
/* cons = GET_CONS(list); */
/* list = cons.cdr; */
/* } */
/* } */
/* void run() { */
/* do { */
/* prn_print(evl_eval(rdr_read(GLOBAL_ENV_GET(STANDARD_INPUT)), */
/* ALLOC_CONS(GLOBAL_ENV, NIL)), */
/* stdout); */
/* fprintf(stdout, "\n"); */
/* } while(rdr_peekChar(T, GET_STREAM(GLOBAL_ENV_GET(STANDARD_INPUT))) != 0); */
/* } */
/* void start() { */
/* mem_init(1024); */
/* ascii_init(); */
/* GLOBAL_ENV = tbl_alloc(2); // GLOBAL ENV */
/* ALLOC_STATIC_SYMBOL(NIL, "nil"); */
/* ALLOC_STATIC_SYMBOL(T, "t"); */
/* ALLOC_STATIC_SYMBOL(STANDARD_INPUT, "*standard-input*"); */
/* ALLOC_STATIC_SYMBOL(STANDARD_OUTPUT, "*standard-output*"); */
/* ALLOC_STATIC_SYMBOL(READTABLE, "*readtable*"); */
/* ALLOC_NATIVE_FUNC("+", op_add); */
/* ALLOC_NATIVE_FUNC("-", op_sub); */
/* ALLOC_NATIVE_FUNC("*", op_mul); */
/* ALLOC_NATIVE_FUNC("/", op_div); */
/* ALLOC_NATIVE_FUNC("pow", op_pow); */
/* ALLOC_NATIVE_FUNC("sqrt", op_sqrt); */
/* ALLOC_NATIVE_FUNC("&", op_logand); */
/* ALLOC_NATIVE_FUNC("|", op_logior); */
/* ALLOC_NATIVE_FUNC("^", op_logxor); */
/* ALLOC_NATIVE_FUNC("~", op_lognot); */
/* ALLOC_NATIVE_FUNC("list", op_list); */
/* ALLOC_NATIVE_FUNC("car", op_car); */
/* ALLOC_NATIVE_FUNC("cdr", op_cdr); */
/* ALLOC_NATIVE_FUNC("not", op_not); */
/* ALLOC_NATIVE_FUNC("set-reader-macro", op_setReaderMacro); */
/* ALLOC_NATIVE_FUNC("read", op_read); */
/* ALLOC_SPECIAL_FORM("if", op_if); */
/* ALLOC_SPECIAL_FORM("let", op_let); */
/* ALLOC_SPECIAL_FORM("quote", op_quote); */
/* ALLOC_SPECIAL_FORM("and", op_and); */
/* ALLOC_SPECIAL_FORM("or", op_or); */
/* ALLOC_SPECIAL_FORM("def", op_def); */
/* ALLOC_SPECIAL_FORM("set", op_set); */
/* ALLOC_SPECIAL_FORM("fn", op_fn); */
/* /\* ALLOC_STREAM(STANDARD_INPUT, "*standard-input*", stdin); *\/ */
/* /\* ALLOC_STREAM(STANDARD_OUTPUT, "*standard-output*", stdout); *\/ */
/* rdr_init(); */
/* /\* op_print(getFromTable(globalEnv, T), stdout); *\/ */
/* /\* op_print(globalEnv, stdout); *\/ */
/* /\* Stream stream = {"(fn (stream char) ())\0"}; *\/ */
/* /\* tbl_insert(readerMacros, *\/ */
/* /\* '\\', *\/ */
/* /\* mem_alloc(CONS, *\/ */
/* /\* (Data) (Cons) {eval(read(&stream), NIL), NIL})); *\/ */
/* /\* inputStream.text = "(def add (fn (a b) (+ a b))) (add 1 2)\0"; *\/ */
/* /\* inputStream.text = "(if t 1 2)\0"; *\/ */
/* /\* printf("%s\n", inputStream.text); *\/ */
/* FILE* file = fopen("lisp.lisp", "r"); */
/* GLOBAL_ENV_INSERT(STANDARD_INPUT, ALLOC_STREAM(file)); */
/* run(); */
/* fclose(file); */
/* GLOBAL_ENV_SET(STANDARD_INPUT, ALLOC_STREAM(stdin)); */
/* run(); */
/* printf("\n"); */
/* } */

23
implementations/c/src/lisp.h

@ -149,13 +149,15 @@ Pointer set_reader_macro(Pointer c, Pointer fn); @@ -149,13 +149,15 @@ Pointer set_reader_macro(Pointer c, Pointer fn);
Char get_utf8(FILE* s);
Char unget_utf8(Char c, FILE* s);
Char peek_char(Pointer type, Stream stream);
Pointer read_char_macro(Pointer args, Pointer env);
Pointer read_list_macro(Pointer args, Pointer env);
Pointer read_right_paren_macro(Pointer args, Pointer env);
Pointer prin1(Pointer data, Stream stream);
Pointer print(Pointer data, Stream stream);
Pointer eval(Pointer data, Pointer env);
/* CONSTRUCTORS */
Pointer array(size_t size);
Pointer table(size_t size);
@ -176,8 +178,8 @@ Pointer cons_fn(Pointer args, Pointer env); @@ -176,8 +178,8 @@ 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 env);
Pointer list_fn(Pointer args, Pointer env);
Pointer add_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);
@ -187,23 +189,22 @@ Pointer logand_fn(Pointer args, Pointer env); @@ -187,23 +189,22 @@ 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 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 env);
Pointer print_fn(Pointer args, Pointer env);
Pointer not_fn(Pointer args, Pointer env);
/* SPECIAL FORMS */
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 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 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);
Pointer print_fn(Pointer args, Pointer env);
#define TYPE(p) memory_get(p)->type
#define SIZE(p) memory_get(p)->size

14
implementations/c/src/reader.c

@ -160,7 +160,7 @@ static char space[] = "SPACE"; @@ -160,7 +160,7 @@ static char space[] = "SPACE";
static char tab[] = "TAB";
static char newline[] = "NEWLINE";
Pointer read_char_macro_fn(Pointer args, Pointer env) {
Pointer read_char_macro(Pointer args, Pointer env) {
(void) env;
Pointer streamPtr = CAR(args);
Stream stream = streamPtr == NIL
@ -226,7 +226,7 @@ Pointer read_char_macro_fn(Pointer args, Pointer env) { @@ -226,7 +226,7 @@ Pointer read_char_macro_fn(Pointer args, Pointer env) {
return UNDEFINED; // ERROR!
}
Pointer read_list_macro_fn(Pointer args, Pointer env) {
Pointer read_list_macro(Pointer args, Pointer env) {
Pointer streamPtr = CAR(args);
Stream stream = streamPtr == NIL
? STREAM(environment_get(env, STANDARD_INPUT))
@ -237,7 +237,7 @@ Pointer read_list_macro_fn(Pointer args, Pointer env) { @@ -237,7 +237,7 @@ Pointer read_list_macro_fn(Pointer args, Pointer env) {
if (!feof(stream)) {
if (c != ')') {
cdr = read_list_macro_fn(args, env);
cdr = read_list_macro(args, env);
} else {
get_utf8(stream);
}
@ -246,7 +246,7 @@ Pointer read_list_macro_fn(Pointer args, Pointer env) { @@ -246,7 +246,7 @@ Pointer read_list_macro_fn(Pointer args, Pointer env) {
return cons(car, cdr);
}
Pointer read_right_paren_macro_fn(Pointer args, Pointer env) {
Pointer read_right_paren_macro(Pointer args, Pointer env) {
(void) args; (void) env;
return NIL; // TODO: Should return an error when the error system is set.
}
@ -254,9 +254,9 @@ Pointer read_right_paren_macro_fn(Pointer args, Pointer env) { @@ -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, '(', 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));
READTABLE = table_set(READTABLE, '(', native_func(read_list_macro));
READTABLE = table_set(READTABLE, ')', native_func(read_list_macro));
READTABLE = table_set(READTABLE, '\\', native_func(read_char_macro));
/* GLOBALS = table_set(GLOBALS, */
/* symbol("*standard-input*", sizeof("*standard-input*")), */
/* stream(stdin)); */

25
implementations/c/tests/reader-test.c

@ -26,24 +26,19 @@ START_TEST(read_char_fn_test) { @@ -26,24 +26,19 @@ START_TEST(read_char_fn_test) {
START_TEST(read_fn_test) {
FILE* s;
printf("%s: %d\n", __FILE__, __LINE__);
char hello[] = "hello";
s = fmemopen(hello, sizeof(hello), "r");
printf("%s: %d\n", __FILE__, __LINE__);
ck_assert_str_eq(SYMBOL(read_fn(LIST(stream(s)), NIL)).data, hello);
printf("%s: %d\n", __FILE__, __LINE__);
fclose(s);
char funny[] = "69.420";
s = fmemopen(funny, sizeof(funny), "r");
ck_assert_double_eq(NUMBER(read_fn(LIST(stream(s)), NIL)), 69.420);
printf("%s: %d\n", __FILE__, __LINE__);
fclose(s);
char list[] = "(1 2 3)";
s = fmemopen(list, sizeof(list), "r");
Pointer result = read_fn(LIST(stream(s)), NIL);
printf("%s: %d: %d\n", __FILE__, __LINE__, TYPE(CAR(result)));
ck_assert_double_eq(NUMBER(CAR(result)), 1);
ck_assert_double_eq(NUMBER(CAR(CDR(result))), 2);
ck_assert_double_eq(NUMBER(CAR(CDR(CDR(result)))), 3);
@ -65,17 +60,17 @@ START_TEST(read_char_macro_fn_test) { @@ -65,17 +60,17 @@ START_TEST(read_char_macro_fn_test) {
char a[] = "a";
s = fmemopen(a, sizeof(a), "r");
ck_assert_uint_eq(CHAR(read_char_macro_fn(LIST(stream(s)), NIL)), 'a');
ck_assert_uint_eq(CHAR(read_char_macro(LIST(stream(s)), NIL)), 'a');
fclose(s);
char ab[] = "ab";
s = fmemopen(ab, sizeof(ab), "r");
ck_assert_uint_eq(read_char_macro_fn(LIST(stream(s)), NIL), UNDEFINED);
ck_assert_uint_eq(read_char_macro(LIST(stream(s)), NIL), UNDEFINED);
fclose(s);
unsigned char smile[] = "😄";
s = fmemopen(smile, sizeof(smile), "r");
Pointer result = read_char_macro_fn(LIST(stream(s)), NIL);
Pointer result = read_char_macro(LIST(stream(s)), NIL);
ck_assert_int_eq(CHAR(result) & 0xFF, smile[0]);
ck_assert_int_eq((CHAR(result) >> 8) & 0xFF, smile[1]);
ck_assert_int_eq((CHAR(result) >> 16) & 0xFF, smile[2]);
@ -84,34 +79,34 @@ START_TEST(read_char_macro_fn_test) { @@ -84,34 +79,34 @@ START_TEST(read_char_macro_fn_test) {
char smile2[] = "😄😄";
s = fmemopen(smile2, sizeof(smile2), "r");
ck_assert_uint_eq(read_char_macro_fn(LIST(stream(s)), NIL), UNDEFINED);
ck_assert_uint_eq(read_char_macro(LIST(stream(s)), NIL), UNDEFINED);
fclose(s);
char space[] = "space";
s = fmemopen(space, sizeof(space), "r");
ck_assert_uint_eq(CHAR(read_char_macro_fn(LIST(stream(s)), NIL)), ' ');
ck_assert_uint_eq(CHAR(read_char_macro(LIST(stream(s)), NIL)), ' ');
fclose(s);
char tab[] = "tab";
s = fmemopen(tab, sizeof(tab), "r");
ck_assert_uint_eq(CHAR(read_char_macro_fn(LIST(stream(s)), NIL)), '\t');
ck_assert_uint_eq(CHAR(read_char_macro(LIST(stream(s)), NIL)), '\t');
fclose(s);
char newline[] = "newline";
s = fmemopen(newline, sizeof(newline), "r");
ck_assert_uint_eq(CHAR(read_char_macro_fn(LIST(stream(s)), NIL)), '\n');
ck_assert_uint_eq(CHAR(read_char_macro(LIST(stream(s)), NIL)), '\n');
fclose(s);
char space2[] = "space2";
s = fmemopen(space2, sizeof(space2), "r");
ck_assert_uint_eq(read_char_macro_fn(LIST(stream(s)), NIL), UNDEFINED);
ck_assert_uint_eq(read_char_macro(LIST(stream(s)), NIL), UNDEFINED);
fclose(s);
} END_TEST
START_TEST(read_list_macro_fn_test) {
char list[] = "1 2 3)";
FILE* s = fmemopen(list, sizeof(list), "r");
Pointer result = read_list_macro_fn(LIST(stream(s)), NIL);
Pointer result = read_list_macro(LIST(stream(s)), NIL);
ck_assert_double_eq(NUMBER(CAR(result)), 1);
ck_assert_double_eq(NUMBER(CAR(CDR(result))), 2);
ck_assert_double_eq(NUMBER(CAR(CDR(CDR(result)))), 3);
@ -119,7 +114,7 @@ START_TEST(read_list_macro_fn_test) { @@ -119,7 +114,7 @@ START_TEST(read_list_macro_fn_test) {
} END_TEST
START_TEST(read_right_paren_macro_fn_test) {
ck_assert_uint_eq(read_right_paren_macro_fn(NIL, NIL), NIL);
ck_assert_uint_eq(read_right_paren_macro(NIL, NIL), NIL);
} END_TEST
static char buffer[] = "hello, world";

Loading…
Cancel
Save