-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGenerator.hs
98 lines (80 loc) · 3.09 KB
/
Generator.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
{-# LANGUAGE UnicodeSyntax #-}
module Generator
( runString
, genString
, genFile
, runFile
, generateTable
, expandExpression
) where
---- Language Import
import AST
import Unlambda.AST
import Encoding
import Config
import ErrorHandler
import qualified Unlambda.Run as Unlambda hiding (runFile)
---- Parser Import
import Parser
-- import Text.Regex
---- Translation Import
import Translator
---- Table Management Import
import Control.Monad.State
import Control.Monad
getFunction :: String -> SymbolTable -> (Bλ, Int)
getFunction name table = case lookup name table of
Just s -> s
Nothing -> failWith $ "Generator Error\nfunction " ++ name ++ " does not exist"
prependTable :: Symbol -> StateT SymbolTable IO ()
prependTable s = state $ \ss -> ((), s:ss)
partialApply :: Integer -> Unlambda -> [Unlambda]
partialApply arity unl = take (fromInteger arity + 1) (iterate ('`':) unl)
expandExpression :: Bλ -> SymbolTable -> Bλ
expandExpression λ table = eE λ where
eE (Idx n) = Idx n
eE (Unl s) = Unl s
eE (Abs λ) = Abs (eE λ)
eE (App e l r) = if e
then reduceToNormal $ App e (eE l) (eE r)
else App e (eE l) (eE r)
eE (Fun "__EQ__" args) = let xs = map (reduceToNormal . eE) args in
if all (== head xs) (tail xs)
then Abs $ Abs (Idx 1)
else Abs $ Abs (Idx 0)
eE (Fun name args) = let (function, arity) = getFunction name table in
if length args > arity
then failWith $ "Generator Error\ntoo many arguments, " ++ name ++ " has arity " ++ show arity
else eE $ foldl (App True) function args
generateTable :: Sequence -> StateT SymbolTable IO Unlambda
generateTable [] = failWith "Generator Error\nnon-library files must terminate with an evaluation (!!)"
generateTable ((Import file):ss) = do
lib <- lift $ parseFile (Config.preludePath ++ file ++ ".bru")
generateTable (lib ++ ss)
generateTable [Express λ] = do
table <- get
let formatλ = case lookup "__FORMATTER__" table of
Just (f,_) -> App True f λ
Nothing -> λ
return $ generate (expandExpression formatλ table)
generateTable ((Assign name λ a):ss) = do
curr <- get
let resolved = expandExpression λ curr
prependTable (name, (resolved, fromInteger a))
generateTable ss
genString :: String -> IO (Unlambda, SymbolTable)
genString s = runStateT (generateTable . parseString $ s) []
runString :: String -> IO Eλ
runString s = do
prog <- fmap fst . genString $ s
Unlambda.run prog
genFile :: FilePath -> IO (Unlambda, SymbolTable)
genFile f = do
file <- parseFile f
runStateT (generateTable file) []
runFile :: FilePath -> IO Eλ
runFile f = do
prog <- fmap fst . genFile $ f
Unlambda.run prog
-- TODO possibly fix muplite Express statements
-- FIXME or not, nah