on master help sign in
interface parameter is passed to codegen
commitd3f9138ecc82a0a2015a2d1a4b9e351247fd5888 authorRui Barreiro <rui.barreiro@gmail.com> dateThu 14 Sep 2017, 18:36 (9 years ago) parent8d2ae1a
5 files changed+27−22
codegen/idris-codegen-node/Main.hs+8−3 src/IRTS/CodegenJavaScript.hs+9−10 src/IRTS/Compiler.hs+1−1 src/IRTS/JavaScript/Codegen.hs+6−7 src/Idris/REPL.hs+3−1
codegen/idris-codegen-node/Main.hs+8−3
@@ -10,10 +10,12 @@ import IRTS.Compiler
1010
1111import Paths_idris
1212
13+import Control.Monad
1314import System.Environment
1415import System.Exit
1516
1617data Opts = Opts { inputs :: [FilePath]
18+ , interface :: Bool
1719 , output :: FilePath
1820 }
1921
@@ -23,17 +25,20 @@ showUsage = do putStrLn "A code generator which is intended to be called by the
2325
2426getOpts :: IO Opts
2527getOpts = do xs <- getArgs
26− return $ process (Opts [] "main.js") xs
28+ return $ process (Opts [] False "main.js") xs
2729 where
2830 process opts ("-o":o:xs) = process (opts { output = o }) xs
31+ process opts ("--interface":xs) = process (opts { interface = True }) xs
2932 process opts (x:xs) = process (opts { inputs = x:inputs opts }) xs
3033 process opts [] = opts
3134
3235jsMain :: Opts -> Idris ()
3336jsMain opts = do elabPrims
3437 loadInputs (inputs opts) Nothing
35− mainProg <- elabMain
36− ir <- compile (Via IBCFormat "node") (output opts) (Just mainProg)
38+ mainProg <- if interface opts
39+ then return Nothing
40+ else liftM Just elabMain
41+ ir <- compile (Via IBCFormat "node") (output opts) mainProg
3742 runIO $ codegenNode ir
3843
3944main :: IO ()
src/IRTS/CodegenJavaScript.hs+9−10
@@ -55,8 +55,15 @@ codegenJavaScript ci =
5555
5656codegenNode :: CodeGenerator
5757codegenNode ci =
58− do
59− if outputType ci == Executable then
58+ if interfaces ci then
59+ codegenJs (CGConf { header = ""
60+ , footer = ""
61+ , jsbnPath = "jsbn/jsbn-browser.js"
62+ , extraRunTime = "Runtime-node.js"
63+ }
64+ )
65+ ci
66+ else
6067 do
6168 codegenJs (CGConf { header = "#!/usr/bin/env node\n"
6269 , footer = ""
@@ -69,11 +76,3 @@ codegenNode ci =
6976 , executable = True
7077 , writable = True
7178 })
72− else
73− codegenJs (CGConf { header = ""
74− , footer = ""
75− , jsbnPath = "jsbn/jsbn-browser.js"
76− , extraRunTime = "Runtime-node.js"
77− }
78− )
79− ci
src/IRTS/Compiler.hs+1−1
@@ -152,7 +152,7 @@ generate codegen mainmod ir
152152 return fn
153153 let cmd = "idris-codegen-" ++ cg
154154 args = [input, "-o", outputFile ir] ++ compilerFlags ir
155− exit <- rawSystem cmd args
155+ exit <- rawSystem cmd (if interfaces ir then "--interface" : args else args)
156156 when (exit /= ExitSuccess) $
157157 putStrLn ("FAILURE: " ++ show cmd ++ " " ++ show args)
158158 Bytecode -> dumpBC (simpleDecls ir) (outputFile ir)
src/IRTS/JavaScript/Codegen.hs+6−7
@@ -114,11 +114,10 @@ codegenJs conf ci =
114114 debug <- isYes <$> lookupEnv "IDRISJS_DEBUG"
115115 let defs' = Map.fromList $ liftDecls ci
116116 let defs = globlToCon defs'
117− let isExec = outputType ci == Executable
118− putStrLn $ show $ isExec
119− let used = if isExec then
120− Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
121− else Map.elems $ removeDeadCode defs (getExpNames $ exportDecls ci)
117+ let iface = interfaces ci
118+ let used = if iface then
119+ Map.elems $ removeDeadCode defs (getExpNames $ exportDecls ci)
120+ else Map.elems $ removeDeadCode defs [sMN 0 "runMain"]
122121 when debug $ do
123122 writeFile (outputFile ci ++ ".LDeclsDebug") $ (unlines $ intersperse "" $ map show used) ++ "\n\n\n"
124123 putStrLn $ "Finished calculating used"
@@ -149,8 +148,8 @@ codegenJs conf ci =
149148 , doPartials (partialApplications stats), "\n"
150149 , doHiddenClasses (hiddenClasses stats), "\n"
151150 , out, "\n"
152− , if isExec then jsName (sMN 0 "runMain") `T.append` "();\n"
153− else T.concat ["module.exports = {\n", T.intercalate ";\n" $ concatMap makeExportDecls (exportDecls ci), "\n};\n"]
151+ , if iface then T.concat ["module.exports = {\n", T.intercalate ";\n" $ concatMap makeExportDecls (exportDecls ci), "\n};\n"]
152+ else jsName (sMN 0 "runMain") `T.append` "();\n"
154153 , "}.call(this))"
155154 , footer conf
156155 ]
src/Idris/REPL.hs+3−1
@@ -79,6 +79,7 @@ import Idris.Termination
7979import Idris.TypeSearch (searchByType)
8080import Idris.WhoCalls
8181import IRTS.Compiler
82+import IRTS.CodegenCommon
8283import Network
8384import Prelude hiding (id, (.), (<$>))
8485import System.Console.Haskeline as H
@@ -1284,7 +1285,8 @@ process fn (Compile codegen f)
12841285 return (Just m')
12851286 ir <- compile codegen f m
12861287 i <- getIState
1287− runIO $ generate codegen (fst (head (idris_imported i))) ir
1288+ let ir' = ir {interfaces = iface}
1289+ runIO $ generate codegen (fst (head (idris_imported i))) ir'
12881290 where fc = fileFC "main"
12891291process fn (LogLvl i) = setLogLevel i
12901292process fn (LogCategory cs) = setLogCats cs