Debian Codegraph @

terps/tads/tads2/prs.c

raw symbols SCIP
1 #ifdef RCSID
2 static char RCSid[] =
3 "$Header: d:/cvsroot/tads/TADS2/PRS.C,v 1.2 1999/05/17 02:52:13 MJRoberts Exp $";
4 #endif
5
6 /*
7 * Copyright (c) 1991, 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 prs.c - code parser
15 Function
16 Parse TADS code
17 Notes
18 This module contains the TADS code parser. TADS code is basically
19 a series of object definitions (a function is just a special kind
20 of object). So, this module has a function that reads from a line
21 source and generates a compiled object. Compiled objects are
22 completely self-contained, apart from references to other objects.
23 We initially allocate a block of space for the object, then make
24 it larger if need be. If it's a class object, we will reduce its
25 size down to its actual size, because class objects generally are
26 not modified at run-time.
27 Modified
28 04/11/99 CNebel - Fix signing errors.
29 09/14/92 MJRoberts - note where an object was created
30 08/27/91 MJRoberts - creation
31 */
32
33 #include <string.h>
34 #include <assert.h>
35 #include "os.h"
36 #include "std.h"
37 #include "mcm.h"
38 #include "tok.h"
39 #include "prs.h"
40 #include "lst.h"
41 #include "prp.h"
42 #include "obj.h"
43 #include "opc.h"
44 #include "emt.h"
45 #include "mch.h"
46 #include "voc.h"
47
48
49 /* allocate space from node pool */
50 static prsndef *prsalo(prscxdef *ctx, int subnodes);
51
52 /* parse an expression (can be called recursively) */
53 static prsndef *prsxexp(prscxdef *ctx, int inlist);
54
55 /* add a symbol to the symbol table if it's not already there */
56 void prsdef(prscxdef *ctx, tokdef *tok, int typ)
57 {
58 toktdef *tab;
59
60 tab = (typ == TOKSTLABEL ? ctx->prscxgtab : ctx->prscxstab);
61
62 if (tok->toktyp == TOKTSYMBOL && tok->toksym.tokstyp == TOKSTUNK)
63 {
64 if (!tab) errsig(ctx->prscxerr, ERR_UNDFSYM);
65
66 /* see if it's been defined since we last looked */
67 if (!(*tab->toktfsea)(tab, tok->toknam, tok->toklen,
68 tok->tokhash, &tok->toksym))
69 {
70 /* still not defined; add to the symbol table */
71 (*tab->toktfadd)(tab, tok->toknam, tok->toklen,
72 TOKSTUNK, 0, tok->tokhash);
73 (*tab->toktfsea)(tab, tok->toknam, tok->toklen,
74 tok->tokhash, &tok->toksym);
75 }
76 }
77 if (tok->toktyp == TOKTSYMBOL && tok->toksym.tokstyp == TOKSTUNK)
78 {
79 if (!tab) errsig(ctx->prscxerr, ERR_UNDFSYM);
80
81 switch(typ)
82 {
83 case TOKSTPROP:
84 tok->toksym.toksval = ctx->prscxprp++;
85 break;
86
87 case TOKSTFWDOBJ:
88 case TOKSTFWDFN:
89 /* fill in the symbol with the object information */
90 prsdefobj(ctx, tok, typ);
91 break;
92
93 case TOKSTEXTERN:
94 tok->toksym.toksval = ctx->prscxextc++;
95 break;
96
97 case TOKSTLABEL:
98 tok->toksym.toksval = emtglbl(ctx->prscxemt);
99 break;
100 }
101 tok->toksym.tokstyp = typ;
102 (*tab->toktfset)(tab, &tok->toksym);
103 }
104 }
105
106 /* define a symbol as an object or forward object */
107 void prsdefobj(prscxdef *ctx, tokdef *tok, int typ)
108 {
109 mcmon newobj;
110 uchar *p;
111
112 /* allocate the object and clear its memory */
113 p = mcmalo(ctx->prscxmem, (ushort)PRSOBJSIZ, &newobj);
114 memset(p, 0, (size_t)OBJDEFSIZ);
115
116 /* save the line position after the object header */
117 linppos(ctx->prscxtok->tokcxlin, (char *)p + OBJDEFSIZ,
118 (uint)(PRSOBJSIZ - OBJDEFSIZ));
119
120 #ifdef OS_ERRLINE
121 /* save the source line for error displays later on */
122 {
123 int len;
124 int need;
125
126 /* compute how much space we'll need for the line */
127 len = ctx->prscxtok->tokcxlin->linlen + 1;
128 need = len + OBJDEFSIZ
129 + strlen((char *)p + OBJDEFSIZ) + 1;
130
131 /* reallocate the object if we need more room */
132 if (need > PRSOBJSIZ)
133 p = mcmrealo(ctx->prscxmem, newobj, need);
134
135 /* copy the line */
136 p += OBJDEFSIZ;
137 p += strlen((char *)p) + 1;
138 memcpy(p, ctx->prscxtok->tokcxlin->linbuf, (size_t)len);
139 p[len] = '\0';
140 }
141 #endif
142
143 /* unlock the object */
144 mcmtch(ctx->prscxmem, newobj);
145 mcmunlck(ctx->prscxmem, newobj);
146
147 /* set the symbol to point to the new object */
148 tok->toksym.toksval = newobj;
149 }
150
151 /* require a property identifier, returning the property number */
152 prpnum prsrqpr(prscxdef *ctx)
153 {
154 tokdef *tok = &ctx->prscxtok->tokcxcur;
155 prpnum ret;
156
157 /* make sure we get a symbol */
158 if (tok->toktyp != TOKTSYMBOL)
159 errsig(ctx->prscxerr, ERR_REQSYM);
160
161 /* if it's an undefined symbol, make it a property id */
162 if (tok->toksym.tokstyp == TOKSTUNK)
163 prsdef(ctx, tok, TOKSTPROP);
164
165 /* make sure we have a property */
166 if (tok->toksym.tokstyp != TOKSTPROP)
167 errsig(ctx->prscxerr, ERR_REQPRP);
168
169 ret = tok->toksym.toksval;
170 toknext(ctx->prscxtok);
171 return(ret);
172 }
173
174 /* signal a "missing required token" error, finding token name */
175 void prssigreq(prscxdef *ctx, int t)
176 {
177 int i;
178 char *p;
179 static struct
180 {
181 int tokid;
182 char *toknam;
183 } toklist[] =
184 {
185 { TOKTSEM, "semicolon" },
186 { TOKTCOLON, "colon" },
187 { TOKTFUNCTION, "\"function\"" },
188 { TOKTCOMMA, "comma" },
189 { TOKTLBRACE, "left brace ('{')" },
190 { TOKTRPAR, "right paren (')')" },
191 { TOKTRBRACK, "right square bracket (']')" },
192 { TOKTWHILE, "\"while\"" },
193 { TOKTLPAR, "left paren ('(')" },
194 { TOKTEQ, "'='" },
195 { 0, (char *)0 }
196 };
197
198 for (i = 0 ; toklist[i].toknam ; ++i)
199 {
200 if (toklist[i].tokid == t)
201 {
202 p = toklist[i].toknam;
203 break;
204 }
205 }
206 if (!toklist[i].toknam)
207 p = "<unknown token>";
208 errsig1(ctx->prscxerr, ERR_REQTOK, ERRTSTR, p);
209 }
210
211 /* check for and skip a required token */
212 void prsreq(prscxdef *ctx, int t)
213 {
214 tokdef *tok = &ctx->prscxtok->tokcxcur;
215
216 if (tok->toktyp != t)
217 prssigreq(ctx, t);
218 toknext(ctx->prscxtok);
219 }
220
221 /* get next token, require it to be a specific token, and skip it */
222 void prsnreq(prscxdef *ctx, int t)
223 {
224 tokdef *tok = &ctx->prscxtok->tokcxcur;
225
226 toknext(ctx->prscxtok);
227 if (tok->toktyp != t)
228 prssigreq(ctx, t);
229 toknext(ctx->prscxtok);
230 }
231
232 /* allocate bytes from node pool */
233 uchar *prsbalo(prscxdef *ctx, uint siz)
234 {
235 uchar *ret;
236
237 /* round size for alignment, and see if we have room for it */
238 siz = osrndsz(siz);
239 if (ctx->prscxnrem < siz) errsig(ctx->prscxerr, ERR_NONODE);
240
241 /* adjust sizes and pointers, and return the new node */
242 ret = ctx->prscxnode;
243 ctx->prscxnrem -= siz;
244 ctx->prscxnode += siz;
245 return(ret);
246 }
247
248 /* allocate a node with a given number of subnodes from node pool */
249 static prsndef *prsalo(prscxdef *ctx, int subnodes)
250 {
251 uint siz;
252
253 /* figure size of node we actually need */
254 siz = offsetof(prsndef, prsnv.prsnvt);
255 if (subnodes)
256 siz += (subnodes * sizeof(prsndef *));
257 else
258 siz += sizeof(tokdef);
259
260 return((prsndef *)prsbalo(ctx, siz));
261 }
262
263 /* begin a string in expression evaluation mode (puts in node pool) */
264 ushort prsxsst(prscxdef *ctx)
265 {
266 /* make sure there's space for the length prefix (a ushort) */
267 if (ctx->prscxnrem < 2)
268 errsig(ctx->prscxerr, ERR_NONODE);
269
270 ctx->prscxsofs = ctx->prscxnode - &ctx->prscxpool[0];
271 ctx->prscxnode += 2;
272 ctx->prscxnrem -= 2;
273 ctx->prscxslen = 2;
274 return(ctx->prscxsofs);
275 }
276
277 /* add bytes to a string in expression mode */
278 void prsxsad(prscxdef *ctx, char *p, ushort len)
279 {
280 /* make sure there's room for the extra bytes */
281 if (ctx->prscxnrem < len)
282 errsig(ctx->prscxerr, ERR_NONODE);
283
284 memcpy(ctx->prscxnode, p, (size_t)len);
285 ctx->prscxnode += len;
286 ctx->prscxnrem -= len;
287 ctx->prscxslen += len;
288 }
289
290 /* terminate a string in expression evaluation mode */
291 void prsxsend(prscxdef *ctx)
292 {
293 ushort siz;
294 ushort rounded;
295
296 /* figure total size of string, including padding for alignment */
297 siz = ctx->prscxslen;
298
299 /* write length at beginning of string */
300 oswp2(&ctx->prscxpool[ctx->prscxsofs], siz);
301
302 /* if desired, write the string to the strings file */
303 if (ctx->prscxstrfile != 0)
304 {
305 uchar *str;
306
307 str = &ctx->prscxpool[ctx->prscxsofs] + 2;
308 str[siz - 2] = '\0';
309 osfputs((char *)str, ctx->prscxstrfile);
310 osfputs("\n", ctx->prscxstrfile);
311 }
312
313 /* round size of string if necessary */
314 rounded = osrndsz(siz);
315 if (rounded - siz)
316 {
317 rounded -= siz;
318 if (ctx->prscxnrem < rounded)
319 errsig(ctx->prscxerr, ERR_NONODE);
320 ctx->prscxnode += rounded;
321 ctx->prscxnrem -= rounded;
322 }
323 }
324
325 /* parse a list */
326 static prsndef *prslst(prscxdef *ctx)
327 {
328 prsndef *node = (prsndef *)0;
329
330 for (;;)
331 {
332 if (ctx->prscxtok->tokcxcur.toktyp == TOKTRBRACK) break;
333 node = prsnew2(ctx, TOKTRBRACK, prsxexp(ctx, TRUE), node);
334
335 /* allow optional comma (just skip it if one is present) */
336 if (ctx->prscxtok->tokcxcur.toktyp == TOKTCOMMA)
337 toknext(ctx->prscxtok);
338 }
339 toknext(ctx->prscxtok); /* skip the right bracket */
340
341 /* if it's an empty list, make an empty list token for it */
342 if (!node)
343 {
344 tokdef t;
345
346 t.toktyp = TOKTLIST;
347 t.tokofs = 0;
348 node = prsnew0(ctx, &t);
349 }
350
351 return(node);
352 }
353
354 /* build a four-way operator node */
355 prsndef *prsnew4(prscxdef *ctx, int t, prsndef *n1, prsndef *n2,
356 prsndef *n3, prsndef *n4)
357 {
358 prsndef *n = prsalo(ctx, 4);
359
360 n->prsntyp = t;
361 n->prsnnlf = 4;
362 n->prsnv.prsnvn[0] = n1;
363 n->prsnv.prsnvn[1] = n2;
364 n->prsnv.prsnvn[2] = n3;
365 n->prsnv.prsnvn[3] = n4;
366 return(n);
367 }
368
369 /* build a tertiary operator node - three subnodes */
370 prsndef *prsnew3(prscxdef *ctx, int t, prsndef *n1, prsndef *n2, prsndef *n3)
371 {
372 prsndef *n = prsalo(ctx, 3);
373
374 n->prsntyp = t;
375 n->prsnnlf = 3;
376 n->prsnv.prsnvn[0] = n1;
377 n->prsnv.prsnvn[1] = n2;
378 n->prsnv.prsnvn[2] = n3;
379 return(n);
380 }
381
382 /* build a binary operator node - two subnodes */
383 prsndef *prsnew2(prscxdef *ctx, int t, prsndef *n1, prsndef *n2)
384 {
385 prsndef *n = prsalo(ctx, 2);
386
387 n->prsntyp = t;
388 n->prsnnlf = 2;
389 n->prsnv.prsnvn[0] = n1;
390 n->prsnv.prsnvn[1] = n2;
391 return(n);
392 }
393
394 /* build a unary operator node - one subnode */
395 prsndef *prsnew1(prscxdef *ctx, int t, prsndef *n)
396 {
397 prsndef *newnode = prsalo(ctx, 1);
398
399 newnode->prsntyp = t;
400 newnode->prsnnlf = 1;
401 newnode->prsnv.prsnvn[0] = n;
402 return newnode;
403 }
404
405 /* build a new value node - no subnodes */
406 prsndef *prsnew0(prscxdef *ctx, tokdef *tokp)
407 {
408 prsndef *n = prsalo(ctx, 0);
409
410 n->prsntyp = 0; /* value node */
411 n->prsnnlf = 0;
412 OSCPYSTRUCT(n->prsnv.prsnvt, *tokp);
413 return(n);
414 }
415
416 /* binary operator definition */
417 typedef struct prsbdef prsbdef;
418 struct prsbdef
419 {
420 int *prsblst; /* list of tokens at this precedence level */
421 prsndef *(*prsblf)(prscxdef *ctx, prsbdef *binctx);
422 prsndef *(*prsbrf)(prscxdef *ctx, prsbdef *binctx);
423 prsbdef *prsbctx; /* secondary context for left & right */
424 int prsbrpt; /* TRUE ==> iterate right operand */
425 };
426
427
428 /* generate code for a function call */
429 static void prsgfn(prscxdef *ctx, prsndef *n, int argcnt);
430
431 /* generate code for an argument list; returns argument count */
432 static int prsgargs(prscxdef *ctx, prsndef *n);
433
434 /* generate code for a property lookup */
435 static void prsgdot(prscxdef *ctx, prsndef *n, int argcnt);
436
437 /* generate code for a property lookup during speculative evaluation */
438 static void prsgdotspec(prscxdef *ctx, prsndef *n);
439
440 /* generate code to assign an l-value */
441 static void prsglval(prscxdef *ctx, int typ, prsndef *n);
442
443 /* Build a general binary node. The context tells us how. */
444 static prsndef *prsxbin(prscxdef *ctx, prsbdef *binctx);
445
446 /* parse an argument list */
447 static prsndef *prsxarg(prscxdef *ctx);
448
449 /* parse an atom */
450 static prsndef *prsxatm(prscxdef *ctx);
451
452 /* parse a structure member expression */
453 static prsndef *prsxmem(prscxdef *ctx);
454
455 /* parse a unary operator expression */
456 static prsndef *prsxun(prscxdef *ctx, prsbdef *binctx);
457
458 /* parse a conditional expression */
459 static prsndef *prsxcnd(prscxdef *ctx, prsbdef *binctx);
460
461 /* factor */
462 static int prsl_fact[] = { TOKTTIMES, TOKTDIV, TOKTMOD, 0 };
463 static prsbdef prsb_fact = { prsl_fact, prsxun, prsxun, 0, TRUE };
464
465 /* term */
466 static int prsl_term[] = { TOKTPLUS, TOKTMINUS, 0 };
467 static prsbdef prsb_term = { prsl_term, prsxbin, prsxbin, &prsb_fact, TRUE };
468
469 /* shift */
470 static int prsl_shift[] = { TOKTSHL, TOKTSHR, 0 };
471 static prsbdef prsb_shift = { prsl_shift, prsxbin, prsxbin, &prsb_term, TRUE };
472
473 /* relational (magnitude comparison) */
474 static int prsl_rel[] = { TOKTGT, TOKTGE, TOKTLT, TOKTLE, 0 };
475 static prsbdef prsb_rel = { prsl_rel, prsxbin, prsxbin, &prsb_shift, FALSE };
476
477 /* comparison */
478 /* WARNING - keep prsl_cmp and prsl_cmp_C in sync with one another */
479 static int prsl_cmp[] =
480 { TOKTEQ, TOKTNE, 0 };
481 static prsbdef prsb_cmp = { prsl_cmp, prsxbin, prsxbin, &prsb_rel, FALSE };
482 static int prsl_cmp_C[] =
483 { TOKTEQEQ, TOKTNE, 0 };
484 static prsbdef prsb_cmp_C =
485 { prsl_cmp_C, prsxbin, prsxbin, &prsb_rel, FALSE };
486
487 /* bitwise AND */
488 static int prsl_band[] = { TOKTBOR, 0 };
489 static prsbdef prsb_band = { prsl_band, prsxbin, prsxbin, &prsb_cmp, TRUE };
490 static prsbdef prsb_band_C = {prsl_band, prsxbin, prsxbin, &prsb_cmp_C, TRUE };
491
492 /* bitwise XOR */
493 static int prsl_xor[] = { TOKTXOR, 0 };
494 static prsbdef prsb_xor = { prsl_xor, prsxbin, prsxbin, &prsb_band, TRUE };
495 static prsbdef prsb_xor_C = { prsl_xor, prsxbin, prsxbin, &prsb_band_C, TRUE };
496
497 /* bitwise OR */
498 static int prsl_bor[] = { TOKTBAND, 0 };
499 static prsbdef prsb_bor = { prsl_bor, prsxbin, prsxbin, &prsb_xor, TRUE };
500 static prsbdef prsb_bor_C = { prsl_bor, prsxbin, prsxbin, &prsb_xor_C, TRUE };
501
502 /* logical AND */
503 static int prsl_and[] = { TOKTAND, 0 };
504 static prsbdef prsb_and = { prsl_and, prsxbin, prsxbin, &prsb_bor, TRUE };
505 static prsbdef prsb_and_C = { prsl_and, prsxbin, prsxbin, &prsb_bor_C, TRUE };
506
507 /* logical OR */
508 static int prsl_or[] = { TOKTOR, 0 };
509 static prsbdef prsb_or = { prsl_or, prsxbin, prsxbin, &prsb_and, TRUE };
510 static prsbdef prsb_or_C = { prsl_or, prsxbin, prsxbin, &prsb_and_C, TRUE };
511
512 /* assignment */
513 /* WARNING - keep prsl_asi and prsl_asi_C in sync with one another */
514 static int prsl_asi[] =
515 { TOKTASSIGN, TOKTPLEQ, TOKTMINEQ, TOKTDIVEQ, TOKTTIMEQ, TOKTMODEQ,
516 TOKTBANDEQ, TOKTBOREQ, TOKTXOREQ, TOKTSHLEQ, TOKTSHREQ, 0 };
517 static int prsl_asi_C[] =
518 { TOKTEQ, TOKTPLEQ, TOKTMINEQ, TOKTDIVEQ, TOKTTIMEQ, TOKTMODEQ,
519 TOKTBANDEQ, TOKTBOREQ, TOKTXOREQ, TOKTSHLEQ, TOKTSHREQ, 0 };
520 static prsbdef prsb_asi = { prsl_asi, prsxcnd, prsxbin, &prsb_asi, FALSE };
521 static prsbdef prsb_asi_C =
522 { prsl_asi_C, prsxcnd, prsxbin, &prsb_asi_C, FALSE };
523
524 /* comma */
525 static int prsl_cma[] = { TOKTCOMMA, 0 };
526 static prsbdef prsb_cma = { prsl_cma, prsxbin, prsxbin, &prsb_asi, TRUE };
527 static prsbdef prsb_cma_C = { prsl_cma, prsxbin, prsxbin, &prsb_asi_C, TRUE };
528
529
530 /* determine if we're in C-mode - return true if so, false otherwise */
531 static int prs_cmode(prscxdef *ctx)
532 {
533 return (ctx->prscxtok->tokcxflg & TOKCXFCMODE) != 0;
534 }
535
536 /* build general binary node, iterating to the right if desired */
537 static prsndef *prsxbin(prscxdef *ctx, prsbdef *binctx)
538 {
539 prsndef *n;
540 int t;
541 int *lp;
542
543 n = (*binctx->prsblf)(ctx, binctx->prsbctx);
544 do
545 {
546 t = ctx->prscxtok->tokcxcur.toktyp;
547 for (lp = binctx->prsblst ; *lp && *lp != t ; ++lp) ;
548 if (*lp == t)
549 {
550 /*
551 * if inside a list, check to see if this is a binary
552 * operator that can also server as a unary operator -- if
553 * so, interpret it as a unary operator and warn about it
554 */
555 if (ctx->prscxflg & PRSCXFLST)
556 {
557 static int ambig_ops[] = { TOKTBAND, TOKTPLUS, TOKTMINUS, 0 };
558 static char *ambig_names[] = { "&", "+", "-" };
559
560 for (lp = ambig_ops ; *lp && *lp != t ; ++lp) ;
561 if (*lp)
562 {
563 errlog1(ctx->prscxerr, ERR_AMBIGBIN,
564 ERRTSTR, ambig_names[lp - ambig_ops]);
565 break;
566 }
567 }
568
569 /* skip the operator */
570 toknext(ctx->prscxtok);
571
572 /* use C-style operators if in C mode */
573 if (prs_cmode(ctx))
574 {
575 switch(t)
576 {
577 case TOKTEQEQ:
578 t = TOKTEQ;
579 break;
580
581 case TOKTEQ:
582 t = TOKTASSIGN;
583 break;
584 }
585 }
586
587 /* generate a node for the binary operator */
588 n = prsnew2(ctx, t, n, (*binctx->prsbrf)(ctx, binctx->prsbctx));
589 }
590 else
591 break;
592 } while (binctx->prsbrpt);
593
594 return(n);
595 }
596
597 static prsndef *prsxarg(prscxdef *ctx)
598 {
599 prsndef *n;
600 int t;
601
602 /* arguments are assignments: just under comma-expressions */
603 n = prsxbin(ctx, prs_cmode(ctx) ? &prsb_asi_C : &prsb_asi);
604
605 t = ctx->prscxtok->tokcxcur.toktyp;
606 toknext(ctx->prscxtok);
607 switch(t)
608 {
609 case TOKTRPAR:
610 return(n);
611
612 case TOKTCOMMA:
613 return(prsnew2(ctx, TOKTRPAR, n, prsxarg(ctx)));
614
615 default:
616 errsig(ctx->prscxerr, ERR_REQARG);
617 NOTREACHEDV(prsndef *);
618 return 0;
619 }
620 }
621
622 static prsndef *prsxatm(prscxdef *ctx)
623 {
624 prsndef *n;
625 tokdef *tok = &ctx->prscxtok->tokcxcur;
626 tokdef newtok;
627 prpnum pr;
628
629 switch(tok->toktyp)
630 {
631 case TOKTPOUND:
632 case TOKTBAND:
633 toknext(ctx->prscxtok);
634 if (ctx->prscxtok->tokcxcur.toktyp == TOKTSYMBOL &&
635 (ctx->prscxtok->tokcxcur.toksym.tokstyp == TOKSTFWDFN ||
636 ctx->prscxtok->tokcxcur.toksym.tokstyp == TOKSTFUNC))
637 {
638 n = prsnew0(ctx, tok);
639 toknext(ctx->prscxtok);
640 return(n);
641 }
642 else
643 {
644 pr = prsrqpr(ctx);
645 newtok.toktyp = TOKTPOUND;
646 newtok.tokofs = pr;
647 return(prsnew0(ctx, &newtok));
648 }
649
650 case TOKTLPAR:
651 toknext(ctx->prscxtok);
652 n = prsxexp(ctx, FALSE);
653 prsreq(ctx, TOKTRPAR);
654 return(n);
655
656 case TOKTLBRACK:
657 toknext(ctx->prscxtok);
658 return(prslst(ctx));
659
660 case TOKTNUMBER:
661 case TOKTNIL:
662 case TOKTTRUE:
663 case TOKTSSTRING:
664 case TOKTDSTRING:
665 /* create and return a new terminal node for this item */
666 n = prsnew0(ctx, tok);
667 toknext(ctx->prscxtok);
668 return n;
669
670 case TOKTSYMBOL:
671 /* create a new terminal node for the symbol */
672 n = prsnew0(ctx, tok);
673 toknext(ctx->prscxtok);
674
675 /*
676 * if it was 'inherited', and there's another symbol name
677 * following, we have the explicit superclass notation - parse
678 * it
679 */
680 if (n->prsnv.prsnvt.toksym.tokstyp == TOKSTINHERIT
681 && tok->toktyp == TOKTSYMBOL)
682 {
683 /* build a new node for the explicit superclass */
684 n = prsnew0(ctx, tok);
685
686 /* build a containing node for the inheritance operator */
687 n = prsnew1(ctx, TOKTEXPINH, n);
688
689 /* skip the superclass name token */
690 toknext(ctx->prscxtok);
691 }
692
693 /* return the new node */
694 return n;
695
696 default:
697 errsig(ctx->prscxerr, ERR_REQOPN);
698 return 0;
699 NOTREACHEDV(prsndef *);
700 }
701 }
702
703 static prsndef *prsxmem(prscxdef *ctx)
704 {
705 prsndef *n1, *n2;
706 tokdef *tok;
707 tokdef newtok;
708 prpnum prop;
709
710 n1 = prsxatm(ctx);
711 for ( ;; )
712 {
713 tok = &ctx->prscxtok->tokcxcur;
714 switch(tok->toktyp)
715 {
716 case TOKTDOT:
717 if (toknext(ctx->prscxtok) == TOKTLPAR)
718 {
719 toknext(ctx->prscxtok);
720 n2 = prsxexp(ctx, FALSE);
721 prsreq(ctx, TOKTRPAR);
722 }
723 else if (ctx->prscxtok->tokcxcur.toktyp == TOKTSYMBOL &&
724 ctx->prscxtok->tokcxcur.toksym.tokstyp == TOKSTLOCAL)
725 {
726 n2 = prsnew0(ctx, &ctx->prscxtok->tokcxcur);
727 toknext(ctx->prscxtok);
728 }
729 else
730 {
731 prop = prsrqpr(ctx);
732 newtok.toktyp = TOKTPOUND;
733 newtok.tokofs = prop;
734 n2 = prsnew0(ctx, &newtok);
735 }
736
737 /* see if there's an argument list for the method */
738 if (ctx->prscxtok->tokcxcur.toktyp == TOKTLPAR)
739 {
740 toknext(ctx->prscxtok);
741 n1 = prsnew3(ctx, TOKTDOT, n1, n2, prsxarg(ctx));
742 }
743 else
744 n1 = prsnew2(ctx, TOKTDOT, n1, n2);
745
746 break;
747
748 case TOKTLBRACK:
749 if (ctx->prscxflg & PRSCXFLST) return(n1);
750 toknext(ctx->prscxtok);
751 n2 = prsxexp(ctx, FALSE);
752 n1 = prsnew2(ctx, TOKTLBRACK, n1, n2);
753 prsreq(ctx, TOKTRBRACK);
754 break;
755
756 case TOKTLPAR:
757 if (toknext(ctx->prscxtok) == TOKTRPAR)
758 {
759 toknext(ctx->prscxtok);
760 n1 = prsnew1(ctx, TOKTLPAR, n1);
761 }
762 else
763 n1 = prsnew2(ctx, TOKTLPAR, n1, prsxarg(ctx));
764 break;
765
766 case TOKTINC:
767 case TOKTDEC:
768 n1 = prsnew1(ctx, tok->toktyp + 1, n1);
769 toknext(ctx->prscxtok);
770 return(n1);
771
772 default:
773 return(n1);
774 }
775 }
776 }
777
778 /* parse a unary operator expression */
779 static prsndef *prsxun(prscxdef *ctx, prsbdef *binctx)
780 {
781 int t;
782
783 VARUSED(binctx);
784
785 t = ctx->prscxtok->tokcxcur.toktyp;
786 if (t == TOKTPLUS || t == TOKTMINUS || t == TOKTNOT
787 || t == TOKTINC || t == TOKTDEC || t == TOKTTILDE
788 || t == TOKTDELETE)
789 {
790 toknext(ctx->prscxtok);
791 return(prsnew1(ctx, t, prsxun(ctx, (prsbdef *)0)));
792 }
793 else if (t == TOKTNEW)
794 {
795 if (toknext(ctx->prscxtok) == TOKTOBJECT)
796 {
797 tokdef tok;
798
799 toknext(ctx->prscxtok);
800 tok.toktyp = TOKTNEW;
801 return prsnew0(ctx, &tok);
802 }
803 else
804 return prsnew1(ctx, t, prsxun(ctx, (prsbdef *)0));
805 }
806 return(prsxmem(ctx));
807 }
808
809 /* parse a ternary conditional expression */
810 static prsndef *prsxcnd(prscxdef *ctx, prsbdef *binctx)
811 {
812 prsndef *n1, *n2;
813
814 VARUSED(binctx);
815
816 n1 = prsxbin(ctx, prs_cmode(ctx) ? &prsb_or_C : &prsb_or);
817 for (;;)
818 {
819 if (ctx->prscxtok->tokcxcur.toktyp != TOKTQUESTION) break;
820 toknext(ctx->prscxtok);
821 n2 = prsxexp(ctx, FALSE);
822 prsreq(ctx, TOKTCOLON);
823 n1 = prsnew3(ctx, TOKTQUESTION, n1, n2,
824 prsxbin(ctx, prs_cmode(ctx) ? &prsb_asi_C : &prsb_asi));
825 }
826 return(n1);
827 }
828
829 /* parse an expression (called recursively during expression parsing) */
830 static prsndef *prsxexp(prscxdef *ctx, int inlist)
831 {
832 ushort oldflg = ctx->prscxflg;
833 prsndef *retval;
834
835 if (inlist)
836 {
837 ctx->prscxflg |= PRSCXFLST;
838 retval = prsxbin(ctx, (prs_cmode(ctx) ? &prsb_asi_C : &prsb_asi));
839 }
840 else
841 {
842 ctx->prscxflg &= ~PRSCXFLST;
843 retval = prsxbin(ctx, prs_cmode(ctx) ? &prsb_cma_C : &prsb_cma);
844 }
845
846 ctx->prscxflg = oldflg;
847 return(retval);
848 }
849
850 /* fold constants in a parse tree */
851 static prsndef *prsfold(prscxdef *ctx, prsndef *node)
852 {
853 int i;
854 prsndef **n;
855 int can_fold;
856 int typ;
857 tokdef *tok1, *tok2;
858 int typ1, typ2;
859 long val1, val2;
860 prsndef *ncur;
861
862 can_fold = TRUE; /* assume we can do some folding */
863
864 /* if this is a list-construction node, special handling is needed */
865 if (node->prsntyp == TOKTRBRACK)
866 {
867 for (i=0, ncur = node ; ncur ; ++i, ncur = ncur->prsnv.prsnvn[1])
868 {
869 ncur->prsnv.prsnvn[0] = prsfold(ctx, ncur->prsnv.prsnvn[0]);
870 typ = TOKTINVALID;
871 if (ncur->prsnv.prsnvn[0]->prsnnlf != 0 ||
872 ((typ = ncur->prsnv.prsnvn[0]->prsnv.prsnvt.toktyp)
873 != TOKTNUMBER && typ != TOKTSSTRING && typ != TOKTLIST
874 && typ != TOKTNIL && typ != TOKTTRUE && typ != TOKTPOUND
875 && typ != TOKTSYMBOL))
876 can_fold = FALSE; /* not constant - can't fold */
877
878 if (typ == TOKTSYMBOL
879 && (ncur->prsnv.prsnvn[0]->prsnv.prsnvt.toksym.tokstyp
880 == TOKSTLOCAL
881 || ncur->prsnv.prsnvn[0]->prsnv.prsnvt.toksym.tokstyp
882 == TOKSTSELF))
883 can_fold = FALSE;
884
885 if (ncur->prsnv.prsnvn[0]->prsnnlf == 0 && typ == TOKTSYMBOL)
886 prsdef(ctx, &ncur->prsnv.prsnvn[0]->prsnv.prsnvt,
887 TOKSTFWDOBJ);
888 }
889 if (can_fold)
890 {
891 tokdef t;
892 emtlidef *lst;
893 prsndef *retval;
894 emtledef *ele;
895
896 lst = (emtlidef *)prsbalo(ctx, (uint)(sizeof(emtlidef) +
897 (i - 1) * sizeof(emtledef)));
898 t.toktyp = TOKTLIST;
899 t.tokofs = ((uchar *)lst) - &ctx->prscxpool[0];
900 retval = prsnew0(ctx, &t);
901
902 lst->emtlicnt = i;
903
904 for (ele = &lst->emtliele[0] ; node ;
905 node = node->prsnv.prsnvn[1], ++ele)
906 {
907 ncur = node->prsnv.prsnvn[0];
908 switch(ele->emtletyp = ncur->prsnv.prsnvt.toktyp)
909 {
910 case TOKTLIST:
911 case TOKTSSTRING:
912 case TOKTPOUND:
913 ele->emtleval = ncur->prsnv.prsnvt.tokofs;
914 break;
915
916 case TOKTSYMBOL:
917 switch(ncur->prsnv.prsnvt.toksym.tokstyp)
918 {
919 case TOKSTFUNC:
920 case TOKSTFWDFN:
921 ele->emtletyp = TOKTFUNCTION;
922 break;
923 case TOKSTOBJ:
924 case TOKSTFWDOBJ:
925 ele->emtletyp = TOKTOBJECT;
926 break;
927 case TOKSTPROP:
928 ele->emtletyp = TOKTDOT;
929 break;
930 case TOKSTPROPSPEC:
931 ele->emtletyp = TOKTDOT;
932 break;
933 default:
934 errsig(ctx->prscxerr, ERR_INVLSTE);
935 }
936 ele->emtleval = ncur->prsnv.prsnvt.toksym.toksval;
937 break;
938
939 default:
940 ele->emtleval = ncur->prsnv.prsnvt.tokval;
941 break;
942 }
943 }
944
945 node = retval;
946 }
947 return(node);
948 }
949
950 /*
951 * If we have sub-nodes, try to fold them. If they fold down to
952 * leaf nodes, we can try to apply the expression to them.
953 */
954 for (i = node->prsnnlf, n = &node->prsnv.prsnvn[0] ; i ; ++n, --i)
955 {
956 *n = prsfold(ctx, *n);
957 if ((*n)->prsnnlf != 0 ||
958 ((typ = (*n)->prsnv.prsnvt.toktyp) != TOKTNUMBER &&
959 typ != TOKTSSTRING && typ != TOKTLIST &&
960 typ != TOKTNIL && typ != TOKTTRUE && typ != TOKTPOUND))
961 can_fold = FALSE; /* not constant - can't fold */
962 }
963
964 /* if at a leaf, or subnodes are non-constant, can't fold anything */
965 if (node->prsnnlf == 0 || !can_fold) return(node);
966
967 switch(node->prsnnlf)
968 {
969 case 1:
970 tok1 = &node->prsnv.prsnvn[0]->prsnv.prsnvt;
971 typ1 = tok1->toktyp;
972 val1 = tok1->tokval;
973
974 switch(node->prsntyp)
975 {
976 case TOKTPLUS:
977 break;
978
979 case TOKTMINUS:
980 if (typ1 != TOKTNUMBER)
981 errsig(ctx->prscxerr, ERR_INVOP);
982 tok1->tokval = -val1;
983 break;
984
985 case TOKTNOT:
986 if (!prsvlog(typ1))
987 errsig(ctx->prscxerr, ERR_INVOP);
988 tok1->toktyp = (prs2log(typ1, val1) == TOKTNIL ?
989 TOKTTRUE : TOKTNIL);
990 break;
991
992 case TOKTTILDE:
993 if (typ1 != TOKTNUMBER)
994 errsig(ctx->prscxerr, ERR_INVOP);
995 tok1->tokval = ~val1;
996 break;
997
998 default:
999 return(node);
1000 }
1001 break;
1002
1003 case 2:
1004 tok1 = &node->prsnv.prsnvn[0]->prsnv.prsnvt;
1005 tok2 = &node->prsnv.prsnvn[1]->prsnv.prsnvt;
1006 val1 = tok1->tokval;
1007 val2 = tok2->tokval;
1008 typ1 = tok1->toktyp;
1009 typ2 = tok2->toktyp;
1010
1011 switch(node->prsntyp)
1012 {
1013 case TOKTOR:
1014 case TOKTAND:
1015 if (!prsvlog(typ1) || !prsvlog(typ2))
1016 errsig(ctx->prscxerr, ERR_INVOP);
1017 typ1 = (prs2log(typ1, val1) == TOKTTRUE);
1018 typ2 = (prs2log(typ2, val2) == TOKTTRUE);
1019
1020 if (node->prsntyp == TOKTAND) typ1 = typ1 && typ2;
1021 else typ1 = typ1 || typ2;
1022
1023 tok1->toktyp = (typ1 ? TOKTTRUE : TOKTNIL);
1024 break;
1025
1026 case TOKTPLUS:
1027 case TOKTMINUS:
1028 if (typ1 == TOKTSSTRING || typ1 == TOKTLIST || typ2 == TOKTLIST)
1029 return(node);
1030 /* FALLTHROUGH */
1031
1032 case TOKTEQ:
1033 case TOKTNE:
1034 case TOKTGT:
1035 case TOKTGE:
1036 case TOKTLT:
1037 case TOKTLE:
1038 if (typ1 != TOKTNUMBER || typ2 != TOKTNUMBER)
1039 return(node);
1040 /* FALLTHROUGH */
1041
1042 case TOKTXOR:
1043 if ((typ1 == TOKTTRUE || typ1 == TOKTNIL)
1044 && (typ2 == TOKTTRUE || typ2 == TOKTNIL))
1045 {
1046 int a, b;
1047
1048 a = (typ1 == TOKTTRUE ? 1 : 0);
1049 b = (typ2 == TOKTTRUE ? 1 : 0);
1050 tok1->toktyp = ((a ^ b) ? TOKTTRUE : TOKTNIL);
1051 break;
1052 }
1053 /* FALLTHROUGH */
1054
1055 case TOKTDIV:
1056 case TOKTTIMES:
1057 case TOKTMOD:
1058 case TOKTSHL:
1059 case TOKTSHR:
1060 if (typ1 != TOKTNUMBER || typ2 != TOKTNUMBER)
1061 errsig(ctx->prscxerr, ERR_INVOP);
1062 switch(node->prsntyp)
1063 {
1064 case TOKTPLUS:
1065 val1 += val2;
1066 break;
1067 case TOKTMINUS:
1068 val1 -= val2;
1069 break;
1070 case TOKTTIMES:
1071 val1 *= val2;
1072 break;
1073 case TOKTDIV:
1074 if (val2 == 0)
1075 errsig(ctx->prscxerr, ERR_DIVZERO);
1076 val1 /= val2;
1077 break;
1078 case TOKTMOD:
1079 if (val2 == 0)
1080 errsig(ctx->prscxerr, ERR_DIVZERO);
1081 val1 %= val2;
1082 break;
1083 case TOKTXOR:
1084 val1 ^= val2;
1085 break;
1086 case TOKTSHL:
1087 val1 <<= val2;
1088 break;
1089 case TOKTSHR:
1090 val1 >>= val2;
1091 break;
1092 case TOKTEQ:
1093 typ1 = (val1 == val2 ? TOKTTRUE : TOKTNIL);
1094 break;
1095 case TOKTNE:
1096 typ1 = (val1 != val2 ? TOKTTRUE : TOKTNIL);
1097 break;
1098 case TOKTGT:
1099 typ1 = (val1 > val2 ? TOKTTRUE : TOKTNIL);
1100 break;
1101 case TOKTLT:
1102 typ1 = (val1 < val2 ? TOKTTRUE : TOKTNIL);
1103 break;
1104 case TOKTLE:
1105 typ1 = (val1 <= val2 ? TOKTTRUE : TOKTNIL);
1106 break;
1107 case TOKTGE:
1108 typ1 = (val1 >= val2 ? TOKTTRUE : TOKTNIL);
1109 break;
1110
1111 default:
1112 return(node);
1113 }
1114 tok1->toktyp = typ1;
1115 tok1->tokval = val1;
1116 break;
1117
1118 default:
1119 return(node);
1120 }
1121 break;
1122
1123 case 3:
1124 tok1 = &node->prsnv.prsnvn[0]->prsnv.prsnvt;
1125 if (!prsvlog(tok1->toktyp)) errsig(ctx->prscxerr, ERR_INVOP);
1126 if (prs2log(tok1->toktyp, tok1->tokval) == TOKTTRUE)
1127 return(node->prsnv.prsnvn[1]);
1128 else
1129 return(node->prsnv.prsnvn[2]);
1130
1131 default:
1132 return(node);
1133 }
1134
1135 /* return the first sub-node, which has the folded value */
1136 return(node->prsnv.prsnvn[0]);
1137 }
1138
1139 /* start parsing an initializer expression (resets node pool) */
1140 prsndef *prsxini(prscxdef *ctx)
1141 {
1142 prsndef *node;
1143
1144 node = prsxbin(ctx, (prs_cmode(ctx) ? &prsb_asi_C : &prsb_asi));
1145 return(prsfold(ctx, node)); /* fold constants */
1146 }
1147
1148 /* start parsing an expression (folds constants) */
1149 prsndef *prsexpr(prscxdef *ctx)
1150 {
1151 return(prsfold(ctx, prsxbin(ctx,
1152 prs_cmode(ctx) ? &prsb_cma_C : &prsb_cma)));
1153 }
1154
1155 /* parse an expression and generate code for it immediately */
1156 void prsxgen(prscxdef *ctx)
1157 {
1158 prsndef *exprnode;
1159
1160 exprnode = prsexpr(ctx);
1161 prsgexp(ctx, exprnode);
1162 prsrstn(ctx);
1163 }
1164
1165 /*
1166 * Check a parse tree for speculative execution problems.
1167 */
1168 static void prs_check_spec(prscxdef *ctx, prsndef *expr)
1169 {
1170 int i;
1171
1172 /*
1173 * check the type of this node - if it's an assignment or a function
1174 * call, prohibit it
1175 */
1176 if (expr->prsnnlf == 0)
1177 {
1178 /* value node - check for a symbol */
1179 if (expr->prsnv.prsnvt.toktyp == TOKTSYMBOL)
1180 {
1181 switch(expr->prsnv.prsnvt.toksym.tokstyp)
1182 {
1183 case TOKSTPROP:
1184 /*
1185 * convert this to a speculative evaluation property, so
1186 * that the emitter knows to emit the correct opcode for
1187 * it
1188 */
1189 expr->prsnv.prsnvt.toksym.tokstyp = TOKSTPROPSPEC;
1190 break;
1191
1192 case TOKSTBIFN:
1193 case TOKSTINHERIT:
1194 case TOKSTEXTERN:
1195 /* don't allow calls */
1196 errsig(ctx->prscxerr, ERR_BADSPECEXPR);
1197
1198 default:
1199 /* others are okay */
1200 break;
1201 }
1202 }
1203 else if (expr->prsnv.prsnvt.toktyp == TOKTDSTRING)
1204 {
1205 /* don't allow double-quoted strings in speculative evaluation */
1206 errsig(ctx->prscxerr, ERR_BADSPECEXPR);
1207 }
1208 }
1209 else
1210 {
1211 switch(expr->prsntyp)
1212 {
1213 case TOKTINC:
1214 case TOKTDEC:
1215 case TOKTPOSTINC:
1216 case TOKTPOSTDEC:
1217 case TOKTNEW:
1218 case TOKTDELETE:
1219 case TOKTLPAR:
1220 case TOKTASSIGN:
1221 case TOKTPLEQ:
1222 case TOKTMINEQ:
1223 case TOKTDIVEQ:
1224 case TOKTMODEQ:
1225 case TOKTTIMEQ:
1226 case TOKTBANDEQ:
1227 case TOKTBOREQ:
1228 case TOKTXOREQ:
1229 case TOKTSHLEQ:
1230 case TOKTSHREQ:
1231 case TOKTRPAR:
1232 /* assignments and function calls are all prohibited */
1233 errsig(ctx->prscxerr, ERR_BADSPECEXPR);
1234
1235 case TOKTDOT:
1236 /* property lookup - only allow no argument version */
1237 if (expr->prsnnlf == 3)
1238 errsig(ctx->prscxerr, ERR_BADSPECEXPR);
1239
1240 /* replace with data-only (no method call) version */
1241 expr->prsntyp = TOKTDOTSPEC;
1242 break;
1243
1244 default:
1245 /* other expressions are acceptable */
1246 break;
1247 }
1248
1249 /* go through all subnodes of this node */
1250 for (i = 0 ; i < expr->prsnnlf ; ++i)
1251 prs_check_spec(ctx, expr->prsnv.prsnvn[i]);
1252 }
1253 }
1254
1255 /*
1256 * Parse an expression and generate code for it using speculative
1257 * evaluation rules
1258 */
1259 void prsxgen_spec(prscxdef *ctx)
1260 {
1261 prsndef *exprnode;
1262
1263 /* parse the expression */
1264 exprnode = prsexpr(ctx);
1265
1266 /* check for speculative execution problems */
1267 prs_check_spec(ctx, exprnode);
1268
1269 /* generate the expression, then reset the parse node pool */
1270 prsgexp(ctx, exprnode);
1271 prsrstn(ctx);
1272 }
1273
1274
1275 /*
1276 * Parse an expression and generate code, checking the expression for a
1277 * possibly incorrect assigment if in C mode.
1278 */
1279 void prsxgen_pia(prscxdef *ctx)
1280 {
1281 prsndef *exprnode;
1282
1283 /* parse the expression */
1284 exprnode = prsexpr(ctx);
1285
1286 /* check for a possibly incorrect assignment if in C mode */
1287 if ((ctx->prscxtok->tokcxflg & TOKCXFCMODE)
1288 && exprnode && exprnode->prsntyp == TOKTASSIGN)
1289 errlog(ctx->prscxerr, ERR_PIA);
1290
1291 /* generate the expression, and reset the parse node pool */
1292 prsgexp(ctx, exprnode);
1293 prsrstn(ctx);
1294 }
1295
1296
1297
1298 /* opcodes for general binary operators */
1299 uchar prsbopl[] =
1300 {
1301 OPCADD, OPCSUB, OPCDIV, OPCMUL, 0, OPCEQ, OPCNE, OPCGT, OPCGE,
1302 OPCLT, OPCLE, OPCMOD, OPCBAND, OPCBOR, OPCXOR, OPCSHL, OPCSHR
1303 };
1304
1305 /* descend an expression tree and generate code for the expression */
1306 void prsgexp(prscxdef *ctx, prsndef *n)
1307 {
1308 int cnt;
1309
1310 switch(n->prsnnlf)
1311 {
1312 case 0:
1313 prsdef(ctx, &n->prsnv.prsnvt, TOKSTFWDOBJ);
1314 emtval(ctx->prscxemt, &n->prsnv.prsnvt, ctx->prscxpool);
1315 break;
1316
1317 case 1:
1318 switch(n->prsntyp)
1319 {
1320 case TOKTINC:
1321 case TOKTDEC:
1322 case TOKTPOSTINC:
1323 case TOKTPOSTDEC:
1324 prsglval(ctx, n->prsntyp, n->prsnv.prsnvn[0]);
1325 break;
1326
1327 case TOKTPLUS:
1328 case TOKTMINUS:
1329 case TOKTNOT:
1330 case TOKTTILDE:
1331 case TOKTNEW:
1332 case TOKTDELETE:
1333 prsgexp(ctx, n->prsnv.prsnvn[0]);
1334 if (n->prsntyp != TOKTPLUS)
1335 emtop(ctx->prscxemt,
1336 n->prsntyp == TOKTMINUS ? OPCNEG :
1337 n->prsntyp == TOKTTILDE ? OPCBNOT :
1338 n->prsntyp == TOKTNEW ? OPCNEW :
1339 n->prsntyp == TOKTDELETE ? OPCDELETE :
1340 OPCNOT);
1341 break;
1342
1343 case TOKTLPAR:
1344 prsgfn(ctx, n->prsnv.prsnvn[0], 0);
1345 break;
1346
1347 default:
1348 errsig(ctx->prscxerr, ERR_REQUNO);
1349 }
1350 break;
1351
1352 case 2:
1353 switch(n->prsntyp)
1354 {
1355 case TOKTAND:
1356 case TOKTOR:
1357 {
1358 uint lab;
1359
1360 lab = emtglbl(ctx->prscxemt);
1361 prsgexp(ctx, n->prsnv.prsnvn[0]);
1362 emtjmp(ctx->prscxemt,
1363 (uchar)(n->prsntyp == TOKTAND ? OPCJSF : OPCJST),
1364 lab);
1365 prsgexp(ctx, n->prsnv.prsnvn[1]);
1366 emtslbl(ctx->prscxemt, &lab, TRUE);
1367 break;
1368 }
1369
1370 case TOKTPLUS:
1371 case TOKTMINUS:
1372 case TOKTDIV:
1373 case TOKTMOD:
1374 case TOKTTIMES:
1375 case TOKTEQ:
1376 case TOKTNE:
1377 case TOKTGT:
1378 case TOKTGE:
1379 case TOKTLT:
1380 case TOKTLE:
1381 case TOKTBAND:
1382 case TOKTBOR:
1383 case TOKTXOR:
1384 case TOKTSHL:
1385 case TOKTSHR:
1386 prsgexp(ctx, n->prsnv.prsnvn[0]);
1387 prsgexp(ctx, n->prsnv.prsnvn[1]);
1388 emtop(ctx->prscxemt, prsbopl[n->prsntyp - TOKTPLUS]);
1389 break;
1390
1391 case TOKTCOMMA:
1392 prsgexp(ctx, n->prsnv.prsnvn[0]);
1393 prsgexp(ctx, n->prsnv.prsnvn[1]);
1394 break;
1395
1396 case TOKTLPAR: /* function call with arguments */
1397 cnt = prsgargs(ctx, n->prsnv.prsnvn[1]); /* push arguments */
1398 prsgfn(ctx, n->prsnv.prsnvn[0], cnt);
1399 break;
1400
1401 case TOKTASSIGN:
1402 case TOKTPLEQ:
1403 case TOKTMINEQ:
1404 case TOKTDIVEQ:
1405 case TOKTMODEQ:
1406 case TOKTTIMEQ:
1407 case TOKTBANDEQ:
1408 case TOKTBOREQ:
1409 case TOKTXOREQ:
1410 case TOKTSHLEQ:
1411 case TOKTSHREQ:
1412 prsgexp(ctx, n->prsnv.prsnvn[1]); /* eval right side of := */
1413 prsglval(ctx, n->prsntyp, n->prsnv.prsnvn[0]);
1414 break;
1415
1416 case TOKTDOT:
1417 prsgdot(ctx, n, 0);
1418 break;
1419
1420 case TOKTDOTSPEC:
1421 prsgdotspec(ctx, n);
1422 break;
1423
1424 case TOKTLBRACK:
1425 prsgexp(ctx, n->prsnv.prsnvn[0]); /* push list being indexed */
1426 prsgexp(ctx, n->prsnv.prsnvn[1]); /* push the index value */
1427 emtop(ctx->prscxemt, OPCINDEX);
1428 break;
1429
1430 case TOKTRBRACK:
1431 {
1432 prsndef *nsub;
1433 uint cnt;
1434
1435 for (cnt = 0, nsub = n ; nsub
1436 ; ++cnt, nsub = nsub->prsnv.prsnvn[1])
1437 prsgexp(ctx, nsub->prsnv.prsnvn[0]);
1438 emtop(ctx->prscxemt, OPCCONS); /* construction operator */
1439 emtint2(ctx->prscxemt, cnt);
1440 break;
1441 }
1442
1443 default:
1444 errsig(ctx->prscxerr, ERR_INVBIN);
1445 }
1446 break;
1447
1448 case 3:
1449 switch(n->prsntyp)
1450 {
1451 case TOKTQUESTION:
1452 {
1453 uint lfalse; /* label for FALSE branch */
1454 uint ldone; /* label for end of condition */
1455
1456 prsgexp(ctx, n->prsnv.prsnvn[0]); /* evaluate condition */
1457 lfalse = emtglbl(ctx->prscxemt);
1458 emtjmp(ctx->prscxemt, OPCJF, lfalse);
1459 prsgexp(ctx, n->prsnv.prsnvn[1]); /* eval TRUE expr */
1460 ldone = emtglbl(ctx->prscxemt);
1461 emtjmp(ctx->prscxemt, OPCJMP, ldone);
1462 emtslbl(ctx->prscxemt, &lfalse, TRUE);
1463 prsgexp(ctx, n->prsnv.prsnvn[2]); /* eval FALSE branch */
1464 emtslbl(ctx->prscxemt, &ldone, TRUE);
1465 break;
1466 }
1467 case TOKTDOT:
1468 cnt = prsgargs(ctx, n->prsnv.prsnvn[2]);
1469 prsgdot(ctx, n, cnt);
1470 break;
1471 }
1472 break;
1473 }
1474 }
1475
1476 /* generate code for initializer */
1477 void prsgini(prscxdef *ctx, prsndef *node, uint curfr)
1478 {
1479 int lcl;
1480
1481 /* descend tree - always generate in order specified by user */
1482 if (node->prsnv.prsnvn[2])
1483 prsgini(ctx, node->prsnv.prsnvn[2], curfr);
1484
1485 /* generate OPCLINE if one was specified */
1486 if (node->prsnnlf == 4)
1487 {
1488 uchar *p;
1489 uint oldofs;
1490
1491 /* tell the line source about the OPCLINE instruction */
1492 dbgclin(ctx->prscxtok, ctx->prscxemt->emtcxobj,
1493 (uint)ctx->prscxemt->emtcxofs);
1494
1495 /* now actually emit the OPCLINE instruction */
1496 oldofs = ctx->prscxemt->emtcxofs;
1497 p = ctx->prscxpool + node->prsnv.prsnvn[3]->prsnv.prsnvt.tokofs;
1498 emtmem(ctx->prscxemt, p, (uint)(*(p + 1) + 1));
1499
1500 /* fix it up with the correct frame */
1501 emtint2at(ctx->prscxemt, curfr, oldofs + 2);
1502 }
1503
1504 prsgexp(ctx, node->prsnv.prsnvn[0]);
1505 emtop(ctx->prscxemt, OPCSETLCL);
1506 lcl = node->prsnv.prsnvn[1]->prsnv.prsnvt.tokval;
1507 emtint2(ctx->prscxemt, lcl);
1508 }
1509
1510 /* generate code for a function all */
1511 static void prsgfn(prscxdef *ctx, prsndef *n, int cnt)
1512 {
1513 if (n->prsnnlf == 0 && n->prsnv.prsnvt.toktyp == TOKTSYMBOL
1514 && (n->prsnv.prsnvt.toksym.tokstyp == TOKSTUNK
1515 || n->prsnv.prsnvt.toksym.tokstyp != TOKSTLOCAL))
1516 {
1517 tokdef *t = &n->prsnv.prsnvt;
1518
1519 /* if symbol is undefined, add to symbol table as a new function */
1520 prsdef(ctx, t, TOKSTFWDFN);
1521
1522 /* non-local-var symbol: interpret as a function */
1523 switch(t->toksym.tokstyp)
1524 {
1525 case TOKSTFUNC:
1526 case TOKSTFWDFN:
1527 emtop(ctx->prscxemt, OPCCALL);
1528 break;
1529 case TOKSTEXTERN:
1530 emtop(ctx->prscxemt, OPCCALLEXT);
1531 break;
1532 case TOKSTBIFN:
1533 emtop(ctx->prscxemt, OPCBUILTIN);
1534 break;
1535 default:
1536 errlog(ctx->prscxerr, ERR_REQFCN);
1537 }
1538 emtbyte(ctx->prscxemt, cnt); /* emit argument count byte */
1539 emtint2(ctx->prscxemt, t->toksym.toksval);
1540 }
1541 else
1542 {
1543 /* expression - evaluate, and assume it yields a function pointer */
1544 prsgexp(ctx, n);
1545 emtop(ctx->prscxemt, OPCPTRCALL);
1546 emtbyte(ctx->prscxemt, cnt); /* argument count byte */
1547 }
1548 }
1549
1550 /* generate code for a property lookup */
1551 static void prsgdot(prscxdef *ctx, prsndef *n, int cnt)
1552 {
1553 prsndef *n0 = n->prsnv.prsnvn[0];
1554 prsndef *n1 = n->prsnv.prsnvn[1];
1555 int inh; /* flag: "inherited.prop" */
1556 int slf; /* flag: "self.prop" */
1557 int objflg; /* flag: LHS of dot is constant object value */
1558 int exp_inh; /* flag: "inherited superclass.prop" */
1559 uchar reg_op; /* operator to use for regular property */
1560 uchar ptr_op; /* operator to use for property pointer */
1561 objnum exp_inh_obj; /* explicit superclass object to inherit from */
1562
1563 /* presume we'll have a regular object expression for the LHS */
1564 inh = exp_inh = slf = objflg = FALSE;
1565
1566 /* set up the default opcodes for ordinary object expressions */
1567 reg_op = OPCGETP;
1568 ptr_op = OPCPTRGETP;
1569
1570 /* check for special left-hand-side values */
1571 if (n0->prsnnlf == 0 && n0->prsnv.prsnvt.toktyp == TOKTSYMBOL)
1572 {
1573 switch(n0->prsnv.prsnvt.toksym.tokstyp)
1574 {
1575 case TOKSTINHERIT:
1576 inh = TRUE;
1577 reg_op = OPCINHERIT;
1578 ptr_op = OPCPTRINH;
1579 break;
1580
1581 case TOKSTSELF:
1582 if (ctx->prscxflg & PRSCXFFUNC) errlog(ctx->prscxerr, ERR_NOSELF);
1583 slf = TRUE;
1584 reg_op = OPCGETPSELF;
1585 ptr_op = OPCGETPPTRSELF;
1586 break;
1587
1588 case TOKSTOBJ:
1589 case TOKSTFWDOBJ:
1590 objflg = TRUE;
1591 reg_op = OPCGETPOBJ;
1592 break;
1593 }
1594 }
1595 else if (n0->prsnnlf == 1 && n0->prsntyp == TOKTEXPINH)
1596 {
1597 tokdef *sc_tok;
1598
1599 /*
1600 * explicit superclass inheritance - this is inheritance, but
1601 * with an explicit superclass value
1602 */
1603 inh = TRUE;
1604 exp_inh = TRUE;
1605 reg_op = OPCEXPINH;
1606 ptr_op = OPCEXPINHPTR;
1607
1608 /* get the explicit superclass subnode */
1609 sc_tok = &n0->prsnv.prsnvn[0]->prsnv.prsnvt;
1610
1611 /* make sure the superclass is an object */
1612 prsdef(ctx, sc_tok, TOKSTFWDOBJ);
1613
1614 /* get the explicit superclass - it's the subnode */
1615 exp_inh_obj = sc_tok->toksym.toksval;
1616 }
1617
1618 /*
1619 * push object whose property we're getting unless inheriting or
1620 * getting a property of 'self' or of a fixed object (in which cases
1621 * the object is implicit in the opcode, rather than being on the
1622 * stack)
1623 */
1624 if (!inh && !slf && !objflg)
1625 prsgexp(ctx, n0);
1626
1627 if (n1->prsnnlf == 0
1628 && n1->prsnv.prsnvt.toktyp == TOKTPOUND)
1629 {
1630 /* RHS is a property symbol - do a simple GETP (or INHERIT) */
1631 prpnum prop = n1->prsnv.prsnvt.tokofs;
1632
1633 /*
1634 * emit the opcode and argument count - use the regular
1635 * (non-pointer) opcode, since we have a regular property value
1636 */
1637 emtop(ctx->prscxemt, reg_op);
1638 emtbyte(ctx->prscxemt, cnt);
1639
1640 /* if we are encoding the object in the instruction, emit it now */
1641 if (objflg)
1642 emtint2(ctx->prscxemt, n0->prsnv.prsnvt.toksym.toksval);
1643
1644 /* add the property value */
1645 emtint2(ctx->prscxemt, prop);
1646
1647 /*
1648 * if this is an "inherited" with an explicit superclass, add
1649 * the explicit superclass value
1650 */
1651 if (exp_inh)
1652 emtint2(ctx->prscxemt, exp_inh_obj);
1653 }
1654 else
1655 {
1656 /*
1657 * evaluate the LHS unconditionally, even if it's just a fixed
1658 * object, since we have no special instruction that encodes an
1659 * object value in a pointer property evaluation
1660 */
1661 if (objflg)
1662 prsgexp(ctx, n0);
1663
1664 /* RHS is an expression - evaluate it */
1665 prsgexp(ctx, n1);
1666
1667 /*
1668 * emit the opcode and the byte count - use the pointer opcode,
1669 * since we have a property pointer
1670 */
1671 emtop(ctx->prscxemt, ptr_op);
1672 emtbyte(ctx->prscxemt, cnt);
1673
1674 /*
1675 * if this is an "inherited" with an explicit superclass, add
1676 * the explicit superclass value
1677 */
1678 if (exp_inh)
1679 emtint2(ctx->prscxemt, exp_inh_obj);
1680 }
1681 }
1682
1683 /* generate code for a property lookup during speculative evaluation */
1684 static void prsgdotspec(prscxdef *ctx, prsndef *n)
1685 {
1686 prsndef *n0 = n->prsnv.prsnvn[0];
1687 prsndef *n1 = n->prsnv.prsnvn[1];
1688
1689 /* push object whose property we're getting */
1690 prsgexp(ctx, n0);
1691
1692 if (n1->prsnnlf == 0
1693 && n1->prsnv.prsnvt.toktyp == TOKTPOUND)
1694 {
1695 /* RHS is a property symbol - do a simple GETP (or INHERIT) */
1696 prpnum prop = n1->prsnv.prsnvt.tokofs;
1697
1698 /* push the data property */
1699 emtop(ctx->prscxemt, OPCGETPDATA);
1700 emtint2(ctx->prscxemt, prop);
1701 }
1702 else
1703 {
1704 /* RHS is an expression - evaluate it */
1705 prsgexp(ctx, n1);
1706
1707 /* get the data property */
1708 emtop(ctx->prscxemt, OPCPTRGETPDATA);
1709 }
1710 }
1711
1712 /* token -> assignment type field conversions */
1713 static uchar prsglcvt[] =
1714 {
1715 OPCASIINC | OPCASIPRE, /* pre ++ */
1716 OPCASIINC | OPCASIPOST, /* post ++ */
1717 OPCASIDEC | OPCASIPRE, /* pre -- */
1718 OPCASIDEC | OPCASIPOST, /* post ++ */
1719 OPCASIADD, /* += */
1720 OPCASISUB, /* -= */
1721 OPCASIDIV, /* /= */
1722 OPCASIMUL, /* *= */
1723 OPCASIDIR /* := */
1724 };
1725
1726 /* extended assignment type field conversions */
1727 static uchar prsglcvt_ext[] =
1728 {
1729 OPCASIMOD, /* %= */
1730 OPCASIBAND, /* &= */
1731 OPCASIBOR, /* |= */
1732 OPCASIXOR, /* ^= */
1733 OPCASISHL, /* <<= */
1734 OPCASISHR /* >>= */
1735 };
1736
1737 /* generate code for an l-value */
1738 static void prsglval(prscxdef *ctx, int typ, prsndef *n)
1739 {
1740 uchar op;
1741 uchar op2;
1742
1743 if (typ - TOKTINC < sizeof(prsglcvt)/sizeof(prsglcvt[0]))
1744 {
1745 op = OPCASI_MASK | prsglcvt[typ - TOKTINC]; /* form basic op code */
1746 op2 = 0;
1747 }
1748 else
1749 {
1750 op = OPCASI_MASK | OPCASIEXT;
1751 op2 = prsglcvt_ext[typ - TOKTMODEQ];
1752 }
1753
1754 switch(n->prsnnlf)
1755 {
1756 case 0:
1757 {
1758 int lclnum;
1759 int styp;
1760
1761 /* DON'T assume this is a 'self.' property if unknown */
1762 /* DON'T prsdef(ctx, &n->prsnv.prsnvt, TOKSTPROP); */
1763
1764 if (n->prsnv.prsnvt.toktyp == TOKTSYMBOL)
1765 {
1766 if (ctx->prscxflg & PRSCXFWTCH)
1767 errsig(ctx->prscxerr, ERR_WTCHLCL);
1768
1769 if ((styp = n->prsnv.prsnvt.toksym.tokstyp) == TOKSTLOCAL)
1770 {
1771 emtop(ctx->prscxemt, op | OPCASILCL);
1772 if (op2) emtop(ctx->prscxemt, op2);
1773 lclnum = n->prsnv.prsnvt.toksym.toksval;
1774 emtint2(ctx->prscxemt, lclnum);
1775 }
1776 else if (styp == TOKSTPROP)
1777 {
1778 if (ctx->prscxflg & PRSCXFFUNC)
1779 errlog(ctx->prscxerr, ERR_NOSELF);
1780
1781 emtop(ctx->prscxemt, OPCPUSHSELF);
1782 emtop(ctx->prscxemt, op | OPCASIPRP);
1783 if (op2) emtop(ctx->prscxemt, op2);
1784 lclnum = n->prsnv.prsnvt.toksym.toksval;
1785 emtint2(ctx->prscxemt, lclnum);
1786 }
1787 else if (styp == TOKSTPROPSPEC)
1788 errsig(ctx->prscxerr, ERR_BADSPECEXPR);
1789 else
1790 errlog(ctx->prscxerr, ERR_INVASI);
1791 }
1792 else
1793 errlog(ctx->prscxerr, ERR_INVASI);
1794 }
1795 break;
1796
1797 case 2:
1798 switch(n->prsntyp)
1799 {
1800 case TOKTDOT:
1801 {
1802 prsndef *n1 = n->prsnv.prsnvn[1];
1803 int base_opc;
1804 int direct_prop;
1805 prpnum prop;
1806
1807 /* generate the expression providing the object */
1808 prsgexp(ctx, n->prsnv.prsnvn[0]);
1809
1810 /* determine the correct base opcode */
1811 if (n1->prsnnlf == 0 && n1->prsnv.prsnvt.toktyp == TOKTPOUND)
1812 {
1813
1814 /* simple property assignment */
1815 direct_prop = TRUE;
1816 base_opc = OPCASIPRP;
1817
1818 /* remember the property id */
1819 prop = n1->prsnv.prsnvt.tokofs;
1820 }
1821 else
1822 {
1823 /* assignment through property pointer */
1824 direct_prop = FALSE;
1825 base_opc = OPCASIPRPPTR;
1826
1827 /* generate the property pointer expression */
1828 prsgexp(ctx, n1);
1829 }
1830
1831 /* generate the appropriate property assignment opcode */
1832 emtop(ctx->prscxemt, op | base_opc);
1833 if (op2) emtop(ctx->prscxemt, op2);
1834
1835 /* if it's a simple assignment, add the property code */
1836 if (direct_prop) emtint2(ctx->prscxemt, prop);
1837
1838 break;
1839 }
1840
1841 case TOKTLBRACK:
1842 prsgexp(ctx, n->prsnv.prsnvn[0]); /* evaluate list */
1843 prsgexp(ctx, n->prsnv.prsnvn[1]); /* evaluate index */
1844 emtop(ctx->prscxemt, op | OPCASIIND);
1845 if (op2) emtop(ctx->prscxemt, op2);
1846 prsglval(ctx, TOKTASSIGN, n->prsnv.prsnvn[0]); /* set list */
1847 break;
1848
1849 default:
1850 errlog(ctx->prscxerr, ERR_INVASI);
1851 }
1852 break;
1853
1854 default:
1855 errlog(ctx->prscxerr, ERR_INVASI);
1856 }
1857 }
1858
1859 /* generate code for an argument list; returns argument count */
1860 static int prsgargs(prscxdef *ctx, prsndef *n)
1861 {
1862 int cnt = 0;
1863
1864 if (n->prsnnlf == 2 && n->prsntyp == TOKTRPAR)
1865 {
1866 cnt = prsgargs(ctx, n->prsnv.prsnvn[1]);
1867 prsgexp(ctx, n->prsnv.prsnvn[0]);
1868 }
1869 else
1870 prsgexp(ctx, n);
1871
1872 return(cnt + 1);
1873 }
1874