1+ {-# LANGUAGE OverloadedStrings #-}
2+ module MicroHsExp where
3+
4+ import CLTerm
5+ import MicroHs.Exp
6+ import MicroHs.Expr
7+ import MicroHs.ExpPrint (toStringCMdl )
8+ import MicroHs.Desugar (LDef )
9+ import MicroHs.Ident
10+
11+ {- -
12+
13+ data Exp
14+ = Var Ident
15+ | App Exp Exp
16+ | Lam Ident Exp
17+ | Lit Lit
18+ deriving (Eq)
19+
20+ type LDef = (Ident, Exp)
21+
22+ data SLoc = SLoc FilePath Line Col
23+ type Line = Int
24+ type Col = Int
25+
26+ data Ident = Ident SLoc Text
27+
28+
29+ -- The argument is all definitions and the main expression.
30+ -- The result is the number of definitions, all foreign export identifiers, and the program as a string.
31+ toStringCMdl :: ([LDef], Exp) -> (Int, [Ident], String)
32+
33+
34+ --}
35+
36+ example :: Exp
37+ example = Lit (LInt 42 )
38+ -- Var sampleIdent
39+ -- `App` Lam sampleIdent (Var sampleIdent)
40+ -- `App` Lit (LInt 42)
41+
42+ sampleIdent :: Ident
43+ sampleIdent = mkIdent " sample"
44+
45+ samplePrg :: ([LDef ], Exp )
46+ samplePrg = ([(sampleIdent, example)], example)
47+
48+ addExp :: Exp
49+ addExp = App (Lit (LPrim " IO.print" )) (App (App (Lit (LPrim " +" )) (Lit (LInt 42 ))) (Lit (LInt 23 )))
50+
51+ addPrg :: ([LDef ], Exp )
52+ addPrg = ([] , addExp)
53+
54+
55+ toMhsExp :: CL -> Exp
56+ toMhsExp (Com c) = Lit (LPrim (show c))
57+ toMhsExp (INT i) = Lit (LInt (fromIntegral i))
58+ toMhsExp (t :@ u) = App (toMhsExp t) (toMhsExp u)
59+
60+ toMhsPrg :: CL -> String
61+ toMhsPrg cl = let (n, exps, prg) = toStringCMdl ([] , toMhsExp cl)
62+ in prg
63+
64+ toIon :: CL -> String
65+ toIon (Com c) = show c
66+ toIon (INT i) = " (" ++ show i ++ " )"
67+ toIon (t :@ u) = " `" ++ toIon t ++ toIon u
68+
69+ test :: CL
70+ test = Com B :@ Com S :@ (Com B :@ Com B )
71+
72+ main :: IO ()
73+ main = do
74+
75+ let (n, exps, prg) = toStringCMdl addPrg
76+ putStrLn $ " Number of definitions: " ++ show n
77+ putStrLn $ " Foreign exports: " ++ show exps
78+ putStrLn $ " Program: " ++ prg
79+
80+ -- let (n, exps, prg) = toStringCMdl samplePrg
81+ -- putStrLn $ "Number of definitions: " ++ show n
82+ -- putStrLn $ "Foreign exports: " ++ show exps
83+ -- putStrLn $ "Program: " ++ prg
84+
85+ -- let mhsExp = toMhsExp test
86+ -- putStrLn $ "MicroHs expression: " ++ show mhsExp
87+
88+ -- putStrLn $ "Test expression: " ++ (show $ toStringCMdl ([], mhsExp))
0 commit comments