diff --git a/compiler/compiler.go b/compiler/compiler.go index 94189c5..ec1c7e0 100644 --- a/compiler/compiler.go +++ b/compiler/compiler.go @@ -169,6 +169,12 @@ func Compile(ast []types.AstNode) Module { // MAGIC and VERSION don't change until a newer version of WebAssembly gets released module = append(module, defaults.MAGIC...) module = append(module, defaults.VERSION...) + + // if the module is empty return + if node.Expression.Value == nil { + return module + } + case texts.FuncStatement: // num of types (i32, f32, i64, f64) inside the function functionType = append(functionType, sectionData{0x01}...) diff --git a/compiler/parser.go b/compiler/parser.go index 89acc14..5179cbb 100644 --- a/compiler/parser.go +++ b/compiler/parser.go @@ -54,7 +54,12 @@ func Parser(tokens []types.Token) []types.AstNode { currentToken := iterator.next(tokens) + // If the module is empty return the Ast with only the module if currentToken.done { + nodes = append(nodes, types.AstNode{ + Type: texts.ModuleStatement, + Expression: types.ExpressionNode{}, + }) return nodes } @@ -88,8 +93,9 @@ func parseStatement(currentToken *iteratorEmulatorStruct, eatToken func(val stri case "module": eatToken("module") return types.AstNode{ - Type: texts.ModuleStatement, - Expression: types.ExpressionNode{}, + Type: texts.ModuleStatement, + // Check if the module is empty by inspecting the next node + Expression: parseExpression(currentToken, eatToken, index), } case "func": @@ -222,7 +228,12 @@ func parseExpression(currentToken *iteratorEmulatorStruct, eatToken func(val str return log - } + // Make node aware of which node is coming after + default: + log := types.ExpressionNode{ + Value: currentToken.token.Value, + } - return types.ExpressionNode{} + return log + } } diff --git a/example/js/index.js b/example/js/index.js index e98a6fc..8d40fbc 100644 --- a/example/js/index.js +++ b/example/js/index.js @@ -85,14 +85,25 @@ const runLunaAddition = async () => { try { const wasmer = await WebAssembly.instantiate(wasm); - const fn = Object.keys(wasmer.instance.exports)[0] - moduleContainer.innerHTML = `
Compiled successfully!\nExported function ${fn}
\n` + const fn = Object.keys(wasmer.instance.exports)[0]; + + // if module is empty, then make input section disappear + if (typeof wasmer.instance.exports[fn] == "undefined" && wasm.length == 8) { + moduleContainer.innerHTML = `Module is empty
\n` + input1.setAttribute("disabled", "true") + input2.setAttribute("disabled", "true") + btn.setAttribute('disabled', "true") + } else { + moduleContainer.innerHTML = `Compiled successfully!\nExported function ${fn}
\n` + input1.removeAttribute("disabled") + input2.removeAttribute("disabled") + btn.removeAttribute('disabled') + } for (const hex of wasm) { moduleContainer.innerHTML += `${hex}
` } - btn.removeAttribute('disabled') btn.addEventListener('click', () => { const n1 = Number(input1.value); diff --git a/example/main.wasm b/example/main.wasm index 763e8dd..b00b606 100755 Binary files a/example/main.wasm and b/example/main.wasm differ