Skip to content

Commit

Permalink
improve wat parser for native support. #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Horcrux7 committed Mar 12, 2019
1 parent 2a72dfa commit 3d5a986
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 10 deletions.
41 changes: 35 additions & 6 deletions src/de/inetsoftware/jwebassembly/watparser/WatParser.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2018 -2019 Volker Berlin (i-net software)
Copyright 2018 - 2019 Volker Berlin (i-net software)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,14 +37,13 @@
*/
public class WatParser extends WasmCodeBuilder {

public WatParser() {
}

/**
* Parse the given wasm text format and generate a list of WasmInstuctions
*
* @param wat
* the text format content of a function
* @param lineNumber
* the line number for an error message
*/
public void parse( String wat, int lineNumber ) {
try {
Expand All @@ -70,14 +69,44 @@ public void parse( String wat, int lineNumber ) {
case "i32.add":
addNumericInstruction( NumericOperator.add, ValueType.i32, javaCodePos );
break;
case "i32.trunc_sat_f32_s":
addConvertInstruction( ValueTypeConvertion.f2i, javaCodePos );
break;
case "i64.extend_i32_s":
addConvertInstruction( ValueTypeConvertion.i2l, javaCodePos );
break;
case "i64.trunc_sat_f64_s":
addConvertInstruction( ValueTypeConvertion.d2l, javaCodePos );
break;
case "f32.convert_i32_s":
addConvertInstruction( ValueTypeConvertion.i2f, javaCodePos );
break;
case "f32.div":
addNumericInstruction( NumericOperator.div, ValueType.f32, javaCodePos );
break;
case "f32.max":
addNumericInstruction( NumericOperator.max, ValueType.f32, javaCodePos );
break;
case "f32.mul":
addNumericInstruction( NumericOperator.mul, ValueType.f32, javaCodePos );
break;
case "f32.sub":
addNumericInstruction( NumericOperator.sub, ValueType.f32, javaCodePos );
break;
case "f64.convert_i64_s":
addConvertInstruction( ValueTypeConvertion.l2d, javaCodePos );
break;
case "f64.div":
addNumericInstruction( NumericOperator.div, ValueType.f64, javaCodePos );
break;
case "f64.max":
addNumericInstruction( NumericOperator.max, ValueType.f64, javaCodePos );
break;
case "i64.extend_i32_s":
addConvertInstruction( ValueTypeConvertion.i2l, javaCodePos );
case "f64.mul":
addNumericInstruction( NumericOperator.mul, ValueType.f64, javaCodePos );
break;
case "f64.sub":
addNumericInstruction( NumericOperator.sub, ValueType.f64, javaCodePos );
break;
// case "call":
// addCallInstruction( method, javaCodePos );
Expand Down
63 changes: 59 additions & 4 deletions test/de/inetsoftware/jwebassembly/module/WatParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,24 +102,74 @@ public void i32_add() throws IOException {
test( "i32.add" );
}

@Test
public void i32_const() throws IOException {
test( " i32.const -7 " );
}

@Test
public void i32_trunc_sat_f32_s() throws IOException {
test( "i32.trunc_sat_f32_s" );
}

@Test
public void i64_extend_i32_s() throws IOException {
test( "i64.extend_i32_s" );
}

@Test
public void i64_trunc_sat_f64_s() throws IOException {
test( "i64.trunc_sat_f64_s" );
}

@Test
public void f32_convert_i32_s() throws IOException {
test( "f32.convert_i32_s" );
}

@Test
public void f32_div() throws IOException {
test( "f32.div" );
}

@Test
public void f32_max() throws IOException {
test( "f32.max" );
}

@Test
public void f32_mul() throws IOException {
test( "f32.mul" );
}

@Test
public void f32_sub() throws IOException {
test( "f32.sub" );
}

@Test
public void f64_convert_i64_s() throws IOException {
test( "f64.convert_i64_s" );
}

@Test
public void f64_div() throws IOException {
test( "f64.div" );
}

@Test
public void f64_max() throws IOException {
test( "f64.max" );
}

@Test
public void i32_const() throws IOException {
test( " i32.const -7 " );
public void f64_mul() throws IOException {
test( "f64.mul" );
}

@Test
public void i64_extend_i32_s() throws IOException {
test( "i64.extend_i32_s" );
public void f64_sub() throws IOException {
test( "f64.sub" );
}

@Test
Expand All @@ -131,4 +181,9 @@ public void return_() throws IOException {
public void errorMissingToken() throws IOException {
testError( "i32.const", "Missing Token in wasm text format after token: i32.const" );
}

@Test
public void errorUnknownToken() throws IOException {
testError( "foobar", "Unknown WASM token: foobar" );
}
}

0 comments on commit 3d5a986

Please sign in to comment.