on master help sign in
Merge pull request #3173 from melted/portable_json

Export intermediate representation as JSON

commit5ef9bf46f3f891d636a7517f8f79907c3600cf1c authorNiklas Larsson <metaniklas@gmail.com> dateTue 17 May 2016, 11:10 (10 years ago) parent536372fd2a8c3e
14 files changed+351−40
codegen/idris-codegen-c/Main.hs+1−1 codegen/idris-codegen-javascript/Main.hs+1−1 codegen/idris-codegen-node/Main.hs+1−1 idris.cabal+3−1 src/IRTS/Compiler.hs+16−7 src/IRTS/Portable.hs+277 src/Idris/AbsSyntax.hs+9−4 src/Idris/AbsSyntaxTree.hs+5−8 src/Idris/CmdOptions.hs+8−1 src/Idris/DeepSeq.hs+5−2 src/Idris/IBC.hs+16−4 src/Idris/Parser.hs+6−6 src/Idris/REPL.hs+1−2 src/Idris/REPL/Parser.hs+2−2
codegen/idris-codegen-c/Main.hs+1−1
@@ -40,7 +40,7 @@ c_main opts = do runIO setupBundledCC
4040 mainProg <- if interface opts
4141 then liftM Just elabMain
4242 else return Nothing
43− ir <- compile (Via "c") (output opts) mainProg
43+ ir <- compile (Via IBCFormat "c") (output opts) mainProg
4444 runIO $ codegenC ir
4545
4646main :: IO ()
codegen/idris-codegen-javascript/Main.hs+1−1
@@ -34,7 +34,7 @@ jsMain :: Opts -> Idris ()
3434jsMain opts = do elabPrims
3535 loadInputs (inputs opts) Nothing
3636 mainProg <- elabMain
37− ir <- compile (Via "javascript") (output opts) (Just mainProg)
37+ ir <- compile (Via IBCFormat "javascript") (output opts) (Just mainProg)
3838 runIO $ codegenJavaScript ir
3939
4040main :: IO ()
codegen/idris-codegen-node/Main.hs+1−1
@@ -33,7 +33,7 @@ jsMain :: Opts -> Idris ()
3333jsMain opts = do elabPrims
3434 loadInputs (inputs opts) Nothing
3535 mainProg <- elabMain
36− ir <- compile (Via "node") (output opts) (Just mainProg)
36+ ir <- compile (Via IBCFormat "node") (output opts) (Just mainProg)
3737 runIO $ codegenNode ir
3838
3939main :: IO ()
idris.cabal+3−1
@@ -606,7 +606,7 @@ Extra-source-files:
606606 test/interactive013/input
607607 test/interactive013/run
608608 test/interactive013/*.idr
609−
609+
610610 test/interfaces001/*.idr
611611 test/interfaces001/run
612612 test/interfaces001/expected
@@ -1031,6 +1031,7 @@ Library
10311031 , IRTS.Inliner
10321032 , IRTS.Lang
10331033 , IRTS.LangOpts
1034+ , IRTS.Portable
10341035 , IRTS.Simplified
10351036 , IRTS.System
10361037
@@ -1051,6 +1052,7 @@ Library
10511052 , Tools_idris
10521053
10531054 Build-depends: base >=4 && <5
1055+ , aeson >= 0.6 && < 0.12
10541056 , annotated-wl-pprint >= 0.7 && < 0.8
10551057 , ansi-terminal < 0.7
10561058 , ansi-wl-pprint < 0.7
src/IRTS/Compiler.hs+16−7
@@ -12,6 +12,7 @@ import IRTS.DumpBC
1212import IRTS.CodegenJavaScript
1313import IRTS.Inliner
1414import IRTS.Exports
15+import IRTS.Portable
1516
1617import Idris.AbsSyntax
1718import Idris.AbsSyntaxTree
@@ -125,13 +126,21 @@ generate :: Codegen -> FilePath -> CodegenInfo -> IO ()
125126generate codegen mainmod ir
126127 = case codegen of
127128 -- Built-in code generators (FIXME: lift these out!)
128− Via "c" -> codegenC ir
129+ Via _ "c" -> codegenC ir
129130 -- Any external code generator
130− Via cg -> do let cmd = "idris-codegen-" ++ cg
131− args = [mainmod, "-o", outputFile ir] ++ compilerFlags ir
132− exit <- rawSystem cmd args
133− when (exit /= ExitSuccess) $
134− putStrLn ("FAILURE: " ++ show cmd ++ " " ++ show args)
131+ Via fm cg -> do input <- case fm of
132+ IBCFormat -> return mainmod
133+ JSONFormat -> do
134+ tempdir <- getTemporaryDirectory
135+ (fn, h) <- openTempFile tempdir "idris-cg.json"
136+ writePortable h ir
137+ hClose h
138+ return fn
139+ let cmd = "idris-codegen-" ++ cg
140+ args = [input, "-o", outputFile ir] ++ compilerFlags ir
141+ exit <- rawSystem cmd args
142+ when (exit /= ExitSuccess) $
143+ putStrLn ("FAILURE: " ++ show cmd ++ " " ++ show args)
135144 Bytecode -> dumpBC (simpleDecls ir) (outputFile ir)
136145
137146irMain :: TT Name -> Idris LDecl
@@ -350,7 +359,7 @@ irTerm vs env tm@(App _ f a) = do
350359
351360 -- overapplied
352361 GT -> ifail ("overapplied data constructor: " ++ show tm ++
353− "\nDEBUG INFO:\n" ++
362+ "\nDEBUG INFO:\n" ++
354363 "Arity: " ++ show arity ++ "\n" ++
355364 "Arguments: " ++ show args ++ "\n" ++
356365 "Pruned arguments: " ++ show argsPruned)
src/IRTS/Portable.hsnew file+277
@@ -0,0 +1,277 @@
1+{-# LANGUAGE OverloadedStrings #-}
2+
3+module IRTS.Portable (writePortable) where
4+
5+import Data.Aeson
6+import qualified Data.ByteString.Lazy as B
7+import qualified Data.Text as T
8+
9+import Idris.Core.CaseTree
10+import Idris.Core.TT
11+
12+import IRTS.Bytecode
13+import IRTS.CodegenCommon
14+import IRTS.Defunctionalise
15+import IRTS.Lang
16+import IRTS.Simplified
17+
18+import System.IO
19+
20+data CodegenFile = CGFile {
21+ fileType :: String,
22+ version :: Int,
23+ cgInfo :: CodegenInfo
24+}
25+
26+-- Update the version when the format changes
27+formatVersion :: Int
28+formatVersion = 1
29+
30+writePortable :: Handle -> CodegenInfo -> IO ()
31+writePortable file ci = do
32+ let json = encode $ CGFile "idris-codegen" formatVersion ci
33+ B.hPut file json
34+
35+instance ToJSON CodegenFile where
36+ toJSON (CGFile ft v ci) = object ["file-type" .= ft,
37+ "version" .= v,
38+ "codegen-info" .= toJSON ci]
39+
40+instance ToJSON CodegenInfo where
41+ toJSON ci = object ["output-file" .= (outputFile ci),
42+ "includes" .= (includes ci),
43+ "import-dirs" .= (importDirs ci),
44+ "compile-objs" .= (compileObjs ci),
45+ "compile-libs" .= (compileLibs ci),
46+ "compiler-flags" .= (compilerFlags ci),
47+ "interfaces" .= (interfaces ci),
48+ "exports" .= (exportDecls ci),
49+ "lift-decls" .= (liftDecls ci),
50+ "defun-decls" .= (defunDecls ci),
51+ "simple-decls" .= (simpleDecls ci),
52+ "bytecode" .= (map toBC (simpleDecls ci))]
53+
54+instance ToJSON Name where
55+ toJSON n = toJSON $ showCG n
56+
57+instance ToJSON ExportIFace where
58+ toJSON (Export n f xs) = object ["ffi-desc" .= n,
59+ "interface-file" .= f,
60+ "exports" .= xs]
61+
62+instance ToJSON FDesc where
63+ toJSON (FCon n) = object ["FCon" .= n]
64+ toJSON (FStr s) = object ["FStr" .= s]
65+ toJSON (FUnknown) = object ["FUnknown" .= Null]
66+ toJSON (FIO fd) = object ["FIO" .= fd]
67+ toJSON (FApp n xs) = object ["FApp" .= (n, xs)]
68+
69+instance ToJSON Export where
70+ toJSON (ExportData fd) = object ["ExportData" .= fd]
71+ toJSON (ExportFun n dsc ret args) = object ["ExportFun" .= (n, dsc, ret, args)]
72+
73+instance ToJSON LDecl where
74+ toJSON (LFun opts name args def) = object ["LFun" .= (opts, name, args, def)]
75+ toJSON (LConstructor name tag ar) = object ["LConstructor" .= (name, tag, ar)]
76+
77+instance ToJSON LOpt where
78+ toJSON Inline = String "Inline"
79+ toJSON NoInline = String "NoInline"
80+
81+instance ToJSON LExp where
82+ toJSON (LV lv) = object ["LV" .= lv]
83+ toJSON (LApp tail exp args) = object ["LApp" .= (tail, exp, args)]
84+ toJSON (LLazyApp name exps) = object ["LLazyApp" .= (name, exps)]
85+ toJSON (LLazyExp exp) = object ["LLazyExp" .= exp]
86+ toJSON (LForce exp) = object ["LForce" .= exp]
87+ toJSON (LLet name a b) = object ["LLet" .= (name, a, b)]
88+ toJSON (LLam args exp) = object ["LLam" .= (args, exp)]
89+ toJSON (LProj exp i) = object ["LProj" .= (exp, i)]
90+ toJSON (LCon lv i n exps) = object ["LCon" .= (lv, i, n, exps)]
91+ toJSON (LCase ct exp alts) = object ["LCase" .= (ct, exp, alts)]
92+ toJSON (LConst c) = object ["LConst" .= c]
93+ toJSON (LForeign fd ret exps) = object ["LForeign" .= (fd, ret, exps)]
94+ toJSON (LOp prim exps) = object ["LOp" .= (prim, exps)]
95+ toJSON LNothing = object ["LNothing" .= Null]
96+ toJSON (LError s) = object ["LError" .= s]
97+
98+instance ToJSON LVar where
99+ toJSON (Loc i) = object ["Loc" .= i]
100+ toJSON (Glob n) = object ["Glob" .= n]
101+
102+instance ToJSON CaseType where
103+ toJSON Updatable = String "Updatable"
104+ toJSON Shared = String "Shared"
105+
106+instance ToJSON LAlt where
107+ toJSON (LConCase i n ns exp) = object ["LConCase" .= (i, n, ns, exp)]
108+ toJSON (LConstCase c exp) = object ["LConstCase" .= (c, exp)]
109+ toJSON (LDefaultCase exp) = object ["LDefaultCase" .= exp]
110+
111+instance ToJSON Const where
112+ toJSON (I i) = object ["int" .= i]
113+ toJSON (BI i) = object ["bigint" .= (show i)]
114+ toJSON (Fl d) = object ["double" .= d]
115+ toJSON (Ch c) = object ["char" .= (show c)]
116+ toJSON (Str s) = object ["string" .= s]
117+ toJSON (B8 b) = object ["bits8" .= b]
118+ toJSON (B16 b) = object ["bits16" .= b]
119+ toJSON (B32 b) = object ["bits32" .= b]
120+ toJSON (B64 b) = object ["bits64" .= b]
121+ toJSON (AType at) = object ["atype" .= at]
122+ toJSON StrType = object ["strtype" .= Null]
123+ toJSON WorldType = object ["worldtype" .= Null]
124+ toJSON TheWorld = object ["theworld" .= Null]
125+ toJSON VoidType = object ["voidtype" .= Null]
126+ toJSON Forgot = object ["forgot" .= Null]
127+
128+instance ToJSON ArithTy where
129+ toJSON (ATInt it) = object ["ATInt" .= it]
130+ toJSON ATFloat = object ["ATFloat" .= Null]
131+
132+instance ToJSON IntTy where
133+ toJSON it = toJSON $ intTyName it
134+
135+instance ToJSON PrimFn where
136+ toJSON (LPlus aty) = object ["LPlus" .= aty]
137+ toJSON (LMinus aty) = object ["LMinus" .= aty]
138+ toJSON (LTimes aty) = object ["LTimes" .= aty]
139+ toJSON (LUDiv aty) = object ["LUDiv" .= aty]
140+ toJSON (LSDiv aty) = object ["LSDiv" .= aty]
141+ toJSON (LAnd ity) = object ["LAnd" .= ity]
142+ toJSON (LOr ity) = object ["LOr" .= ity]
143+ toJSON (LXOr ity) = object ["LXOr" .= ity]
144+ toJSON (LCompl ity) = object ["LCompl" .= ity]
145+ toJSON (LSHL ity) = object ["LSHL" .= ity]
146+ toJSON (LLSHR ity) = object ["LLSHR" .= ity]
147+ toJSON (LASHR ity) = object ["LASHR" .= ity]
148+ toJSON (LEq aty) = object ["LEq" .= aty]
149+ toJSON (LLt ity) = object ["LLt" .= ity]
150+ toJSON (LLe ity) = object ["LLe" .= ity]
151+ toJSON (LGt ity) = object ["LGt" .= ity]
152+ toJSON (LGe ity) = object ["LGe" .= ity]
153+ toJSON (LSLt aty) = object ["LSLt" .= aty]
154+ toJSON (LSLe aty) = object ["LSLe" .= aty]
155+ toJSON (LSGt aty) = object ["LSGt" .= aty]
156+ toJSON (LSGe aty) = object ["LSGe" .= aty]
157+ toJSON (LZExt from to) = object ["LZExt" .= (from, to)]
158+ toJSON (LSExt from to) = object ["LSExt" .= (from, to)]
159+ toJSON (LTrunc from to) = object ["LTrunc" .= (from, to)]
160+ toJSON LStrConcat = object ["LStrConcat" .= Null]
161+ toJSON LStrLt = object ["LStrLt" .= Null]
162+ toJSON LStrEq = object ["LStrEq" .= Null]
163+ toJSON LStrLen = object ["LStrLen" .= Null]
164+ toJSON (LIntFloat ity) = object ["LIntFloat" .= ity]
165+ toJSON (LFloatInt ity) = object ["LFloatInt" .= ity]
166+ toJSON (LIntStr ity) = object ["LIntStr" .= ity]
167+ toJSON (LStrInt ity) = object ["LStrInt" .= ity]
168+ toJSON (LIntCh ity) = object ["LIntCh" .= ity]
169+ toJSON (LChInt ity) = object ["LChInt" .= ity]
170+ toJSON LFloatStr = object ["LFloatStr" .= Null]
171+ toJSON LStrFloat = object ["LStrFloat" .= Null]
172+ toJSON (LBitCast from to) = object ["LBitCast" .= (from, to)]
173+ toJSON LFExp = object ["LFExp" .= Null]
174+ toJSON LFLog = object ["LFLog" .= Null]
175+ toJSON LFSin = object ["LFSin" .= Null]
176+ toJSON LFCos = object ["LFCos" .= Null]
177+ toJSON LFTan = object ["LFTan" .= Null]
178+ toJSON LFASin = object ["LFASin" .= Null]
179+ toJSON LFACos = object ["LFACos" .= Null]
180+ toJSON LFATan = object ["LFATan" .= Null]
181+ toJSON LFSqrt = object ["LFSqrt" .= Null]
182+ toJSON LFFloor = object ["LFFloor" .= Null]
183+ toJSON LFCeil = object ["LFCeil" .= Null]
184+ toJSON LFNegate = object ["LFNegate" .= Null]
185+ toJSON LStrHead = object ["LStrHead" .= Null]
186+ toJSON LStrTail = object ["LStrTail" .= Null]
187+ toJSON LStrCons = object ["LStrCons" .= Null]
188+ toJSON LStrIndex = object ["LStrIndex" .= Null]
189+ toJSON LStrRev = object ["LStrRev" .= Null]
190+ toJSON LStrSubstr = object ["LStrSubstr" .= Null]
191+ toJSON LReadStr = object ["LReadStr" .= Null]
192+ toJSON LWriteStr = object ["LWriteStr" .= Null]
193+ toJSON LSystemInfo = object ["LSystemInfo" .= Null]
194+ toJSON LFork = object ["LFork" .= Null]
195+ toJSON LPar = object ["LPar" .= Null]
196+ toJSON (LExternal name) = object ["LExternal" .= name]
197+ toJSON LNoOp = object ["LNoOp" .= Null]
198+
199+
200+
201+
202+
203+instance ToJSON DDecl where
204+ toJSON (DFun name args exp) = object ["DFun" .= (name, args, exp)]
205+ toJSON (DConstructor name tag arity) = object ["DConstructor" .= (name, tag, arity)]
206+
207+instance ToJSON DExp where
208+ toJSON (DV lv) = object ["DV" .= lv]
209+ toJSON (DApp tail name exps) = object ["DApp" .= (tail, name, exps)]
210+ toJSON (DLet name a b) = object ["DLet" .= (name, a, b)]
211+ toJSON (DUpdate name exp) = object ["DUpdate" .= (name,exp)]
212+ toJSON (DProj exp i) = object ["DProj" .= (exp, i)]
213+ toJSON (DC lv i name exp) = object ["DC" .= (lv, i, name, exp)]
214+ toJSON (DCase ct exp alts) = object ["DCase" .= (ct, exp, alts)]
215+ toJSON (DChkCase exp alts) = object ["DChkCase" .= (exp, alts)]
216+ toJSON (DConst c) = object ["DConst" .= c]
217+ toJSON (DForeign fd ret exps) = object ["DForeign" .= (fd, ret, exps)]
218+ toJSON (DOp prim exps) = object ["DOp" .= (prim, exps)]
219+ toJSON DNothing = object ["DNothing" .= Null]
220+ toJSON (DError s) = object ["DError" .= s]
221+
222+instance ToJSON DAlt where
223+ toJSON (DConCase i n ns exp) = object ["DConCase" .= (i, n, ns, exp)]
224+ toJSON (DConstCase c exp) = object ["DConstCase" .= (c, exp)]
225+ toJSON (DDefaultCase exp) = object ["DDefaultCase" .= exp]
226+
227+instance ToJSON SDecl where
228+ toJSON (SFun name args i exp) = object ["SFun" .= (name, args, i, exp)]
229+
230+instance ToJSON SExp where
231+ toJSON (SV lv) = object ["SV" .= lv]
232+ toJSON (SApp tail name exps) = object ["SApp" .= (tail, name, exps)]
233+ toJSON (SLet lv a b) = object ["SLet" .= (lv, a, b)]
234+ toJSON (SUpdate lv exp) = object ["SUpdate" .= (lv, exp)]
235+ toJSON (SProj lv i) = object ["DProj" .= (lv, i)]
236+ toJSON (SCon lv i name vars) = object ["SCon" .= (lv, i, name, vars)]
237+ toJSON (SCase ct lv alts) = object ["SCase" .= (ct, lv, alts)]
238+ toJSON (SChkCase lv alts) = object ["DChkCase" .= (lv, alts)]
239+ toJSON (SConst c) = object ["SConst" .= c]
240+ toJSON (SForeign fd ret exps) = object ["SForeign" .= (fd, ret, exps)]
241+ toJSON (SOp prim vars) = object ["DOp" .= (prim, vars)]
242+ toJSON SNothing = object ["SNothing" .= Null]
243+ toJSON (SError s) = object ["SError" .= s]
244+
245+instance ToJSON SAlt where
246+ toJSON (SConCase i j n ns exp) = object ["SConCase" .= (i, j, n, ns, exp)]
247+ toJSON (SConstCase c exp) = object ["SConstCase" .= (c, exp)]
248+ toJSON (SDefaultCase exp) = object ["SDefaultCase" .= exp]
249+
250+instance ToJSON BC where
251+ toJSON (ASSIGN r1 r2) = object ["ASSIGN" .= (r1, r2)]
252+ toJSON (ASSIGNCONST r c) = object ["ASSIGNCONST" .= (r, c)]
253+ toJSON (UPDATE r1 r2) = object ["UPDATE" .= (r1, r2)]
254+ toJSON (MKCON con mr i regs) = object ["MKCON" .= (con, mr, i, regs)]
255+ toJSON (CASE b r alts def) = object ["CASE" .= (b, r, alts, def)]
256+ toJSON (PROJECT r loc arity) = object ["PROJECT" .= (r, loc, arity)]
257+ toJSON (PROJECTINTO r1 r2 loc) = object ["PROJECTINTO" .= (r1, r2, loc)]
258+ toJSON (CONSTCASE r alts def) = object ["CONSTCASE" .= (r, alts, def)]
259+ toJSON (CALL name) = object ["CALL" .= name]
260+ toJSON (TAILCALL name) = object ["TAILCALL" .= name]
261+ toJSON (FOREIGNCALL r fd ret exps) = object ["FOREIGNCALL" .= (r, fd, ret, exps)]
262+ toJSON (SLIDE i) = object ["SLIDE" .= i]
263+ toJSON (RESERVE i) = object ["RESERVE" .= i]
264+ toJSON (ADDTOP i) = object ["ADDTOP" .= i]
265+ toJSON (TOPBASE i) = object ["TOPBASE" .= i]
266+ toJSON (BASETOP i) = object ["BASETOP" .= i]
267+ toJSON REBASE = object ["REBASE" .= Null]
268+ toJSON STOREOLD = object ["STOREOLD" .= Null]
269+ toJSON (OP r prim args) = object ["OP" .= (r, prim, args)]
270+ toJSON (NULL r) = object ["NULL" .= r]
271+ toJSON (ERROR s) = object ["ERROR" .= s]
272+
273+instance ToJSON Reg where
274+ toJSON RVal = object ["RVal" .= Null]
275+ toJSON (T i) = object ["T" .= i]
276+ toJSON (L i) = object ["L" .= i]
277+ toJSON Tmp = object ["Tmp" .= Null]
src/Idris/AbsSyntax.hs+9−4
@@ -45,7 +45,11 @@ getContext :: Idris Context
4545getContext = do i <- getIState; return (tt_ctxt i)
4646
4747forCodegen :: Codegen -> [(Codegen, a)] -> [a]
48−forCodegen tgt xs = [x | (tgt', x) <- xs, tgt == tgt']
48+forCodegen tgt xs = [x | (tgt', x) <- xs, eqLang tgt tgt']
49+ where
50+ eqLang (Via _ x) (Via _ y) = x == y
51+ eqLang Bytecode Bytecode = True
52+ eqLang _ _ = False
4953
5054getObjectFiles :: Codegen -> Idris [FilePath]
5155getObjectFiles tgt = do i <- getIState; return (forCodegen tgt $ idris_objs i)
@@ -459,13 +463,13 @@ addInstance int res n i
459463-- Returns the old list, so we can revert easily at the end of a block
460464
461465addOpenImpl :: [Name] -> Idris [Name]
462−addOpenImpl ns = do ist <- getIState
466+addOpenImpl ns = do ist <- getIState
463467 ns' <- mapM (checkValid ist) ns
464468 let open = idris_openimpls ist
465469 putIState $ ist { idris_openimpls = nub (ns' ++ open) }
466470 return open
467471 where
468− checkValid ist n
472+ checkValid ist n
469473 = case lookupCtxtName n (idris_implicits ist) of
470474 [(n', _)] -> return n'
471475 [] -> throwError (NoSuchVariable n)
@@ -946,6 +950,7 @@ codegen :: Idris Codegen
946950codegen = do i <- getIState
947951 return (opt_codegen (idris_options i))
948952
953+
949954setOutputTy :: OutputType -> Idris ()
950955setOutputTy t = do i <- getIState
951956 let opts = idris_options i
@@ -2329,7 +2334,7 @@ shadow n n' t = sm 0 t where
23292334 sm 0 (PTyped x y) = PTyped (sm 0 x) (sm 0 y)
23302335 sm 0 (PPair f hls p x y) = PPair f hls p (sm 0 x) (sm 0 y)
23312336 sm 0 (PDPair f hls p x t y) = PDPair f hls p (sm 0 x) (sm 0 t) (sm 0 y)
2332− sm 0 (PAlternative ms a as)
2337+ sm 0 (PAlternative ms a as)
23332338 = PAlternative (map shadowAlt ms) a (map (sm 0) as)
23342339 sm 0 (PTactics ts) = PTactics (map (fmap (sm 0)) ts)
23352340 sm 0 (PProof ts) = PProof (map (fmap (sm 0)) ts)
src/Idris/AbsSyntaxTree.hs+5−8
@@ -112,7 +112,7 @@ defaultOpts = IOption { opt_logLevel = 0
112112 , opt_verbose = True
113113 , opt_nobanner = False
114114 , opt_quiet = False
115− , opt_codegen = Via "c"
115+ , opt_codegen = Via IBCFormat "c"
116116 , opt_outputTy = Executable
117117 , opt_ibcsubdir = ""
118118 , opt_importdirs = []
@@ -186,7 +186,7 @@ data DefaultTotality = DefaultCheckingTotal -- ^ Total
186186 | DefaultCheckingPartial -- ^ Partial
187187 | DefaultCheckingCovering -- ^Total coverage, but may diverge
188188 deriving (Show, Eq)
189−
189+
190190-- | The global state used in the Idris monad
191191data IState = IState {
192192 tt_ctxt :: Context, -- ^ All the currently defined names and their terms
@@ -386,18 +386,15 @@ throwError = Trans.lift . throwE
386386
387387-- Commands in the REPL
388388
389−data Codegen = Via String
390−-- | ViaC
391−-- | ViaJava
392−-- | ViaNode
393−-- | ViaJavaScript
394−-- | ViaLLVM
389+data Codegen = Via IRFormat String
395390 | Bytecode
396391 deriving (Show, Eq)
397392{-!
398393deriving instance NFData Codegen
399394!-}
400395
396+data IRFormat = IBCFormat | JSONFormat deriving (Show, Eq)
397+
401398data HowMuchDocs = FullDocs | OverviewDocs
402399
403400-- | REPL commands
src/Idris/CmdOptions.hs+8−1
@@ -149,6 +149,12 @@ parseFlags = many $
149149 <|> ((\s -> UseCodegen $ parseCodegen s) <$> strOption (long "codegen"
150150 <> metavar "TARGET"
151151 <> help "Select code generator: C, Javascript, Node and bytecode are bundled with Idris"))
152+
153+
154+ <|> ((\s -> UseCodegen $ Via JSONFormat s) <$> strOption (long "portable-codegen"
155+ <> metavar "TARGET"
156+ <> help "Pass the name of the code generator. This option is for codegens that take JSON formatted IR."))
157+
152158 <|> (CodegenArgs <$> strOption (long "cg-opt"
153159 <> metavar "ARG"
154160 <> help "Arguments to pass to code generator"))
@@ -206,7 +212,8 @@ preProcOpts (x:xs) ys = preProcOpts xs (x:ys)
206212
207213parseCodegen :: String -> Codegen
208214parseCodegen "bytecode" = Bytecode
209−parseCodegen cg = Via (map toLower cg)
215+parseCodegen cg = Via IBCFormat (map toLower cg)
216+
210217
211218parseLogCats :: Monad m => String -> m [LogCat]
212219parseLogCats s =
src/Idris/DeepSeq.hs+5−2
@@ -361,9 +361,12 @@ instance NFData FnInfo where
361361 rnf (FnInfo x1) = rnf x1 `seq` ()
362362
363363instance NFData Codegen where
364− rnf (Via x1) = rnf x1 `seq` ()
364+ rnf (Via x1 x2) = rnf x1 `seq` rnf x2 `seq` ()
365365 rnf Bytecode = ()
366366
367+instance NFData IRFormat where
368+ rnf _ = ()
369+
367370instance NFData LogCat where
368371 rnf _ = ()
369372
@@ -700,7 +703,7 @@ instance NFData DefaultTotality where
700703 rnf DefaultCheckingTotal = ()
701704 rnf DefaultCheckingPartial = ()
702705 rnf DefaultCheckingCovering = ()
703−
706+
704707instance NFData IState where
705708 rnf (IState x1 x2 x3 x4 x5 x6 x7 x8 x9 x10 x11 x12 x13 x14 x15 x16 x17 x18 x19 x20
706709 x21 x22 x23 x24 x25 x26 x27 x28 x29 x30 x31 x32 x33 x34 x35 x36 x37 x38 x39 x40
src/Idris/IBC.hs+16−4
@@ -40,7 +40,7 @@ import System.Directory
4040import Codec.Archive.Zip
4141
4242ibcVersion :: Word16
43−ibcVersion = 142
43+ibcVersion = 143
4444
4545-- When IBC is being loaded - we'll load different things (and omit different
4646-- structures/definitions) depending on which phase we're in
@@ -2461,13 +2461,25 @@ instance Binary SSymbol where
24612461instance Binary Codegen where
24622462 put x
24632463 = case x of
2464− Via str -> do putWord8 0
2465− put str
2464+ Via ir str -> do putWord8 0
2465+ put ir
2466+ put str
24662467 Bytecode -> putWord8 1
24672468 get
24682469 = do i <- getWord8
24692470 case i of
24702471 0 -> do x1 <- get
2471− return (Via x1)
2472+ x2 <- get
2473+ return (Via x1 x2)
24722474 1 -> return Bytecode
24732475 _ -> error "Corrupted binary data for Codegen"
2476+
2477+instance Binary IRFormat where
2478+ put x = case x of
2479+ IBCFormat -> putWord8 0
2480+ JSONFormat -> putWord8 1
2481+ get = do i <- getWord8
2482+ case i of
2483+ 0 -> return IBCFormat
2484+ 1 -> return JSONFormat
2485+ _ -> error "Corrupted binary data for IRFormat"
src/Idris/Parser.hs+6−6
@@ -781,7 +781,7 @@ params syn =
781781 return [PParams fc ns' (concat ds)]
782782 <?> "parameters declaration"
783783
784−{- | Parses an open block
784+{- | Parses an open block
785785
786786-}
787787
@@ -790,7 +790,7 @@ openInterface syn =
790790 do reservedHL "using"
791791 reservedHL "implementation"
792792 fc <- getFC
793− ns <- sepBy1 fnName (lchar ',')
793+ ns <- sepBy1 fnName (lchar ',')
794794
795795 openBlock
796796 ds <- many (decl syn)
@@ -799,7 +799,7 @@ openInterface syn =
799799 <?> "open interface declaration"
800800
801801
802−
802+
803803
804804{- | Parses a mutual declaration (for mutually recursive functions)
805805
@@ -877,7 +877,7 @@ classBlock syn = do reservedHL "where"
877877 ist <- get
878878 let cd' = annotate syn ist cd
879879
880− ds <- many (notEndBlock >> try (instance_ True syn)
880+ ds <- many (notEndBlock >> try (instance_ True syn)
881881 <|> do x <- data_ syn
882882 return [x]
883883 <|> fnDecl syn)
@@ -1324,7 +1324,7 @@ Codegen ::= 'C'
13241324-}
13251325codegen_ :: IdrisParser Codegen
13261326codegen_ = do n <- fst <$> identifier
1327− return (Via (map toLower n))
1327+ return (Via IBCFormat (map toLower n))
13281328 <|> do reserved "Bytecode"; return Bytecode
13291329 <?> "code generation language"
13301330
@@ -1812,7 +1812,7 @@ loadSource lidr f toline
18121812 logLvl 2 $ "Totality checking " ++ show deftots
18131813 mapM_ (\x -> do tot <- getTotality x
18141814 case tot of
1815− Total _ ->
1815+ Total _ ->
18161816 do let opts = case lookupCtxtExact x (idris_flags i) of
18171817 Just os -> os
18181818 Nothing -> []
src/Idris/REPL.hs+1−2
@@ -1249,7 +1249,6 @@ process fn (Compile codegen f)
12491249 [pexp $ PRef fc [] mainname])
12501250 return (Just m')
12511251 ir <- compile codegen f m
1252−
12531252 i <- getIState
12541253 runIO $ generate codegen (fst (head (idris_imported i))) ir
12551254 where fc = fileFC "main"
@@ -1688,7 +1687,7 @@ idrisMain opts =
16881687 Object else Executable
16891688 xs -> last xs
16901689 let cgn = case opt getCodegen opts of
1691− [] -> Via "c"
1690+ [] -> Via IBCFormat "c"
16921691 xs -> last xs
16931692 let cgFlags = opt getCodegenArgs opts
16941693
src/Idris/REPL/Parser.hs+2−2
@@ -358,13 +358,13 @@ cmd_pprint name = do
358358
359359cmd_compile :: String -> P.IdrisParser (Either String Command)
360360cmd_compile name = do
361− let defaultCodegen = Via "c"
361+ let defaultCodegen = Via IBCFormat "c"
362362
363363 let codegenOption :: P.IdrisParser Codegen
364364 codegenOption = do
365365 let bytecodeCodegen = discard (P.symbol "bytecode") *> return Bytecode
366366 viaCodegen = do x <- fst <$> P.identifier
367− return (Via (map toLower x))
367+ return (Via IBCFormat (map toLower x))
368368 bytecodeCodegen <|> viaCodegen
369369
370370 let hasOneArg = do