-
Notifications
You must be signed in to change notification settings - Fork 9
/
hash.ast
143 lines (117 loc) · 3.69 KB
/
hash.ast
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
------------------------------------------------------------------
-- hash.ast
--
-- this module implements the HashTable structure
--
-- (c) Lutz Hamel, University of Rhode Island
------------------------------------------------------------------
------------------------------------------------------------------
structure Hash with
------------------------------------------------------------------
-- Asteroid hash table implementation using Python dictionaries
------------------------------------------------------------------
data table.
------------------------------------------------------------------
function __init__
------------------------------------------------------------------
-- constructor for HashTable initializes the underlying dictionary
with none do escape
"
this_val = state.symbol_table.lookup_sym('this')
# destructure object
(OBJECT,
(STRUCT_ID, (ID, struct_id)),
(MEMBER_NAMES, (LIST, member_names)),
(OBJECT_MEMORY, (LIST, memory))) = this_val
# the table is in the first slot in the object memory
# store the dictionary as a foreign object in this slot
memory[0] = ('foreign', dict())
"
end -- __init__
------------------------------------------------------------------
function insert
------------------------------------------------------------------
with (name,value) do
return escape
"
global __retval__
this_val = state.symbol_table.lookup_sym('this')
name_val = state.symbol_table.lookup_sym('name')
value_val = state.symbol_table.lookup_sym('value')
# destructure object
(OBJECT,
(STRUCT_ID, (ID, struct_id)),
(MEMBER_NAMES, (LIST, member_names)),
(OBJECT_MEMORY, (LIST, memory))) = this_val
# the table is in the first slot in the object memory
# insert name-value pair
(FOREIGN, dictionary) = memory[0]
dictionary[name_val] = value_val
__retval__ = this_val
"
with item_list:%list do
for (k,v) in item_list do
this @insert (k,v).
end
return this
end -- insert
------------------------------------------------------------------
function get
------------------------------------------------------------------
with name do return escape
"
global __retval__
this_val = state.symbol_table.lookup_sym('this')
name_val = state.symbol_table.lookup_sym('name')
# destructure object
(OBJECT,
(STRUCT_ID, (ID, struct_id)),
(MEMBER_NAMES, (LIST, member_names)),
(OBJECT_MEMORY, (LIST, memory))) = this_val
# the table is in the first slot in the object memory
# get the value_val associated with name_val
(FOREIGN, dictionary) = memory[0]
if name_val not in dictionary.keys():
__retval__ = ('none', None)
else:
__retval__ = dictionary[name_val]
"
end -- get
------------------------------------------------------------------
function aslist
------------------------------------------------------------------
with none do return escape
"
global __retval__
this_val = state.symbol_table.lookup_sym('this')
# destructure object
(OBJECT,
(STRUCT_ID, (ID, struct_id)),
(MEMBER_NAMES, (LIST, member_names)),
(OBJECT_MEMORY, (LIST, memory))) = this_val
# the table is in the first slot in the object memory
# get the value_val associated with name_val
(FOREIGN, dictionary) = memory[0]
# zip the keys and the values
kl = dictionary.keys()
vl = dictionary.values()
l = list(zip(kl,vl))
# turn Python tuples into Asteroid tuples and
# put them onto an output list
out_list = []
for (i,t) in l:
out_list.append(('tuple',[i,t]))
__retval__ = ('list', out_list)
"
end -- aslist
------------------------------------------------------------------
function __str__
------------------------------------------------------------------
with none do
return tostring (this @aslist ()).
end -- __str__
end -- Hash structure
-- here for backwards compatibility
function hash with none do
return Hash().
end