-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstack.mdtlbl
More file actions
145 lines (136 loc) · 3.95 KB
/
stack.mdtlbl
File metadata and controls
145 lines (136 loc) · 3.95 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#**
* 栈包装, 可以方便的以栈形式操作内存, 配合 function 可以很方便的操作
* 批量读取时, 左边的参数应该更靠近栈顶
*#
Builtin.BindSep! '.';
const NewStack = (match @ {
$Memory {
NewStack! Memory Memory 0;
}
Memory Floor {
take Result = $;
NewStack! Result Memory Floor;
}
$Result Memory Floor {
$.top = Floor - 1;
const $.Floor = Floor;
const $.Memory = Memory;
const $.Iter = (match @ => {
const $.Prev = ..->top;
const $.Memory = ..->Memory;
const $.Get = (match @ => { setres ...Prev; });
const $.GetTo = (match @ => $Result { Result = ...Prev; });
const $.RawNext = (match @ {
B { setres ...RawNext[B 1]; }
B Step {
const $.Prev = B->Prev;
const $.Memory = B->Memory;
const $.Step = Step;
const $.Get = (match @ => { setres ...GetTo[$]; });
const $.GetTo = (match @ => $Result {
Result = ...Prev - ...Step;
const ...Get = Result;
const ...Prev = Result;
const ...GetTo = (match @ => $Result {
Result = ...Get;
});
});
const $.RawNext = B->RawNext;
const $.Next = (setres ...RawNext[*.. @];);
const $.Read = (match @ {
{ setres ...Read[$]; }
$Result { read Result ...Memory ...Get[]; }
});
const $.Write = (match @ => Value {
setres ..;
write Value ...Memory ...Get[];
});
}
});
const $.Next = (setres ...RawNext[*.. @];);
const $.Read = (match @ {
{ setres ...Read[$]; }
$Result { read Result ...Memory ...Get[]; }
});
const $.Write = (match @ => Value {
setres ..;
write Value ...Memory ...Get[];
});
});
const $.Read = (match @ {
{ setres ...Read[$]; }
$Result @ {
take Iter = ...Iter[];
Iter.Read! Result;
take Iter = Iter.Next[];
inline@ Result {
Iter.Read! Result;
take Iter = Iter.Next[];
}
}
});
const $.Write = (match @ => $Value @ {
take Iter = ...Iter[].Write[Value].Next[];
inline@ Value {
take Iter = Iter.Write[Value].Next[];
}
});
const $.Push = (match @ {
{}
@ Value {
write Value ...Memory (*++...top);
...Push! @;
}
});
const $.Pop = (match @ {
{
...top--;
setres ..;
}
$Result {
...Read! Result;
...top--;
}
@ {
inline@ Result {
...Read! Result;
...top--;
}
}
});
}
});
NewStack! bank1;
bank1.Push! 1 2 3;
print bank1.Read[a b c];
print b c;
bank1.Write! a b c;
bank1.Pop!;
bank1.Pop! x;
print x;
#* >>>
op sub bank1.top 0 1
op add bank1.top bank1.top 1
write 3 bank1 bank1.top
op add bank1.top bank1.top 1
write 2 bank1 bank1.top
op add bank1.top bank1.top 1
write 1 bank1 bank1.top
read a bank1 bank1.top
op sub __41 bank1.top 1
read b bank1 __41
op sub __53 __41 1
read c bank1 __53
print a
print b
print c
write a bank1 bank1.top
op sub __81 bank1.top 1
write b bank1 __81
op sub __93 __81 1
write c bank1 __93
op sub bank1.top bank1.top 1
read x bank1 bank1.top
op sub bank1.top bank1.top 1
print x
*#