| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
#ifndef VMGRAM_H |
| 21 |
#define VMGRAM_H |
| 22 |
|
| 23 |
#include <stdlib.h> |
| 24 |
#include <string.h> |
| 25 |
|
| 26 |
#include "os.h" |
| 27 |
#include "t3std.h" |
| 28 |
#include "vmtype.h" |
| 29 |
#include "vmglob.h" |
| 30 |
#include "vmobj.h" |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
enum vmobjgram_meta_fnset |
| 37 |
{ |
| 38 |
|
| 39 |
VMOBJGRAM_UNDEF = 0, |
| 40 |
|
| 41 |
|
| 42 |
VMOBJGRAM_PARSE = 1 |
| 43 |
}; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
enum vmgram_match_type |
| 50 |
{ |
| 51 |
|
| 52 |
VMGRAM_MATCH_UNDEF = 0, |
| 53 |
|
| 54 |
|
| 55 |
VMGRAM_MATCH_PROD = 1, |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
VMGRAM_MATCH_SPEECH = 2, |
| 62 |
|
| 63 |
|
| 64 |
VMGRAM_MATCH_LITERAL = 3, |
| 65 |
|
| 66 |
|
| 67 |
VMGRAM_MATCH_TOKTYPE = 4, |
| 68 |
|
| 69 |
|
| 70 |
VMGRAM_MATCH_STAR = 5, |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
VMGRAM_MATCH_NSPEECH = 6 |
| 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 |
struct vmgram_match_info |
| 123 |
{ |
| 124 |
vm_prop_id_t prop; |
| 125 |
}; |
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
struct vm_gram_ext |
| 131 |
{ |
| 132 |
|
| 133 |
const char *image_data_; |
| 134 |
size_t image_data_size_; |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
vm_obj_id_t comparator_; |
| 144 |
|
| 145 |
|
| 146 |
uint modified_ : 1; |
| 147 |
|
| 148 |
|
| 149 |
uint hashes_cached_ : 1; |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
uint has_circular_alt : 1; |
| 157 |
|
| 158 |
|
| 159 |
class CVmGramProdMem *mem_; |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
vmgram_match_info *prop_enum_arr_; |
| 167 |
size_t prop_enum_max_; |
| 168 |
|
| 169 |
|
| 170 |
struct vmgram_alt_info **alts_; |
| 171 |
|
| 172 |
|
| 173 |
size_t alt_cnt_; |
| 174 |
|
| 175 |
|
| 176 |
size_t alt_alo_; |
| 177 |
}; |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
struct vmgram_alt_info |
| 184 |
{ |
| 185 |
vmgram_alt_info(size_t ntoks); |
| 186 |
~vmgram_alt_info(); |
| 187 |
|
| 188 |
|
| 189 |
int score; |
| 190 |
int badness; |
| 191 |
|
| 192 |
|
| 193 |
int del; |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
vm_obj_id_t proc_obj; |
| 200 |
|
| 201 |
|
| 202 |
struct vmgram_tok_info *toks; |
| 203 |
size_t tok_cnt; |
| 204 |
}; |
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
struct vmgram_tok_info |
| 210 |
{ |
| 211 |
vmgram_tok_info() { typ = VMGRAM_MATCH_UNDEF; } |
| 212 |
~vmgram_tok_info() |
| 213 |
{ |
| 214 |
switch (typ) |
| 215 |
{ |
| 216 |
case VMGRAM_MATCH_LITERAL: |
| 217 |
t3free(typinfo.lit.str); |
| 218 |
break; |
| 219 |
|
| 220 |
case VMGRAM_MATCH_NSPEECH: |
| 221 |
t3free(typinfo.nspeech.props); |
| 222 |
break; |
| 223 |
} |
| 224 |
} |
| 225 |
|
| 226 |
void set_literal(const char *str, size_t len) |
| 227 |
{ |
| 228 |
set_literal(len); |
| 229 |
memcpy(typinfo.lit.str, str, len); |
| 230 |
} |
| 231 |
|
| 232 |
void set_literal(size_t len) |
| 233 |
{ |
| 234 |
typ = VMGRAM_MATCH_LITERAL; |
| 235 |
typinfo.lit.len = len; |
| 236 |
typinfo.lit.hash = 0; |
| 237 |
typinfo.lit.str = (char *)t3malloc(len); |
| 238 |
} |
| 239 |
|
| 240 |
void set_nspeech(size_t cnt) |
| 241 |
{ |
| 242 |
typ = VMGRAM_MATCH_NSPEECH; |
| 243 |
typinfo.nspeech.cnt = cnt; |
| 244 |
typinfo.nspeech.props = (vm_prop_id_t *)t3malloc( |
| 245 |
cnt * sizeof(vm_prop_id_t)); |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
vm_prop_id_t prop; |
| 254 |
|
| 255 |
|
| 256 |
uchar typ; |
| 257 |
|
| 258 |
|
| 259 |
union |
| 260 |
{ |
| 261 |
|
| 262 |
vm_obj_id_t prod_obj; |
| 263 |
|
| 264 |
|
| 265 |
vm_prop_id_t speech_prop; |
| 266 |
|
| 267 |
|
| 268 |
struct |
| 269 |
{ |
| 270 |
size_t cnt; |
| 271 |
vm_prop_id_t *props; |
| 272 |
} nspeech; |
| 273 |
|
| 274 |
|
| 275 |
struct |
| 276 |
{ |
| 277 |
|
| 278 |
char *str; |
| 279 |
size_t len; |
| 280 |
|
| 281 |
|
| 282 |
uint hash; |
| 283 |
} lit; |
| 284 |
|
| 285 |
|
| 286 |
uint32_t toktyp_enum; |
| 287 |
|
| 288 |
} typinfo; |
| 289 |
}; |
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
class CVmObjGramProd: public CVmObject |
| 296 |
{ |
| 297 |
friend class CVmMetaclassGramProd; |
| 298 |
friend struct CVmGramCompResults; |
| 299 |
|
| 300 |
public: |
| 301 |
|
| 302 |
static class CVmMetaclass *metaclass_reg_; |
| 303 |
class CVmMetaclass *get_metaclass_reg() const { return metaclass_reg_; } |
| 304 |
|
| 305 |
|
| 306 |
virtual int is_of_metaclass(class CVmMetaclass *meta) const |
| 307 |
{ |
| 308 |
|
| 309 |
return (meta == metaclass_reg_ |
| 310 |
|| CVmObject::is_of_metaclass(meta)); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
static vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, |
| 315 |
uint argc); |
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
static int call_stat_prop(VMG_ vm_val_t *result, |
| 322 |
const uchar **pc_ptr, uint *argc, |
| 323 |
vm_prop_id_t prop) |
| 324 |
{ return CVmObject::call_stat_prop(vmg_ result, pc_ptr, argc, prop); } |
| 325 |
|
| 326 |
|
| 327 |
static int is_gramprod_obj(VMG_ vm_obj_id_t obj) |
| 328 |
{ return vm_objp(vmg_ obj)->is_of_metaclass(metaclass_reg_); } |
| 329 |
|
| 330 |
|
| 331 |
void notify_delete(VMG_ int in_root_set); |
| 332 |
|
| 333 |
|
| 334 |
int get_prop(VMG_ vm_prop_id_t prop, vm_val_t *val, |
| 335 |
vm_obj_id_t self, vm_obj_id_t *source_obj, uint *argc); |
| 336 |
|
| 337 |
|
| 338 |
void set_prop(VMG_ class CVmUndo *undo, |
| 339 |
vm_obj_id_t self, vm_prop_id_t prop, const vm_val_t *val); |
| 340 |
|
| 341 |
|
| 342 |
void notify_new_savept() { } |
| 343 |
|
| 344 |
|
| 345 |
void apply_undo(VMG_ struct CVmUndoRecord *rec); |
| 346 |
|
| 347 |
|
| 348 |
void discard_undo(VMG_ struct CVmUndoRecord *rec); |
| 349 |
|
| 350 |
|
| 351 |
void mark_undo_ref(VMG_ struct CVmUndoRecord *rec); |
| 352 |
|
| 353 |
|
| 354 |
void remove_stale_undo_weak_ref(VMG_ struct CVmUndoRecord *) { } |
| 355 |
|
| 356 |
|
| 357 |
void mark_refs(VMG_ uint state); |
| 358 |
|
| 359 |
|
| 360 |
void remove_stale_weak_refs(VMG0_); |
| 361 |
|
| 362 |
|
| 363 |
void load_from_image(VMG_ vm_obj_id_t self, const char *ptr, size_t siz); |
| 364 |
|
| 365 |
|
| 366 |
void reset_to_image(VMG_ vm_obj_id_t self); |
| 367 |
void save_to_file(VMG_ class CVmFile *fp); |
| 368 |
void restore_from_file(VMG_ vm_obj_id_t self, |
| 369 |
class CVmFile *fp, class CVmObjFixup *fixups); |
| 370 |
|
| 371 |
|
| 372 |
int is_changed_since_load() const { return FALSE; } |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
virtual ulong rebuild_image(VMG_ char *buf, ulong buflen); |
| 379 |
|
| 380 |
|
| 381 |
virtual void convert_to_const_data(VMG_ class CVmConstMapper *, |
| 382 |
vm_obj_id_t) { } |
| 383 |
|
| 384 |
protected: |
| 385 |
|
| 386 |
CVmObjGramProd(VMG0_); |
| 387 |
|
| 388 |
|
| 389 |
void clear_alts(); |
| 390 |
|
| 391 |
|
| 392 |
void ensure_alts(size_t cnt, size_t margin); |
| 393 |
|
| 394 |
|
| 395 |
void insert_alt(vm_obj_id_t self, size_t idx, vmgram_alt_info *alt); |
| 396 |
void delete_alt(size_t idx); |
| 397 |
|
| 398 |
|
| 399 |
void check_circular_refs(vm_obj_id_t self); |
| 400 |
|
| 401 |
|
| 402 |
void build_alt_props(VMG_ vm_obj_id_t match_obj); |
| 403 |
size_t build_alt_props_list(VMG_ vm_obj_id_t match_obj, |
| 404 |
class CVmObjList *lst); |
| 405 |
|
| 406 |
|
| 407 |
void add_alt(VMG_ vm_obj_id_t self, class CTcGramProdAlt *alt, |
| 408 |
vm_obj_id_t dict, vm_obj_id_t match); |
| 409 |
|
| 410 |
|
| 411 |
void delete_alt_undo(VMG_ vm_obj_id_t self, int idx); |
| 412 |
|
| 413 |
|
| 414 |
void delete_marked_alts(VMG_ vm_obj_id_t self, vm_obj_id_t dict); |
| 415 |
|
| 416 |
|
| 417 |
int find_literal_in_alts(const char *str, size_t len); |
| 418 |
|
| 419 |
|
| 420 |
void save_to_stream(VMG_ class CVmStream *dst); |
| 421 |
|
| 422 |
|
| 423 |
void mark_alt_refs(VMG_ const vmgram_alt_info *alt, uint state); |
| 424 |
|
| 425 |
|
| 426 |
void load_alts(VMG_ vm_obj_id_t self, |
| 427 |
class CVmStream *src, class CVmObjFixup *fixups); |
| 428 |
|
| 429 |
|
| 430 |
int getp_undef(VMG_ vm_obj_id_t, vm_val_t *, uint *) { return FALSE; } |
| 431 |
|
| 432 |
|
| 433 |
int getp_parse(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 434 |
|
| 435 |
|
| 436 |
int getp_get_gram_info(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 437 |
|
| 438 |
|
| 439 |
int getp_deleteAlt(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 440 |
|
| 441 |
|
| 442 |
int getp_clearAlts(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 443 |
|
| 444 |
|
| 445 |
int getp_addAlt(VMG_ vm_obj_id_t self, vm_val_t *val, uint *argc); |
| 446 |
|
| 447 |
|
| 448 |
vm_gram_ext *get_ext() const { return (vm_gram_ext *)ext_; } |
| 449 |
|
| 450 |
|
| 451 |
static void enum_props_cb(VMG_ void *ctx, vm_prop_id_t prop, |
| 452 |
const vm_val_t *match_val); |
| 453 |
|
| 454 |
|
| 455 |
static int find_prop_in_tok(const struct vmgramprod_tok *tok, |
| 456 |
vm_prop_id_t prop); |
| 457 |
|
| 458 |
|
| 459 |
void enqueue_alts(VMG_ class CVmGramProdMem *mem, |
| 460 |
const struct vmgramprod_tok *tok, |
| 461 |
size_t tok_cnt, size_t start_tok_pos, |
| 462 |
struct CVmGramProdState *state, |
| 463 |
struct CVmGramProdQueue *queues, |
| 464 |
vm_obj_id_t self, int circ_only, |
| 465 |
struct CVmGramProdMatch *circ_match, |
| 466 |
class CVmObjDict *dict); |
| 467 |
|
| 468 |
|
| 469 |
static struct CVmGramProdState * |
| 470 |
enqueue_new_state(class CVmGramProdMem *mem, |
| 471 |
size_t start_tok_pos, |
| 472 |
struct CVmGramProdState *enclosing_state, |
| 473 |
const vmgram_alt_info *altp, vm_obj_id_t self, |
| 474 |
int *need_to_clone, |
| 475 |
struct CVmGramProdQueue *queues, |
| 476 |
int circular_alt); |
| 477 |
|
| 478 |
|
| 479 |
static struct CVmGramProdState * |
| 480 |
create_new_state(class CVmGramProdMem *mem, |
| 481 |
size_t start_tok_pos, |
| 482 |
struct CVmGramProdState *enclosing_state, |
| 483 |
const vmgram_alt_info *altp, vm_obj_id_t self, |
| 484 |
int *need_to_clone, int circular_alt); |
| 485 |
|
| 486 |
|
| 487 |
static void enqueue_state(struct CVmGramProdState *state, |
| 488 |
struct CVmGramProdQueue *queues); |
| 489 |
|
| 490 |
|
| 491 |
static void process_work_queue(VMG_ CVmGramProdMem *mem, |
| 492 |
const struct vmgramprod_tok *tok, |
| 493 |
size_t tok_cnt, |
| 494 |
struct CVmGramProdQueue *queues, |
| 495 |
class CVmObjDict *dict); |
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
static void process_work_queue_head(VMG_ CVmGramProdMem *mem, |
| 500 |
const struct vmgramprod_tok *tok, |
| 501 |
size_t tok_cnt, |
| 502 |
struct CVmGramProdQueue *queues, |
| 503 |
class CVmObjDict *dict); |
| 504 |
|
| 505 |
|
| 506 |
static void build_match_tree(VMG_ const struct CVmGramProdMatch *match, |
| 507 |
const vm_val_t *tok_list, |
| 508 |
const vm_val_t *tok_match_list, |
| 509 |
vm_val_t *retval, |
| 510 |
size_t *first_tok, size_t *last_tok); |
| 511 |
|
| 512 |
|
| 513 |
void cache_hashes(VMG_ CVmObjDict *dict); |
| 514 |
|
| 515 |
|
| 516 |
static unsigned int calc_str_hash(VMG_ class CVmObjDict *dict, |
| 517 |
const vm_val_t *strval, |
| 518 |
const char *str, size_t len); |
| 519 |
|
| 520 |
|
| 521 |
static int tok_equals_lit(VMG_ const struct vmgramprod_tok *tok, |
| 522 |
const char *lit, size_t lit_len, |
| 523 |
class CVmObjDict *dict, |
| 524 |
vm_val_t *match_result); |
| 525 |
|
| 526 |
|
| 527 |
static int (CVmObjGramProd::*func_table_[])(VMG_ vm_obj_id_t self, |
| 528 |
vm_val_t *retval, uint *argc); |
| 529 |
}; |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
class CVmMetaclassGramProd: public CVmMetaclass |
| 537 |
{ |
| 538 |
public: |
| 539 |
|
| 540 |
const char *get_meta_name() const { return "grammar-production/030002"; } |
| 541 |
|
| 542 |
|
| 543 |
void create_for_image_load(VMG_ vm_obj_id_t id) |
| 544 |
{ |
| 545 |
new (vmg_ id) CVmObjGramProd(vmg0_); |
| 546 |
G_obj_table->set_obj_gc_characteristics(id, TRUE, TRUE); |
| 547 |
} |
| 548 |
|
| 549 |
|
| 550 |
void create_for_restore(VMG_ vm_obj_id_t id) |
| 551 |
{ |
| 552 |
new (vmg_ id) CVmObjGramProd(vmg0_); |
| 553 |
G_obj_table->set_obj_gc_characteristics(id, TRUE, TRUE); |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
vm_obj_id_t create_from_stack(VMG_ const uchar **pc_ptr, uint argc) |
| 558 |
{ return CVmObjGramProd::create_from_stack(vmg_ pc_ptr, argc); } |
| 559 |
|
| 560 |
|
| 561 |
int call_stat_prop(VMG_ vm_val_t *result, |
| 562 |
const uchar **pc_ptr, uint *argc, |
| 563 |
vm_prop_id_t prop) |
| 564 |
{ |
| 565 |
return CVmObjGramProd:: |
| 566 |
call_stat_prop(vmg_ result, pc_ptr, argc, prop); |
| 567 |
} |
| 568 |
}; |
| 569 |
|
| 570 |
|
| 571 |
#endif |
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
VM_REGISTER_METACLASS(CVmObjGramProd) |
| 577 |
|