picolisp/src/Evaluator.hs

22 lines
695 B
Haskell
Raw Normal View History

module Evaluator where
import Expression
import Primitives
2021-02-27 23:20:42 +00:00
-- Evals (reduces) an expression
2021-02-24 19:23:58 +00:00
eval :: Expression -> Either String Expression
eval n@(Number _) = Right n
eval s@(Symbol _) = Right s
2021-02-27 23:20:42 +00:00
eval (SExpr es) = apply . SExpr =<< mapM eval es
2021-02-27 23:20:42 +00:00
-- Applies, in a SExpr, a function to its arguments
2021-02-24 19:23:58 +00:00
apply :: Expression -> Either String Expression
apply (SExpr (f:args)) = case f of
2021-02-24 19:23:58 +00:00
Symbol "+" -> Right $ pAdd args
Symbol "-" -> Right $ pSub args
Symbol "*" -> Right $ pMul args
Symbol "/" -> Right $ pDiv args
2021-02-27 23:20:42 +00:00
Symbol x -> Left $ show x ++ ": no primitive functions not supported"
_ -> Left "object not applicable"
2021-02-24 19:23:58 +00:00
apply _ = Left "object not applicable"