-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmempack.mdtlbl
More file actions
89 lines (84 loc) · 2 KB
/
mempack.mdtlbl
File metadata and controls
89 lines (84 loc) · 2 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
#**
* 用于方便的在多个逻辑中利用共享内存块传递数字,
* 进行简单的包装, 避免直接使用地址进行不利于维护的读写
*
* Load: 读取内存并更新值
* Store: 更新值并写入内存
* Read: 仅读取内存
* Write: 仅写入内存
* Exchange: 将本地值和内存值交换, 或写入本地值并读入内存值
*#
Builtin.BindSep! '.';
Builtin.MissesMatch! 1;
const MemPack = (match @ {
[@impl] Mem Addr $Handle {
const $.Mem = Mem;
const $.Addr = Addr;
const $.Store = ([Mem Addr ..H](match @ {
{
setres H;
write H Mem Addr;
}
Value {
H = Value;
setres H.Store[];
}
}));
const $.Load = ([Mem Addr ..H](match @ {
{
setres H;
read H Mem Addr;
}
Result {
setres H;
Result = H;
read H Mem Addr;
}
}));
const $.Read = ([Mem Addr ..H](match @ {
{
read $ Mem Addr;
}
$Result {
read Result Mem Addr;
}
}));
const $.Write = ([Mem Addr ..H](match @ {
Value {
setres H;
write Value Mem Addr;
}
}));
const $.Exchange = ([Mem Addr ..H](match @ {
$Result {
read Result Mem Addr;
write H Mem Addr;
}
{
setres H;
H.Exchange! +Tmp;
H = Tmp;
}
}));
}
Mem Addr Fst @ {
MemPack! @impl Mem Addr Fst;
inline@ Handle {
take*Addr = Addr+1;
MemPack! @impl Mem Addr Handle;
}
}
});
MemPack! cell1 0, num foo;
num.Store! 2;
foo.Write! 3;
print num", "foo.Load[];
#* >>>
set num 2
write num cell1 0
write 3 cell1 1
print num
print ", "
read foo cell1 1
print foo
*#