diff options
author | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2022-02-22 12:29:23 +0000 |
---|---|---|
committer | Peter Mikkelsen <petermikkelsen10@gmail.com> | 2022-02-22 12:29:23 +0000 |
commit | e8e6feeb95cdc3b81a2c17b5a342a3d0b170ccb4 (patch) | |
tree | 8952eb98e03c3153143e8d5598988ef537153135 | |
parent | 67cbbe89c1b876b88557def75ec376d9f3330588 (diff) |
Prepare for error guards
-rw-r--r-- | apl9.h | 1 | ||||
-rw-r--r-- | eval.c | 28 | ||||
-rw-r--r-- | lexer.c | 7 |
3 files changed, 28 insertions, 8 deletions
@@ -120,6 +120,7 @@ struct Array struct Statement { int ntoks; + int errorguard; /* if guard != nil, is the guard an error guard? */ Datum **toks; Statement *guard; Statement *next; @@ -113,24 +113,36 @@ retry: int guardOK = 1; if(stmt->toks[0]->tag != ArrayTag) guardOK = 0; - else if(GetSize(stmt->toks[0]->array) != 1) + else if(!stmt->errorguard && GetSize(stmt->toks[0]->array) != 1) + guardOK = 0; + else if(stmt->errorguard && GetSize(stmt->toks[0]->array) < 1) guardOK = 0; else if(GetType(stmt->toks[0]->array) != AtypeInt) guardOK = 0; - else if(stmt->toks[0]->array->intdata[0] != 0 && stmt->toks[0]->array->intdata[0] != 1) + else if(!stmt->errorguard && stmt->toks[0]->array->intdata[0] != 0 && stmt->toks[0]->array->intdata[0] != 1) guardOK = 0; - if(!guardOK) - throwerror(L"Guard expected single valued boolean", EDomain); - else{ + if(!guardOK){ + if(!stmt->errorguard) + throwerror(L"Guard expected single valued boolean", EDomain); + else + throwerror(L"Error guard expected an array of error numbers", EDomain); + }else{ stop = 0; - if(stmt->toks[0]->array->intdata[0] == 1) - return eval(stmt->guard, toplevel); + if(!stmt->errorguard){ + if(stmt->toks[0]->array->intdata[0] == 1) + return eval(stmt->guard, toplevel); + }else + print("Not registering error guard for error codes: %S\n", ppdatum(stmt->toks[0])); } } if(stmt->next && !stop) return eval(stmt->next, toplevel); - else + else if(!stmt->guard) return stmt->toks[0]; + else{ + throwerror(L"No value produced", EValue); + return nil; + } }else return nil; } @@ -88,11 +88,18 @@ lexline(InputStream *input, int toplevel) }else if(!toplevel && peek == ':'){ Rune buf[MAX_LINE_LENGTH]; Rune *p = buf; + int errorguard = 0; + if(getrune(input) == ':') + errorguard = 1; + else + ungetrune(input); + while((peek = getrune(input)) != L'⋄' && peek != '\n' && !inputEOF(input)) *p++ = peek; *p = 0; ungetrune(input); stmt->guard = lexlinestr(buf, toplevel); + stmt->errorguard = errorguard; stmt->ntoks--; }else if(peek == '{'){ int unclosed = 1; |