diff --git a/src/Evaluator.hs b/src/Evaluator.hs index 4d2d982..b5b8bd6 100644 --- a/src/Evaluator.hs +++ b/src/Evaluator.hs @@ -8,17 +8,18 @@ type Proc = String type Args = [Expr] eval :: Expr -> Expr -eval (IntE x) = IntE x -eval (VarE v) = VarE v -eval (DefinitionE v exp) = NilE -eval (ProcedureE p args) = apply p args -eval NilE = NilE +eval (IntE x) = IntE x +eval (VarE v) = VarE v +eval (DefinitionE v exp) = NilE +eval (ProcedureE p args) = apply p args +eval NilE = NilE apply :: Proc -> Args -> Expr apply p 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 enviroments builtinPlus :: [Expr] -> Expr builtinPlus [IntE x, IntE y] = IntE $ x + y diff --git a/src/ExprParser.hs b/src/ExprParser.hs index 49e40c6..061b41c 100644 --- a/src/ExprParser.hs +++ b/src/ExprParser.hs @@ -9,11 +9,14 @@ import ExprType parseExpression :: String -> Either ParseError Expr parseExpression s = do tokns <- parse parseTokens "lexical error" s - expr <- parse parseExpressionFromTokens "parsing error" tokns + expr <- parse expressionFromTokens "parsing error" tokns return expr -parseExpressionFromTokens :: GenParser Token st Expr -parseExpressionFromTokens = intE <|> try defineE <|> try nilE <|> varE <|> procedureE +expressionFromTokens :: GenParser Token st Expr +expressionFromTokens = do + expr <- intE <|> try defineE <|> try nilE <|> varE <|> procedureE + _ <- eof + return expr intE :: GenParser Token st Expr intE = do @@ -24,7 +27,7 @@ procedureE :: GenParser Token st Expr procedureE = do _ <- parseLeftParenT (VarT p) <- parseVarT - args <- many parseExpressionFromTokens + args <- many expressionFromTokens _ <- parseRightParenT return $ ProcedureE p args @@ -43,7 +46,7 @@ defineE = do _ <- parseLeftParenT _ <- parseDefineT (VarT var) <- parseVarT - expr <- parseExpressionFromTokens + expr <- expressionFromTokens _ <- parseRightParenT return $ DefinitionE var expr