summaryrefslogtreecommitdiff
path: root/src/lang/basic.cc
blob: 54d16a90e6199cda8464ec6227765167881074d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*************************************************************************
 *
 * HTCd - Copyright (C) 1998-2006 Henrik Rydberg
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <lang/env.h>

///////////////////////////////////////////////////

TOK_IMPL(asis) { out<<env.arg(0)->expr; }
TOK_IMPL(mute) {}
TOK_IMPL(comment) {}

///////////////////////////////////////////////////

TOK_IMPL(cond) {
  Cond(*env.toks,env[0],env[1],tok_t::PUBLIC);
}
TOK_IMPL(global) {
  Global(*env.toks,env[0],env[1],tok_t::PUBLIC);
}
TOK_IMPL(const) {
  Const(*env.toks,env[0],env[1],tok_t::PUBLIC);
}
TOK_IMPL(reconst) {
  Const(*env.toks,env[0],env[1],tok_t::PUBLIC,1);
}

///////////////////////////////////////////////////

TOK_IMPL(type) {
  tok_t* p=env.toks->get(env[0]);
  if(env.is_clear(p)) out<<p->mod;
}
TOK_IMPL(desc) {
  tok_t* p=env.toks->get(env[0]);
  if(env.is_clear(p)) out<<Desc(*p);
}
TOK_IMPL(expr) {
  tok_t* p=env.toks->get(env[0]);
  if(env.is_clear(p)&&p->is_func()) ((func_t*)p)->x.trav(out,12);
}
TOK_IMPL(clear) {
  ClearToken(*env.toks,env[0]);
}

//////////////////////////////////////////////////////

TOK_IMPL(exist) { out<<env.glob_find(env[0]); }
TOK_IMPL(nexist) { out<<!env.glob_find(env[0]); }
TOK_IMPL(empty) { out<<env.glob(env[0]).empty(); }
TOK_IMPL(nempty) { out<<env.glob(env[0]).nempty(); }

///////////////////////////////////////////////////

TOK_IMPL(export) {
  rpile list; env.gloc_list(list,env[1]);
  int n=*env[1]=='@'?env[1].size()-1:env[1].size();
  for(int i=0;i<list.size();i++)
    Global(*env.toks,env[0]+list[i].right(n),env.gloc(list[i]),tok_t::PUBLIC);
}

TOK_IMPL(import) {
  if(*env[0]=='@') env.glob_adb(env[0].popf(),env[1]);
  else {
    rpile list; env.glob_list(list,env[1]);
    for(int i=0;i<list.size();i++)
      env.aloc(env[0]+list[i].right(env[1].size()),env.glob(list[i]));
  }
}

TOK_IMPL(list) {
  mstring fx; env.parg(fx,-1);
  rpile list; env.gloc_list(list,fx);
  int n=*fx=='@'?fx.size()-1:fx.size();
  for(int i=0;i<list.size();i++) out<<list[i].right(n)<<"\n";
}

TOK_IMPL(globlist) {
  mstring fx; env.parg(fx,-1);
  rpile list; env.glob_list(list,fx);
  int n=*fx=='@'?fx.size()-1:fx.size();
  for(int i=0;i<list.size();i++) out<<list[i].right(n)<<"\n";
}

TOK_IMPL(lrec) {
  sref name=env[0],first,rest=env[1];
  mstring init; env.parg(init,-1);
  while(Split(first,rest)) env.aloc(name+first,init);
}

///////////////////////////////////////////////////

TOK_IMPL(arg) { if(env.up) env.up->parg(out,atoi(env[0])-1); }
TOK_IMPL(args) { if(env.up) out<<env.up->args; else out<<0; }

///////////////////////////////////////////////////

TOK_IMPL(random) {
  out<<Random(atoi(env[0]));
}

//////////////////////////////////////////////////////

