Skip to content

Commit

Permalink
add compile to c command: ucomp
Browse files Browse the repository at this point in the history
  • Loading branch information
Haoqiang Fan committed Jan 1, 2017
1 parent a152a19 commit d588177
Show file tree
Hide file tree
Showing 4 changed files with 920 additions and 32 deletions.
17 changes: 17 additions & 0 deletions UCompile.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module UCompile where
import ULambdaExpression
import qualified Data.Map as Map

compileToC :: LExpr -> [Char]
compileToC l = header++compileToCS l (Map.fromList []) 0 footer where
header="void main_program(){\n"
footer=";\n}"
compileToCS :: LExpr -> Map.Map [Char] Int -> Int -> ShowS
compileToCS l names curlevel = case l of
LInt v -> showString ("makeInt("++(show v)++")")
LDouble v -> showString ("makeDouble("++(show v)++")")
LRef key -> case (Map.member key names) of
True -> showString ("makeRef("++(show (curlevel-(names Map.! key)))++")")
False -> showString ("makeBuiltin(\""++key++"\")")
LAbs key l2 -> showString "makeAbs(\n" . compileToCS l2 (Map.insert key curlevel names) (curlevel+1) . showString ")"
LApply l1 l2 -> showString "makeApply(\n" . (compileToCS l1 names curlevel) . showString ",\n" . (compileToCS l2 names curlevel) . showString ")"
Loading

0 comments on commit d588177

Please sign in to comment.