Separate AST into ast.hs and add unit type/value
This commit is contained in:
parent
acce1a15dd
commit
54ea8baa54
|
@ -0,0 +1,22 @@
|
||||||
|
module AST where
|
||||||
|
|
||||||
|
data AST = Add AST AST
|
||||||
|
| Mul AST AST
|
||||||
|
| Block [AST]
|
||||||
|
| FunDef String (Pattern, AST)
|
||||||
|
| Def String AST
|
||||||
|
| Var String
|
||||||
|
| Lambda [(Pattern, [AST])]
|
||||||
|
| Call String [AST]
|
||||||
|
| UnitConst
|
||||||
|
| ListConst [AST]
|
||||||
|
| StrConst String
|
||||||
|
| IntConst Integer
|
||||||
|
deriving (Show, Eq)
|
||||||
|
|
||||||
|
data Pattern = VarP String
|
||||||
|
| IntP Integer
|
||||||
|
| UnitP
|
||||||
|
| ConsP Pattern Pattern
|
||||||
|
| ListP [Pattern]
|
||||||
|
deriving (Show, Eq)
|
26
interp.hs
26
interp.hs
|
@ -1,24 +1,11 @@
|
||||||
import Control.Monad.State (State, runState, evalState, get, put)
|
import Control.Monad.State (State, runState, evalState, get, put)
|
||||||
import qualified Data.Map as M
|
import qualified Data.Map as M
|
||||||
|
import AST
|
||||||
|
import Parser (parseProgram)
|
||||||
|
|
||||||
data AST = Add AST AST
|
data Value = IntV Integer
|
||||||
| Def String AST
|
|
||||||
| Var String
|
|
||||||
| Lambda [(Pattern, [AST])]
|
|
||||||
| Call String [AST]
|
|
||||||
| ListConst [AST]
|
|
||||||
| StrConst String
|
|
||||||
| IntConst Int
|
|
||||||
deriving (Show, Eq)
|
|
||||||
|
|
||||||
data Pattern = VarP String
|
|
||||||
| IntP Int
|
|
||||||
| ConsP Pattern Pattern
|
|
||||||
| ListP [Pattern]
|
|
||||||
deriving (Show, Eq)
|
|
||||||
|
|
||||||
data Value = IntV Int
|
|
||||||
| StrV String
|
| StrV String
|
||||||
|
| UnitV
|
||||||
| ListV [Value]
|
| ListV [Value]
|
||||||
| FnV [(Pattern, [AST])] -- pattern->body bindings
|
| FnV [(Pattern, [AST])] -- pattern->body bindings
|
||||||
deriving (Show, Eq)
|
deriving (Show, Eq)
|
||||||
|
@ -37,6 +24,8 @@ eval :: AST -> InterpState Value
|
||||||
eval (IntConst i) = return $ IntV i
|
eval (IntConst i) = return $ IntV i
|
||||||
eval (StrConst s) = return $ StrV s
|
eval (StrConst s) = return $ StrV s
|
||||||
|
|
||||||
|
eval UnitConst = return UnitV
|
||||||
|
|
||||||
eval (ListConst v) =
|
eval (ListConst v) =
|
||||||
mapM eval v >>= \xs ->
|
mapM eval v >>= \xs ->
|
||||||
return $ ListV xs
|
return $ ListV xs
|
||||||
|
@ -76,6 +65,9 @@ patternBindings (IntP n) (IntV v)
|
||||||
| otherwise = Nothing
|
| otherwise = Nothing
|
||||||
patternBindings (IntP n) _ = Nothing
|
patternBindings (IntP n) _ = Nothing
|
||||||
|
|
||||||
|
patternBindings UnitP UnitV = Just M.empty
|
||||||
|
patternBindings UnitP _ = Nothing
|
||||||
|
|
||||||
patternBindings (ConsP x (ListP [])) (ListV (y:[])) = patternBindings x y
|
patternBindings (ConsP x (ListP [])) (ListV (y:[])) = patternBindings x y
|
||||||
patternBindings (ConsP _ _) (ListV (_:[])) = Nothing
|
patternBindings (ConsP _ _) (ListV (_:[])) = Nothing
|
||||||
patternBindings (ConsP xp xsp) (ListV (x:xs)) =
|
patternBindings (ConsP xp xsp) (ListV (x:xs)) =
|
||||||
|
|
Loading…
Reference in New Issue