Skip to content

Commit

Permalink
Bindings use namespace M
Browse files Browse the repository at this point in the history
  • Loading branch information
Unisay committed Apr 21, 2024
1 parent cc90531 commit ef2c4cc
Show file tree
Hide file tree
Showing 21 changed files with 201 additions and 160 deletions.
70 changes: 35 additions & 35 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions lib/Language/PureScript/Backend.hs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ compileModules outputDir foreignDir appOrModule = do
let needsRuntimeLazy = Tagged (any untag needsRuntimeLazys)
chunk Lua.fromUberModule foreignDir needsRuntimeLazy appOrModule uberModule
pure CompilationResult {lua = optimizeChunk chunk, ir = uberModule}

linkerMode AppOrModule Linker.LinkMode
linkerMode = \case
AsModule psModuleName Linker.LinkAsModule psModuleName
Expand Down
80 changes: 50 additions & 30 deletions lib/Language/PureScript/Backend/Lua.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ module Language.PureScript.Backend.Lua
( fromUberModule
, fromIR
, fromName
, fromQName
-- , qualifyName
, qualifyName
, Error (..)
) where
Expand Down Expand Up @@ -66,23 +66,28 @@ fromUberModule
Linker.UberModule
ExceptT (Variant e) IO Lua.Chunk
fromUberModule foreigns needsRuntimeLazy appOrModule uber = (`evalStateT` 0) do
(chunk, usesObjectUpdate) (`runAccumT` NoObjectUpdate) do
((bindings, returnStat), usesObjectUpdate) (`runAccumT` NoObjectUpdate) do
foreignBindings
forM (Linker.uberModuleForeigns uber) \(IR.QName modname name, irExp) do
exp asExpression <$> fromIR foreigns Set.empty modname irExp
pure $ Lua.assign (Lua.VarName (fromQName modname name)) exp
pure $ mkBinding modname (fromName name) exp

bindings
Linker.uberModuleBindings uber & foldMapM \case
IR.Standalone (IR.QName modname name, irExp) do
exp fromIR foreigns Set.empty modname irExp
pure . DList.singleton $
Lua.assignVar (fromQName modname name) (asExpression exp)
mkBinding modname (fromName name) (asExpression exp)
IR.RecursiveGroup recGroup do
recBinds forM (toList recGroup) \(IR.QName modname name, irExp)
(fromQName modname name,) . asExpression
(modname,name,) . asExpression
<$> fromIR foreigns Set.empty modname irExp
let declarations = Lua.local0 . fst <$> DList.fromList recBinds
assignments = DList.fromList (uncurry Lua.assignVar <$> recBinds)
let declarations = DList.fromList do
(modname, name, _exp) recBinds
pure $ mkBinding modname (fromName name) Lua.Nil
assignments = DList.fromList do
(modname, name, exp) recBinds
pure $ mkBinding modname (fromName name) exp
pure $ declarations <> assignments

returnExp
Expand All @@ -100,25 +105,36 @@ fromUberModule foreigns needsRuntimeLazy appOrModule uber = (`evalStateT` 0) do
where
name = IR.identToName ident

pure $
DList.fromList foreignBindings
<> DList.snoc bindings (Lua.Return (Lua.ann returnExp))
pure
( DList.fromList foreignBindings <> bindings
, Lua.Return (Lua.ann returnExp)
)

