1
0
Fork 0
agulator/agda/Eval.agda

21 lines
1.0 KiB
Plaintext
Raw Normal View History

module Eval where
open import Agda.Builtin.Bool
open import Agda.Builtin.Maybe
open import Agda.Builtin.Nat
open import Data.Nat.DivMod using (_/_)
open import Parse
evalBin : Result BinExpr → Result Nat
evalBin (emit nothing rem) = emit↑ rem
2022-09-30 00:43:35 +00:00
evalBin (emit (just (bin (Oper o) (Digit a) (Digit b))) rem) with o
... | '+' = emit↓ (a + b) rem
... | '-' = emit↓ (a - b) rem
... | '*' = emit↓ (a * b) rem
2022-09-30 01:16:32 +00:00
... | '/' with (b == zero)
... | true = emit↑ rem
2022-09-30 04:07:05 +00:00
... | false = emit↓ (a / (suc (b - 1))) rem -- todo: why -1 tho
2022-09-30 01:16:32 +00:00
evalBin (emit (just (bin (Oper o) (Digit a) (Digit b))) rem) | _ = emit↑ rem
evalBin (emit (just (bin _ _ _)) rem) = emit↑ rem