From d491136eeb94b748225be50bdcc86c74cdbd154e Mon Sep 17 00:00:00 2001 From: Thomas Lively Date: Thu, 21 Sep 2023 12:42:07 -0700 Subject: [Parser] Parse if-else in the new wat parser and IRBuilder (#5963) Parse both the straight-line and folded versions of if, including the abbreviations that allow omitting the else clause. In the IRBuilder, generalize the scope stack to be able to track scopes other than blocks and add methods for visiting the beginnings of ifs and elses. --- src/parser/contexts.h | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) (limited to 'src/parser/contexts.h') diff --git a/src/parser/contexts.h b/src/parser/contexts.h index 210945e8d..dae938f7f 100644 --- a/src/parser/contexts.h +++ b/src/parser/contexts.h @@ -312,7 +312,12 @@ struct NullInstrParserCtx { InstrT makeBlock(Index, std::optional, BlockTypeT) { return Ok{}; } - InstrT finishBlock(Index, InstrsT) { return Ok{}; } + template + InstrT makeIf(Index, std::optional, BlockTypeT) { + return Ok{}; + } + InstrT visitEnd(Index, InstrsT) { return Ok{}; } + InstrT visitElse(Index) { return Ok{}; } InstrT makeUnreachable(Index) { return Ok{}; } InstrT makeNop(Index) { return Ok{}; } @@ -994,10 +999,20 @@ struct ParseDefsCtx : TypeParserCtx { type.getSignature().results)); } - Result<> finishBlock(Index pos, InstrsT) { + Result<> makeIf(Index pos, std::optional label, HeapType type) { + // TODO: validate labels? + // TODO: Move error on input types to here? + return withLoc( + pos, + irBuilder.makeIf(label ? *label : Name{}, type.getSignature().results)); + } + + Result<> visitEnd(Index pos, InstrsT) { return withLoc(pos, irBuilder.visitEnd()); } + Result<> visitElse(Index pos) { return withLoc(pos, irBuilder.visitElse()); } + Result<> makeUnreachable(Index pos) { return withLoc(pos, irBuilder.makeUnreachable()); } -- cgit v1.2.3