add infix and preamble
[fp.git] / ast.icl
1 implementation module ast
2
3 import StdList
4 import StdOverloaded
5 import Data.Func
6 import Text.PPrint
7
8 instance toString AST where toString a = display $ renderCompact $ pretty a
9 instance toString Function where toString a = display $ renderCompact $ pretty a
10 instance toString Expression where toString a = display $ renderCompact $ pretty a
11 instance toString Value where toString a = display $ renderCompact $ pretty a
12
13 instance Pretty Expression
14 where
15 pretty (Literal v) = pretty v
16 pretty (Variable v) = string v
17 pretty (Apply a b) = parens (pretty a <+> pretty b)
18 pretty (Lambda a b) = string "\\" <-> string a <-> string "->" <-> pretty b
19 pretty (Code b) = string "code" <+> string b
20
21 instance Pretty Function
22 where
23 pretty (Function n args a) = string n <+> fold (<+>) (map string args) <+> string "=" <+> pretty a
24
25 instance Pretty AST
26 where
27 pretty (AST a) = pretty a
28
29 instance Pretty Value
30 where
31 pretty (Int i) = int i
32 pretty (Bool b) = bool b
33 pretty (Char c) = char c