1
0
Fork 0
This commit is contained in:
Alex Kotov 2023-05-05 12:42:35 +04:00
parent 9b01119121
commit d64ce94c77
Signed by: kotovalexarian
GPG key ID: 553C0EBBEB5D5F08
2 changed files with 45 additions and 1 deletions

View file

@ -16,6 +16,9 @@ OBJS = \
run: lisp
./lisp
repl: lisp
./lisp --repl
test: lisp
./lisp --test

View file

@ -13,6 +13,7 @@
#include <string.h>
static void run();
static void repl();
static void test();
int main(int argc, char **argv)
@ -23,6 +24,8 @@ int main(int argc, char **argv)
run();
} else if (argc == 2 && strcmp(argv[1], "--test") == 0) {
test();
} else if (argc == 2 && strcmp(argv[1], "--repl") == 0) {
repl();
} else {
abort();
}
@ -116,8 +119,10 @@ struct Object *eval_list(struct Object *const object)
void run()
{
Tokens tokens = Tokens_new();
Lexer lexer = Lexer_new(tokens);
assert(tokens);
Lexer lexer = Lexer_new(tokens);
assert(lexer);
char chr;
while ((chr = getchar()) != EOF) {
Lexer_lex(lexer, chr);
@ -146,6 +151,42 @@ void run()
Object_print(result, 0);
}
void repl()
{
while (true) {
Tokens tokens = Tokens_new();
assert(tokens);
Lexer lexer = Lexer_new(tokens);
assert(lexer);
printf("> ");
while (true) {
const char chr = getchar();
if (chr == EOF) {
putchar('\n');
exit(EXIT_SUCCESS);
}
if (chr == '\n') {
Lexer_lex(lexer, '\n');
break;
}
Lexer_lex(lexer, chr);
}
LEXER_DELETE(lexer);
struct Object *const program = parse(tokens);
TOKENS_DELETE(tokens);
struct Object *const result = eval(program);
eval(Object_build_list(2, Object_new_symbol("displayln"), result));
}
}
// Macros
static void test_if();
// Basic data structures