diff --git a/Makefile b/Makefile index 260fe35..7ccbb41 100644 --- a/Makefile +++ b/Makefile @@ -15,8 +15,8 @@ WASM2C := $(WABT_DIR)/bin/wasm2c # %.exe: %.c # cc $^ -o $@ -I $(WABT_DIR)/wasm2c -server: venv/.done - venv/bin/python -m http.server +examples: venv/.done $(subst .py,.wasm,$(wildcard examples/*.py)) + venv/bin/python3 -m http.server --directory examples test: venv/.done WAT2WASM=$(WAT2WASM) venv/bin/pytest tests $(TEST_FLAGS) @@ -34,3 +34,5 @@ venv/.done: requirements.txt touch $@ .SECONDARY: # Keep intermediate files + +.PHONY: examples diff --git a/examples/fib.html b/examples/fib.html new file mode 100644 index 0000000..841e8c7 --- /dev/null +++ b/examples/fib.html @@ -0,0 +1,34 @@ + + +Examples - Fibonacci + + +

Fibonacci

+ +Source - WebAssembly + +
+ + + + + + diff --git a/examples/fib.py b/examples/fib.py index ae15091..8536245 100644 --- a/examples/fib.py +++ b/examples/fib.py @@ -1,10 +1,11 @@ -def helper(n: i32, a: i32, b: i32) -> i32: +def helper(n: i64, a: i64, b: i64) -> i64: if n < 1: return a + b return helper(n - 1, a + b, a) -def fib(n: i32) -> i32: +@exported +def fib(n: i64) -> i64: if n == 0: return 0 @@ -14,5 +15,5 @@ def fib(n: i32) -> i32: return helper(n - 1, 0, 1) @exported -def testEntry() -> i32: +def testEntry() -> i64: return fib(40) diff --git a/examples/index.html b/examples/index.html new file mode 100644 index 0000000..0093a41 --- /dev/null +++ b/examples/index.html @@ -0,0 +1,11 @@ + + +Examples + + +

Examples

+ + + diff --git a/py2wasm/compiler.py b/py2wasm/compiler.py index 7563c75..c8fe879 100644 --- a/py2wasm/compiler.py +++ b/py2wasm/compiler.py @@ -43,6 +43,13 @@ I32_OPERATOR_MAP = { # TODO: Introduce UInt32 type '>=': 'ge_s', } +I64_OPERATOR_MAP = { # TODO: Introduce UInt32 type + '<': 'lt_s', + '>': 'gt_s', + '<=': 'le_s', + '>=': 'ge_s', +} + def expression(inp: ourlang.Expression) -> Statements: if isinstance(inp, ourlang.ConstantInt32): yield wasm.Statement('i32.const', str(inp.value)) @@ -79,6 +86,9 @@ def expression(inp: ourlang.Expression) -> Statements: if operator := OPERATOR_MAP.get(inp.operator, None): yield wasm.Statement(f'i64.{operator}') return + if operator := I64_OPERATOR_MAP.get(inp.operator, None): + yield wasm.Statement(f'i64.{operator}') + return if isinstance(inp.type, ourlang.OurTypeFloat32): if operator := OPERATOR_MAP.get(inp.operator, None): yield wasm.Statement(f'f32.{operator}') diff --git a/py2wasm/wasm.py b/py2wasm/wasm.py index 49612cd..75c4567 100644 --- a/py2wasm/wasm.py +++ b/py2wasm/wasm.py @@ -216,7 +216,12 @@ class Function: """ Generates the text version """ - header = ('(export "{}")' if self.exported else '${}').format(self.name) + header = f'${self.name}' # Name for internal use + + if self.exported: + # Name for external use + # TODO: Consider: Make exported('export_name') work + header += f' (export "{self.name}")' for nam, typ in self.params: header += f' (param ${nam} {typ.to_wasm()})'