From e8e6feeb95cdc3b81a2c17b5a342a3d0b170ccb4 Mon Sep 17 00:00:00 2001 From: Peter Mikkelsen Date: Tue, 22 Feb 2022 12:29:23 +0000 Subject: Prepare for error guards --- apl9.h | 1 + eval.c | 28 ++++++++++++++++++++-------- lexer.c | 7 +++++++ 3 files changed, 28 insertions(+), 8 deletions(-) diff --git a/apl9.h b/apl9.h index d8afcb1..d115ea3 100644 --- a/apl9.h +++ b/apl9.h @@ -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; diff --git a/eval.c b/eval.c index 58a745b..4131871 100644 --- a/eval.c +++ b/eval.c @@ -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; } diff --git a/lexer.c b/lexer.c index 0024320..4e9a4e4 100644 --- a/lexer.c +++ b/lexer.c @@ -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; -- cgit v1.2.3