Skip to content

Commit

Permalink
Fix norm
Browse files Browse the repository at this point in the history
  • Loading branch information
Saverio976 committed Jan 10, 2024
1 parent 841fce5 commit 7ce4072
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
8 changes: 6 additions & 2 deletions lvtc/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ test9 =
] [(WatAST.I32, 1)]
]

main2 :: IO ()
main2 =
print (aSTToWatLike test7)
>> print (watToWasm test9)

main :: IO ()
main =
print (runParser parseInstruction test1)
Expand All @@ -79,5 +84,4 @@ main =
>> print (runParser parseInstruction test6)
>> print (runParser (proceedAlias <$> parseAllExpression) text)
>> print (runParser parseFuncDeclaration test8)
>> print (aSTToWatLike test7)
>> print (watToWasm test9)
>> main2
22 changes: 13 additions & 9 deletions lvtc/src/WatLike.hs
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,14 @@ transformType x = x
registerParams :: FuncDeclare -> FuncDeclare
registerParams (((isExp, fName, [], typ), ins), varsIndex, oName) =
(((isExp, fName, [], transformType typ), ins), varsIndex, oName)
registerParams (((isExp, fName, (pName, pTyp):vParams, typ), ins), varsIndex, oName) =
registerParams (((isExp, fName, (pName, pTyp):vParams, typ)
, ins), varsIndex, oName) =
(((isExp, fName', newParams:vParams', vTyp'), ins), varsIndex'', oName)
where
(varsIndex', indVar) = getRegisterIndex pName varsIndex
(((_, fName', vParams', vTyp'), _), varsIndex'', _) =
registerParams (((isExp, fName, vParams, typ), ins), varsIndex', oName)
registerParams (((isExp, fName, vParams, typ), ins)
, varsIndex', oName)
newParams = (show indVar, transformType pTyp)

registerAllFuncs :: [FuncDeclaration] -> [Index] -> [Index]
Expand All @@ -146,12 +148,12 @@ changeIndexes [] funcsIndex = ([], funcsIndex)
changeIndexes (((isExp, fName, vars, typ), ins):xs) funcsIndex =
(newFunc:funcs, funcsIndex''')
where
(funcsIndex', indFunc) = getRegisterIndex fName funcsIndex
(((_, _, vars', typ'), ins'), varsIndex, _) =
(funcsIndex', iFunc) = getRegisterIndex fName funcsIndex
(((_, _, vars', typ'), ins'), vIndex, _) =
registerParams (((isExp, fName, vars, typ), ins), [], fName)
(ins'', varsIndex'', funcsIndex'') =
modifyAll ins' varsIndex funcsIndex'
newFunc = (((isExp, show indFunc, vars', typ'), ins''), varsIndex'', fName)
(ins'', vIndex'', funcsIndex'') =
modifyAll ins' vIndex funcsIndex'
newFunc = (((isExp, show iFunc, vars', typ'), ins''), vIndex'', fName)
(funcs, funcsIndex''') = changeIndexes xs funcsIndex''

------------------------------------------------------------------------------
Expand Down Expand Up @@ -276,12 +278,14 @@ instructionsToWatLike (x:xs) oldFuncs varsIndex =
funcToWatLike' :: FuncDeclare -> ([FuncDeclare], [Index]) -> FuncDeclare
funcToWatLike' (((isExp, fName, fParams, fRet), []), varsIndex, oName) _ =
(((isExp, fName, fParams, fRet), []), varsIndex, oName)
funcToWatLike' (((isExp, fName, fParams, fRet), ins:inss), varsIndex, oName) oldFuncs =
funcToWatLike' (((isExp, fName, fParams, fRet), ins:inss),
varsIndex, oName) oldFuncs =
(((isExp, fName, fParams, fRet), ins' ++ inss'), varsIndex'', oName)
where
(varsIndex', ins') = instructionToWatLike ins oldFuncs varsIndex
thisFunc = (((isExp, fName, fParams, fRet), inss), varsIndex', oName)
(((_, _, _, _), inss'), varsIndex'', _) = funcToWatLike' thisFunc oldFuncs
(((_, _, _, _), inss'), varsIndex'', _) =
funcToWatLike' thisFunc oldFuncs

funcToWatLike :: FuncDeclare -> WatLikeState -> WatLikeState
funcToWatLike (((isExp, fName, fParams, fRet), fInss), varsIndex, originName)
Expand Down
10 changes: 5 additions & 5 deletions lvtc/src/WatLikeToWat.hs
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ instructionsToWat = concatMap instructionToWat
watLikeToWat :: FuncDeclare -> FuncDef
watLikeToWat (((isExp, fName, params, returnType), ins), vars, originName)
| isBuiltinsFunc fName = getBuiltinWat fName
| otherwise = FuncDef isExp originName indexName paramType retType opcodes varsDecl
| otherwise = FuncDef isExp originName iName pType rType opcodes vDecl
where
indexName = read fName :: Int32
paramType = paramsToTypes params
retType = typeStringToType returnType
varsDecl = groupVarsToDecl $ varsToDecl vars ins params
iName = read fName :: Int32
pType = paramsToTypes params
rType = typeStringToType returnType
vDecl = groupVarsToDecl $ varsToDecl vars ins params
opcodes = instructionsToWat ins

watsLikeToWat :: [FuncDeclare] -> [FuncDef]
Expand Down
3 changes: 2 additions & 1 deletion lvtc/src/WatToWasm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ funcDefsToTypeSection f =
-- Function Section

getIndexType :: [([Type], Type)] -> Int -> FuncDef -> Int
getIndexType [] _ (FuncDef _ oName _ _ _ _ _) = error ("No Type in type section: " ++ oName)
getIndexType [] _ (FuncDef _ oName _ _ _ _ _) =
error ("No Type in type section: " ++ oName)
getIndexType ((p', r'):xs) ind (FuncDef i o n p r b l)
| p == p' && r == r' = ind
| otherwise = getIndexType xs (ind + 1) (FuncDef i o n p r b l)
Expand Down

0 comments on commit 7ce4072

Please sign in to comment.