Skip to content

Commit

Permalink
Look for runtime identifier conflicts - fixesi issue #38
Browse files Browse the repository at this point in the history
  • Loading branch information
andyarvanitis committed Sep 2, 2019
1 parent 907dd3f commit bc8ddad
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/CodeGen/IL.hs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,10 @@ moduleToIL (Module _ coms mn _ imps _ foreigns decls) project =
importToIL = AST.everything (++) modRef
where
modRef (AST.Indexer _ (AST.Var _ _) (AST.Var _ mname))
| not $ T.null mname = [mname]
| not $ T.null mname = [extract mname]
modRef _ = []
extract :: Text -> Text
extract = T.replace "_" "." . T.dropWhileEnd (=='_')

-- | Replaces the `ModuleName`s in the AST so that the generated code refers to
-- the collision-avoiding renamed module imports.
Expand Down
17 changes: 15 additions & 2 deletions src/CodeGen/IL/Common.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ import Language.PureScript.Names
import qualified Language.PureScript.Constants as C

moduleNameToIL :: ModuleName -> Text
moduleNameToIL (ModuleName pns) = T.intercalate "_" (runProperName `map` pns)
moduleNameToIL (ModuleName pns) =
let name = T.intercalate "_" (runProperName `map` pns)
in if nameIsILBuiltIn name then (name <> "_") else name

moduleNameToIL' :: ModuleName -> Text
moduleNameToIL' (ModuleName pns) = T.intercalate "." (runProperName `map` pns)
Expand Down Expand Up @@ -71,7 +73,18 @@ nameIsILReserved name =
nameIsILBuiltIn :: Text -> Bool
nameIsILBuiltIn name =
name `elem`
[
[ "Any"
, "Apply"
, "Contains"
, "Dict"
, "EffFn"
, "Fn"
, "Foreign"
, "Get"
, "Length"
, "Once"
, "Run"
, "Undefined"
]

ilAnyReserved :: [Text]
Expand Down
3 changes: 1 addition & 2 deletions src/CodeGen/IL/Printer.hs
Original file line number Diff line number Diff line change
Expand Up @@ -442,15 +442,14 @@ interfaceSource _ _ _ = ""

implHeaderSource :: Text -> [Text] -> Text -> Text
implHeaderSource mn imports otherPrefix =
let imports' = T.replace "_" "." <$> imports in
"// Code generated by psgo. DO NOT EDIT.\n\n" <>
"package " <> (if mn == "Main" then "main" else mn) <> "\n\n" <>
"import . \"" <> runtime <> "\"\n" <>
(if mn == "Main"
then "import _ \"" <> otherPrefix <> "/" <> ffiLoader <> "\"\n"
else "\n") <>
"import (\n" <>
(T.concat $ formatImport <$> imports') <> ")\n\n" <>
(T.concat $ formatImport <$> imports) <> ")\n\n" <>
"type _ = Any\n\n"
where
formatImport :: Text -> Text
Expand Down

0 comments on commit bc8ddad

Please sign in to comment.