summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFelix Perktold <Felix.Perktold@student.uibk.ac.at>2025-12-20 23:26:03 +0100
committerFelix Perktold <Felix.Perktold@student.uibk.ac.at>2025-12-20 23:26:03 +0100
commit6e416e831fc968b6db5721a4806bdcf34cf2e87f (patch)
tree9961e1873067a35e0d788e1445a34e79aea39982
parent6506e5c3d24f01b3a662a58884e210f4c9e023b8 (diff)
add list syntactic sugar, eg: (1 2) instead of (1 . (2 . ()))
-rw-r--r--lisp.y13
1 files changed, 10 insertions, 3 deletions
diff --git a/lisp.y b/lisp.y
index c2ca8f5..a15b451 100644
--- a/lisp.y
+++ b/lisp.y
@@ -23,7 +23,7 @@ static int is_integer(double x) {
%token <ival> LPAREN RPAREN DOT WHITESPACE
%token <ival> PLUS MINUS MULT DIV
-%type <val> sexpr atom
+%type <val> sexpr atom list list_items
%start sexprs
@@ -32,9 +32,10 @@ opt_ws: /* matches no whitespace */
| WHITESPACE;
sexprs: sexpr { print_val($1); printf("\n"); }
- | sexprs opt_ws sexpr { print_val($3); };
+ | sexprs opt_ws sexpr { print_val($3); printf("\n"); };
sexpr: atom
+ | list
| LPAREN opt_ws sexpr opt_ws DOT opt_ws sexpr opt_ws RPAREN
{
$$ = cons($3, $7);
@@ -42,6 +43,13 @@ sexpr: atom
printf("\n");
};
+list: LPAREN opt_ws list_items opt_ws RPAREN
+ { $$ = $3; };
+
+list_items: /* empty list */ { $$ = make_nil(); }
+ | sexpr opt_ws list_items { $$ = cons($1, $3); }
+ | sexpr WHITESPACE DOT WHITESPACE sexpr { $$ = cons($1, $5); };
+
atom: NUMBER
{
if(is_integer($1)) {
@@ -50,7 +58,6 @@ atom: NUMBER
$$ = make_double($1);
}
}
- | LPAREN opt_ws RPAREN { $$ = make_nil(); }
| STRING { $$ = make_string($1); };
%%