-
Notifications
You must be signed in to change notification settings - Fork 0
/
C++Front.hs
40 lines (33 loc) · 1 KB
/
C++Front.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
{-# LANGUAGE QuasiQuotes #-}
module Main where
import LamParser
import Transformer
import StringQQ
----------------------------------------------------------------------
-- compile
compileHelper :: [Symbol] -> LamExpr -> String
compileHelper env (Sym s) =
if elem s env
then s
else "NForm_ptr(new NForm(\"" ++ s ++ "\"))"
compileHelper env (Lam s e) = "lambda(" ++ s ++ "," ++ compileHelper (s:env) e ++ ")"
compileHelper env (App f p) =
let fn = compileHelper env f
pn = compileHelper env p
in "apply(" ++ fn ++ "," ++ pn ++ ")"
compile :: LamExpr -> String
compile = compileHelper [] . rename
addMain :: String -> String
addMain s = "int main() {std::cout << show("
++ s
++ ") << std::endl;return 0; }\n"
----------------------------------------------------------------------
headerStr :: String
headerStr = [stringQQ|
#include "lam_header.h"
|]
main :: IO ()
main =
do val <- getContents
putStrLn headerStr
putStrLn $ addMain $ compile $ readExpr val