diff options
author | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2022-01-08 22:45:00 +0000 |
---|---|---|
committer | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2022-01-08 22:45:00 +0000 |
commit | 1ef3119fe613823a2145126c58948361ca7d3cd8 (patch) | |
tree | 5252d957ae512e1a727c9dec2b31e7b2a1d63e56 /lexer.c | |
parent | 214cdacca02552649d63f9045fdb8a17cfbb6fca (diff) |
Add initial code, just to get started
Diffstat (limited to 'lexer.c')
-rw-r--r-- | lexer.c | 65 |
1 files changed, 65 insertions, 0 deletions
@@ -0,0 +1,65 @@ +#include <u.h> +#include <libc.h> +#include <bio.h> + +#include "apl9.h" + +Rune primfuncnames[] = L"+-×÷*⍟⌹○!?|⌈⌊⊥⊤⊣⊢=≠≤<>≥≡≢∨∧⍲⍱↑↓⊂⊃⊆⌷⍋⍒⍳⍸∊⍷∪∩~,⍪⍴⌽⊖⍉⍎⍕"; +Rune primmonopnames[] = L"¨⍨⌸⌶&"; +Rune primdyadopnames[] = L"⍣.∘⍤⍥@⍠⌺"; +Rune primhybridnames[] = L"/\⌿⍀"; + +Datum * +lexline(Rune *line, int *ntoks) +{ + int offset = 0; + int len = runestrlen(line); + Datum *tokens = mallocz(sizeof(Datum) * MAX_LINE_TOKENS, 1); + *ntoks = 0; + while(offset < len){ + if(isspacerune(line[offset])){ + offset++; + continue; + }else if(runestrchr(L"(){}[]", line[offset])){ + switch(line[offset]){ + case '(': tokens[*ntoks].tag = LParTag; break; + case ')': tokens[*ntoks].tag = RParTag; break; + case '{': tokens[*ntoks].tag = LCurlTag; break; + case '}': tokens[*ntoks].tag = RCurlTag; break; + case '[': tokens[*ntoks].tag = LBracketTag; break; + case ']': tokens[*ntoks].tag = RBracketTag; break; + } + tokens[*ntoks].strrep = runesmprint("%C", line[offset]); + offset++; + }else if(runestrchr(primfuncnames, line[offset])){ + tokens[*ntoks].tag = FunctionTag; + tokens[*ntoks].strrep = runesmprint("%C", line[offset]); + offset++; + }else if(runestrchr(primmonopnames, line[offset])){ + tokens[*ntoks].tag = MonadicOpTag; + tokens[*ntoks].strrep = runesmprint("%C", line[offset]); + offset++; + }else if(runestrchr(primdyadopnames, line[offset])){ + tokens[*ntoks].tag = DyadicOpTag; + tokens[*ntoks].strrep = runesmprint("%C", line[offset]); + offset++; + }else if(isdigitrune(line[offset])){ + char buf[64]; + char *p = buf; + while(isdigitrune(line[offset])){ + p += runetochar(p, &line[offset]); + offset++; + } + *p = 0; + tokens[*ntoks].tag = ArrayTag; + tokens[*ntoks].array = mkscalarint(atoll(buf)); + tokens[*ntoks].strrep = runesmprint("%s", buf); + }else{ + print("Can't lex: %S\n", &line[offset]); + *ntoks = 0; + break; + } + (*ntoks)++; + } + return tokens; +}
\ No newline at end of file |