aboutsummaryrefslogtreecommitdiff
path: root/works/life/compile-principle-experiment/4/syn.y
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-02-12 15:55:21 +0800
committerYuqian Yang <crupest@crupest.life>2025-02-12 15:55:21 +0800
commit1ecfd0ab7f1f511268fd6404dbc110c3c277b48c (patch)
tree49449a4076ded9bd937a51679318edbe2a532cae /works/life/compile-principle-experiment/4/syn.y
parent55d8b025e8d6ea971e8ee5762c892405fedc316b (diff)
parentf8c10dd1fc55e60f35286475356e48c4f642eb63 (diff)
downloadcrupest-1ecfd0ab7f1f511268fd6404dbc110c3c277b48c.tar.gz
crupest-1ecfd0ab7f1f511268fd6404dbc110c3c277b48c.tar.bz2
crupest-1ecfd0ab7f1f511268fd6404dbc110c3c277b48c.zip
import(life): IMPORT crupest/life COMPLETE.
Diffstat (limited to 'works/life/compile-principle-experiment/4/syn.y')
-rw-r--r--works/life/compile-principle-experiment/4/syn.y39
1 files changed, 39 insertions, 0 deletions
diff --git a/works/life/compile-principle-experiment/4/syn.y b/works/life/compile-principle-experiment/4/syn.y
new file mode 100644
index 0000000..ebe113f
--- /dev/null
+++ b/works/life/compile-principle-experiment/4/syn.y
@@ -0,0 +1,39 @@
+%{
+#include "main.h"
+#include <stdio.h>
+#define code2(c1,c2) code(c1); code(c2)
+#define code3(c1,c2,c3) code(c1); code(c2); code(c3)
+%}
+%union {
+ Symbol *sym; /* symbol table pointer */
+ double val;
+ Inst *inst; /* machine instruction */
+}
+%token <sym> NUMBER VAR BLTIN UNDEF
+%right '='
+%left '+' '-'
+%left '*' '/'
+%left UNARYMINUS
+%right '^' /* exponentiation */
+%%
+list: /* nothing */
+ | list '\n'
+ | list asgn '\n' { code2(mypop, STOP); return 1; }
+ | list expr '\n' { code2(print, STOP); return 1; }
+ | list error '\n' { yyerrok; }
+ ;
+asgn: VAR '=' expr { code3(varpush,(Inst)$1,assign); }
+ ;
+expr: NUMBER { code2(constpush, (Inst)$1); }
+ | VAR { code3(varpush, (Inst)$1, eval); }
+ | asgn
+ | BLTIN '(' expr ')' { code2(bltin, (Inst)$1->value.ptr); }
+ | '(' expr ')'
+ | expr '+' expr { code(add); }
+ | expr '-' expr { code(sub); }
+ | expr '*' expr { code(mul); }
+ | expr '/' expr { code(mydiv); }
+ | expr '^' expr { code(power); }
+ | '-' expr %prec UNARYMINUS { code(negate); }
+ ;
+%%