Browse Source

repl!

master
Gabriel Pariat 3 years ago
parent
commit
76171dbdee
  1. 12
      implementations/c/src/lisp.c
  2. 1
      implementations/c/src/lisp.h
  3. 1
      implementations/c/src/main.c
  4. 18
      implementations/c/src/reader.c
  5. 5
      implementations/c/tests/check_ptlisp.log
  6. 2
      implementations/c/tests/ptlisp-test.c

12
implementations/c/src/lisp.c

@ -198,6 +198,11 @@ Pointer fn_fn(Pointer args, Pointer env) {
return func(args, env); return func(args, env);
} }
Pointer exit_fn(Pointer args, Pointer env) {
(void) args; (void) env;
exit(1);
}
#define SET_FUNC(s, fn) environment_set(NIL, symbol1(s), native_func(fn)) #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)) #define SET_FORM(s, fn) environment_set(NIL, symbol1(s), special_form(fn))
@ -226,6 +231,7 @@ void init(void) {
SET_FUNC("~", lognot_fn); SET_FUNC("~", lognot_fn);
SET_FUNC("peek-char", peek_char_fn); SET_FUNC("peek-char", peek_char_fn);
SET_FUNC("read-char", read_char_fn); SET_FUNC("read-char", read_char_fn);
SET_FUNC("exit", exit_fn);
SET_FORM("if", if_fn); SET_FORM("if", if_fn);
SET_FORM("let", let_fn); SET_FORM("let", let_fn);
@ -238,7 +244,9 @@ void init(void) {
} }
void repl(void) { void repl(void) {
Pointer readed = read_fn(NIL, NIL); while(true) {
print(eval(readed, NIL), stdout); printf(">>> ");
print(eval(read_fn(NIL, NIL), NIL), stdout);
printf("\n"); printf("\n");
}
} }

1
implementations/c/src/lisp.h

@ -195,6 +195,7 @@ Pointer read_fn(Pointer args, Pointer env);
Pointer set_reader_macro_fn(Pointer args, Pointer env); Pointer set_reader_macro_fn(Pointer args, Pointer env);
Pointer print_fn(Pointer args, Pointer env); Pointer print_fn(Pointer args, Pointer env);
Pointer not_fn(Pointer args, Pointer env); Pointer not_fn(Pointer args, Pointer env);
Pointer exit_fn(Pointer args, Pointer env);
/* SPECIAL FORMS */ /* SPECIAL FORMS */
Pointer if_fn(Pointer args, Pointer env); Pointer if_fn(Pointer args, Pointer env);

1
implementations/c/src/main.c

@ -3,7 +3,6 @@
#include <stdbool.h> #include <stdbool.h>
int main() { int main() {
printf(">>> ");
init(); init();
repl(); repl();
return 0; return 0;

18
implementations/c/src/reader.c

@ -11,24 +11,24 @@ Pointer character(Char c) {
} }
unsigned get_utf8(FILE* s) { unsigned get_utf8(FILE* s) {
unsigned c = getc(s); unsigned c = fgetc(s);
if (c & 0x80 && c & 0x40) { if (c & 0x80 && c & 0x40) {
switch(c >> 3 & 0x07) { switch(c >> 3 & 0x07) {
case 0: case 0:
case 1: case 1:
case 2: case 2:
case 3: case 3:
c |= getc(s) << 8; c |= fgetc(s) << 8;
break; break;
case 4: case 4:
case 5: case 5:
c |= getc(s) << 8; c |= fgetc(s) << 8;
c |= getc(s) << 16; c |= fgetc(s) << 16;
break; break;
case 6: case 6:
c |= getc(s) << 8; c |= fgetc(s) << 8;
c |= getc(s) << 16; c |= fgetc(s) << 16;
c |= getc(s) << 24; c |= fgetc(s) << 24;
break; break;
default: break; default: break;
} }
@ -59,12 +59,12 @@ Char peek_char(Pointer type, Stream stream) {
return unget_utf8(get_utf8(stream), stream); return unget_utf8(get_utf8(stream), stream);
} }
if (type == T) { if (type == T) {
while(!feof(stream) && (c = get_utf8(stream)) == ' '); while(isspace((char) (c = get_utf8(stream))) && !feof(stream));
if (feof(stream)) return 0; if (feof(stream)) return 0;
return unget_utf8(c, stream); return unget_utf8(c, stream);
} }
if (TYPE(type) == CHAR) { if (TYPE(type) == CHAR) {
while(!feof(stream) && (c = get_utf8(stream)) != CHAR(type)); while((c = get_utf8(stream)) != CHAR(type) && !feof(stream));
if (feof(stream)) return 0; if (feof(stream)) return 0;
return unget_utf8(c, stream); return unget_utf8(c, stream);
} }

5
implementations/c/tests/check_ptlisp.log

@ -8,11 +8,6 @@ Running suite(s): Memory
Evaluator Evaluator
Lisp Lisp
Reader Reader
reader-test.c: 29
reader-test.c: 32
reader-test.c: 34
reader-test.c: 40
reader-test.c: 46: 2
Printer Printer
100%: Checks: 87, Failures: 0, Errors: 0 100%: Checks: 87, Failures: 0, Errors: 0
PASS check_ptlisp (exit status: 0) PASS check_ptlisp (exit status: 0)

2
implementations/c/tests/ptlisp-test.c

@ -16,7 +16,7 @@ int main(void) {
srunner_add_suite(sr, make_reader_test_suite()); srunner_add_suite(sr, make_reader_test_suite());
srunner_add_suite(sr, make_printer_test_suite()); srunner_add_suite(sr, make_printer_test_suite());
srunner_set_fork_status(sr, CK_NOFORK); /* srunner_set_fork_status(sr, CK_NOFORK); */
srunner_run_all(sr, CK_NORMAL); srunner_run_all(sr, CK_NORMAL);
nf = srunner_ntests_failed(sr); nf = srunner_ntests_failed(sr);
srunner_free(sr); srunner_free(sr);

Loading…
Cancel
Save