commit f901bf766d5599930a55e9fbe766e8168ba4a2f4
parent b30dc248b357b4afd8055d55e704bf2447140d15
Author: thing1 <l.standen@posteo.com>
Date: Wed, 15 Oct 2025 08:43:09 +0100
replaced the user input with a shell
Diffstat:
| A | TODO.md | | | 5 | +++++ |
| M | expr.c | | | 54 | ++++++++++++++++++++++++++++++++++++++++-------------- |
2 files changed, 45 insertions(+), 14 deletions(-)
diff --git a/TODO.md b/TODO.md
@@ -0,0 +1,5 @@
+- make it so the lexer can lex an EOF, this should allow for both the input and shell to be from stdin
+- make a cleanup function that free's the tree
+- make a function that makes a truth table
+ - make a function that increments all the vals by looping through and making a carry
+- make a function that can take a tree and print it as algebra
diff --git a/expr.c b/expr.c
@@ -1,18 +1,19 @@
#include <stdio.h>
#include <stdbool.h>
+#include <stdlib.h>
+#include <ctype.h>
+#include <string.h>
+
#include "expr.h"
+#define SAME(s1, s2) (strcmp(s1, s2) == 0)
+
extern FILE *yyin;
extern void yyparse();
extern btree *program;
extern char vars[26];
bool vals[26];
-void
-flush() {
- while (getchar() != '\n') ;
-}
-
bool
eval(btree *p) {
bool l, r;
@@ -33,6 +34,19 @@ eval(btree *p) {
}
}
+bool
+read_bool(char *s) {
+ if (s[1] != '\n') goto err;
+
+ switch (*s) {
+ case '0': return false;
+ case '1': return true;
+ }
+err:
+ fprintf(stderr, "expected 1 or 0\n");
+ return false;
+}
+
int
main(int argc, char **argv) {
yyin = stdin;
@@ -45,17 +59,29 @@ main(int argc, char **argv) {
yyparse();
- for (int i = 0; i < 26; i++) {
- if (vars[i]) {
- printf("%c = ", vars[i]);
- scanf("%d", &vals[i]);
- flush();
+ char line[128];
+ bool res = eval(program);
+
+ for (;;) {
+ printf("> ");
+ fgets(line, 128, stdin);
+ if (isupper(line[0])) {
+ vals[(int)line[0] - 65] = read_bool(line + 2);
+ }
+ else if (SAME(line, "s\n"))
+ printf("res = %s\n", (res) ? "true" : "false");
+ else if (SAME(line, "q\n"))
+ break;
+ else if (SAME(line, "p\n")) {
+ for (int i = 0; i < 26; i++) {
+ if (vars[i])
+ printf("%c = %s\n", vars[i], (vals[i]) ? "true" : "false");
+ }
}
+
+ else
+ fprintf(stderr, "?\n");
}
-
- bool res = eval(program);
-
- printf("res = %s\n", (res) ? "true" : "false");
fclose(yyin);
return 0;