-
Notifications
You must be signed in to change notification settings - Fork 138
/
stack.pir
85 lines (72 loc) · 1.32 KB
/
stack.pir
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# Copyright (C) 2002-2009, Parrot Foundation.
#
# stack__duplicate()
#
# duplicate element at top of stack
# befunge stack:
# before: ... v
# after: ... v v
# no return value
#
.sub "stack__duplicate"
$I0 = stack__pop()
$P0 = get_global "stack"
push $P0, $I0
push $P0, $I0
set_global "stack", $P0
.return()
.end
#
# val = stack__pop()
#
# pop and return a value from the stack, or 0 if stack is empty.
# befunge stack:
# before: ... v
# after: ...
#
.sub "stack__pop"
$P0 = get_global "stack"
$I0 = $P0
if $I0 > 0 goto STACK__POP__POP
.return(0)
STACK__POP__POP:
$I1 = pop $P0
set_global "stack", $P0
.return($I1)
.end
#
# stack__push(v)
#
# push a value on the stack.
# befunge stack:
# before: ...
# after: ... v
# no return value
#
.sub "stack__push"
.param int v
$P0 = get_global "stack"
push $P0, v
set_global "stack", $P0
.end
#
# stack__swap()
#
# swap.
# befunge stack:
# before: ... a b
# after: ... b a
# no return value
#
.sub "stack__swap"
$I0 = stack__pop()
$I1 = stack__pop()
stack__push($I0)
stack__push($I1)
.end
########################################################################
# Local Variables:
# mode: pir
# fill-column: 100
# End:
# vim: expandtab shiftwidth=4 ft=pir: