Minor improvements
This commit is contained in:
parent
24ed67ae71
commit
fd39ca4129
|
@ -8,17 +8,18 @@ type Proc = String
|
||||||
type Args = [Expr]
|
type Args = [Expr]
|
||||||
|
|
||||||
eval :: Expr -> Expr
|
eval :: Expr -> Expr
|
||||||
eval (IntE x) = IntE x
|
eval (IntE x) = IntE x
|
||||||
eval (VarE v) = VarE v
|
eval (VarE v) = VarE v
|
||||||
eval (DefinitionE v exp) = NilE
|
eval (DefinitionE v exp) = NilE
|
||||||
eval (ProcedureE p args) = apply p args
|
eval (ProcedureE p args) = apply p args
|
||||||
eval NilE = NilE
|
eval NilE = NilE
|
||||||
|
|
||||||
apply :: Proc -> Args -> Expr
|
apply :: Proc -> Args -> Expr
|
||||||
apply p args
|
apply p args
|
||||||
| p == "+" = builtinPlus $ map eval args
|
| p == "+" = builtinPlus $ map eval args
|
||||||
|
-- TODO: create a separated file for builtinProcs
|
||||||
-- TODO: create a BuiltinProc or something like that in data Expr, and make + a builtin proc
|
-- TODO: create a BuiltinProc or something like that in data Expr, and make + a builtin proc
|
||||||
|
-- TODO: create enviroments
|
||||||
|
|
||||||
builtinPlus :: [Expr] -> Expr
|
builtinPlus :: [Expr] -> Expr
|
||||||
builtinPlus [IntE x, IntE y] = IntE $ x + y
|
builtinPlus [IntE x, IntE y] = IntE $ x + y
|
||||||
|
|
|
@ -9,11 +9,14 @@ import ExprType
|
||||||
parseExpression :: String -> Either ParseError Expr
|
parseExpression :: String -> Either ParseError Expr
|
||||||
parseExpression s = do
|
parseExpression s = do
|
||||||
tokns <- parse parseTokens "lexical error" s
|
tokns <- parse parseTokens "lexical error" s
|
||||||
expr <- parse parseExpressionFromTokens "parsing error" tokns
|
expr <- parse expressionFromTokens "parsing error" tokns
|
||||||
return expr
|
return expr
|
||||||
|
|
||||||
parseExpressionFromTokens :: GenParser Token st Expr
|
expressionFromTokens :: GenParser Token st Expr
|
||||||
parseExpressionFromTokens = intE <|> try defineE <|> try nilE <|> varE <|> procedureE
|
expressionFromTokens = do
|
||||||
|
expr <- intE <|> try defineE <|> try nilE <|> varE <|> procedureE
|
||||||
|
_ <- eof
|
||||||
|
return expr
|
||||||
|
|
||||||
intE :: GenParser Token st Expr
|
intE :: GenParser Token st Expr
|
||||||
intE = do
|
intE = do
|
||||||
|
@ -24,7 +27,7 @@ procedureE :: GenParser Token st Expr
|
||||||
procedureE = do
|
procedureE = do
|
||||||
_ <- parseLeftParenT
|
_ <- parseLeftParenT
|
||||||
(VarT p) <- parseVarT
|
(VarT p) <- parseVarT
|
||||||
args <- many parseExpressionFromTokens
|
args <- many expressionFromTokens
|
||||||
_ <- parseRightParenT
|
_ <- parseRightParenT
|
||||||
return $ ProcedureE p args
|
return $ ProcedureE p args
|
||||||
|
|
||||||
|
@ -43,7 +46,7 @@ defineE = do
|
||||||
_ <- parseLeftParenT
|
_ <- parseLeftParenT
|
||||||
_ <- parseDefineT
|
_ <- parseDefineT
|
||||||
(VarT var) <- parseVarT
|
(VarT var) <- parseVarT
|
||||||
expr <- parseExpressionFromTokens
|
expr <- expressionFromTokens
|
||||||
_ <- parseRightParenT
|
_ <- parseRightParenT
|
||||||
return $ DefinitionE var expr
|
return $ DefinitionE var expr
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue