From 5eeb355805e0d5d91981dbe7eacb078f41da0437 Mon Sep 17 00:00:00 2001 From: Felix Perktold Date: Sat, 20 Dec 2025 21:59:28 +0100 Subject: init commit --- lisp.y | 59 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 lisp.y (limited to 'lisp.y') diff --git a/lisp.y b/lisp.y new file mode 100644 index 0000000..29031a4 --- /dev/null +++ b/lisp.y @@ -0,0 +1,59 @@ +%{ +#include +#include +#include +#include "lisp.h" + +int yylex(void); +int yyerror(const char *s); + +static int is_integer(double x) { + return floor(x) == x && isfinite(x); +} +%} + +%union { + int ival; + double dval; + const char *strval; + value *val; +} +%token NUMBER +%token STRING +%token LPAREN RPAREN DOT WHITESPACE +%token PLUS MINUS MULT DIV + +%type sexpr atom sexprs + +%start sexprs + +%% +opt_ws: /* matches no whitespace */ + | WHITESPACE; + +sexprs: sexpr + | sexprs opt_ws sexpr; + +sexpr: atom + | LPAREN opt_ws sexpr opt_ws DOT opt_ws sexpr opt_ws RPAREN + { + $$ = cons($3, $7); + print_val($$); + printf("\n"); + }; + +atom: NUMBER + { + if(is_integer($1)) { + $$ = make_int($1); + } else { + $$ = make_double($1); + } + } + | LPAREN opt_ws RPAREN { $$ = make_nil(); } + | STRING { $$ = make_string($1); }; +%% + +int yyerror(const char *s) { + printf("%s\n",s); +} -- cgit v1.2.3