Increase MAX_STATIC_DATA
[bazic.git] / store.h
1 global store_bottom = 0;
2 global store_eop = 0;
3 global store_top = 0;
4
5 constant VN_NEXT = 0;
6 constant VN_TYPE = 1;
7 constant VN_VALUE = 2;
8 constant VN__SIZE = (3*2);
9
10 global store_firstvar = 0;
11
12 ! Initialise the program store.
13
14 [ store_init bottom top;
15         store_bottom = bottom;
16         store_top = top;
17         store_bottom->0 = 0;
18         store_eop = store_bottom;
19         string_init();
20 ];
21
22 ! --- Heap management -------------------------------------------------------
23
24 ! Heap reinitialisation; all heap pointers are invalidated.
25
26 [ store_heapclean;
27         store_firstvar = 0;
28 ];
29
30 ! Create a new variable.
31
32 [ store_newvar varname type value  node;
33         node = mem_alloc(VN__SIZE + strlen(varname) + 1);
34         node-->VN_NEXT = store_firstvar;
35         node-->VN_TYPE = type;
36         node-->VN_VALUE = value;
37         strcpy(node+VN__SIZE, varname);
38         store_firstvar = node;
39 ];
40
41 ! Look for an old variable.
42
43 [ store_searchvar varname  node;
44         node = store_firstvar;
45         while (node)
46         {
47                 if (strcmp(varname, node+VN__SIZE) == 0)
48                         return node;
49                 node = node-->VN_NEXT;
50         }
51         return 0;
52 ];
53
54 ! Assign a value to a variable.
55
56 [ store_assign varname type value  node;
57         node = store_searchvar(varname);
58         if (node)
59         {
60                 node-->VN_TYPE = type;
61                 node-->VN_VALUE = value;
62                 return;
63         }
64         store_newvar(varname, type, value);
65 ];
66
67 ! Get the contents of a variable.
68
69 [ store_lookup varname  node val;
70         node = store_searchvar(varname);
71         if (node)
72         {
73                 val = mem_alloc(4);
74                 val-->0 = node-->VN_TYPE;
75                 val-->1 = node-->VN_VALUE;
76                 return val;
77         }
78         return 0;
79 ];
80
81 ! List all variables.
82
83 [ store_listvars  var count;
84         var = store_firstvar;
85         count = 0;
86         print "Variables:^";
87         while (var)
88         {
89                 print "  ", (astring) var+VN__SIZE;
90                 switch (var-->VN_TYPE)
91                 {
92                         TYPE_INT:
93                                 print " integer ", var-->VN_VALUE;
94                                 break;
95
96                         TYPE_STRING:
97                                 print " string ~";
98                                 string_print(var-->VN_VALUE);
99                                 print "~";
100                                 break;
101
102                         default:
103                                 print " unknown type";
104                 }
105                 print "^";
106                                 
107                 var = var-->VN_NEXT;
108                 count++;
109         }
110         print "Total: ", count, "^";
111 ];
112
113 ! --- Program storage -------------------------------------------------------
114
115 ! List the current program.
116
117 [ store_listprogram  line linelen;
118         line = store_bottom;
119         while (linelen = line->0)
120         {
121                 print " ", (line+1)-->0;
122                 detokenise_stream(line+3);
123                 line = line + linelen;
124         }
125 ];
126
127 [ store_listprogramhex  line linelen;
128         line = store_bottom;
129         while (linelen = line->0)
130         {
131                 print " ", (line+1)-->0;
132                 detokenise_stream(line+3);
133                 print "-> ";
134                 hexdump(line, linelen);
135                 line = line + linelen;
136         }
137 ];
138
139 ! Add a line to the program.
140
141 [ store_addline linenum cmd linelen  line i;
142         line = store_bottom;
143         while (line->0)
144         {
145                 if ((line+1)-->0 >= linenum)
146                         break;
147                 line = line + line->0;
148         }
149
150         ! Do we need to append this line to the end of the program?
151
152         if (line->0 == 0)
153         {
154                 ! Yes.
155
156                 line = store_eop;
157                 store_eop->(linelen+3) = 0;
158                 store_eop = store_eop + linelen + 3;
159         }
160         else
161         {
162                 ! Do we need to remove an existing line first?
163
164                 if ((line+1)-->0 == linenum)
165                 {
166                         i = line->0;
167                         memcpy(line, line+i, store_eop-line-i);
168                         store_eop = store_eop - i;
169                         store_eop->0 = 0;
170                 }
171
172                 ! If there's no actual data in the new line, give up.
173
174                 if (linelen == 1)
175                         return 0;
176
177                 ! Open up space for the new line.
178
179                 memcpy(line+linelen+3, line, store_eop-line);
180                 store_eop = store_eop + linelen + 3;
181                 store_eop->0 = 0;
182         }
183
184         ! Copy the line in.
185
186         line->0 = linelen+3;
187         (line+1)-->0 = linenum;
188         memcpy(line+3, cmd, linelen);
189
190         return 0;
191 ];
192
193 ! Find a line, by number.
194
195 [ store_findline lineno  line;
196         line = store_bottom;
197         while (line->0 && ((line+1)-->0 ~= lineno))
198                 line = line + line->0;
199         if (line->0 == 0)
200                 return 0;
201         return line;
202 ];
203