From 69c2d1ec67c1deeab04e248e8076a7dbb40af28f Mon Sep 17 00:00:00 2001 From: Devine Lu Linvega Date: Thu, 4 Apr 2024 09:50:17 -0700 Subject: [PATCH] Tokenizing rules --- src/modal.c | 50 ++++++++++++++++++++++++++++++++++++-------------- test.modal | 1 + 2 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/modal.c b/src/modal.c index 138fed7..9ff9ab5 100644 --- a/src/modal.c +++ b/src/modal.c @@ -39,30 +39,46 @@ static Rule rules[0x100]; static void addrule(FILE *f) { - char c; - printf("Add rule: "); + int depth = 0; + char c, *left, *right; + left = next; while(f && fread(&c, 1, 1, f) && c && c != 0xa) { - printf("%c", c); + if(c == '(') depth++; + if(c == ')') --depth; + if(c == ' ' && !depth) + break; + else + *next++ = c; } - printf("\n"); + *next++ = 0; + + right = next; + while(f && fread(&c, 1, 1, f) && c && c != 0xa) { + if(c == '(') depth++; + if(c == ')') --depth; + if(c == ' ' && !depth) + break; + else + *next++ = c; + } + *next++ = 0; + + printf("rule: %s -> %s\n", left, right); } /* program */ -static char program[0x1000]; +static char program[0x1000], *prog_ = program; static void -addprogram(FILE *f){ +addprogram(FILE *f) +{ char c; - printf("Add program: "); - while(f && fread(&c, 1, 1, f) && c && c != 0xa) { - printf("%c", c); - } - printf("\n"); + while(f && fread(&c, 1, 1, f) && c && c != 0xa) + *prog_++ = c; + *prog_++ = 0x20; } -static int phase; - static void tokenize(char *t, FILE *f) { @@ -90,6 +106,12 @@ walk(FILE *f) return 1; } +static void +display(Rule *r, char *p) +{ + printf("Program: %s\n", p); +} + static int eval(char *path) { @@ -97,7 +119,7 @@ eval(char *path) if(!(f = fopen(path, "r"))) return !printf("Invalid file: %s\n", path); walk(f); - + display(rules, program); fclose(f); return 1; } diff --git a/test.modal b/test.modal index 4ad83e9..03d47e7 100644 --- a/test.modal +++ b/test.modal @@ -1,4 +1,5 @@ <> (dup ?x) (?x ?x done) <> hello bye +<> (a (b (c))) (end) (dup hello) \ No newline at end of file