mirror of
https://github.com/i-net-software/JWebAssembly.git
synced 2025-03-15 02:44:47 +01:00
improve wat parser
This commit is contained in:
parent
b934de9556
commit
6d4bf1d347
@ -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;
|
||||
}
|
||||
}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user