Skip to content

Commit 6aa7be5

Browse files
committed
fix(translate): improve error handling for local variables edge cases
1 parent 8f33015 commit 6aa7be5

4 files changed

Lines changed: 24 additions & 1 deletion

File tree

lib/Translation/ForthToMLIR/ForthToMLIR.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1028,6 +1028,11 @@ LogicalResult ForthParser::parseBody(Value &stack) {
10281028
Value step = popOp.getValue();
10291029
emitLoopEnd(loc, ctx, step, stack);
10301030

1031+
//=== { outside word definition ===
1032+
} else if (currentToken.text == "{") {
1033+
return emitError(
1034+
"local variables can only be declared inside a word definition");
1035+
10311036
//=== Normal word ===
10321037
} else {
10331038
Value newStack = emitOperation(currentToken.text, stack, loc);
@@ -1098,7 +1103,9 @@ LogicalResult ForthParser::parseLocals(Value &stack) {
10981103
consume();
10991104
}
11001105

1101-
// Skip '--' and output names until '}'
1106+
// Skip '--' and output names until '}'. Per ANS Forth, output names are
1107+
// documentation-only and have no semantic effect; we intentionally ignore
1108+
// them.
11021109
if (currentToken.kind == Token::Kind::Word && currentToken.text == "--") {
11031110
consume(); // consume '--'
11041111
while (currentToken.kind != Token::Kind::EndOfFile) {
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
\ RUN: %not %warpforth-translate --forth-to-mlir %s 2>&1 | %FileCheck %s
2+
\ CHECK: local variables can only be declared inside a word definition
3+
\! kernel main
4+
{ x y -- }
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
\ RUN: %not %warpforth-translate --forth-to-mlir %s 2>&1 | %FileCheck %s
2+
\ CHECK: local variable name 'DATA' conflicts with parameter name
3+
\! kernel main
4+
\! param DATA i64[256]
5+
: BAD { data -- } data ;
6+
BAD
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
\ RUN: %not %warpforth-translate --forth-to-mlir %s 2>&1 | %FileCheck %s
2+
\ CHECK: local variable name 'BUF' conflicts with shared memory name
3+
\! kernel main
4+
\! shared BUF i64[64]
5+
: BAD { buf -- } buf ;
6+
BAD

0 commit comments

Comments
 (0)