forked from nsmryan/HEAL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostfix.hs
More file actions
41 lines (38 loc) · 1.04 KB
/
Postfix.hs
File metadata and controls
41 lines (38 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
module Postfix (
postfix,
dup,
tuck,
over,
swap,
rot,
drp,
nip
) where
import Linear as L
import Operators
import Data.Monoid
import Prelude hiding (take)
import Data.Foldable as F
-- Postfix evaluator for a sequence of operators
postfix ops = if null result then Nothing else Just (L.head result) where
result = applyOp (F.foldl mappend mempty ops) []
--helper functions
dup' (a:as) = a:a:as
dup' as = as
tuck' (a:a':as) = a:a':a:as
tuck' as = as
over' (a:a':as) = a':a:a':as
over' as = as
rot' (a:a':a'':as) = a'':a:a':as
rot' as = as
swap' = drp' . tuck'
drp' = Prelude.drop 1
nip' = drp' . swap'
-- Operators for basic stack manipulation.
dup = OP { eats=1, leaves=2, applyOp=dup', name="dup"}
tuck = OP { eats=2, leaves=3, applyOp=tuck', name="tuck"}
over = OP { eats=2, leaves=3, applyOp=over', name="over"}
swap = OP { eats=2, leaves=2, applyOp=swap', name="swap"}
rot = OP { eats=3, leaves=3, applyOp=rot', name="rot"}
drp = OP { eats=1, leaves=0, applyOp=drp', name="drop"}
nip = OP { eats=2, leaves=1, applyOp=nip', name="nip"}