void envAddBasic(tokmap& T)
{
  TOK_ADD(asis,"r",tok_t::PUBLIC,
          "%Display body as is\n"
          "%Syntax: \\asis{body}\n"
          );
  TOK_ADD(mute,"x",tok_t::PUBLIC,
          "%Evaluate but don't display\n"
          "%Syntax: \\mute{body}\n"
          );
  T.add(new binary_t(sref("*"),
                     sref("%Commentary - don't show or evaluate body\n"
                          "%Syntax: \\*{body}\n"),
                     tok_t::PUBLIC,sref("r"),impl_comment),0,1);
  
  //////////////////////////////////////////////////////
  
  TOK_ADD(global,"xx",tok_t::PUBLIC,
          "%Set global variable\n"
          "%Syntax: \\global{varname}{expr}\n"
          );
  TOK_ADD(cond,"xx",tok_t::PUBLIC,
          "%Set global variable if not already defined\n"
          "%Syntax: \\cond{varname}{expr}\n"
          );
  TOK_ADD(const,"xx",tok_t::PUBLIC,
          "%Set global constant\n"
          "%Syntax: \\cond{varname}{expr}\n"
          "%\n"
          "%Generates an error if already defined\n"
          );
  TOK_ADD(reconst,"xx",tok_t::PRIVATE,
          "%Redefine global constant\n"
          "%Syntax: \\cond{varname}{expr}\n"
          "%\n"
          "%Takes precedence over any existing constant\n"
          );
  
  //////////////////////////////////////////////////////
  
  TOK_ADD(type,"x",tok_t::PRIVATE,
          "%Show variable type\n"
          "%Syntax: \\type{varname}\n"
          "%\n"
          "%The type is an integer bit-field consisting of:\n"
          "%\n"
          "%1   CONSTANT\n"
          "%2   RESTRICT\n"
          "%4   NORMAL\n"
          "%8   SYSONLY\n"
          "%16  INIT\n"
          "%32  RUNNABLE\n"
          "%64  FUNCTION\n"
          "%128 BINARY\n"
          "%\n"
          "%The access modes consists of\n"
          "%\n"
          "%INIT\n"
          "%SYSONLY\n"
          "%PRIVATE = NORMAL|SYSONLY\n"
          "%PUBLIC = PRIVATE|RESTRICT\n"
          "%\n"
          "%Those values are matched against the running modes\n"
          "%to determine access. See run.\n"
          );
  TOK_ADD(desc,"x",tok_t::PRIVATE,
          "%Show variable description\n"
          "%Syntax: \\desc{varname}\n"
          "%\n"
          "%Collects all lines starting with %.\n"
          );
  TOK_ADD(expr,"x",tok_t::PRIVATE,
          "%Show the expression tree of function\n"
          "%Syntax: \\expr{varname}\n"
          "%\n"
          "%If desc is a function, the expression tree is shown.\n"
          "%Mostly interesting for real expression, i.e., when the\n"
          "%function body was defined within parenthesis.\n"
          );
  TOK_ADD(clear,"x",tok_t::SYSONLY,
          "%Clear variables\n"
          "%Syntax: \\clear{partial varname}\n"
          "%\n"
          "%Clears all global variables on the current level\n"
          "%beginning with the partial variable name.\n"
          );
  
  //////////////////////////////////////////////////////
  
  TOK_ADD(exist,"x",tok_t::PUBLIC,
          "%True if variable exist\n"
          "%Syntax: \\exist{varname}\n"
          );
  TOK_ADD(nexist,"x",tok_t::PUBLIC,
          "%True if variable does not exist\n"
          "%Syntax: \\nexist{varname}\n"
          );
  TOK_ADD(empty,"x",tok_t::PUBLIC,
          "%True if variable is empty\n"
          "%Syntax: \\empty{varname}\n"
          "%\n"
          "%Generates an error if varname does not exist.\n"
          );
  TOK_ADD(nempty,"x",tok_t::PUBLIC,
          "%True if variable is non-empty\n"
          "%Syntax: \\nempty{varname}\n"
          "%\n"
          "%Generates an error if varname does not exist.\n"
          );
  
  //////////////////////////////////////////////////////
  
  TOK_ADD(export,"xx",tok_t::SYSONLY,
          "%Export local variables\n"
          "%Syntax: \\export{global partial name}{local partial name}\n"
          "%\n"
          "%Copies a sequence of local variables, beginning with the\n"
          "%local partial name, from the same - not nescessarily the current -\n"
          "%local stack into a set of global variables beginning with\n"
          "%the global partial name.\n"
          "%\n"
          "%Copies all local variables in the current stack if no\n"
          "%partial name is given.\n"
          "%\n"
          "%Copies all variables in the current database record\n"
          "%if the partial name @ is given.\n"
          );
  TOK_ADD(import,"xx",tok_t::SYSONLY,
          "%Import global variables\n"
          "%Syntax: \\import{local partial name}{global partial name}\n"
          "%\n"
          "%Copies a sequence of global variables, beginning with the\n"
          "%global partial name, from the same - not nescessarily the\n"
          "%current - global level into a set of local variables beginning\n"
          "%with the local partial name.\n"
          "%\n"
          "%Copies all global variables on the current level if no\n"
          "%partial name is given.\n"
          "%\n"
          "%Creates a new or updates an existing database record if\n"
          "%the partial name @ is given.\n"
          );
  TOK_ADD(list,"?",tok_t::PUBLIC,
          "%List local variables\n"
          "%Syntax: \\list<partial name>\n"
          "%\n"
          "%Generates a list of names, beginning with the partial name,\n"
          "%from the same - not nescessarily the current - local stack.\n"
          "%\n"
          "%Generates all local variables in the current stack if no\n"
          "%partial name is given.\n"
          "%\n"
          "%Generates a list of tables if @ is given.\n"
          );  
  TOK_ADD(globlist,"?",tok_t::PUBLIC,
          "%List global variables\n"
          "%Syntax: \\globlist<partial name>\n"
          "%\n"
          "%Generates a list of names, beginning with the partial name,\n"
          "%from the same - not nescessarily the current - global level.\n"
          "%\n"
          "%Generates all global variables on the current level if no\n"
          "%partial name is given.\n"
          );    
  TOK_ADD(lrec,"xx?",tok_t::PUBLIC,
          "%Create a local record\n"
          "%Syntax: \\lrec{partial name}{name list}<init>\n"
          "%\n"
          "%Creates a set of local variables, all beginning with the\n"
          "%partial name, and initializes each variable to init.\n"
          );    
  
  //////////////////////////////////////////////////////
  
  TOK_ADD(arg,"x",tok_t::PUBLIC,
          "%Show/Evaluate argument #n.\n"
          "%Syntax: \\arg{n}\n"
          "%\n"
          "%Same as the command \\1, \\2 etc, except this\n"
          "%may be used to index the arguments e.g. using local variables.\n"
          );
  TOK_ADD(args,"",tok_t::PUBLIC,
          "%Number of arguments in the current function\n"
          "%Syntax: \\args\n"
          "%\n"
          "%Note that the optional argument will count as one variable,\n"
          "%and may be accesed using either \\0 or \\arg{\\args}.\n"
          );

  //////////////////////////////////////////////////////

  TOK_ADD(random,"x",tok_t::PRIVATE,
          "%Get n random digits between 0 and 9 inclusive\n"
          "%Syntax: \\random{n}\n"
          );

}