diff options
Diffstat (limited to 'src/lang/basic.cc')
| -rw-r--r-- | src/lang/basic.cc | 315 |
1 files changed, 315 insertions, 0 deletions
diff --git a/src/lang/basic.cc b/src/lang/basic.cc new file mode 100644 index 0000000..54d16a9 --- /dev/null +++ b/src/lang/basic.cc | |||
| @@ -0,0 +1,315 @@ | |||
| 1 | /************************************************************************* | ||
| 2 | * | ||
| 3 | * HTCd - Copyright (C) 1998-2006 Henrik Rydberg | ||
| 4 | * | ||
| 5 | * This program is free software; you can redistribute it and/or modify | ||
| 6 | * it under the terms of the GNU General Public License as published by | ||
| 7 | * the Free Software Foundation; either version 2 of the License, or | ||
| 8 | * (at your option) any later version. | ||
| 9 | * | ||
| 10 | * This program is distributed in the hope that it will be useful, | ||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 13 | * GNU General Public License for more details. | ||
| 14 | * | ||
| 15 | * You should have received a copy of the GNU General Public License | ||
| 16 | * along with this program; if not, write to the Free Software | ||
| 17 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | ||
| 18 | */ | ||
| 19 | |||
| 20 | #include <lang/env.h> | ||
| 21 | |||
| 22 | /////////////////////////////////////////////////// | ||
| 23 | |||
| 24 | TOK_IMPL(asis) { out<<env.arg(0)->expr; } | ||
| 25 | TOK_IMPL(mute) {} | ||
| 26 | TOK_IMPL(comment) {} | ||
| 27 | |||
| 28 | /////////////////////////////////////////////////// | ||
| 29 | |||
| 30 | TOK_IMPL(cond) { | ||
| 31 | Cond(*env.toks,env[0],env[1],tok_t::PUBLIC); | ||
| 32 | } | ||
| 33 | TOK_IMPL(global) { | ||
| 34 | Global(*env.toks,env[0],env[1],tok_t::PUBLIC); | ||
| 35 | } | ||
| 36 | TOK_IMPL(const) { | ||
| 37 | Const(*env.toks,env[0],env[1],tok_t::PUBLIC); | ||
| 38 | } | ||
| 39 | TOK_IMPL(reconst) { | ||
| 40 | Const(*env.toks,env[0],env[1],tok_t::PUBLIC,1); | ||
| 41 | } | ||
| 42 | |||
| 43 | /////////////////////////////////////////////////// | ||
| 44 | |||
| 45 | TOK_IMPL(type) { | ||
| 46 | tok_t* p=env.toks->get(env[0]); | ||
| 47 | if(env.is_clear(p)) out<<p->mod; | ||
| 48 | } | ||
| 49 | TOK_IMPL(desc) { | ||
| 50 | tok_t* p=env.toks->get(env[0]); | ||
| 51 | if(env.is_clear(p)) out<<Desc(*p); | ||
| 52 | } | ||
| 53 | TOK_IMPL(expr) { | ||
| 54 | tok_t* p=env.toks->get(env[0]); | ||
| 55 | if(env.is_clear(p)&&p->is_func()) ((func_t*)p)->x.trav(out,12); | ||
| 56 | } | ||
| 57 | TOK_IMPL(clear) { | ||
| 58 | ClearToken(*env.toks,env[0]); | ||
| 59 | } | ||
| 60 | |||
| 61 | ////////////////////////////////////////////////////// | ||
| 62 | |||
| 63 | TOK_IMPL(exist) { out<<env.glob_find(env[0]); } | ||
| 64 | TOK_IMPL(nexist) { out<<!env.glob_find(env[0]); } | ||
| 65 | TOK_IMPL(empty) { out<<env.glob(env[0]).empty(); } | ||
| 66 | TOK_IMPL(nempty) { out<<env.glob(env[0]).nempty(); } | ||
| 67 | |||
| 68 | /////////////////////////////////////////////////// | ||
| 69 | |||
| 70 | TOK_IMPL(export) { | ||
| 71 | rpile list; env.gloc_list(list,env[1]); | ||
| 72 | int n=*env[1]=='@'?env[1].size()-1:env[1].size(); | ||
| 73 | for(int i=0;i<list.size();i++) | ||
| 74 | Global(*env.toks,env[0]+list[i].right(n),env.gloc(list[i]),tok_t::PUBLIC); | ||
| 75 | } | ||
| 76 | |||
| 77 | TOK_IMPL(import) { | ||
| 78 | if(*env[0]=='@') env.glob_adb(env[0].popf(),env[1]); | ||
| 79 | else { | ||
| 80 | rpile list; env.glob_list(list,env[1]); | ||
| 81 | for(int i=0;i<list.size();i++) | ||
| 82 | env.aloc(env[0]+list[i].right(env[1].size()),env.glob(list[i])); | ||
| 83 | } | ||
| 84 | } | ||
| 85 | |||
| 86 | TOK_IMPL(list) { | ||
| 87 | mstring fx; env.parg(fx,-1); | ||
| 88 | rpile list; env.gloc_list(list,fx); | ||
| 89 | int n=*fx=='@'?fx.size()-1:fx.size(); | ||
| 90 | for(int i=0;i<list.size();i++) out<<list[i].right(n)<<"\n"; | ||
| 91 | } | ||
| 92 | |||
| 93 | TOK_IMPL(globlist) { | ||
| 94 | mstring fx; env.parg(fx,-1); | ||
| 95 | rpile list; env.glob_list(list,fx); | ||
| 96 | int n=*fx=='@'?fx.size()-1:fx.size(); | ||
| 97 | for(int i=0;i<list.size();i++) out<<list[i].right(n)<<"\n"; | ||
| 98 | } | ||
| 99 | |||
| 100 | TOK_IMPL(lrec) { | ||
| 101 | sref name=env[0],first,rest=env[1]; | ||
| 102 | mstring init; env.parg(init,-1); | ||
| 103 | while(Split(first,rest)) env.aloc(name+first,init); | ||
| 104 | } | ||
| 105 | |||
| 106 | /////////////////////////////////////////////////// | ||
| 107 | |||
| 108 | TOK_IMPL(arg) { if(env.up) env.up->parg(out,atoi(env[0])-1); } | ||
| 109 | TOK_IMPL(args) { if(env.up) out<<env.up->args; else out<<0; } | ||
| 110 | |||
| 111 | /////////////////////////////////////////////////// | ||
| 112 | |||
| 113 | TOK_IMPL(random) { | ||
| 114 | out<<Random(atoi(env[0])); | ||
| 115 | } | ||
| 116 | |||
| 117 | ////////////////////////////////////////////////////// | ||
| 118 | |||
| 119 | void envAddBasic(tokmap& T) | ||
| 120 | { | ||
| 121 | TOK_ADD(asis,"r",tok_t::PUBLIC, | ||
| 122 | "%Display body as is\n" | ||
| 123 | "%Syntax: \\asis{body}\n" | ||
| 124 | ); | ||
| 125 | TOK_ADD(mute,"x",tok_t::PUBLIC, | ||
| 126 | "%Evaluate but don't display\n" | ||
| 127 | "%Syntax: \\mute{body}\n" | ||
| 128 | ); | ||
| 129 | T.add(new binary_t(sref("*"), | ||
| 130 | sref("%Commentary - don't show or evaluate body\n" | ||
| 131 | "%Syntax: \\*{body}\n"), | ||
| 132 | tok_t::PUBLIC,sref("r"),impl_comment),0,1); | ||
| 133 | |||
| 134 | ////////////////////////////////////////////////////// | ||
| 135 | |||
| 136 | TOK_ADD(global,"xx",tok_t::PUBLIC, | ||
| 137 | "%Set global variable\n" | ||
| 138 | "%Syntax: \\global{varname}{expr}\n" | ||
| 139 | ); | ||
| 140 | TOK_ADD(cond,"xx",tok_t::PUBLIC, | ||
| 141 | "%Set global variable if not already defined\n" | ||
| 142 | "%Syntax: \\cond{varname}{expr}\n" | ||
| 143 | ); | ||
| 144 | TOK_ADD(const,"xx",tok_t::PUBLIC, | ||
| 145 | "%Set global constant\n" | ||
| 146 | "%Syntax: \\cond{varname}{expr}\n" | ||
| 147 | "%\n" | ||
| 148 | "%Generates an error if already defined\n" | ||
| 149 | ); | ||
| 150 | TOK_ADD(reconst,"xx",tok_t::PRIVATE, | ||
| 151 | "%Redefine global constant\n" | ||
| 152 | "%Syntax: \\cond{varname}{expr}\n" | ||
| 153 | "%\n" | ||
| 154 | "%Takes precedence over any existing constant\n" | ||
| 155 | ); | ||
| 156 | |||
| 157 | ////////////////////////////////////////////////////// | ||
| 158 | |||
| 159 | TOK_ADD(type,"x",tok_t::PRIVATE, | ||
| 160 | "%Show variable type\n" | ||
| 161 | "%Syntax: \\type{varname}\n" | ||
| 162 | "%\n" | ||
| 163 | "%The type is an integer bit-field consisting of:\n" | ||
| 164 | "%\n" | ||
| 165 | "%1 CONSTANT\n" | ||
| 166 | "%2 RESTRICT\n" | ||
| 167 | "%4 NORMAL\n" | ||
| 168 | "%8 SYSONLY\n" | ||
| 169 | "%16 INIT\n" | ||
| 170 | "%32 RUNNABLE\n" | ||
| 171 | "%64 FUNCTION\n" | ||
| 172 | "%128 BINARY\n" | ||
| 173 | "%\n" | ||
| 174 | "%The access modes consists of\n" | ||
| 175 | "%\n" | ||
| 176 | "%INIT\n" | ||
| 177 | "%SYSONLY\n" | ||
| 178 | "%PRIVATE = NORMAL|SYSONLY\n" | ||
| 179 | "%PUBLIC = PRIVATE|RESTRICT\n" | ||
| 180 | "%\n" | ||
| 181 | "%Those values are matched against the running modes\n" | ||
| 182 | "%to determine access. See run.\n" | ||
| 183 | ); | ||
| 184 | TOK_ADD(desc,"x",tok_t::PRIVATE, | ||
| 185 | "%Show variable description\n" | ||
| 186 | "%Syntax: \\desc{varname}\n" | ||
| 187 | "%\n" | ||
| 188 | "%Collects all lines starting with %.\n" | ||
| 189 | ); | ||
| 190 | TOK_ADD(expr,"x",tok_t::PRIVATE, | ||
| 191 | "%Show the expression tree of function\n" | ||
| 192 | "%Syntax: \\expr{varname}\n" | ||
| 193 | "%\n" | ||
| 194 | "%If desc is a function, the expression tree is shown.\n" | ||
| 195 | "%Mostly interesting for real expression, i.e., when the\n" | ||
| 196 | "%function body was defined within parenthesis.\n" | ||
| 197 | ); | ||
| 198 | TOK_ADD(clear,"x",tok_t::SYSONLY, | ||
| 199 | "%Clear variables\n" | ||
| 200 | "%Syntax: \\clear{partial varname}\n" | ||
| 201 | "%\n" | ||
| 202 | "%Clears all global variables on the current level\n" | ||
| 203 | "%beginning with the partial variable name.\n" | ||
| 204 | ); | ||
| 205 | |||
| 206 | ////////////////////////////////////////////////////// | ||
| 207 | |||
| 208 | TOK_ADD(exist,"x",tok_t::PUBLIC, | ||
| 209 | "%True if variable exist\n" | ||
| 210 | "%Syntax: \\exist{varname}\n" | ||
| 211 | ); | ||
| 212 | TOK_ADD(nexist,"x",tok_t::PUBLIC, | ||
| 213 | "%True if variable does not exist\n" | ||
| 214 | "%Syntax: \\nexist{varname}\n" | ||
| 215 | ); | ||
| 216 | TOK_ADD(empty,"x",tok_t::PUBLIC, | ||
| 217 | "%True if variable is empty\n" | ||
| 218 | "%Syntax: \\empty{varname}\n" | ||
| 219 | "%\n" | ||
| 220 | "%Generates an error if varname does not exist.\n" | ||
| 221 | ); | ||
| 222 | TOK_ADD(nempty,"x",tok_t::PUBLIC, | ||
| 223 | "%True if variable is non-empty\n" | ||
| 224 | "%Syntax: \\nempty{varname}\n" | ||
| 225 | "%\n" | ||
| 226 | "%Generates an error if varname does not exist.\n" | ||
| 227 | ); | ||
| 228 | |||
| 229 | ////////////////////////////////////////////////////// | ||
| 230 | |||
| 231 | TOK_ADD(export,"xx",tok_t::SYSONLY, | ||
| 232 | "%Export local variables\n" | ||
| 233 | "%Syntax: \\export{global partial name}{local partial name}\n" | ||
| 234 | "%\n" | ||
| 235 | "%Copies a sequence of local variables, beginning with the\n" | ||
| 236 | "%local partial name, from the same - not nescessarily the current -\n" | ||
| 237 | "%local stack into a set of global variables beginning with\n" | ||
| 238 | "%the global partial name.\n" | ||
| 239 | "%\n" | ||
| 240 | "%Copies all local variables in the current stack if no\n" | ||
| 241 | "%partial name is given.\n" | ||
| 242 | "%\n" | ||
| 243 | "%Copies all variables in the current database record\n" | ||
| 244 | "%if the partial name @ is given.\n" | ||
| 245 | ); | ||
| 246 | TOK_ADD(import,"xx",tok_t::SYSONLY, | ||
| 247 | "%Import global variables\n" | ||
| 248 | "%Syntax: \\import{local partial name}{global partial name}\n" | ||
| 249 | "%\n" | ||
| 250 | "%Copies a sequence of global variables, beginning with the\n" | ||
| 251 | "%global partial name, from the same - not nescessarily the\n" | ||
| 252 | "%current - global level into a set of local variables beginning\n" | ||
| 253 | "%with the local partial name.\n" | ||
| 254 | "%\n" | ||
| 255 | "%Copies all global variables on the current level if no\n" | ||
| 256 | "%partial name is given.\n" | ||
| 257 | "%\n" | ||
| 258 | "%Creates a new or updates an existing database record if\n" | ||
| 259 | "%the partial name @ is given.\n" | ||
| 260 | ); | ||
| 261 | TOK_ADD(list,"?",tok_t::PUBLIC, | ||
| 262 | "%List local variables\n" | ||
| 263 | "%Syntax: \\list<partial name>\n" | ||
| 264 | "%\n" | ||
| 265 | "%Generates a list of names, beginning with the partial name,\n" | ||
| 266 | "%from the same - not nescessarily the current - local stack.\n" | ||
| 267 | "%\n" | ||
| 268 | "%Generates all local variables in the current stack if no\n" | ||
| 269 | "%partial name is given.\n" | ||
| 270 | "%\n" | ||
| 271 | "%Generates a list of tables if @ is given.\n" | ||
| 272 | ); | ||
| 273 | TOK_ADD(globlist,"?",tok_t::PUBLIC, | ||
| 274 | "%List global variables\n" | ||
| 275 | "%Syntax: \\globlist<partial name>\n" | ||
| 276 | "%\n" | ||
| 277 | "%Generates a list of names, beginning with the partial name,\n" | ||
| 278 | "%from the same - not nescessarily the current - global level.\n" | ||
| 279 | "%\n" | ||
| 280 | "%Generates all global variables on the current level if no\n" | ||
| 281 | "%partial name is given.\n" | ||
| 282 | ); | ||
| 283 | TOK_ADD(lrec,"xx?",tok_t::PUBLIC, | ||
| 284 | "%Create a local record\n" | ||
| 285 | "%Syntax: \\lrec{partial name}{name list}<init>\n" | ||
| 286 | "%\n" | ||
| 287 | "%Creates a set of local variables, all beginning with the\n" | ||
| 288 | "%partial name, and initializes each variable to init.\n" | ||
| 289 | ); | ||
| 290 | |||
| 291 | ////////////////////////////////////////////////////// | ||
| 292 | |||
| 293 | TOK_ADD(arg,"x",tok_t::PUBLIC, | ||
| 294 | "%Show/Evaluate argument #n.\n" | ||
| 295 | "%Syntax: \\arg{n}\n" | ||
| 296 | "%\n" | ||
| 297 | "%Same as the command \\1, \\2 etc, except this\n" | ||
| 298 | "%may be used to index the arguments e.g. using local variables.\n" | ||
| 299 | ); | ||
| 300 | TOK_ADD(args,"",tok_t::PUBLIC, | ||
| 301 | "%Number of arguments in the current function\n" | ||
| 302 | "%Syntax: \\args\n" | ||
| 303 | "%\n" | ||
| 304 | "%Note that the optional argument will count as one variable,\n" | ||
| 305 | "%and may be accesed using either \\0 or \\arg{\\args}.\n" | ||
| 306 | ); | ||
| 307 | |||
| 308 | ////////////////////////////////////////////////////// | ||
| 309 | |||
| 310 | TOK_ADD(random,"x",tok_t::PRIVATE, | ||
| 311 | "%Get n random digits between 0 and 9 inclusive\n" | ||
| 312 | "%Syntax: \\random{n}\n" | ||
| 313 | ); | ||
| 314 | |||
| 315 | } | ||
