improve wat parser

This commit is contained in:
Volker Berlin 2019-07-25 16:50:38 +02:00
parent b934de9556
commit 6d4bf1d347
2 changed files with 39 additions and 1 deletions

View File

@ -168,6 +168,24 @@ public class WatParser extends WasmCodeBuilder {
case "return":
addBlockInstruction( WasmBlockOperator.RETURN, null, javaCodePos, lineNumber );
break;
case "if":
Object data = ValueType.empty;
if( "(".equals( get( tokens, i+1 ) ) ) {
i++;
if( "result".equals( get( tokens, ++i ) ) && ")".equals( get( tokens, ++i + 1) ) ) {
data = ValueType.valueOf( get( tokens, i++ ) );
} else {
throw new WasmException( "Unknown WASM token: " + get( tokens, i-1 ), lineNumber );
}
}
addBlockInstruction( WasmBlockOperator.IF, data, javaCodePos, lineNumber );
break;
case "else":
addBlockInstruction( WasmBlockOperator.ELSE, null, javaCodePos, lineNumber );
break;
case "end":
addBlockInstruction( WasmBlockOperator.END, null, javaCodePos, lineNumber );
break;
default:
throw new WasmException( "Unknown WASM token: " + tok, lineNumber );
}
@ -227,10 +245,20 @@ public class WatParser extends WasmCodeBuilder {
case '\n':
case '\r':
case '\t':
case '(':
case ')':
if( off < i ) {
tokens.add( wat.substring( off, i ) );
}
off = i + 1;
switch(ch) {
case '(':
tokens.add( "(" );
break;
case ')':
tokens.add( ")" );
break;
}
break;
}
}

View File

@ -263,7 +263,17 @@ public class WatParserTest {
@Test
public void return_() throws IOException {
test( "return\n" );
test( "return" );
}
@Test
public void ifElseEnd() throws IOException {
test( "if else end" );
}
@Test
public void ifElseEndI32() throws IOException {
test( "if (result i32) else end" );
}
@Test