summaryrefslogtreecommitdiff
path: root/lexer.c
diff options
context:
space:
mode:
authorPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-12 20:41:43 +0000
committerPeter Mikkelsen <petermikkelsen10@gmail.com>2022-01-12 20:41:43 +0000
commit32713db68c6f5509e9f910bd2893d207d1585ef7 (patch)
tree1035252407d2a1d1a1b4cb4e9397f5f599dc77ae /lexer.c
parent55ca6248ffff6d9e1669fc49fe9de8489adeb7f9 (diff)
Rework parenthesis, so they are build by the lexer, but evaluated by the evaluator/parser.
Diffstat (limited to 'lexer.c')
-rw-r--r--lexer.c32
1 files changed, 26 insertions, 6 deletions
diff --git a/lexer.c b/lexer.c
index 1c35f91..ceccf2f 100644
--- a/lexer.c
+++ b/lexer.c
@@ -23,10 +23,8 @@ lexline(Rune *line)
if(isspacerune(line[offset])){
offset++;
continue;
- }else if(runestrchr(L"()[]←⋄⍝", line[offset])){
+ }else if(runestrchr(L"[]←⋄⍝", line[offset])){
switch(line[offset]){
- case '(': stmt->toks[stmt->ntoks].tag = LParTag; break;
- case ')': stmt->toks[stmt->ntoks].tag = RParTag; break;
case '[': stmt->toks[stmt->ntoks].tag = LBracketTag; break;
case ']': stmt->toks[stmt->ntoks].tag = RBracketTag; break;
case L'←': stmt->toks[stmt->ntoks].tag = ArrowTag; break;
@@ -34,22 +32,44 @@ lexline(Rune *line)
case L'⍝': goto end;
}
offset++;
- }else if(line[offset] == L'{'){
+ }else if(line[offset] == '{'){
Rune buf[MAX_LINE_LENGTH];
Rune *p = buf;
offset++;
- while(line[offset] != L'}' && offset < len){
+ while(line[offset] != '}' && offset < len){
*p = line[offset];
p++;
offset++;
}
- if(line[offset] != L'}')
+ if(line[offset] != '}')
goto syntax_error;
*p = 0;
offset++;
stmt->toks[stmt->ntoks].tag = FunctionTag;
stmt->toks[stmt->ntoks].func.type = FunctypeDfn;
stmt->toks[stmt->ntoks].func.dfn = runestrdup(buf);
+ }else if(line[offset] == '('){
+ int unclosed = 1;
+ Rune buf[MAX_LINE_LENGTH];
+ Rune *p = buf;
+ offset++;
+ while((line[offset] != ')' || unclosed > 1) && offset < len){
+ if(line[offset] == '(')
+ unclosed++;
+ else if(line[offset] == ')')
+ unclosed--;
+ *p = line[offset];
+ p++;
+ offset++;
+ }
+ if(line[offset] != ')')
+ goto syntax_error;
+ *p = 0;
+ offset++;
+ stmt->toks[stmt->ntoks].tag = LParTag;
+ stmt->toks[stmt->ntoks].stmt = *lexline(buf);
+ stmt->ntoks++;
+ stmt->toks[stmt->ntoks].tag = RParTag;
}else if(p = runestrchr(primfuncnames, line[offset])){
stmt->toks[stmt->ntoks].tag = FunctionTag;
stmt->toks[stmt->ntoks].func.type = FunctypePrim;