summaryrefslogtreecommitdiff
path: root/s1
diff options
context:
space:
mode:
authorrodri <rgl@antares-labs.eu>2024-08-31 16:06:55 +0000
committerrodri <rgl@antares-labs.eu>2024-08-31 16:06:55 +0000
commit51f3bfd0e73d5e78d9fe71e6106a2a883619e18f (patch)
tree611536307d7cb4242304d6ec9df280043ba1de4e /s1
downloadrhoc-51f3bfd0e73d5e78d9fe71e6106a2a883619e18f.tar.gz
rhoc-51f3bfd0e73d5e78d9fe71e6106a2a883619e18f.tar.bz2
rhoc-51f3bfd0e73d5e78d9fe71e6106a2a883619e18f.zip
stage 1.
Diffstat (limited to 's1')
-rw-r--r--s1/mkfile10
-rw-r--r--s1/rhoc.y88
2 files changed, 98 insertions, 0 deletions
diff --git a/s1/mkfile b/s1/mkfile
new file mode 100644
index 0000000..ec4ce25
--- /dev/null
+++ b/s1/mkfile
@@ -0,0 +1,10 @@
+</$objtype/mkfile
+
+TARG=rhoc1
+OFILES=\
+ y.tab.$O
+YFILES=\
+ rhoc.y
+BIN=/$objtype/bin
+
+</sys/src/cmd/mkone
diff --git a/s1/rhoc.y b/s1/rhoc.y
new file mode 100644
index 0000000..4015fc2
--- /dev/null
+++ b/s1/rhoc.y
@@ -0,0 +1,88 @@
+%{
+#define YYSTYPE double
+%}
+%token NUMBER
+%left '+' '-'
+%left '*' '/'
+%left UNMIN
+%%
+list: /* ε */
+ | list '\n'
+ | list expr '\n' { print("\t%.8g\n", $2); }
+ ;
+expr: NUMBER
+ | '-' expr %prec UNMIN { $$ = -$2; }
+ | expr '+' expr { $$ = $1 + $3; }
+ | expr '-' expr { $$ = $1 - $3; }
+ | expr '*' expr { $$ = $1 * $3; }
+ | expr '/' expr { $$ = $1 / $3; }
+ | expr '%' expr { $$ = fmod($1, $3); }
+ | expr '(' expr ')' { $$ = $1 * $3; }
+ | '(' expr ')' { $$ = $2; }
+ ;
+%%
+#include <u.h>
+#include <libc.h>
+#include <ctype.h>
+#include <bio.h>
+
+Biobuf *bin;
+int lineno;
+int prompt;
+
+int yyparse(void);
+
+void
+yyerror(char *msg)
+{
+ fprint(2, "%s at line %d\n", msg, lineno);
+}
+
+int
+yylex(void)
+{
+ int c;
+
+ if(prompt){
+ print("%d: ", lineno);
+ prompt--;
+ }
+ while((c = Bgetc(bin)) == ' ' || c == '\t')
+ ;
+ if(c == '.' || isdigit(c)){
+ Bungetc(bin);
+ Bgetd(bin, &yylval);
+ return NUMBER;
+ }
+ if(c == '\n'){
+ lineno++;
+ prompt++;
+ }
+ return c;
+}
+
+void
+usage(void)
+{
+ fprint(2, "usage: %s\n", argv0);
+ exits("usage");
+}
+
+void
+main(int argc, char *argv[])
+{
+ ARGBEGIN{
+ default: usage();
+ }ARGEND;
+
+ if(argc > 0)
+ usage();
+ bin = Bfdopen(0, OREAD);
+ if(bin == nil)
+ sysfatal("Bfdopen: %r");
+ lineno++;
+ prompt++;
+ yyparse();
+ Bterm(bin);
+ exits(0);
+}