我正在尝试编写一个程序,该程序接受任何通用语句,对其进行评估,并在评估时显示每个步骤。
例如,如果我们有一系列赋值语句,它应该运行为:
评估作业声明 1
显示评估结果1
评估赋值语句 2
显示评估结果 2
现在,它只显示最终结果。代码是
> evalS_maybe :: Statement -> Store -> Maybe Store
> evalS_maybe w@(While e s1) s = evalS_maybe (If e
> (Sequence s1 (While e s1))
> Skip)
> s
> evalS_maybe Skip s = Just s
> evalS_maybe (Sequence s1 s2) s = do
> sq <-evalS_maybe s1 s
> evalS_maybe s2 sq
> evalS_maybe (Assign x e ) s = do
> ag <-evalE_maybe e s
> return ( Map.insert x ag s )
> evalS_maybe (If e s1 s2) s = do
> b2 <- evalE_maybe e s
> case b2 of
> BoolVal True -> evalS_maybe s1 s
> BoolVal False -> evalS_maybe s2 s
> _ -> return s
编辑:
其余使用的数据类型是:
> type Variable = String
> data Statement =
> Assign Variable Expression
> | If Expression Statement Statement
> | While Expression Statement
> | Sequence Statement Statement
> | Skip
> deriving (Show)
> data Expression =
> Var Variable
> | Val Value
> | Op Bop Expression Expression
> deriving (Show)
> data Bop =
> Plus
> | Minus
> | Times
> | Divide
> | Gt
> | Ge
> | Lt
> | Le
> deriving (Show)
> data Value =
> IntVal Int
> | BoolVal Bool
> deriving (Show)
> type Store = Map Variable Value
有人可以帮忙吗?提前致谢!