| 1 | #ifdef RCSID |
| 2 | static char RCSid[] = |
| 3 | "$Header$"; |
| 4 | #endif |
| 5 | |
| 6 | /* |
| 7 | * Copyright (c) 2001, 2002 Michael J. Roberts. All Rights Reserved. |
| 8 | * |
| 9 | * Please see the accompanying license file, LICENSE.TXT, for information |
| 10 | * on using and copying this software. |
| 11 | */ |
| 12 | /* |
| 13 | Name |
| 14 | vmrunsym.cpp - runtime symbol table |
| 15 | Function |
| 16 | |
| 17 | Notes |
| 18 | |
| 19 | Modified |
| 20 | 02/17/01 MJRoberts - Creation |
| 21 | */ |
| 22 | |
| 23 | #include "t3std.h" |
| 24 | #include "vmtype.h" |
| 25 | #include "vmrunsym.h" |
| 26 | |
| 27 | /* |
| 28 | * delete - deletes all of our symbol list entries |
| 29 | */ |
| 30 | CVmRuntimeSymbols::~CVmRuntimeSymbols() |
| 31 | { |
| 32 | vm_runtime_sym *sym; |
| 33 | vm_runtime_sym *nxt; |
| 34 | |
| 35 | /* delete each symbol */ |
| 36 | for (sym = head_ ; sym != 0 ; sym = nxt) |
| 37 | { |
| 38 | /* |
| 39 | * remember the next one, since we're deleting our link pointer |
| 40 | * along with the structure |
| 41 | */ |
| 42 | nxt = sym->nxt; |
| 43 | |
| 44 | /* delete it */ |
| 45 | t3free(sym); |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | /* |
| 50 | * add a symbol |
| 51 | */ |
| 52 | void CVmRuntimeSymbols::add_sym(const char *sym, size_t len, |
| 53 | const vm_val_t *val) |
| 54 | { |
| 55 | vm_runtime_sym *new_sym; |
| 56 | |
| 57 | /* allocate a new structure */ |
| 58 | new_sym = (vm_runtime_sym *)t3malloc(osrndsz( |
| 59 | sizeof(vm_runtime_sym) + len)); |
| 60 | |
| 61 | /* copy the data */ |
| 62 | new_sym->val = *val; |
| 63 | new_sym->len = len; |
| 64 | memcpy(new_sym->sym = (char *)(new_sym + 1), sym, len); |
| 65 | |
| 66 | /* there are no macro parameters */ |
| 67 | new_sym->macro_expansion = 0; |
| 68 | new_sym->macro_exp_len = 0; |
| 69 | new_sym->macro_argc = 0; |
| 70 | new_sym->macro_args = 0; |
| 71 | new_sym->macro_flags = 0; |
| 72 | |
| 73 | /* link it into our list */ |
| 74 | new_sym->nxt = 0; |
| 75 | if (tail_ == 0) |
| 76 | head_ = new_sym; |
| 77 | else |
| 78 | tail_->nxt = new_sym; |
| 79 | |
| 80 | /* it's the new tail */ |
| 81 | tail_ = new_sym; |
| 82 | |
| 83 | /* count it */ |
| 84 | ++cnt_; |
| 85 | } |
| 86 | |
| 87 | /* |
| 88 | * Add a macro definition |
| 89 | */ |
| 90 | vm_runtime_sym *CVmRuntimeSymbols::add_macro(const char *sym, size_t len, |
| 91 | size_t explen, |
| 92 | unsigned int flags, |
| 93 | int argc, size_t arglen) |
| 94 | { |
| 95 | /* allocate the structure */ |
| 96 | vm_runtime_sym *new_sym = (vm_runtime_sym *)t3malloc(osrndsz( |
| 97 | sizeof(vm_runtime_sym) |
| 98 | + (argc * sizeof(char*)) |
| 99 | + len |
| 100 | + explen |
| 101 | + arglen)); |
| 102 | |
| 103 | /* set up the internal allocation pointers within the structure */ |
| 104 | if (argc != 0) |
| 105 | { |
| 106 | new_sym->macro_args = (char **)(new_sym + 1); |
| 107 | new_sym->macro_expansion = (char *)(new_sym->macro_args + argc); |
| 108 | new_sym->macro_args[0] = new_sym->macro_expansion + explen; |
| 109 | new_sym->sym = new_sym->macro_args[0] + arglen; |
| 110 | } |
| 111 | else |
| 112 | { |
| 113 | new_sym->macro_args = 0; |
| 114 | new_sym->macro_expansion = (char *)(new_sym + 1); |
| 115 | new_sym->sym = new_sym->macro_expansion + explen; |
| 116 | } |
| 117 | |
| 118 | /* copy the basic data */ |
| 119 | new_sym->val.set_nil(); |
| 120 | new_sym->len = len; |
| 121 | memcpy(new_sym->sym, sym, len); |
| 122 | |
| 123 | /* copy the basic macro data */ |
| 124 | new_sym->macro_argc = argc; |
| 125 | new_sym->macro_flags = flags; |
| 126 | new_sym->macro_exp_len = explen; |
| 127 | |
| 128 | /* link it into our list */ |
| 129 | new_sym->nxt = 0; |
| 130 | if (tail_ == 0) |
| 131 | head_ = new_sym; |
| 132 | else |
| 133 | tail_->nxt = new_sym; |
| 134 | |
| 135 | /* it's the new tail */ |
| 136 | tail_ = new_sym; |
| 137 | |
| 138 | /* count it */ |
| 139 | ++cnt_; |
| 140 | |
| 141 | /* return the new symbol */ |
| 142 | return new_sym; |
| 143 | } |
| 144 | |
| 145 | |
| 146 | /* |
| 147 | * Find the name for a value |
| 148 | */ |
| 149 | const char *CVmRuntimeSymbols::find_val_name(VMG_ const vm_val_t *val, |
| 150 | size_t *name_len) const |
| 151 | { |
| 152 | vm_runtime_sym *sym; |
| 153 | |
| 154 | /* search the table */ |
| 155 | for (sym = head_ ; sym != 0 ; sym = sym->nxt) |
| 156 | { |
| 157 | /* check for a match to the value */ |
| 158 | if (val->equals(vmg_ &sym->val)) |
| 159 | { |
| 160 | /* it's a match - return the name */ |
| 161 | *name_len = sym->len; |
| 162 | return sym->sym; |
| 163 | } |
| 164 | } |
| 165 | |
| 166 | /* didn't find it - return null */ |
| 167 | *name_len = 0; |
| 168 | return 0; |
| 169 | } |