diff --git a/src/Language/Wasm/Parser.y b/src/Language/Wasm/Parser.y index 4270abe..d3d7b08 100644 --- a/src/Language/Wasm/Parser.y +++ b/src/Language/Wasm/Parser.y @@ -296,13 +296,6 @@ EOF { Lexeme _ EOF } %% -functype :: { FuncType } - : '(' 'func' params_results { $3 } - -params_results :: { FuncType } - : ')' { FuncType [] [] } - | '(' paramsresultstypeuse ')' { $2 } - name :: { TL.Text } : string { $1 } @@ -538,9 +531,6 @@ plaininstr :: { PlainInstr } | 'f32.reinterpret/i32' { FReinterpretI BS32 } | 'f64.reinterpret/i64' { FReinterpretI BS64 } -typedef :: { TypeDef } - : 'type' opt(ident) functype ')' { TypeDef $2 $3 } - typeuse :: { TypeUse } : '(' typeuse1 { $2 } | {- empty -} { AnonimousTypeUse $ FuncType [] [] } @@ -553,6 +543,28 @@ typedtypeuse :: { Maybe FuncType } : '(' paramsresultstypeuse { Just $2 } | {- empty -} { Nothing } +typedef :: { TypeDef } + : 'type' opt(ident) functype ')' { TypeDef $2 $3 } + +functype :: { FuncType } + : '(' 'func' params_results { $3 } + +params_results :: { FuncType } + : ')' { emptyFuncType } + | '(' params_results1 { $2 } + +params_results1 :: { FuncType } + : 'param' list(valtype) ')' params_results { mergeFuncType (FuncType (map (ParamType Nothing) $2) []) $4 } + | 'param' ident valtype ')' params_results { mergeFuncType (FuncType [ParamType (Just $2) $3] []) $5 } + | results1 { $1 } + +results :: { FuncType } + : ')' { emptyFuncType } + | '(' results1 { $2 } + +results1 :: { FuncType } + : 'result' list(valtype) ')' results { mergeFuncType (FuncType [] $2) $4 } + paramsresultstypeuse :: { FuncType } : paramsresultstypeuse '(' paramsresulttypeuse { mergeFuncType $1 $3 } | paramsresulttypeuse { $1 } diff --git a/src/Language/Wasm/Validate.hs b/src/Language/Wasm/Validate.hs index a783542..520ab0e 100644 --- a/src/Language/Wasm/Validate.hs +++ b/src/Language/Wasm/Validate.hs @@ -389,7 +389,7 @@ isFunctionValid Function {funcType, locals, body} mod@Module {types} = let ctx = ctxFromModule (params ++ locals) [r] r mod in case runChecker ctx $ getExpressionType body of Left err -> err - Right arr -> if arr == (empty ==> results) then Valid else TypeMismatch + Right arr -> if arr == (empty ==> results) || arr == (Any ==> Any) then Valid else TypeMismatch functionsShouldBeValid :: Validator functionsShouldBeValid mod@Module {functions} = diff --git a/tests/Test.hs b/tests/Test.hs index 7356770..24e2629 100644 --- a/tests/Test.hs +++ b/tests/Test.hs @@ -31,7 +31,7 @@ compile file = do main :: IO () main = do files <- Directory.listDirectory "tests/samples" - -- let files = ["endianess.wast"] + -- let files = ["func.wast"] -- compile "fact.wast" syntaxTestCases <- (`mapM` files) $ \file -> do content <- LBS.readFile $ "tests/samples/" ++ file