From b99be4933bcb850cbaf96f89750e925ecf2e8386 Mon Sep 17 00:00:00 2001 From: Felix Perktold Date: Tue, 23 Dec 2025 13:36:33 +0100 Subject: rename eq? to equal?, fix syntax error on empty newline --- Makefile | 2 +- lisp.c | 2 +- lisp.h | 2 +- lisp.l | 5 +++-- lisp.y | 9 +++++---- main.c | 2 +- 6 files changed, 12 insertions(+), 10 deletions(-) diff --git a/Makefile b/Makefile index d6d3fae..095a7ae 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ LEX = flex YACC = bison -CC = gcc -g -Wall -Werror +CC = gcc -g CFLAGS = -DYYDEBUG=1 LIBS = -lreadline -lfl -lm OBJS = lisp.o lisp.tab.o lex.yy.o diff --git a/lisp.c b/lisp.c index 1695ff7..6b34bab 100644 --- a/lisp.c +++ b/lisp.c @@ -356,7 +356,7 @@ int value_eq(value *a, value *b) { return 1; } -value *builtin_eq(env *e, value *args) { +value *builtin_equal(env *e, value *args) { value *v_prev = eval(e, car(args)); args = cdr(args); diff --git a/lisp.h b/lisp.h index 45986a6..a422c9b 100644 --- a/lisp.h +++ b/lisp.h @@ -76,7 +76,7 @@ value *builtin_car(env *e, value *args); value *builtin_cdr(env *e, value *args); value *builtin_reverse(env *e, value *args); value *builtin_isnull(env *e, value *args); -value *builtin_eq(env *e, value *args); +value *builtin_equal(env *e, value *args); value *builtin_add(env *e, value *args); value *builtin_sub(env *e, value *args); value *builtin_mul(env *e, value *args); diff --git a/lisp.l b/lisp.l index fa2203a..cb0453a 100644 --- a/lisp.l +++ b/lisp.l @@ -7,8 +7,9 @@ int tcnt = 0; %} %% -[[:space:]]+ { /* ignore whitespace */ } -";"[^\n]* { /* ignore comment */ } +[[:space:]&&[^\n]]+ { /* ignore whitespace */ } +";"[^\n]* { /* ignore comment */ } +\n { return NLINE; } "(" { return LPAREN; } ")" { return RPAREN; } "\." { return DOT; } diff --git a/lisp.y b/lisp.y index 5a39640..086ef93 100644 --- a/lisp.y +++ b/lisp.y @@ -8,21 +8,21 @@ int yyerror(const char *s); %} %union { - int ival; double dval; const char *strval; value *val; } %token NUMBER %token STRING SYMBOL -%token LPAREN RPAREN DOT QUOTE +%token LPAREN RPAREN DOT QUOTE NLINE %type expr atom list list_items %start exprs %% -exprs: expr +exprs: /* nothing */ + | expr { value *v = eval(global_env, $1); printf(";> "); @@ -38,7 +38,8 @@ exprs: expr expr: atom | list | LPAREN expr DOT expr RPAREN { $$ = cons($2, $4); } - | QUOTE expr { $$ = cons(make_symbol("quote"), cons($2, make_nil())); }; + | QUOTE expr { $$ = cons(make_symbol("quote"), cons($2, make_nil())); } + | NLINE { $$ = make_nil(); }; list: LPAREN list_items RPAREN { $$ = $2; }; diff --git a/main.c b/main.c index 3f851a8..e6f6f97 100644 --- a/main.c +++ b/main.c @@ -80,7 +80,7 @@ int main(int argc, char **argv) { env_define(global_env, "cdr", make_builtin(builtin_cdr)); env_define(global_env, "reverse", make_builtin(builtin_reverse)); env_define(global_env, "null?", make_builtin(builtin_isnull)); - env_define(global_env, "eq?", make_builtin(builtin_eq)); + env_define(global_env, "equal?", make_builtin(builtin_equal)); env_define(global_env, "+", make_builtin(builtin_add)); env_define(global_env, "-", make_builtin(builtin_sub)); env_define(global_env, "*", make_builtin(builtin_mul)); -- cgit v1.2.3