module AST.Ast (Ast(..), showAst, printAst, cleanAst) where
import Common.Type.Integer (IntValue(..), intValueToInt)
import qualified Data.Text as DT
data Ast
= AInteger IntValue
| ABool Bool
| ASymbol DT.Text
| AVoid
| AList [Ast]
| ADefineFunc DT.Text [(DT.Text, DT.Text)] DT.Text Ast
| ADefineLambda [DT.Text] Ast
| ADefineStruct DT.Text [(DT.Text, DT.Text)]
| ASetVar DT.Text DT.Text Ast
| ASetStruct DT.Text [(DT.Text, Ast)]
| ACall Ast [Ast]
| AAccessStruct Ast DT.Text
| AImport DT.Text
| AIf Ast Ast Ast
| AWhile Ast Ast
| AFor Ast Ast Ast Ast
| AReturn Ast
| AExprStmt Ast
| ABlock [Ast]
| ABreak
| APos Int Int Ast
deriving (Int -> Ast -> ShowS
[Ast] -> ShowS
Ast -> String
(Int -> Ast -> ShowS)
-> (Ast -> String) -> ([Ast] -> ShowS) -> Show Ast
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
$cshowsPrec :: Int -> Ast -> ShowS
showsPrec :: Int -> Ast -> ShowS
$cshow :: Ast -> String
show :: Ast -> String
$cshowList :: [Ast] -> ShowS
showList :: [Ast] -> ShowS
Show, Ast -> Ast -> Bool
(Ast -> Ast -> Bool) -> (Ast -> Ast -> Bool) -> Eq Ast
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
$c== :: Ast -> Ast -> Bool
== :: Ast -> Ast -> Bool
$c/= :: Ast -> Ast -> Bool
/= :: Ast -> Ast -> Bool
Eq)
showAst :: Ast -> String
showAst :: Ast -> String
showAst (AInteger IntValue
i) = Int -> String
forall a. Show a => a -> String
Prelude.show (Int -> String) -> Int -> String
forall a b. (a -> b) -> a -> b
$ IntValue -> Int
intValueToInt IntValue
i
showAst (ABool Bool
True) = String
"#t"
showAst (ABool Bool
False) = String
"#f"
showAst (ASymbol Text
s) = Text -> String
DT.unpack Text
s
showAst (AList [Ast]
xs) = String
"(" String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
Prelude.unwords ((Ast -> String) -> [Ast] -> [String]
forall a b. (a -> b) -> [a] -> [b]
Prelude.map Ast -> String
showAst [Ast]
xs) String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
")"
showAst (ADefineLambda [Text]
_ Ast
_) = String
"#<lambda>"
showAst (APos Int
_ Int
_ Ast
ast) = Ast -> String
showAst Ast
ast
showAst (ABlock [Ast]
xs) = String
"{ " String -> ShowS
forall a. [a] -> [a] -> [a]
++ [String] -> String
unwords ((Ast -> String) -> [Ast] -> [String]
forall a b. (a -> b) -> [a] -> [b]
map Ast -> String
showAst [Ast]
xs) String -> ShowS
forall a. [a] -> [a] -> [a]
++ String
" }"
showAst Ast
ABreak = String
"break"
showAst Ast
other = Ast -> String
forall a. Show a => a -> String
Prelude.show Ast
other
printAst :: Ast -> IO ()
printAst :: Ast -> IO ()
printAst Ast
ast = String -> IO ()
putStrLn (Ast -> String
forall a. Show a => a -> String
Prelude.show Ast
ast)
cleanAst :: Ast -> Ast
cleanAst :: Ast -> Ast
cleanAst (APos Int
_ Int
_ Ast
ast) = Ast -> Ast
cleanAst Ast
ast
cleanAst (ABlock [Ast]
xs) = [Ast] -> Ast
ABlock ((Ast -> Ast) -> [Ast] -> [Ast]
forall a b. (a -> b) -> [a] -> [b]
map Ast -> Ast
cleanAst [Ast]
xs)
cleanAst (AList [Ast]
xs) = [Ast] -> Ast
AList ((Ast -> Ast) -> [Ast] -> [Ast]
forall a b. (a -> b) -> [a] -> [b]
map Ast -> Ast
cleanAst [Ast]
xs)
cleanAst (ADefineFunc Text
n [(Text, Text)]
a Text
r Ast
b) = Text -> [(Text, Text)] -> Text -> Ast -> Ast
ADefineFunc Text
n [(Text, Text)]
a Text
r (Ast -> Ast
cleanAst Ast
b)
cleanAst (ADefineLambda [Text]
args Ast
body) = [Text] -> Ast -> Ast
ADefineLambda [Text]
args (Ast -> Ast
cleanAst Ast
body)
cleanAst (ASetVar Text
n Text
t Ast
expr) = Text -> Text -> Ast -> Ast
ASetVar Text
n Text
t (Ast -> Ast
cleanAst Ast
expr)
cleanAst (ASetStruct Text
n [(Text, Ast)]
fields) =
Text -> [(Text, Ast)] -> Ast
ASetStruct Text
n (((Text, Ast) -> (Text, Ast)) -> [(Text, Ast)] -> [(Text, Ast)]
forall a b. (a -> b) -> [a] -> [b]
map (\(Text
f, Ast
a) -> (Text
f, Ast -> Ast
cleanAst Ast
a)) [(Text, Ast)]
fields)
cleanAst (ACall Ast
func [Ast]
args) = Ast -> [Ast] -> Ast
ACall (Ast -> Ast
cleanAst Ast
func) ((Ast -> Ast) -> [Ast] -> [Ast]
forall a b. (a -> b) -> [a] -> [b]
map Ast -> Ast
cleanAst [Ast]
args)
cleanAst (AIf Ast
c Ast
t Ast
e) = Ast -> Ast -> Ast -> Ast
AIf (Ast -> Ast
cleanAst Ast
c) (Ast -> Ast
cleanAst Ast
t) (Ast -> Ast
cleanAst Ast
e)
cleanAst (AWhile Ast
c Ast
b) = Ast -> Ast -> Ast
AWhile (Ast -> Ast
cleanAst Ast
c) (Ast -> Ast
cleanAst Ast
b)
cleanAst (AFor Ast
i Ast
c Ast
u Ast
b) =
Ast -> Ast -> Ast -> Ast -> Ast
AFor (Ast -> Ast
cleanAst Ast
i) (Ast -> Ast
cleanAst Ast
c) (Ast -> Ast
cleanAst Ast
u) (Ast -> Ast
cleanAst Ast
b)
cleanAst (AReturn Ast
e) = Ast -> Ast
AReturn (Ast -> Ast
cleanAst Ast
e)
cleanAst (AExprStmt Ast
e) = Ast -> Ast
AExprStmt (Ast -> Ast
cleanAst Ast
e)
cleanAst (AAccessStruct Ast
obj Text
field) = Ast -> Text -> Ast
AAccessStruct (Ast -> Ast
cleanAst Ast
obj) Text
field
cleanAst Ast
ABreak = Ast
ABreak
cleanAst Ast
other = Ast
other