on master help sign in
Set up C generator
commit79e6bf7db8f6117ed54c75ead2cbf79ef2501bc3 authorEdwin Brady <eb@cs.st-andrews.ac.uk> dateFri 31 Aug 2012, 11:13 (14 years ago) parentb70780f
4 files changed+52−3
idris.cabal+2−1 rts/idris_main.c+5 src/IRTS/CodegenC.hs+34 src/IRTS/LParser.hs+11−2
idris.cabal+2−1
@@ -40,7 +40,7 @@ Description: Idris is a general purpose language with full dependent types.
4040Cabal-Version: >= 1.6
4141Build-type: Custom
4242
43−Data-files: rts/libidris_rts.a rts/idris_rts.h
43+Data-files: rts/libidris_rts.a rts/idris_rts.h rts/idris_main.c
4444Extra-source-files: lib/Makefile lib/*.idr lib/prelude/*.idr lib/network/*.idr
4545 lib/control/monad/*.idr lib/language/*.idr
4646 tutorial/examples/*.idr
@@ -71,6 +71,7 @@ Executable idris
7171
7272 RTS.Bytecode, RTS.SC, RTS.PreC, RTS.CodegenC,
7373 IRTS.Lang, IRTS.LParser, IRTS.Bytecode, IRTS.Simplified,
74+ IRTS.CodegenC,
7475
7576 Paths_idris
7677
rts/idris_main.cnew file+5
@@ -0,0 +1,5 @@
1+int main(int argc, char* argv[]) {
2+ VM* vm = init_vm(1024000, 1024000);
3+
4+ _idris_main(vm, NULL);
5+}
src/IRTS/CodegenC.hsnew file+34
@@ -0,0 +1,34 @@
1+module IRTS.CodegenC where
2+
3+import IRTS.Bytecode
4+import IRTS.Lang
5+import Core.TT
6+import Paths_idris
7+
8+import Data.Char
9+
10+cname :: Name -> String
11+cname n = "_idris_" ++ concatMap cchar (show n)
12+ where cchar x | isAlpha x || isDigit x = [x]
13+ | otherwise = "_" ++ show (fromEnum x) ++ "_"
14+
15+indent i = take (i * 4) (repeat ' ')
16+
17+creg RVal = "RVAL"
18+creg (L i) = "LOC(" ++ show i ++ ")"
19+creg (T i) = "TOP(" ++ show i ++ ")"
20+
21+toDecl :: Name -> String
22+toDecl f = "void " ++ cname f ++ "(VM*, VAL*);\n"
23+
24+toC :: Name -> [BC] -> String
25+toC f code
26+ = -- "/* " ++ show code ++ "*/\n\n" ++
27+ "void " ++ cname f ++ "(VM* vm, VAL* oldbase) {" ++
28+ concatMap (bcc 1) code ++ "}\n\n"
29+
30+bcc :: Int -> BC -> String
31+bcc i (ASSIGN l r) = indent i ++ creg l ++ " = " ++ creg r ++ ";\n"
32+
33+bcc i _ = indent i ++ "// not done yet\n"
34+
src/IRTS/LParser.hs+11−2
@@ -5,6 +5,8 @@ import Core.TT
55import IRTS.Lang
66import IRTS.Simplified
77import IRTS.Bytecode
8+import IRTS.CodegenC
9+import Paths_idris
810
911import Text.ParserCombinators.Parsec
1012import Text.ParserCombinators.Parsec.Error
@@ -47,10 +49,17 @@ fovm f = do defs <- parseFOVM f
4749 let tagged = addTags defs
4850 let ctxtIn = addAlist tagged emptyContext
4951 let checked = checkDefs ctxtIn tagged
50− print checked
52+ -- print checked
5153 case checked of
5254 OK c -> do let bc = map toBC c
53− print bc
55+ -- print bc
56+ let h = concatMap toDecl (map fst bc)
57+ let cc = concatMap (uncurry toC) bc
58+ putStrLn h
59+ putStrLn cc
60+ d <- getDataDir
61+ mprog <- readFile (d ++ "/rts/idris_main.c")
62+ putStrLn mprog
5463
5564parseFOVM :: FilePath -> IO [(Name, LDecl)]
5665parseFOVM fname = do putStrLn $ "Reading " ++ fname