Browse Source

handling reader macro

master
Gabriel Pariat 3 years ago
parent
commit
67751a2f47
  1. 13
      implementations/c/src/lisp.c
  2. 1
      implementations/c/src/lisp.h
  3. 14
      implementations/c/src/reader.c

13
implementations/c/src/lisp.c

@ -303,12 +303,14 @@ void init(void) { @@ -303,12 +303,14 @@ void init(void) {
SET_FUNC("peek-char", peek_char_fn);
SET_FUNC("read-char", read_char_fn);
SET_FUNC("read", read_fn);
SET_FUNC("set-reader-macro", set_reader_macro_fn);
SET_FUNC("exit", exit_fn);
SET_FUNC("=", eq_fn);
SET_FUNC("<", lt_fn);
SET_FUNC(">", gt_fn);
SET_FUNC("<=", le_fn);
SET_FUNC(">=", ge_fn);
SET_FUNC("print", print_fn);
SET_FORM("if", if_fn);
SET_FORM("let", let_fn);
@ -321,10 +323,19 @@ void init(void) { @@ -321,10 +323,19 @@ void init(void) {
SET_FORM("defmacro", defmacro_fn);
}
/*
(set-reader-macro \' (fn (s c) (list quote (read s))))
*/
void repl(void) {
while(true) {
printf(">>> ");
print(eval(read_fn(NIL, NIL), NIL), stdout);
Pointer readed = read_fn(NIL, NIL);
char c;
while((c = getc(stdin)) && isspace(c) && c != '\n');
if (c != '\n') ungetc(c, stdin);
print(eval(readed, NIL), stdout);
printf("\n");
}
}

1
implementations/c/src/lisp.h

@ -144,7 +144,6 @@ Pointer string_clear(Pointer str); @@ -144,7 +144,6 @@ Pointer string_clear(Pointer str);
Pointer environment_get(Pointer env, Pointer key);
Pointer environment_set(Pointer env, Pointer key, Pointer value);
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);

14
implementations/c/src/reader.c

@ -114,7 +114,7 @@ Pointer read1(Pointer streamPtr, Pointer env) { @@ -114,7 +114,7 @@ Pointer read1(Pointer streamPtr, Pointer 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);
return eval(LIST(macro, streamPtr, character(get_utf8(stream))), env);
default: break;
}
}
@ -139,22 +139,14 @@ Pointer read1(Pointer streamPtr, Pointer env) { @@ -139,22 +139,14 @@ Pointer read1(Pointer streamPtr, Pointer env) {
Pointer read_fn(Pointer args, Pointer env) {
Pointer streamPtr = CAR(args);
streamPtr = streamPtr == NIL ? environment_get(env, STANDARD_INPUT) : streamPtr;
Stream stream = STREAM(streamPtr);
Pointer result = read1(streamPtr, env);
char c;
while((c = getc(stream)) && isspace(c) && c != '\n');
if (c != '\n') ungetc(c, stream);
return result;
}
Pointer set_reader_macro(Pointer c, Pointer fn) {
READTABLE = table_set(READTABLE, c, fn);
return T;
}
Pointer set_reader_macro_fn(Pointer args, Pointer env) {
(void) env;
return set_reader_macro(CAR(args), CAR(CDR (args)));
READTABLE = table_set(READTABLE, CHAR(CAR(args)), CAR(CDR(args)));
return T;
}
static bool return_char(Stream stream) {

Loading…
Cancel
Save