Debian Codegraph @

terps/tads/tads3/vmpat.cpp

raw symbols SCIP
1 /*
2 * Copyright (c) 2002 by Michael J. Roberts. All Rights Reserved.
3 *
4 * Please see the accompanying license file, LICENSE.TXT, for information
5 * on using and copying this software.
6 */
7 /*
8 Name
9 vmpat.cpp - regular-expression compiled pattern object
10 Function
11
12 Notes
13
14 Modified
15 08/27/02 MJRoberts - Creation
16 */
17
18 #include <stdlib.h>
19 #include "os.h"
20 #include "vmtype.h"
21 #include "vmobj.h"
22 #include "vmmeta.h"
23 #include "vmglob.h"
24 #include "vmregex.h"
25 #include "vmpat.h"
26 #include "vmstack.h"
27 #include "vmbif.h"
28 #include "vmbiftad.h"
29 #include "vmfile.h"
30 #include "vmrun.h"
31
32
33 /* ------------------------------------------------------------------------ */
34 /*
35 * Statics
36 */
37 static CVmMetaclassPattern metaclass_reg_obj;
38 CVmMetaclass *CVmObjPattern::metaclass_reg_ = &metaclass_reg_obj;
39
40 /* function table */
41 int (CVmObjPattern::
42 *CVmObjPattern::func_table_[])(VMG_ vm_obj_id_t self,
43 vm_val_t *retval, uint *argc) =
44 {
45 &CVmObjPattern::getp_undef,
46 &CVmObjPattern::getp_get_str
47 };
48
49 /* ------------------------------------------------------------------------ */
50 /*
51 * create
52 */
53 CVmObjPattern::CVmObjPattern(VMG_ re_compiled_pattern *pat,
54 const vm_val_t *src_str)
55 {
56 /* allocate my extension data */
57 ext_ = (char *)G_mem->get_var_heap()
58 ->alloc_mem(sizeof(vmobj_pat_ext), this);
59
60 /* remember my source data */
61 set_orig_str(src_str);
62
63 /* remember the compiled pattern */
64 set_pattern(pat);
65 }
66
67 /* ------------------------------------------------------------------------ */
68 /*
69 * notify of deletion
70 */
71 void CVmObjPattern::notify_delete(VMG_ int in_root_set)
72 {
73 /* free my extension data */
74 if (ext_ != 0)
75 {
76 /*
77 * Free my pattern, if I've compiled it. (Note that we must not
78 * call get_pattern() here, because doing so would unnecessarily
79 * create a pattern if we haven't already done so - that would be
80 * stupid, because the only reason we're asking for it is so that
81 * we can delete it.)
82 */
83 if (get_ext()->pat != 0)
84 CRegexParser::free_pattern(get_ext()->pat);
85
86 /* free the extension */
87 if (!in_root_set)
88 G_mem->get_var_heap()->free_mem(ext_);
89 }
90 }
91
92 /* ------------------------------------------------------------------------ */
93 /*
94 * Create from the stack
95 */
96 vm_obj_id_t CVmObjPattern::create_from_stack(VMG_ const uchar **pc_ptr,
97 uint argc)
98 {
99 const char *strval;
100 re_status_t stat;
101 re_compiled_pattern *pat;
102 vm_obj_id_t id;
103
104 /* check arguments */
105 if (argc != 1)
106 err_throw(VMERR_WRONG_NUM_OF_ARGS);
107
108 /* retrieve the string, but leave it on the stack */
109 strval = G_stk->get(0)->get_as_string(vmg0_);
110 if (strval == 0)
111 err_throw(VMERR_STRING_VAL_REQD);
112
113 /* compile the string */
114 stat = G_bif_tads_globals->rex_parser->compile_pattern(
115 strval + VMB_LEN, vmb_get_len(strval), &pat);
116
117 /* if we failed to compile the pattern, throw an error */
118 if (stat != RE_STATUS_SUCCESS)
119 err_throw(VMERR_BAD_TYPE_BIF);
120
121 /* create a new pattern object to hold the pattern */
122 id = vm_new_id(vmg_ FALSE, TRUE, FALSE);
123 new (vmg_ id) CVmObjPattern(vmg_ pat, G_stk->get(0));
124
125 /* discard arguments */
126 G_stk->discard();
127
128 /* return the new object */
129 return id;
130 }
131
132 /* ------------------------------------------------------------------------ */
133 /*
134 * set a property
135 */
136 void CVmObjPattern::set_prop(VMG_ class CVmUndo *,
137 vm_obj_id_t, vm_prop_id_t,
138 const vm_val_t *)
139 {
140 /* we have no properties to set */
141 err_throw(VMERR_INVALID_SETPROP);
142 }
143
144 /* ------------------------------------------------------------------------ */
145 /*
146 * Get a property
147 */
148 int CVmObjPattern::get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val,
149 vm_obj_id_t self, vm_obj_id_t *source_obj,
150 uint *argc)
151 {
152 uint func_idx;
153
154 /* translate the property into a function vector index */
155 func_idx = G_meta_table
156 ->prop_to_vector_idx(metaclass_reg_->get_reg_idx(), prop);
157
158 /* call the function, if we found it */
159 if ((this->*func_table_[func_idx])(vmg_ self, val, argc))
160 {
161 *source_obj = metaclass_reg_->get_class_obj(vmg0_);
162 return TRUE;
163 }
164
165 /* not found - inherit default handling */
166 return CVmObject::get_prop(vmg_ prop, val, self, source_obj, argc);
167 }
168
169
170 /* ------------------------------------------------------------------------ */
171 /*
172 * Mark references
173 */
174 void CVmObjPattern::mark_refs(VMG_ uint state)
175 {
176 const vm_val_t *valp;
177
178 /* if our source value is an object reference, mark it */
179 if (get_ext() != 0
180 && (valp = get_orig_str())->typ == VM_OBJ
181 && valp->val.obj != VM_INVALID_OBJ)
182 {
183 /* it's a reference, so mark it */
184 G_obj_table->mark_all_refs(valp->val.obj, state);
185 }
186 }
187
188 /* ------------------------------------------------------------------------ */
189 /*
190 * Load from an image file
191 */
192 void CVmObjPattern::load_from_image(VMG_ vm_obj_id_t self, const char *ptr,
193 size_t len)
194 {
195 /* if we don't already have an extension, allocate one */
196 if (ext_ == 0)
197 ext_ = (char *)G_mem->get_var_heap()
198 ->alloc_mem(sizeof(vmobj_pat_ext), this);
199
200 /* get our source value */
201 vmb_get_dh(ptr, &get_ext()->str);
202
203 /*
204 * We haven't compiled our pattern yet. Note that it might not be
205 * possible to obtain the text of our string at this point, because it
206 * might be another object and thus might not have been loaded yet.
207 * So, note that we have no pattern yet, and request post-load
208 * initialization, so that we can compile our pattern after we know all
209 * of the other objects have been loaded.
210 */
211 set_pattern(0);
212 G_obj_table->request_post_load_init(self);
213 }
214
215 /* ------------------------------------------------------------------------ */
216 /*
217 * Perform post-load initialization: we compile our pattern here. Note
218 * that we need to wait until now to compile our pattern, since our source
219 * string could be another object, which isn't guaranteed to have been
220 * loaded until we get here.
221 */
222 void CVmObjPattern::post_load_init(VMG_ vm_obj_id_t self)
223 {
224 /* make sure the original string object is initialized */
225 const vm_val_t *origval = get_orig_str();
226 if (origval->typ == VM_OBJ)
227 G_obj_table->ensure_post_load_init(vmg_ origval->val.obj);
228
229 /* get the string value */
230 const char *strval = get_orig_str()->get_as_string(vmg0_);
231 if (strval != 0)
232 {
233 /* if we already have a compiled pattern, delete it */
234 if (get_ext()->pat != 0)
235 CRegexParser::free_pattern(get_ext()->pat);
236
237 /* compile the pattern and store the result */
238 G_bif_tads_globals->rex_parser->compile_pattern(
239 strval + VMB_LEN, vmb_get_len(strval), &get_ext()->pat);
240 }
241 }
242
243 /* ------------------------------------------------------------------------ */
244 /*
245 * save to a file
246 */
247 void CVmObjPattern::save_to_file(VMG_ class CVmFile *fp)
248 {
249 /* write the source string reference */
250 char buf[VMB_DATAHOLDER];
251 vmb_put_dh(buf, get_orig_str());
252 fp->write_bytes(buf, VMB_DATAHOLDER);
253 }
254
255 /* ------------------------------------------------------------------------ */
256 /*
257 * restore from a file
258 */
259 void CVmObjPattern::restore_from_file(VMG_ vm_obj_id_t self,
260 class CVmFile *fp, CVmObjFixup *fixups)
261 {
262 /* if we don't already have an extension, allocate one */
263 if (ext_ == 0)
264 ext_ = (char *)G_mem->get_var_heap()
265 ->alloc_mem(sizeof(vmobj_pat_ext), this);
266
267 /* read the source string reference */
268 char buf[VMB_DATAHOLDER];
269 fp->read_bytes(buf, VMB_DATAHOLDER);
270
271 /* fix it up */
272 fixups->fix_dh(vmg_ buf);
273
274 /* remember it in our extension */
275 vmb_get_dh(buf, &get_ext()->str);
276
277 /*
278 * clear out our pattern and request post-load initialization - we
279 * can't necessarily compile it yet, because we might not have loaded
280 * the source string data, so just make a note that we need to compile
281 * it the next time we need it
282 */
283 set_pattern(0);
284 G_obj_table->request_post_load_init(self);
285 }
286
287 /* ------------------------------------------------------------------------ */
288 /*
289 * property evaluator - get my original string
290 */
291 int CVmObjPattern::getp_get_str(VMG_ vm_obj_id_t self,
292 vm_val_t *retval, uint *argc)
293 {
294 static CVmNativeCodeDesc desc(0);
295
296 /* check arguments */
297 if (get_prop_check_argc(retval, argc, &desc))
298 return TRUE;
299
300 /* retrieve my original string value */
301 *retval = *get_orig_str();
302
303 /* handled */
304 return TRUE;
305 }