summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
Diffstat (limited to 'main.c')
-rw-r--r--main.c62
1 files changed, 62 insertions, 0 deletions
diff --git a/main.c b/main.c
new file mode 100644
index 0000000..04264b1
--- /dev/null
+++ b/main.c
@@ -0,0 +1,62 @@
+#include <u.h>
+#include <libc.h>
+#include <bio.h>
+
+#include "apl9.h"
+
+Rune *prompt(Rune *);
+Datum *evalline(Rune *);
+
+Biobuf *stdin;
+
+void
+main(void)
+{
+ int off = 0;
+ stdin = Bfdopen(0, OREAD);
+
+ while(!off){
+ Rune *input = prompt(L"\t");
+ Datum *result = evalline(input);
+ if(result == nil){
+ if(errormsg == nil)
+ off = 1;
+ else
+ print("ERROR: %S\n", errormsg);
+ }else{
+ ppdatum(*result);
+ free(result);
+ }
+ }
+ exits(nil);
+}
+
+Rune *
+prompt(Rune *pr)
+{
+ Rune line[MAX_LINE_LENGTH];
+
+ print("%S",pr);
+
+ int i = 0;
+ line[0] = 0;
+ while(i == 0 || line[i-1] != '\n')
+ line[i++] = Bgetrune(stdin);
+ line[i-1] = 0;
+ return runestrdup(line);
+}
+
+Datum *
+evalline(Rune *line)
+{
+ int ntoks;
+ Datum *tokens = lexline(line, &ntoks);
+ print("Evaluating line (n=%d): %S\n", ntoks, ppdatums(tokens, ntoks));
+ Datum *result = eval(tokens, &ntoks);
+ if(ntoks == 1)
+ return result;
+ else{
+ free(tokens);
+ return nil;
+ }
+} \ No newline at end of file