pure . mconcat $
[ [Fixture.prim | usesPrimModule uber]
[ [Lua.assign moduleVar (Lua.table []) | not (null bindings)]
, [ mkBinding Fixture.primModule Fixture.undefined Lua.Nil
| usesPrimModule uber
]
, [Fixture.runtimeLazy | untag needsRuntimeLazy && usesRuntimeLazy uber]
, [Fixture.objectUpdate | UsesObjectUpdate [usesObjectUpdate]]
, DList.toList chunk
, toList (DList.snoc bindings returnStat)
]

mkBinding ModuleName Lua.Name Lua.Exp Lua.Statement
mkBinding modname name =
Lua.assign $
Lua.VarField
(Lua.ann (Lua.var moduleVar))
(qualifyName modname name)

moduleVar Lua.Var
moduleVar = Lua.VarName [Lua.name|M|]

asExpression Either Lua.Chunk Lua.Exp Lua.Exp
asExpression = \case
Left chunk Lua.chunkToExpression chunk
Right expr expr

fromQName ModuleName IR.Name Lua.Name
fromQName modname name = qualifyName modname (fromName name)

fromName HasCallStack IR.Name Lua.Name
fromName = Name.makeSafe . IR.nameToText

Expand Down Expand Up @@ -203,13 +219,13 @@ fromIR foreigns topLevelNames modname ir = case ir of
IR.Ref _ann qualifiedName index
pure . Right $ case qualifiedName of
IR.Local name
| topLevelName fromQName modname name
| topLevelName qualifyName modname (fromName name)
, Set.member topLevelName topLevelNames
Lua.varName topLevelName
Lua.varField (Lua.var moduleVar) topLevelName
IR.Local name
Lua.varName (fromNameWithIndex name index)
IR.Imported modname' name
Lua.varName (fromQName modname' name)
Lua.varField (Lua.var moduleVar) (qualifyName modname' (fromName name))
IR.Let _ann bindings bodyExp do
body go bodyExp
recs
Expand All @@ -218,18 +234,22 @@ fromIR foreigns topLevelNames modname ir = case ir of
DList.singleton . Lua.local1 (fromName name) <$> goExp expr
IR.RecursiveGroup grp do
let binds =
toList grp <&> \(_ann, irName, _) do
let name =
if Set.member (fromQName modname irName) topLevelNames
then fromQName modname irName
else fromName irName
Lua.Local name Nothing
assignments forM (toList grp) \(_ann, irName, expr) do
let name =
if Set.member (fromQName modname irName) topLevelNames
then fromQName modname irName
else fromName irName
Lua.assign (Lua.VarName name) <$> goExp expr
toList grp <&> \(_ann, fromName name, _)
Lua.Local
( if Set.member (qualifyName modname name) topLevelNames
then qualifyName modname name
else name
)
Nothing
assignments forM (toList grp) \(_ann, fromName name, expr)
goExp expr
<&> Lua.assign
( Lua.VarName
( if Set.member (qualifyName modname name) topLevelNames
then qualifyName modname name
else name
)
)
pure $ DList.fromList binds <> DList.fromList assignments
pure . Left . DList.toList $
recs <> either DList.fromList (DList.singleton . Lua.return) body
Expand Down
13 changes: 10 additions & 3 deletions lib/Language/PureScript/Backend/Lua/Fixture.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,22 @@ import Data.String.Interpolate (__i)
import Language.PureScript.Backend.Lua.Name (Name, name, unsafeName)
import Language.PureScript.Backend.Lua.Name qualified as Name
import Language.PureScript.Backend.Lua.Types hiding (var)
import Language.PureScript.Backend.IR.Names (ModuleName(..))

--------------------------------------------------------------------------------
-- Hard-coded Lua pieces -------------------------------------------------------

prim Statement
prim = assignVar (primName [name|undefined|]) Nil
prim Name
prim = [name|Prim|]

primModule :: ModuleName
primModule = ModuleName "Prim"

undefined Name
undefined = primName [name|undefined|]

primName Name Name
primName = psluaName . Name.join2 [name|Prim|]
primName = psluaName . Name.join2 prim

uniqueName MonadState Natural m Text m Name
uniqueName prefix = do
Expand Down
4 changes: 2 additions & 2 deletions lib/Language/PureScript/Backend/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@ data AppOrModule

entryPointModule AppOrModule PS.ModuleName
entryPointModule = \case
AsApplication modul _ident modul
AsModule modul modul
AsApplication modname _ident modname
AsModule modname modname
7 changes: 4 additions & 3 deletions test/ps/output/Golden.Annotations.M1/golden.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PSLUA_Golden_Annotations_M1_foreign = (function()
M = {}
M.PSLUA_Golden_Annotations_M1_foreign = (function()
local step = 2
return {
dontInlineClosure = function(i)
Expand All @@ -11,6 +12,6 @@ PSLUA_Golden_Annotations_M1_foreign = (function()
end)()
return {
inlineMe = function(v) if 1 == v then return 2 else return v end end,
dontInlineClosure = PSLUA_Golden_Annotations_M1_foreign.dontInlineClosure,
inlineMeLambda = PSLUA_Golden_Annotations_M1_foreign.inlineMeLambda
dontInlineClosure = M.PSLUA_Golden_Annotations_M1_foreign.dontInlineClosure,
inlineMeLambda = M.PSLUA_Golden_Annotations_M1_foreign.inlineMeLambda
}
5 changes: 3 additions & 2 deletions test/ps/output/Golden.Annotations.M2/golden.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
PSLUA_Golden_Annotations_M1_foreign = (function()
M = {}
M.PSLUA_Golden_Annotations_M1_foreign = (function()
local step = 2
return {
dontInlineClosure = function(i)
Expand Down Expand Up @@ -27,5 +28,5 @@ return {
end
end
end,
inlineIntoMe2 = PSLUA_Golden_Annotations_M1_foreign.dontInlineClosure(PSLUA_Golden_Annotations_M1_foreign.inlineMeLambda(PSLUA_Golden_Annotations_M1_foreign.inlineMeLambda(17)))
inlineIntoMe2 = M.PSLUA_Golden_Annotations_M1_foreign.dontInlineClosure(M.PSLUA_Golden_Annotations_M1_foreign.inlineMeLambda(M.PSLUA_Golden_Annotations_M1_foreign.inlineMeLambda(17)))
}
7 changes: 4 additions & 3 deletions test/ps/output/Golden.CaseStatements.Test/golden.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
PSLUA_Golden_Values_Test_f = function(unused0) return true end
M = {}
M.PSLUA_Golden_Values_Test_f = function(unused0) return true end
return {
a = 1,
b = "b",
c = (function()
local v = function(unused1) return 0 end
if PSLUA_Golden_Values_Test_f(2) then
if PSLUA_Golden_Values_Test_f(1) then return 42 else return v(true) end
if M.PSLUA_Golden_Values_Test_f(2) then
if M.PSLUA_Golden_Values_Test_f(1) then return 42 else return v(true) end
else
return v(true)
end
Expand Down
7 changes: 4 additions & 3 deletions test/ps/output/Golden.Foreign.Lib/golden.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
PSLUA_Golden_Foreign_Lib_foreign = { dead = -100, alive = 100 }
M = {}
M.PSLUA_Golden_Foreign_Lib_foreign = { dead = -100, alive = 100 }
return {
dead = PSLUA_Golden_Foreign_Lib_foreign.dead,
alive = PSLUA_Golden_Foreign_Lib_foreign.alive
dead = M.PSLUA_Golden_Foreign_Lib_foreign.dead,
alive = M.PSLUA_Golden_Foreign_Lib_foreign.alive
}
7 changes: 4 additions & 3 deletions test/ps/output/Golden.Foreign.Test/golden.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
PSLUA_Golden_Foreign_Test_foreign = (function()
M = {}
M.PSLUA_Golden_Foreign_Test_foreign = (function()
local fooBar = 42
return { foo = fooBar + 1, boo = fooBar + 2 }
end)()
return {
foo = PSLUA_Golden_Foreign_Test_foreign.foo,
baz = { [1] = PSLUA_Golden_Foreign_Test_foreign.boo, [2] = 100 }
foo = M.PSLUA_Golden_Foreign_Test_foreign.foo,
baz = { [1] = M.PSLUA_Golden_Foreign_Test_foreign.boo, [2] = 100 }
}
2 changes: 1 addition & 1 deletion test/ps/output/Golden.HelloPrelude.Test/golden.ir
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ UberModule
)
( ObjectProp ( Just Always )
( ForeignImport Nothing
( ModuleName "Data.Unit" ) ".spago/prelude/v7.1.0/src/Data/Unit.purs"
( ModuleName "Data.Unit" ) ".spago/prelude/v7.1.1/src/Data/Unit.purs"
[ ( Nothing, Name "unit" ) ]
)
( PropName "unit" )
Expand Down
Loading

0 comments on commit ef2c4cc

Please sign in to comment.