Hex Artifact Content
Not logged in

Artifact 271574033e4f3a712a43c84fe4a956aec2573449:


0000: 2f 2a 2a 0d 0a 20 2a 20 41 75 74 68 6f 72 73 3a  /**.. * Authors:
0010: 20 6b 2e 69 6e 61 62 61 0d 0a 20 2a 20 4c 69 63   k.inaba.. * Lic
0020: 65 6e 73 65 3a 20 4e 59 53 4c 20 30 2e 39 39 38  ense: NYSL 0.998
0030: 32 20 68 74 74 70 3a 2f 2f 77 77 77 2e 6b 6d 6f  2 http://www.kmo
0040: 6e 6f 73 2e 6e 65 74 2f 6e 79 73 6c 2f 0d 0a 20  nos.net/nysl/.. 
0050: 2a 0d 0a 20 2a 20 45 76 61 6c 75 61 74 6f 72 20  *.. * Evaluator 
0060: 66 6f 72 20 50 6f 6c 65 6d 79 20 70 72 6f 67 72  for Polemy progr
0070: 61 6d 6d 69 6e 67 20 6c 61 6e 67 75 61 67 65 2e  amming language.
0080: 0d 0a 20 2a 2f 0d 0a 6d 6f 64 75 6c 65 20 70 6f  .. */..module po
0090: 6c 65 6d 79 2e 65 76 61 6c 3b 0d 0a 69 6d 70 6f  lemy.eval;..impo
00a0: 72 74 20 70 6f 6c 65 6d 79 2e 5f 63 6f 6d 6d 6f  rt polemy._commo
00b0: 6e 3b 0d 0a 69 6d 70 6f 72 74 20 70 6f 6c 65 6d  n;..import polem
00c0: 79 2e 66 61 69 6c 75 72 65 3b 0d 0a 69 6d 70 6f  y.failure;..impo
00d0: 72 74 20 70 6f 6c 65 6d 79 2e 61 73 74 3b 0d 0a  rt polemy.ast;..
00e0: 69 6d 70 6f 72 74 20 70 6f 6c 65 6d 79 2e 70 61  import polemy.pa
00f0: 72 73 65 3b 0d 0a 69 6d 70 6f 72 74 20 70 6f 6c  rse;..import pol
0100: 65 6d 79 2e 76 61 6c 75 65 3b 0d 0a 69 6d 70 6f  emy.value;..impo
0110: 72 74 20 70 6f 6c 65 6d 79 2e 6c 61 79 65 72 3b  rt polemy.layer;
0120: 0d 0a 69 6d 70 6f 72 74 20 70 6f 6c 65 6d 79 2e  ..import polemy.
0130: 76 61 6c 75 65 3b 0d 0a 69 6d 70 6f 72 74 20 70  value;..import p
0140: 6f 6c 65 6d 79 2e 76 61 6c 75 65 63 6f 6e 76 3b  olemy.valueconv;
0150: 0d 0a 69 6d 70 6f 72 74 20 73 74 64 2e 73 69 67  ..import std.sig
0160: 6e 61 6c 73 3b 0d 0a 0d 0a 2f 2f 2f 20 4f 62 6a  nals;..../// Obj
0170: 65 63 74 73 20 66 6f 72 20 6d 61 69 74 61 69 6e  ects for maitain
0180: 69 6e 67 20 67 6c 6f 62 61 6c 20 65 6e 76 69 72  ing global envir
0190: 6f 6e 6d 65 6e 74 20 61 6e 64 20 65 76 61 6c 75  onment and evalu
01a0: 61 74 69 6f 6e 20 6f 66 20 65 78 70 72 65 73 73  ation of express
01b0: 69 6f 6e 20 6f 6e 20 69 74 0d 0a 63 6c 61 73 73  ion on it..class
01c0: 20 45 76 61 6c 75 61 74 6f 72 0d 0a 7b 0d 0a 70   Evaluator..{..p
01d0: 75 62 6c 69 63 3a 0d 0a 09 2f 2f 2f 20 45 76 61  ublic:.../// Eva
01e0: 6c 75 61 74 65 20 74 68 65 20 41 53 54 0d 0a 09  luate the AST...
01f0: 56 61 6c 75 65 20 65 76 61 6c 41 53 54 28 41 53  Value evalAST(AS
0200: 54 20 65 29 0d 0a 09 09 7b 20 72 65 74 75 72 6e  T e)....{ return
0210: 20 67 65 74 4c 61 79 65 72 45 76 61 6c 28 56 61   getLayerEval(Va
0220: 6c 75 65 4c 61 79 65 72 29 2e 6d 61 63 72 6f 41  lueLayer).macroA
0230: 6e 64 45 76 61 6c 28 65 2c 20 74 68 65 43 6f 6e  ndEval(e, theCon
0240: 74 65 78 74 2c 20 4c 61 79 65 72 45 76 61 6c 2e  text, LayerEval.
0250: 4f 76 65 72 77 72 69 74 65 43 74 78 29 3b 20 7d  OverwriteCtx); }
0260: 0d 0a 0d 0a 09 2f 2f 2f 20 45 76 61 6c 75 61 74  ...../// Evaluat
0270: 65 20 74 68 65 20 73 74 72 69 6e 67 0d 0a 09 56  e the string...V
0280: 61 6c 75 65 20 65 76 61 6c 53 74 72 69 6e 67 28  alue evalString(
0290: 53 2c 54 2e 2e 2e 29 28 53 20 73 74 72 2c 20 54  S,T...)(S str, T
02a0: 20 66 6e 5f 6c 6e 5f 63 6e 29 0d 0a 09 09 7b 20   fn_ln_cn)....{ 
02b0: 72 65 74 75 72 6e 20 65 76 61 6c 41 53 54 28 70  return evalAST(p
02c0: 61 72 73 65 53 74 72 69 6e 67 28 73 74 72 2c 66  arseString(str,f
02d0: 6e 5f 6c 6e 5f 63 6e 29 29 3b 20 7d 0d 0a 0d 0a  n_ln_cn)); }....
02e0: 09 2f 2f 2f 20 45 76 61 6c 75 61 74 65 20 74 68  ./// Evaluate th
02f0: 65 20 66 69 6c 65 0d 0a 09 56 61 6c 75 65 20 65  e file...Value e
0300: 76 61 6c 46 69 6c 65 28 53 2c 54 2e 2e 2e 29 28  valFile(S,T...)(
0310: 53 20 66 69 6c 65 6e 61 6d 65 2c 20 54 20 6c 6e  S filename, T ln
0320: 5f 63 6e 29 0d 0a 09 09 7b 20 72 65 74 75 72 6e  _cn)....{ return
0330: 20 65 76 61 6c 41 53 54 28 70 61 72 73 65 46 69   evalAST(parseFi
0340: 6c 65 28 66 69 6c 65 6e 61 6d 65 2c 6c 6e 5f 63  le(filename,ln_c
0350: 6e 29 29 3b 20 7d 0d 0a 0d 0a 09 2f 2f 2f 20 47  n)); }...../// G
0360: 65 74 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f  et the global co
0370: 6e 74 65 78 74 0d 0a 09 54 61 62 6c 65 20 67 6c  ntext...Table gl
0380: 6f 62 61 6c 43 6f 6e 74 65 78 74 28 29 0d 0a 09  obalContext()...
0390: 09 7b 20 72 65 74 75 72 6e 20 74 68 65 43 6f 6e  .{ return theCon
03a0: 74 65 78 74 3b 20 7d 0d 0a 0d 0a 09 2f 2f 2f 20  text; }...../// 
03b0: 49 6e 69 74 69 61 6c 69 7a 65 20 65 76 61 6c 75  Initialize evalu
03c0: 61 74 6f 72 20 77 69 74 68 20 65 6d 70 74 79 20  ator with empty 
03d0: 63 6f 6e 74 65 78 74 0d 0a 09 74 68 69 73 28 29  context...this()
03e0: 0d 0a 09 7b 0d 0a 09 09 74 68 65 43 6f 6e 74 65  ...{....theConte
03f0: 78 74 20 3d 20 6e 65 77 20 54 61 62 6c 65 3b 0d  xt = new Table;.
0400: 0a 09 09 74 68 65 4c 61 79 65 72 73 5b 56 61 6c  ...theLayers[Val
0410: 75 65 4c 61 79 65 72 5d 20 20 20 20 3d 20 6e 65  ueLayer]    = ne
0420: 77 20 56 61 6c 75 65 4c 61 79 65 72 45 76 61 6c  w ValueLayerEval
0430: 3b 0d 0a 09 09 74 68 65 4c 61 79 65 72 73 5b 4d  ;....theLayers[M
0440: 61 63 72 6f 4c 61 79 65 72 5d 20 20 20 20 3d 20  acroLayer]    = 
0450: 6e 65 77 20 4d 61 63 72 6f 4c 61 79 65 72 45 76  new MacroLayerEv
0460: 61 6c 3b 0d 0a 09 09 74 68 65 4c 61 79 65 72 73  al;....theLayers
0470: 5b 52 61 77 4d 61 63 72 6f 4c 61 79 65 72 5d 20  [RawMacroLayer] 
0480: 3d 20 6e 65 77 20 52 61 77 4d 61 63 72 6f 4c 61  = new RawMacroLa
0490: 79 65 72 45 76 61 6c 3b 0d 0a 09 7d 0d 0a 0d 0a  yerEval;...}....
04a0: 70 72 69 76 61 74 65 3a 0d 0a 09 54 61 62 6c 65  private:...Table
04b0: 20 20 20 20 20 20 20 20 20 20 20 20 74 68 65 43              theC
04c0: 6f 6e 74 65 78 74 3b 0d 0a 09 4c 61 79 65 72 45  ontext;...LayerE
04d0: 76 61 6c 5b 4c 61 79 65 72 5d 20 74 68 65 4c 61  val[Layer] theLa
04e0: 79 65 72 73 3b 0d 0a 0d 0a 09 2f 2f 2f 20 47 65  yers;...../// Ge
04f0: 74 20 74 68 65 20 6c 61 79 65 72 20 65 76 61 6c  t the layer eval
0500: 75 61 74 6f 72 20 66 72 6f 6d 20 6c 61 79 65 72  uator from layer
0510: 20 49 44 0d 0a 09 4c 61 79 65 72 45 76 61 6c 20   ID...LayerEval 
0520: 67 65 74 4c 61 79 65 72 45 76 61 6c 28 4c 61 79  getLayerEval(Lay
0530: 65 72 20 6c 61 79 29 0d 0a 09 7b 0d 0a 09 09 69  er lay)...{....i
0540: 66 28 61 75 74 6f 20 70 20 3d 20 6c 61 79 20 69  f(auto p = lay i
0550: 6e 20 74 68 65 4c 61 79 65 72 73 29 0d 0a 09 09  n theLayers)....
0560: 09 72 65 74 75 72 6e 20 2a 70 3b 0d 0a 09 09 72  .return *p;....r
0570: 65 74 75 72 6e 20 74 68 65 4c 61 79 65 72 73 5b  eturn theLayers[
0580: 6c 61 79 5d 20 3d 20 6e 65 77 20 55 73 65 72 44  lay] = new UserD
0590: 65 66 69 6e 65 64 4c 61 79 65 72 45 76 61 6c 28  efinedLayerEval(
05a0: 6c 61 79 29 3b 0d 0a 09 7d 0d 0a 0d 0a 09 2f 2f  lay);...}.....//
05b0: 2f 20 49 6e 74 65 72 66 61 63 65 20 6f 66 20 6c  / Interface of l
05c0: 61 79 65 72 73 0d 0a 09 61 62 73 74 72 61 63 74  ayers...abstract
05d0: 20 63 6c 61 73 73 20 4c 61 79 65 72 45 76 61 6c   class LayerEval
05e0: 0d 0a 09 7b 0d 0a 09 09 65 6e 75 6d 20 3a 20 62  ...{....enum : b
05f0: 6f 6f 6c 20 7b 20 43 61 73 63 61 64 65 43 74 78  ool { CascadeCtx
0600: 3d 66 61 6c 73 65 2c 20 4f 76 65 72 77 72 69 74  =false, Overwrit
0610: 65 43 74 78 3d 74 72 75 65 20 7d 3b 0d 0a 0d 0a  eCtx=true };....
0620: 09 09 2f 2f 2f 20 43 6f 6e 63 72 65 74 65 20 6c  ../// Concrete l
0630: 61 79 65 72 73 20 73 68 6f 75 6c 64 20 69 6d 70  ayers should imp
0640: 6c 65 6d 65 6e 74 20 74 68 65 73 65 0d 0a 09 09  lement these....
0650: 70 72 6f 74 65 63 74 65 64 20 4c 61 79 65 72 20  protected Layer 
0660: 6c 61 79 65 72 49 44 28 29 3b 0d 0a 09 09 70 72  layerID();....pr
0670: 6f 74 65 63 74 65 64 20 56 61 6c 75 65 20 65 76  otected Value ev
0680: 61 6c 5f 28 20 44 69 65 20 65 2c 20 54 61 62 6c  al_( Die e, Tabl
0690: 65 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d  e ctx, bool ctxM
06a0: 6f 64 20 29 3b 2f 2f 2f 0d 0a 09 09 70 72 6f 74  od );///....prot
06b0: 65 63 74 65 64 20 56 61 6c 75 65 20 65 76 61 6c  ected Value eval
06c0: 5f 28 20 53 74 72 20 65 2c 20 54 61 62 6c 65 20  _( Str e, Table 
06d0: 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64  ctx, bool ctxMod
06e0: 20 29 3b 2f 2f 2f 0d 0a 09 09 70 72 6f 74 65 63   );///....protec
06f0: 74 65 64 20 56 61 6c 75 65 20 65 76 61 6c 5f 28  ted Value eval_(
0700: 20 49 6e 74 20 65 2c 20 54 61 62 6c 65 20 63 74   Int e, Table ct
0710: 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29  x, bool ctxMod )
0720: 3b 2f 2f 2f 0d 0a 09 09 70 72 6f 74 65 63 74 65  ;///....protecte
0730: 64 20 56 61 6c 75 65 20 65 76 61 6c 5f 28 20 56  d Value eval_( V
0740: 61 72 20 65 2c 20 54 61 62 6c 65 20 63 74 78 2c  ar e, Table ctx,
0750: 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29 3b 2f   bool ctxMod );/
0760: 2f 2f 0d 0a 09 09 70 72 6f 74 65 63 74 65 64 20  //....protected 
0770: 56 61 6c 75 65 20 65 76 61 6c 5f 28 20 4c 61 79  Value eval_( Lay
0780: 20 65 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 62   e, Table ctx, b
0790: 6f 6f 6c 20 63 74 78 4d 6f 64 20 29 3b 2f 2f 2f  ool ctxMod );///
07a0: 0d 0a 09 09 70 72 6f 74 65 63 74 65 64 20 56 61  ....protected Va
07b0: 6c 75 65 20 65 76 61 6c 5f 28 20 4c 65 74 20 65  lue eval_( Let e
07c0: 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f  , Table ctx, boo
07d0: 6c 20 63 74 78 4d 6f 64 20 29 3b 2f 2f 2f 0d 0a  l ctxMod );///..
07e0: 09 09 70 72 6f 74 65 63 74 65 64 20 56 61 6c 75  ..protected Valu
07f0: 65 20 65 76 61 6c 5f 28 20 41 70 70 20 65 2c 20  e eval_( App e, 
0800: 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c 20  Table ctx, bool 
0810: 63 74 78 4d 6f 64 20 29 3b 2f 2f 2f 0d 0a 09 09  ctxMod );///....
0820: 70 72 6f 74 65 63 74 65 64 20 56 61 6c 75 65 20  protected Value 
0830: 65 76 61 6c 5f 28 20 46 75 6e 20 65 2c 20 54 61  eval_( Fun e, Ta
0840: 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74  ble ctx, bool ct
0850: 78 4d 6f 64 20 29 3b 2f 2f 2f 0d 0a 0d 0a 09 09  xMod );///......
0860: 2f 2f 2f 20 53 68 6f 75 6c 64 20 6f 76 65 72 72  /// Should overr
0870: 69 64 65 20 74 68 69 73 20 61 6c 73 6f 2c 20 69  ide this also, i
0880: 66 20 74 68 65 20 6c 61 79 65 72 20 6d 61 79 20  f the layer may 
0890: 72 65 74 75 72 6e 20 6e 75 6c 6c 20 66 6f 72 20  return null for 
08a0: 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 0d 0a 09 09  optimization....
08b0: 56 61 6c 75 65 20 65 76 61 6c 54 6f 4e 6f 6e 4e  Value evalToNonN
08c0: 75 6c 6c 28 20 41 53 54 20 65 2c 20 54 61 62 6c  ull( AST e, Tabl
08d0: 65 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d  e ctx, bool ctxM
08e0: 6f 64 20 3d 20 43 61 73 63 61 64 65 43 74 78 20  od = CascadeCtx 
08f0: 29 0d 0a 09 09 09 7b 20 72 65 74 75 72 6e 20 65  ).....{ return e
0900: 76 61 6c 28 65 2c 63 74 78 2c 63 74 78 4d 6f 64  val(e,ctx,ctxMod
0910: 29 3b 20 7d 0d 0a 0d 0a 09 09 2f 2f 2f 20 44 6f  ); }....../// Do
0920: 20 6d 61 63 72 6f 20 65 78 70 61 6e 73 69 6f 6e   macro expansion
0930: 20 66 69 72 73 74 2c 20 61 6e 64 20 74 68 65 6e   first, and then
0940: 20 65 76 61 6c 2e 20 53 68 6f 75 6c 64 20 6f 76   eval. Should ov
0950: 65 72 72 69 64 65 20 74 68 69 73 20 61 6c 73 6f  erride this also
0960: 20 69 66 20 69 74 20 69 73 20 4e 4f 54 20 6d 61   if it is NOT ma
0970: 63 72 6f 20 6c 61 79 65 72 2e 0d 0a 09 09 56 61  cro layer.....Va
0980: 6c 75 65 20 6d 61 63 72 6f 41 6e 64 45 76 61 6c  lue macroAndEval
0990: 28 20 41 53 54 20 65 2c 20 54 61 62 6c 65 20 63  ( AST e, Table c
09a0: 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20  tx, bool ctxMod 
09b0: 3d 20 43 61 73 63 61 64 65 43 74 78 20 29 0d 0a  = CascadeCtx )..
09c0: 09 09 09 7b 20 72 65 74 75 72 6e 20 65 76 61 6c  ...{ return eval
09d0: 54 6f 4e 6f 6e 4e 75 6c 6c 28 65 2c 63 74 78 2c  ToNonNull(e,ctx,
09e0: 63 74 78 4d 6f 64 29 3b 20 7d 0d 0a 0d 0a 09 09  ctxMod); }......
09f0: 2f 2f 2f 20 64 79 6e 61 6d 69 63 2d 6f 76 65 72  /// dynamic-over
0a00: 6c 6f 61 64 2d 72 65 73 6f 6c 75 74 69 6f 6e 0d  load-resolution.
0a10: 0a 09 09 56 61 6c 75 65 20 65 76 61 6c 28 20 41  ...Value eval( A
0a20: 53 54 20 65 2c 20 54 61 62 6c 65 20 63 74 78 2c  ST e, Table ctx,
0a30: 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 3d 20 43   bool ctxMod = C
0a40: 61 73 63 61 64 65 43 74 78 20 29 0d 0a 09 09 7b  ascadeCtx )....{
0a50: 0d 0a 09 09 09 65 6e 75 6d 20 66 75 6e 4e 61 6d  .....enum funNam
0a60: 65 20 3d 20 22 65 76 61 6c 5f 22 3b 20 20 20 20  e = "eval_";    
0a70: 20 20 20 20 20 20 20 20 20 20 20 20 20 2f 2f 20               // 
0a80: 6d 6f 64 69 66 79 20 68 65 72 65 20 66 6f 72 20  modify here for 
0a90: 63 75 73 74 6f 6d 69 7a 61 74 69 6f 6e 0d 0a 09  customization...
0aa0: 09 09 61 6c 69 61 73 20 54 79 70 65 54 75 70 6c  ..alias TypeTupl
0ab0: 65 21 28 65 2c 63 74 78 2c 63 74 78 4d 6f 64 29  e!(e,ctx,ctxMod)
0ac0: 20 70 61 72 61 6d 73 3b 20 20 2f 2f 20 6d 6f 64   params;  // mod
0ad0: 69 66 79 20 68 65 72 65 20 66 6f 72 20 63 75 73  ify here for cus
0ae0: 74 6f 6d 69 7a 61 74 69 6f 6e 0d 0a 0d 0a 09 09  tomization......
0af0: 09 61 6c 69 61 73 20 74 79 70 65 6f 66 28 5f 5f  .alias typeof(__
0b00: 74 72 61 69 74 73 28 67 65 74 4f 76 65 72 6c 6f  traits(getOverlo
0b10: 61 64 73 2c 20 74 68 69 73 2c 20 66 75 6e 4e 61  ads, this, funNa
0b20: 6d 65 29 29 20 6f 76 54 79 70 65 73 3b 0d 0a 09  me)) ovTypes;...
0b30: 09 09 61 6c 69 61 73 20 73 74 61 74 69 63 4d 61  ..alias staticMa
0b40: 70 21 28 66 69 72 73 74 50 61 72 61 6d 2c 20 6f  p!(firstParam, o
0b50: 76 54 79 70 65 73 29 20 20 20 20 20 20 20 20 20  vTypes)         
0b60: 20 20 20 20 20 66 73 74 54 79 70 65 73 3b 0d 0a       fstTypes;..
0b70: 09 09 09 61 6c 69 61 73 20 44 65 72 69 76 65 64  ...alias Derived
0b80: 54 6f 46 72 6f 6e 74 21 28 66 73 74 54 79 70 65  ToFront!(fstType
0b90: 73 29 20 20 20 20 20 20 20 20 20 20 20 20 20 66  s)             f
0ba0: 73 74 54 79 70 65 73 5f 73 6f 72 74 65 64 3b 0d  stTypes_sorted;.
0bb0: 0a 09 09 09 66 6f 72 65 61 63 68 28 69 2c 20 54  ....foreach(i, T
0bc0: 3b 20 66 73 74 54 79 70 65 73 5f 73 6f 72 74 65  ; fstTypes_sorte
0bd0: 64 29 0d 0a 09 09 09 09 73 74 61 74 69 63 20 69  d)......static i
0be0: 66 28 20 69 73 28 54 20 3d 3d 20 74 79 70 65 6f  f( is(T == typeo
0bf0: 66 28 70 61 72 61 6d 73 5b 30 5d 29 29 20 29 20  f(params[0])) ) 
0c00: 7b 7d 20 65 6c 73 65 20 69 66 28 20 61 75 74 6f  {} else if( auto
0c10: 20 5f 78 20 3d 20 63 61 73 74 28 54 29 70 61 72   _x = cast(T)par
0c20: 61 6d 73 5b 30 5d 20 29 0d 0a 09 09 09 09 09 72  ams[0] ).......r
0c30: 65 74 75 72 6e 20 5f 5f 74 72 61 69 74 73 28 67  eturn __traits(g
0c40: 65 74 4f 76 65 72 6c 6f 61 64 73 2c 20 74 68 69  etOverloads, thi
0c50: 73 2c 20 66 75 6e 4e 61 6d 65 29 5b 69 5d 28 5f  s, funName)[i](_
0c60: 78 2c 20 70 61 72 61 6d 73 5b 31 2e 2e 24 5d 29  x, params[1..$])
0c70: 3b 0d 0a 0d 0a 09 09 09 2f 2f 20 6d 6f 64 69 66  ;.......// modif
0c80: 79 20 68 65 72 65 20 74 6f 20 63 75 73 74 6f 6d  y here to custom
0c90: 69 7a 65 20 74 68 65 20 64 65 66 61 75 6c 74 20  ize the default 
0ca0: 62 65 68 61 76 69 6f 72 0d 0a 09 09 09 61 73 73  behavior.....ass
0cb0: 65 72 74 28 66 61 6c 73 65 2c 20 74 65 78 74 28  ert(false, text(
0cc0: 22 65 76 61 6c 28 29 20 66 6f 72 20 22 2c 74 79  "eval() for ",ty
0cd0: 70 65 69 64 28 65 29 2c 22 20 5b 22 2c 65 2e 70  peid(e)," [",e.p
0ce0: 6f 73 2c 22 5d 20 69 73 20 6e 6f 74 20 64 65 66  os,"] is not def
0cf0: 69 6e 65 64 22 29 29 3b 0d 0a 09 09 7d 0d 0a 0d  ined"));....}...
0d00: 0a 09 09 2f 2f 2f 20 46 75 6e 63 74 69 6f 6e 20  .../// Function 
0d10: 63 61 6c 6c 69 6e 67 20 63 6f 6e 76 65 6e 74 69  calling conventi
0d20: 6f 6e 2e 0d 0a 09 09 2f 2f 2f 20 52 75 6e 20 61  on...../// Run a
0d30: 6c 6c 20 61 72 75 67 6d 65 6e 74 20 41 53 54 20  ll arugment AST 
0d40: 69 6e 20 74 68 65 20 61 70 70 72 6f 70 72 69 61  in the appropria
0d50: 74 65 20 6c 61 79 65 72 20 61 6e 64 20 69 6e 76  te layer and inv
0d60: 6f 6b 65 20 74 68 65 20 66 75 6e 63 74 69 6f 6e  oke the function
0d70: 2e 0d 0a 09 09 56 61 6c 75 65 20 69 6e 76 6f 6b  .....Value invok
0d80: 65 46 75 6e 63 74 69 6f 6e 28 56 61 6c 75 65 20  eFunction(Value 
0d90: 66 5f 2c 20 41 53 54 5b 5d 20 61 72 67 73 2c 20  f_, AST[] args, 
0da0: 54 61 62 6c 65 20 63 74 78 2c 20 4c 65 78 50 6f  Table ctx, LexPo
0db0: 73 69 74 69 6f 6e 20 70 6f 73 2c 20 73 74 72 69  sition pos, stri
0dc0: 6e 67 20 63 61 6c 6c 73 74 61 63 6b 6d 73 67 29  ng callstackmsg)
0dd0: 0d 0a 09 09 7b 0d 0a 09 09 09 46 75 6e 56 61 6c  ....{.....FunVal
0de0: 75 65 20 66 20 3d 20 63 61 73 74 28 46 75 6e 56  ue f = cast(FunV
0df0: 61 6c 75 65 29 66 5f 3b 0d 0a 09 09 09 69 66 28  alue)f_;.....if(
0e00: 20 66 20 69 73 20 6e 75 6c 6c 20 29 0d 0a 09 09   f is null )....
0e10: 09 09 74 68 72 6f 77 20 67 65 6e 65 78 21 52 75  ..throw genex!Ru
0e20: 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e 28 70  ntimeException(p
0e30: 6f 73 2c 20 74 65 78 74 28 22 74 72 69 65 64 20  os, text("tried 
0e40: 74 6f 20 63 61 6c 6c 20 6e 6f 6e 2d 66 75 6e 63  to call non-func
0e50: 74 69 6f 6e 3a 20 22 2c 66 29 29 3b 0d 0a 09 09  tion: ",f));....
0e60: 09 69 66 28 20 66 2e 70 61 72 61 6d 73 28 29 2e  .if( f.params().
0e70: 6c 65 6e 67 74 68 20 21 3d 20 61 72 67 73 2e 6c  length != args.l
0e80: 65 6e 67 74 68 20 29 0d 0a 09 09 09 09 74 68 72  ength )......thr
0e90: 6f 77 20 67 65 6e 65 78 21 52 75 6e 74 69 6d 65  ow genex!Runtime
0ea0: 45 78 63 65 70 74 69 6f 6e 28 70 6f 73 2c 0d 0a  Exception(pos,..
0eb0: 09 09 09 09 09 73 70 72 69 6e 74 66 21 28 22 25  .....sprintf!("%
0ec0: 64 20 61 72 67 75 6d 65 6e 74 73 20 72 65 71 75  d arguments requ
0ed0: 69 72 65 64 20 62 75 74 20 25 64 20 70 61 73 73  ired but %d pass
0ee0: 65 64 22 29 28 66 2e 70 61 72 61 6d 73 28 29 2e  ed")(f.params().
0ef0: 6c 65 6e 67 74 68 2c 20 61 72 67 73 2e 6c 65 6e  length, args.len
0f00: 67 74 68 29 29 3b 0d 0a 0d 0a 09 09 09 54 61 62  gth));.......Tab
0f10: 6c 65 20 6e 65 77 43 74 78 20 3d 20 6e 65 77 20  le newCtx = new 
0f20: 54 61 62 6c 65 28 66 2e 64 65 66 69 6e 69 74 69  Table(f.definiti
0f30: 6f 6e 43 6f 6e 74 65 78 74 28 29 2c 20 54 61 62  onContext(), Tab
0f40: 6c 65 2e 4b 69 6e 64 2e 4e 6f 74 50 72 6f 70 61  le.Kind.NotPropa
0f50: 67 61 74 65 53 65 74 29 3b 0d 0a 09 09 09 66 6f  gateSet);.....fo
0f60: 72 65 61 63 68 28 69 2c 70 3b 20 66 2e 70 61 72  reach(i,p; f.par
0f70: 61 6d 73 28 29 29 0d 0a 09 09 09 09 69 66 28 20  ams())......if( 
0f80: 70 2e 6c 61 79 65 72 73 2e 65 6d 70 74 79 20 29  p.layers.empty )
0f90: 0d 0a 09 09 09 09 09 6e 65 77 43 74 78 2e 73 65  .......newCtx.se
0fa0: 74 28 70 2e 6e 61 6d 65 2c 20 6c 61 79 65 72 49  t(p.name, layerI
0fb0: 44 28 29 2c 20 74 68 69 73 2e 65 76 61 6c 54 6f  D(), this.evalTo
0fc0: 4e 6f 6e 4e 75 6c 6c 28 61 72 67 73 5b 69 5d 2c  NonNull(args[i],
0fd0: 20 63 74 78 29 29 3b 0d 0a 09 09 09 09 65 6c 73   ctx));......els
0fe0: 65 0d 0a 09 09 09 09 09 66 6f 72 65 61 63 68 28  e.......foreach(
0ff0: 61 72 67 4c 61 79 3b 20 70 2e 6c 61 79 65 72 73  argLay; p.layers
1000: 29 0d 0a 09 09 09 09 09 7b 0d 0a 09 09 09 09 09  ).......{.......
1010: 09 4c 61 79 65 72 20 6c 6c 20 3d 20 61 72 67 4c  .Layer ll = argL
1020: 61 79 3b 0d 0a 09 09 09 09 09 09 69 66 28 20 69  ay;........if( i
1030: 73 4d 61 63 72 6f 4c 61 79 65 72 28 61 72 67 4c  sMacroLayer(argL
1040: 61 79 29 20 26 26 20 74 79 70 65 69 64 28 74 68  ay) && typeid(th
1050: 69 73 29 21 3d 74 79 70 65 69 64 28 4d 61 63 72  is)!=typeid(Macr
1060: 6f 4c 61 79 65 72 45 76 61 6c 29 20 29 0d 0a 09  oLayerEval) )...
1070: 09 09 09 09 09 09 6c 6c 20 3d 20 52 61 77 4d 61  ......ll = RawMa
1080: 63 72 6f 4c 61 79 65 72 3b 20 2f 2f 20 65 78 70  croLayer; // exp
1090: 6c 69 63 69 74 20 40 6d 61 63 72 6f 20 69 6e 76  licit @macro inv
10a0: 6f 6b 65 73 20 28 72 61 77 6d 61 63 72 6f 29 0d  okes (rawmacro).
10b0: 0a 09 09 09 09 09 09 6e 65 77 43 74 78 2e 73 65  .......newCtx.se
10c0: 74 28 70 2e 6e 61 6d 65 2c 20 61 72 67 4c 61 79  t(p.name, argLay
10d0: 2c 20 67 65 74 4c 61 79 65 72 45 76 61 6c 28 6c  , getLayerEval(l
10e0: 6c 29 2e 65 76 61 6c 54 6f 4e 6f 6e 4e 75 6c 6c  l).evalToNonNull
10f0: 28 61 72 67 73 5b 69 5d 2c 20 63 74 78 29 29 3b  (args[i], ctx));
1100: 0d 0a 09 09 09 09 09 7d 0d 0a 09 09 09 73 63 6f  .......}.....sco
1110: 70 65 20 5f 20 3d 20 6e 65 77 20 50 75 73 68 43  pe _ = new PushC
1120: 61 6c 6c 53 74 61 63 6b 28 70 6f 73 2c 20 63 61  allStack(pos, ca
1130: 6c 6c 73 74 61 63 6b 6d 73 67 29 3b 0d 0a 09 09  llstackmsg);....
1140: 09 72 65 74 75 72 6e 20 66 2e 69 6e 76 6f 6b 65  .return f.invoke
1150: 28 6c 61 79 65 72 49 44 28 29 2c 20 6e 65 77 43  (layerID(), newC
1160: 74 78 2c 20 70 6f 73 29 3b 0d 0a 09 09 7d 0d 0a  tx, pos);....}..
1170: 0d 0a 09 09 2f 2f 2f 20 4c 69 66 74 20 74 68 65  ..../// Lift the
1180: 20 76 61 6c 75 65 20 76 20 74 6f 20 74 68 65 20   value v to the 
1190: 63 75 72 72 65 6e 74 20 6c 61 79 65 72 0d 0a 09  current layer...
11a0: 09 56 61 6c 75 65 20 6c 69 66 74 28 56 61 6c 75  .Value lift(Valu
11b0: 65 20 76 2c 20 54 61 62 6c 65 20 63 74 78 2c 20  e v, Table ctx, 
11c0: 4c 65 78 50 6f 73 69 74 69 6f 6e 20 70 6f 73 29  LexPosition pos)
11d0: 0d 0a 09 09 7b 0d 0a 09 09 09 2f 2f 20 66 75 6e  ....{.....// fun
11e0: 63 74 69 6f 6e 73 20 61 72 65 20 61 75 74 6f 6d  ctions are autom
11f0: 61 74 69 63 61 6c 6c 79 20 6c 69 66 74 65 64 20  atically lifted 
1200: 62 79 20 69 74 73 65 6c 66 0d 0a 09 09 09 69 66  by itself.....if
1210: 28 20 63 61 73 74 28 46 75 6e 56 61 6c 75 65 29  ( cast(FunValue)
1220: 20 76 20 29 0d 0a 09 09 09 09 72 65 74 75 72 6e   v )......return
1230: 20 76 3b 0d 0a 0d 0a 09 09 09 4c 61 79 65 72 20   v;.......Layer 
1240: 6c 61 79 20 3d 20 6c 61 79 65 72 49 44 28 29 3b  lay = layerID();
1250: 0d 0a 0d 0a 09 09 09 2f 2f 20 6c 6f 61 64 20 74  .......// load t
1260: 68 65 20 6c 69 66 74 20 66 75 6e 63 74 69 6f 6e  he lift function
1270: 0d 0a 09 09 09 69 66 28 20 21 63 74 78 2e 68 61  .....if( !ctx.ha
1280: 73 28 6c 61 79 2c 20 4c 69 66 74 4c 61 79 65 72  s(lay, LiftLayer
1290: 29 20 29 0d 0a 09 09 09 09 74 68 72 6f 77 20 67  ) )......throw g
12a0: 65 6e 65 78 21 52 75 6e 74 69 6d 65 45 78 63 65  enex!RuntimeExce
12b0: 70 74 69 6f 6e 28 70 6f 73 2c 20 22 6c 69 66 74  ption(pos, "lift
12c0: 20 66 75 6e 63 74 69 6f 6e 20 66 6f 72 20 22 7e   function for "~
12d0: 6c 61 79 7e 22 20 69 73 20 6e 6f 74 20 72 65 67  lay~" is not reg
12e0: 69 73 74 65 72 65 64 22 20 29 3b 0d 0a 0d 0a 09  istered" );.....
12f0: 09 09 56 61 6c 75 65 20 20 20 20 66 5f 20 3d 20  ..Value    f_ = 
1300: 63 74 78 2e 67 65 74 28 6c 61 79 2c 20 4c 69 66  ctx.get(lay, Lif
1310: 74 4c 61 79 65 72 2c 20 70 6f 73 29 3b 0d 0a 09  tLayer, pos);...
1320: 09 09 46 75 6e 56 61 6c 75 65 20 66 20 20 3d 20  ..FunValue f  = 
1330: 63 61 73 74 28 46 75 6e 56 61 6c 75 65 29 20 66  cast(FunValue) f
1340: 5f 3b 0d 0a 09 09 09 69 66 28 20 66 20 69 73 20  _;.....if( f is 
1350: 6e 75 6c 6c 20 29 0d 0a 09 09 09 09 74 68 72 6f  null )......thro
1360: 77 20 67 65 6e 65 78 21 52 75 6e 74 69 6d 65 45  w genex!RuntimeE
1370: 78 63 65 70 74 69 6f 6e 28 70 6f 73 2c 0d 0a 09  xception(pos,...
1380: 09 09 09 09 74 65 78 74 28 22 6e 6f 6e 2d 66 75  ....text("non-fu
1390: 6e 63 74 69 6f 6e 20 22 2c 20 66 5f 2c 20 22 20  nction ", f_, " 
13a0: 69 73 20 72 65 67 69 73 74 65 72 65 64 20 61 73  is registered as
13b0: 20 74 68 65 20 6c 69 66 74 20 66 75 6e 63 74 69   the lift functi
13c0: 6f 6e 20 66 6f 72 20 22 2c 20 6c 61 79 29 29 3b  on for ", lay));
13d0: 0d 0a 09 09 09 69 66 28 20 66 2e 70 61 72 61 6d  .....if( f.param
13e0: 73 28 29 2e 6c 65 6e 67 74 68 20 21 3d 20 31 20  s().length != 1 
13f0: 7c 7c 20 66 2e 70 61 72 61 6d 73 28 29 5b 30 5d  || f.params()[0]
1400: 2e 6c 61 79 65 72 73 2e 6c 65 6e 67 74 68 3e 3d  .layers.length>=
1410: 31 20 29 0d 0a 09 09 09 09 74 68 72 6f 77 20 67  1 )......throw g
1420: 65 6e 65 78 21 52 75 6e 74 69 6d 65 45 78 63 65  enex!RuntimeExce
1430: 70 74 69 6f 6e 28 70 6f 73 2c 0d 0a 09 09 09 09  ption(pos,......
1440: 09 74 65 78 74 28 22 6c 69 66 74 20 66 75 6e 63  .text("lift func
1450: 74 69 6f 6e 20 6d 75 73 74 20 74 61 6b 65 20 65  tion must take e
1460: 78 61 63 74 6c 79 20 31 20 6e 65 75 74 72 61 6c  xactly 1 neutral
1470: 20 70 61 72 61 6d 65 74 65 72 3a 22 2c 6c 61 79   parameter:",lay
1480: 29 29 3b 0d 0a 0d 0a 09 09 09 2f 2f 20 69 6e 76  ));.......// inv
1490: 6f 6b 65 46 75 6e 63 74 69 6f 6e 0d 0a 09 09 09  okeFunction.....
14a0: 54 61 62 6c 65 20 6e 65 77 43 74 78 20 3d 20 6e  Table newCtx = n
14b0: 65 77 20 54 61 62 6c 65 28 66 2e 64 65 66 69 6e  ew Table(f.defin
14c0: 69 74 69 6f 6e 43 6f 6e 74 65 78 74 28 29 2c 20  itionContext(), 
14d0: 54 61 62 6c 65 2e 4b 69 6e 64 2e 4e 6f 74 50 72  Table.Kind.NotPr
14e0: 6f 70 61 67 61 74 65 53 65 74 29 3b 0d 0a 09 09  opagateSet);....
14f0: 09 6e 65 77 43 74 78 2e 73 65 74 28 66 2e 70 61  .newCtx.set(f.pa
1500: 72 61 6d 73 28 29 5b 30 5d 2e 6e 61 6d 65 2c 20  rams()[0].name, 
1510: 56 61 6c 75 65 4c 61 79 65 72 2c 20 76 29 3b 0d  ValueLayer, v);.
1520: 0a 09 09 09 73 63 6f 70 65 20 5f 20 3d 20 6e 65  ....scope _ = ne
1530: 77 20 50 75 73 68 43 61 6c 6c 53 74 61 63 6b 28  w PushCallStack(
1540: 70 6f 73 2c 20 22 6c 69 66 74 20 74 6f 20 22 7e  pos, "lift to "~
1550: 6c 61 79 29 3b 0d 0a 09 09 09 72 65 74 75 72 6e  lay);.....return
1560: 20 66 2e 69 6e 76 6f 6b 65 28 56 61 6c 75 65 4c   f.invoke(ValueL
1570: 61 79 65 72 2c 20 6e 65 77 43 74 78 2c 20 70 6f  ayer, newCtx, po
1580: 73 29 3b 0d 0a 09 09 7d 0d 0a 09 7d 0d 0a 0d 0a  s);....}...}....
1590: 09 2f 2f 2f 20 45 76 61 6c 75 61 74 6f 72 20 66  ./// Evaluator f
15a0: 6f 72 20 73 74 61 6e 64 61 72 64 20 40 76 61 6c  or standard @val
15b0: 75 65 20 73 65 6d 61 6e 74 69 63 73 0d 0a 09 63  ue semantics...c
15c0: 6c 61 73 73 20 56 61 6c 75 65 4c 61 79 65 72 45  lass ValueLayerE
15d0: 76 61 6c 20 3a 20 4c 61 79 65 72 45 76 61 6c 0d  val : LayerEval.
15e0: 0a 09 7b 0d 0a 09 09 6f 76 65 72 72 69 64 65 20  ..{....override 
15f0: 4c 61 79 65 72 20 6c 61 79 65 72 49 44 28 29 0d  Layer layerID().
1600: 0a 09 09 7b 0d 0a 09 09 09 72 65 74 75 72 6e 20  ...{.....return 
1610: 56 61 6c 75 65 4c 61 79 65 72 3b 0d 0a 09 09 7d  ValueLayer;....}
1620: 0d 0a 09 09 6f 76 65 72 72 69 64 65 20 56 61 6c  ....override Val
1630: 75 65 20 65 76 61 6c 5f 28 20 44 69 65 20 65 2c  ue eval_( Die e,
1640: 20 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c   Table ctx, bool
1650: 20 63 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d 0a   ctxMod )....{..
1660: 09 09 09 74 68 72 6f 77 20 67 65 6e 65 78 21 52  ...throw genex!R
1670: 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e 28  untimeException(
1680: 65 2e 70 6f 73 2c 20 22 75 6e 64 65 66 69 6e 65  e.pos, "undefine
1690: 64 20 63 61 73 65 22 29 3b 0d 0a 09 09 7d 0d 0a  d case");....}..
16a0: 09 09 6f 76 65 72 72 69 64 65 20 56 61 6c 75 65  ..override Value
16b0: 20 65 76 61 6c 5f 28 20 53 74 72 20 65 2c 20 54   eval_( Str e, T
16c0: 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c 20 63  able ctx, bool c
16d0: 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d 0a 09 09  txMod )....{....
16e0: 09 72 65 74 75 72 6e 20 6e 65 77 20 53 74 72 56  .return new StrV
16f0: 61 6c 75 65 28 65 2e 64 61 74 61 29 3b 0d 0a 09  alue(e.data);...
1700: 09 7d 0d 0a 09 09 6f 76 65 72 72 69 64 65 20 56  .}....override V
1710: 61 6c 75 65 20 65 76 61 6c 5f 28 20 49 6e 74 20  alue eval_( Int 
1720: 65 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 62 6f  e, Table ctx, bo
1730: 6f 6c 20 63 74 78 4d 6f 64 20 29 0d 0a 09 09 7b  ol ctxMod )....{
1740: 0d 0a 09 09 09 72 65 74 75 72 6e 20 6e 65 77 20  .....return new 
1750: 49 6e 74 56 61 6c 75 65 28 65 2e 64 61 74 61 29  IntValue(e.data)
1760: 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65 72 72 69  ;....}....overri
1770: 64 65 20 56 61 6c 75 65 20 65 76 61 6c 5f 28 20  de Value eval_( 
1780: 56 61 72 20 65 2c 20 54 61 62 6c 65 20 63 74 78  Var e, Table ctx
1790: 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29 0d  , bool ctxMod ).
17a0: 0a 09 09 7b 0d 0a 09 09 09 72 65 74 75 72 6e 20  ...{.....return 
17b0: 63 74 78 2e 67 65 74 28 65 2e 6e 61 6d 65 2c 20  ctx.get(e.name, 
17c0: 6c 61 79 65 72 49 44 28 29 2c 20 65 2e 70 6f 73  layerID(), e.pos
17d0: 29 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65 72 72  );....}....overr
17e0: 69 64 65 20 56 61 6c 75 65 20 65 76 61 6c 5f 28  ide Value eval_(
17f0: 20 4c 61 79 20 65 2c 20 54 61 62 6c 65 20 63 74   Lay e, Table ct
1800: 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29  x, bool ctxMod )
1810: 0d 0a 09 09 7b 0d 0a 09 09 09 72 65 74 75 72 6e  ....{.....return
1820: 20 67 65 74 4c 61 79 65 72 45 76 61 6c 28 65 2e   getLayerEval(e.
1830: 6c 61 79 65 72 29 2e 65 76 61 6c 54 6f 4e 6f 6e  layer).evalToNon
1840: 4e 75 6c 6c 28 65 2e 65 78 70 72 2c 20 63 74 78  Null(e.expr, ctx
1850: 29 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65 72 72  );....}....overr
1860: 69 64 65 20 56 61 6c 75 65 20 65 76 61 6c 5f 28  ide Value eval_(
1870: 20 4c 65 74 20 65 2c 20 54 61 62 6c 65 20 63 74   Let e, Table ct
1880: 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29  x, bool ctxMod )
1890: 0d 0a 09 09 7b 0d 0a 09 09 09 69 66 28 20 21 63  ....{.....if( !c
18a0: 74 78 4d 6f 64 20 29 0d 0a 09 09 09 09 63 74 78  txMod )......ctx
18b0: 20 3d 20 6e 65 77 20 54 61 62 6c 65 28 63 74 78   = new Table(ctx
18c0: 2c 20 54 61 62 6c 65 2e 4b 69 6e 64 2e 4e 6f 74  , Table.Kind.Not
18d0: 50 72 6f 70 61 67 61 74 65 53 65 74 29 3b 0d 0a  PropagateSet);..
18e0: 09 09 09 56 61 6c 75 65 20 72 69 20 3d 20 74 68  ...Value ri = th
18f0: 69 73 2e 65 76 61 6c 28 65 2e 76 64 65 66 2c 20  is.eval(e.vdef, 
1900: 63 74 78 29 3b 0d 0a 09 09 09 63 74 78 2e 73 65  ctx);.....ctx.se
1910: 74 28 65 2e 6e 61 6d 65 2c 20 65 2e 6c 61 79 65  t(e.name, e.laye
1920: 72 2e 65 6d 70 74 79 20 3f 20 6c 61 79 65 72 49  r.empty ? layerI
1930: 44 28 29 3a 20 65 2e 6c 61 79 65 72 2c 20 72 69  D(): e.layer, ri
1940: 29 3b 0d 0a 09 09 09 72 65 74 75 72 6e 20 74 68  );.....return th
1950: 69 73 2e 65 76 61 6c 28 65 2e 65 78 70 72 2c 20  is.eval(e.expr, 
1960: 63 74 78 2c 20 4f 76 65 72 77 72 69 74 65 43 74  ctx, OverwriteCt
1970: 78 29 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65 72  x);....}....over
1980: 72 69 64 65 20 56 61 6c 75 65 20 65 76 61 6c 5f  ride Value eval_
1990: 28 20 41 70 70 20 65 2c 20 54 61 62 6c 65 20 63  ( App e, Table c
19a0: 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20  tx, bool ctxMod 
19b0: 29 0d 0a 09 09 7b 0d 0a 09 09 09 56 61 6c 75 65  )....{.....Value
19c0: 20 66 20 3d 20 74 68 69 73 2e 65 76 61 6c 28 20   f = this.eval( 
19d0: 65 2e 66 75 6e 2c 20 63 74 78 2c 20 43 61 73 63  e.fun, ctx, Casc
19e0: 61 64 65 43 74 78 20 29 3b 0d 0a 09 09 09 72 65  adeCtx );.....re
19f0: 74 75 72 6e 20 74 68 69 73 2e 69 6e 76 6f 6b 65  turn this.invoke
1a00: 46 75 6e 63 74 69 6f 6e 28 66 2c 20 65 2e 61 72  Function(f, e.ar
1a10: 67 73 2c 20 63 74 78 2c 20 65 2e 70 6f 73 2c 20  gs, ctx, e.pos, 
1a20: 67 65 74 4e 61 6d 65 49 66 50 6f 73 73 69 62 6c  getNameIfPossibl
1a30: 65 28 65 2e 66 75 6e 29 29 3b 0d 0a 09 09 7d 0d  e(e.fun));....}.
1a40: 0a 09 09 6f 76 65 72 72 69 64 65 20 56 61 6c 75  ...override Valu
1a50: 65 20 65 76 61 6c 5f 28 20 46 75 6e 20 65 2c 20  e eval_( Fun e, 
1a60: 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c 20  Table ctx, bool 
1a70: 63 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d 0a 09  ctxMod )....{...
1a80: 09 09 72 65 74 75 72 6e 20 63 72 65 61 74 65 4e  ..return createN
1a90: 65 77 46 75 6e 63 74 69 6f 6e 28 65 2c 20 63 74  ewFunction(e, ct
1aa0: 78 29 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65 72  x);....}....over
1ab0: 72 69 64 65 20 56 61 6c 75 65 20 6d 61 63 72 6f  ride Value macro
1ac0: 41 6e 64 45 76 61 6c 28 20 41 53 54 20 65 2c 20  AndEval( AST e, 
1ad0: 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c 20  Table ctx, bool 
1ae0: 63 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d 0a 09  ctxMod )....{...
1af0: 09 09 2f 2f 20 69 6e 63 72 65 6d 65 6e 74 61 6c  ..// incremental
1b00: 20 65 78 65 63 75 74 69 6f 6e 20 6f 66 20 6c 65   execution of le
1b10: 74 2d 65 78 70 72 65 73 73 69 6f 6e 73 0d 0a 09  t-expressions...
1b20: 09 09 69 66 28 61 75 74 6f 20 6c 65 20 3d 20 63  ..if(auto le = c
1b30: 61 73 74 28 4c 65 74 29 65 29 0d 0a 09 09 09 7b  ast(Let)e).....{
1b40: 0d 0a 09 09 09 09 41 53 54 20 61 69 20 3d 20 72  ......AST ai = r
1b50: 75 6e 4d 61 63 72 6f 28 6c 65 2e 76 64 65 66 2c  unMacro(le.vdef,
1b60: 20 63 74 78 29 3b 0d 0a 0d 0a 09 09 09 09 69 66   ctx);........if
1b70: 28 20 21 63 74 78 4d 6f 64 20 29 0d 0a 09 09 09  ( !ctxMod ).....
1b80: 09 09 63 74 78 20 3d 20 6e 65 77 20 54 61 62 6c  ..ctx = new Tabl
1b90: 65 28 63 74 78 2c 20 54 61 62 6c 65 2e 4b 69 6e  e(ctx, Table.Kin
1ba0: 64 2e 4e 6f 74 50 72 6f 70 61 67 61 74 65 53 65  d.NotPropagateSe
1bb0: 74 29 3b 0d 0a 0d 0a 09 09 09 09 56 61 6c 75 65  t);........Value
1bc0: 20 76 69 20 3d 20 74 68 69 73 2e 65 76 61 6c 28   vi = this.eval(
1bd0: 61 69 2c 20 63 74 78 29 3b 0d 0a 09 09 09 09 63  ai, ctx);......c
1be0: 74 78 2e 73 65 74 28 6c 65 2e 6e 61 6d 65 2c 20  tx.set(le.name, 
1bf0: 6c 65 2e 6c 61 79 65 72 2e 65 6d 70 74 79 20 3f  le.layer.empty ?
1c00: 20 6c 61 79 65 72 49 44 28 29 20 3a 20 6c 65 2e   layerID() : le.
1c10: 6c 61 79 65 72 2c 20 76 69 29 3b 0d 0a 09 09 09  layer, vi);.....
1c20: 09 72 65 74 75 72 6e 20 74 68 69 73 2e 6d 61 63  .return this.mac
1c30: 72 6f 41 6e 64 45 76 61 6c 28 6c 65 2e 65 78 70  roAndEval(le.exp
1c40: 72 2c 20 63 74 78 2c 20 4f 76 65 72 77 72 69 74  r, ctx, Overwrit
1c50: 65 43 74 78 29 3b 0d 0a 09 09 09 7d 0d 0a 09 09  eCtx);.....}....
1c60: 09 65 6c 73 65 0d 0a 09 09 09 09 72 65 74 75 72  .else......retur
1c70: 6e 20 74 68 69 73 2e 65 76 61 6c 28 72 75 6e 4d  n this.eval(runM
1c80: 61 63 72 6f 28 65 2c 63 74 78 2c 63 74 78 4d 6f  acro(e,ctx,ctxMo
1c90: 64 29 2c 20 63 74 78 2c 20 63 74 78 4d 6f 64 29  d), ctx, ctxMod)
1ca0: 3b 0d 0a 09 09 7d 0d 0a 09 7d 0d 0a 0d 0a 09 2f  ;....}...}...../
1cb0: 2f 2f 20 45 76 61 6c 75 61 74 6f 72 20 66 6f 72  // Evaluator for
1cc0: 20 75 73 65 72 2d 64 65 66 69 6e 65 64 20 6c 61   user-defined la
1cd0: 79 65 72 0d 0a 09 63 6c 61 73 73 20 55 73 65 72  yer...class User
1ce0: 44 65 66 69 6e 65 64 4c 61 79 65 72 45 76 61 6c  DefinedLayerEval
1cf0: 20 3a 20 56 61 6c 75 65 4c 61 79 65 72 45 76 61   : ValueLayerEva
1d00: 6c 0d 0a 09 7b 0d 0a 09 09 4c 61 79 65 72 20 74  l...{....Layer t
1d10: 68 65 49 44 3b 0d 0a 09 09 6d 69 78 69 6e 20 53  heID;....mixin S
1d20: 69 6d 70 6c 65 43 6f 6e 73 74 72 75 63 74 6f 72  impleConstructor
1d30: 3b 0d 0a 0d 0a 09 09 6f 76 65 72 72 69 64 65 20  ;......override 
1d40: 4c 61 79 65 72 20 6c 61 79 65 72 49 44 28 29 0d  Layer layerID().
1d50: 0a 09 09 7b 0d 0a 09 09 09 72 65 74 75 72 6e 20  ...{.....return 
1d60: 74 68 65 49 44 3b 0d 0a 09 09 7d 0d 0a 09 09 6f  theID;....}....o
1d70: 76 65 72 72 69 64 65 20 56 61 6c 75 65 20 65 76  verride Value ev
1d80: 61 6c 5f 28 20 44 69 65 20 65 2c 20 54 61 62 6c  al_( Die e, Tabl
1d90: 65 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d  e ctx, bool ctxM
1da0: 6f 64 20 29 0d 0a 09 09 7b 0d 0a 09 09 09 72 65  od )....{.....re
1db0: 74 75 72 6e 20 6e 65 77 20 42 6f 74 74 6f 6d 56  turn new BottomV
1dc0: 61 6c 75 65 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76  alue;....}....ov
1dd0: 65 72 72 69 64 65 20 56 61 6c 75 65 20 65 76 61  erride Value eva
1de0: 6c 5f 28 20 53 74 72 20 65 2c 20 54 61 62 6c 65  l_( Str e, Table
1df0: 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f   ctx, bool ctxMo
1e00: 64 20 29 0d 0a 09 09 7b 0d 0a 09 09 09 72 65 74  d )....{.....ret
1e10: 75 72 6e 20 74 68 69 73 2e 6c 69 66 74 28 73 75  urn this.lift(su
1e20: 70 65 72 2e 65 76 61 6c 5f 28 65 2c 63 74 78 2c  per.eval_(e,ctx,
1e30: 63 74 78 4d 6f 64 29 2c 20 63 74 78 2c 20 65 2e  ctxMod), ctx, e.
1e40: 70 6f 73 29 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76  pos);....}....ov
1e50: 65 72 72 69 64 65 20 56 61 6c 75 65 20 65 76 61  erride Value eva
1e60: 6c 5f 28 20 49 6e 74 20 65 2c 20 54 61 62 6c 65  l_( Int e, Table
1e70: 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f   ctx, bool ctxMo
1e80: 64 20 29 0d 0a 09 09 7b 0d 0a 09 09 09 72 65 74  d )....{.....ret
1e90: 75 72 6e 20 74 68 69 73 2e 6c 69 66 74 28 73 75  urn this.lift(su
1ea0: 70 65 72 2e 65 76 61 6c 5f 28 65 2c 63 74 78 2c  per.eval_(e,ctx,
1eb0: 63 74 78 4d 6f 64 29 2c 20 63 74 78 2c 20 65 2e  ctxMod), ctx, e.
1ec0: 70 6f 73 29 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76  pos);....}....ov
1ed0: 65 72 72 69 64 65 20 56 61 6c 75 65 20 65 76 61  erride Value eva
1ee0: 6c 5f 28 20 56 61 72 20 65 2c 20 54 61 62 6c 65  l_( Var e, Table
1ef0: 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f   ctx, bool ctxMo
1f00: 64 20 29 0d 0a 09 09 7b 0d 0a 09 09 09 69 66 28  d )....{.....if(
1f10: 20 63 74 78 2e 68 61 73 28 65 2e 6e 61 6d 65 2c   ctx.has(e.name,
1f20: 20 6c 61 79 65 72 49 44 28 29 29 20 29 0d 0a 09   layerID()) )...
1f30: 09 09 09 72 65 74 75 72 6e 20 63 74 78 2e 67 65  ...return ctx.ge
1f40: 74 28 65 2e 6e 61 6d 65 2c 20 6c 61 79 65 72 49  t(e.name, layerI
1f50: 44 28 29 29 3b 0d 0a 09 09 09 56 61 6c 75 65 20  D());.....Value 
1f60: 76 3b 0d 0a 09 09 09 74 72 79 20 7b 20 76 20 3d  v;.....try { v =
1f70: 20 63 74 78 2e 67 65 74 28 65 2e 6e 61 6d 65 2c   ctx.get(e.name,
1f80: 20 56 61 6c 75 65 4c 61 79 65 72 2c 20 65 2e 70   ValueLayer, e.p
1f90: 6f 73 29 3b 20 7d 0d 0a 09 09 09 63 61 74 63 68  os); }.....catch
1fa0: 20 28 20 52 75 6e 74 69 6d 65 45 78 63 65 70 74   ( RuntimeExcept
1fb0: 69 6f 6e 20 29 20 7b 0d 0a 09 09 09 09 74 68 72  ion ) {......thr
1fc0: 6f 77 20 67 65 6e 65 78 21 52 75 6e 74 69 6d 65  ow genex!Runtime
1fd0: 45 78 63 65 70 74 69 6f 6e 28 65 2e 70 6f 73 2c  Exception(e.pos,
1fe0: 20 65 2e 6e 61 6d 65 7e 22 20 6e 6f 74 20 66 6f   e.name~" not fo
1ff0: 75 6e 64 20 69 6e 20 6c 61 79 65 72 20 22 7e 6c  und in layer "~l
2000: 61 79 65 72 49 44 28 29 7e 22 20 6e 6f 72 20 22  ayerID()~" nor "
2010: 7e 56 61 6c 75 65 4c 61 79 65 72 29 3b 0d 0a 09  ~ValueLayer);...
2020: 09 09 7d 0d 0a 09 09 09 72 65 74 75 72 6e 20 74  ..}.....return t
2030: 68 69 73 2e 6c 69 66 74 28 76 2c 20 63 74 78 2c  his.lift(v, ctx,
2040: 20 65 2e 70 6f 73 29 3b 0d 0a 09 09 7d 0d 0a 09   e.pos);....}...
2050: 7d 0d 0a 0d 0a 09 2f 2f 20 4d 61 63 72 6f 20 6c  }.....// Macro l
2060: 61 79 65 72 2e 20 46 6f 72 20 6f 70 74 69 6d 69  ayer. For optimi
2070: 7a 61 74 69 6f 6e 2c 20 69 66 20 41 53 54 20 64  zation, if AST d
2080: 69 64 6e 27 74 20 63 68 61 6e 67 65 20 69 74 20  idn't change it 
2090: 72 65 74 75 72 6e 73 20 6e 75 6c 6c 0d 0a 09 63  returns null...c
20a0: 6c 61 73 73 20 4d 61 63 72 6f 4c 61 79 65 72 45  lass MacroLayerE
20b0: 76 61 6c 20 3a 20 4c 61 79 65 72 45 76 61 6c 0d  val : LayerEval.
20c0: 0a 09 7b 0d 0a 09 09 6f 76 65 72 72 69 64 65 20  ..{....override 
20d0: 4c 61 79 65 72 20 6c 61 79 65 72 49 44 28 29 0d  Layer layerID().
20e0: 0a 09 09 7b 0d 0a 09 09 09 72 65 74 75 72 6e 20  ...{.....return 
20f0: 4d 61 63 72 6f 4c 61 79 65 72 3b 0d 0a 09 09 7d  MacroLayer;....}
2100: 0d 0a 09 09 6f 76 65 72 72 69 64 65 20 56 61 6c  ....override Val
2110: 75 65 20 65 76 61 6c 54 6f 4e 6f 6e 4e 75 6c 6c  ue evalToNonNull
2120: 28 20 41 53 54 20 65 2c 20 54 61 62 6c 65 20 63  ( AST e, Table c
2130: 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20  tx, bool ctxMod 
2140: 3d 20 43 61 73 63 61 64 65 43 74 78 20 29 0d 0a  = CascadeCtx )..
2150: 09 09 7b 0d 0a 09 09 09 56 61 6c 75 65 20 76 20  ..{.....Value v 
2160: 3d 20 74 68 69 73 2e 65 76 61 6c 28 65 2c 20 63  = this.eval(e, c
2170: 74 78 2c 20 63 74 78 4d 6f 64 29 3b 0d 0a 09 09  tx, ctxMod);....
2180: 09 72 65 74 75 72 6e 20 76 20 69 73 20 6e 75 6c  .return v is nul
2190: 6c 20 3f 20 61 73 74 32 74 61 62 6c 65 28 65 29  l ? ast2table(e)
21a0: 20 3a 20 76 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76   : v;....}....ov
21b0: 65 72 72 69 64 65 20 56 61 6c 75 65 20 65 76 61  erride Value eva
21c0: 6c 5f 28 20 44 69 65 20 65 2c 20 54 61 62 6c 65  l_( Die e, Table
21d0: 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f   ctx, bool ctxMo
21e0: 64 20 29 0d 0a 09 09 7b 0d 0a 09 09 09 72 65 74  d )....{.....ret
21f0: 75 72 6e 20 6e 75 6c 6c 3b 0d 0a 09 09 7d 0d 0a  urn null;....}..
2200: 09 09 6f 76 65 72 72 69 64 65 20 56 61 6c 75 65  ..override Value
2210: 20 65 76 61 6c 5f 28 20 53 74 72 20 65 2c 20 54   eval_( Str e, T
2220: 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c 20 63  able ctx, bool c
2230: 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d 0a 09 09  txMod )....{....
2240: 09 72 65 74 75 72 6e 20 6e 75 6c 6c 3b 0d 0a 09  .return null;...
2250: 09 7d 0d 0a 09 09 6f 76 65 72 72 69 64 65 20 56  .}....override V
2260: 61 6c 75 65 20 65 76 61 6c 5f 28 20 49 6e 74 20  alue eval_( Int 
2270: 65 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 62 6f  e, Table ctx, bo
2280: 6f 6c 20 63 74 78 4d 6f 64 20 29 0d 0a 09 09 7b  ol ctxMod )....{
2290: 0d 0a 09 09 09 72 65 74 75 72 6e 20 6e 75 6c 6c  .....return null
22a0: 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65 72 72 69  ;....}....overri
22b0: 64 65 20 56 61 6c 75 65 20 65 76 61 6c 5f 28 20  de Value eval_( 
22c0: 56 61 72 20 65 2c 20 54 61 62 6c 65 20 63 74 78  Var e, Table ctx
22d0: 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29 0d  , bool ctxMod ).
22e0: 0a 09 09 7b 0d 0a 09 09 09 69 66 28 20 63 74 78  ...{.....if( ctx
22f0: 2e 68 61 73 28 65 2e 6e 61 6d 65 2c 20 6c 61 79  .has(e.name, lay
2300: 65 72 49 44 28 29 29 20 29 0d 0a 09 09 09 09 72  erID()) )......r
2310: 65 74 75 72 6e 20 63 74 78 2e 67 65 74 28 65 2e  eturn ctx.get(e.
2320: 6e 61 6d 65 2c 20 6c 61 79 65 72 49 44 28 29 2c  name, layerID(),
2330: 20 65 2e 70 6f 73 29 3b 0d 0a 09 09 09 72 65 74   e.pos);.....ret
2340: 75 72 6e 20 6e 75 6c 6c 3b 0d 0a 09 09 7d 0d 0a  urn null;....}..
2350: 09 09 6f 76 65 72 72 69 64 65 20 56 61 6c 75 65  ..override Value
2360: 20 65 76 61 6c 5f 28 20 4c 61 79 20 65 2c 20 54   eval_( Lay e, T
2370: 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c 20 63  able ctx, bool c
2380: 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d 0a 09 09  txMod )....{....
2390: 09 72 65 74 75 72 6e 20 67 65 74 4c 61 79 65 72  .return getLayer
23a0: 45 76 61 6c 28 65 2e 6c 61 79 65 72 29 2e 65 76  Eval(e.layer).ev
23b0: 61 6c 28 65 2e 65 78 70 72 2c 63 74 78 29 3b 0d  al(e.expr,ctx);.
23c0: 0a 09 09 7d 0d 0a 09 09 6f 76 65 72 72 69 64 65  ...}....override
23d0: 20 56 61 6c 75 65 20 65 76 61 6c 5f 28 20 4c 65   Value eval_( Le
23e0: 74 20 65 2c 20 54 61 62 6c 65 20 63 74 78 2c 20  t e, Table ctx, 
23f0: 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29 0d 0a 09  bool ctxMod )...
2400: 09 7b 0d 0a 09 09 09 69 66 28 20 21 63 74 78 4d  .{.....if( !ctxM
2410: 6f 64 20 29 0d 0a 09 09 09 09 63 74 78 20 3d 20  od )......ctx = 
2420: 6e 65 77 20 54 61 62 6c 65 28 63 74 78 2c 20 54  new Table(ctx, T
2430: 61 62 6c 65 2e 4b 69 6e 64 2e 4e 6f 74 50 72 6f  able.Kind.NotPro
2440: 70 61 67 61 74 65 53 65 74 29 3b 0d 0a 0d 0a 09  pagateSet);.....
2450: 09 09 56 61 6c 75 65 20 61 69 20 3d 20 74 68 69  ..Value ai = thi
2460: 73 2e 65 76 61 6c 28 65 2e 76 64 65 66 2c 20 63  s.eval(e.vdef, c
2470: 74 78 29 3b 0d 0a 09 09 09 63 74 78 2e 73 65 74  tx);.....ctx.set
2480: 28 65 2e 6e 61 6d 65 2c 20 4e 6f 6f 70 4c 61 79  (e.name, NoopLay
2490: 65 72 2c 20 6e 75 6c 6c 29 3b 0d 0a 09 09 09 56  er, null);.....V
24a0: 61 6c 75 65 20 61 65 20 3d 20 74 68 69 73 2e 65  alue ae = this.e
24b0: 76 61 6c 28 65 2e 65 78 70 72 2c 20 63 74 78 2c  val(e.expr, ctx,
24c0: 20 4f 76 65 72 77 72 69 74 65 43 74 78 29 3b 0d   OverwriteCtx);.
24d0: 0a 0d 0a 09 09 09 69 66 28 20 61 69 20 69 73 20  ......if( ai is 
24e0: 6e 75 6c 6c 20 26 26 20 61 65 20 69 73 20 6e 75  null && ae is nu
24f0: 6c 6c 20 29 20 72 65 74 75 72 6e 20 6e 75 6c 6c  ll ) return null
2500: 3b 0d 0a 09 09 09 69 66 28 20 61 69 20 69 73 20  ;.....if( ai is 
2510: 6e 75 6c 6c 20 29 20 61 69 20 3d 20 61 73 74 32  null ) ai = ast2
2520: 74 61 62 6c 65 28 65 2e 76 64 65 66 29 3b 0d 0a  table(e.vdef);..
2530: 09 09 09 69 66 28 20 61 65 20 69 73 20 6e 75 6c  ...if( ae is nul
2540: 6c 20 29 20 61 65 20 3d 20 61 73 74 32 74 61 62  l ) ae = ast2tab
2550: 6c 65 28 65 2e 65 78 70 72 29 3b 0d 0a 0d 0a 09  le(e.expr);.....
2560: 09 09 72 65 74 75 72 6e 20 61 73 74 32 74 61 62  ..return ast2tab
2570: 6c 65 28 65 2c 20 64 65 6c 65 67 61 74 65 20 56  le(e, delegate V
2580: 61 6c 75 65 20 28 41 53 54 20 5f 29 7b 0d 0a 09  alue (AST _){...
2590: 09 09 09 69 66 28 5f 20 69 73 20 65 2e 76 64 65  ...if(_ is e.vde
25a0: 66 29 20 7b 20 72 65 74 75 72 6e 20 61 69 3b 20  f) { return ai; 
25b0: 7d 0d 0a 09 09 09 09 69 66 28 5f 20 69 73 20 65  }......if(_ is e
25c0: 2e 65 78 70 72 29 20 7b 20 72 65 74 75 72 6e 20  .expr) { return 
25d0: 61 65 3b 20 7d 0d 0a 09 09 09 09 61 73 73 65 72  ae; }......asser
25e0: 74 28 66 61 6c 73 65 29 3b 0d 0a 09 09 09 7d 29  t(false);.....})
25f0: 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65 72 72 69  ;....}....overri
2600: 64 65 20 56 61 6c 75 65 20 65 76 61 6c 5f 28 20  de Value eval_( 
2610: 41 70 70 20 65 2c 20 54 61 62 6c 65 20 63 74 78  App e, Table ctx
2620: 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29 0d  , bool ctxMod ).
2630: 0a 09 09 7b 0d 0a 09 09 09 56 61 6c 75 65 20 66  ...{.....Value f
2640: 20 3d 20 74 68 69 73 2e 65 76 61 6c 28 20 65 2e   = this.eval( e.
2650: 66 75 6e 2c 20 63 74 78 20 29 3b 0d 0a 09 09 09  fun, ctx );.....
2660: 69 66 28 61 75 74 6f 20 66 66 20 3d 20 63 61 73  if(auto ff = cas
2670: 74 28 46 75 6e 56 61 6c 75 65 29 66 29 0d 0a 09  t(FunValue)f)...
2680: 09 09 09 72 65 74 75 72 6e 20 74 68 69 73 2e 69  ...return this.i
2690: 6e 76 6f 6b 65 46 75 6e 63 74 69 6f 6e 28 66 66  nvokeFunction(ff
26a0: 2c 20 65 2e 61 72 67 73 2c 20 63 74 78 2c 20 65  , e.args, ctx, e
26b0: 2e 70 6f 73 2c 20 67 65 74 4e 61 6d 65 49 66 50  .pos, getNameIfP
26c0: 6f 73 73 69 62 6c 65 28 65 2e 66 75 6e 29 29 3b  ossible(e.fun));
26d0: 0d 0a 09 09 09 65 6c 73 65 0d 0a 09 09 09 7b 0d  .....else.....{.
26e0: 0a 09 09 09 09 62 6f 6f 6c 20 61 6c 6c 4e 75 6c  .....bool allNul
26f0: 6c 20 3d 20 28 66 20 69 73 20 6e 75 6c 6c 29 3b  l = (f is null);
2700: 0d 0a 09 09 09 09 56 61 6c 75 65 5b 5d 20 76 61  ......Value[] va
2710: 73 3b 0d 0a 09 09 09 09 66 6f 72 65 61 63 68 28  s;......foreach(
2720: 61 3b 20 65 2e 61 72 67 73 29 0d 0a 09 09 09 09  a; e.args)......
2730: 7b 0d 0a 09 09 09 09 09 56 61 6c 75 65 20 76 61  {.......Value va
2740: 20 3d 20 74 68 69 73 2e 65 76 61 6c 28 61 2c 20   = this.eval(a, 
2750: 63 74 78 2c 20 43 61 73 63 61 64 65 43 74 78 29  ctx, CascadeCtx)
2760: 3b 0d 0a 09 09 09 09 09 69 66 28 76 61 20 21 69  ;.......if(va !i
2770: 73 20 6e 75 6c 6c 29 20 61 6c 6c 4e 75 6c 6c 20  s null) allNull 
2780: 3d 20 66 61 6c 73 65 3b 0d 0a 09 09 09 09 09 76  = false;.......v
2790: 61 73 20 7e 3d 20 76 61 3b 0d 0a 09 09 09 09 7d  as ~= va;......}
27a0: 0d 0a 09 09 09 09 69 66 28 20 61 6c 6c 4e 75 6c  ......if( allNul
27b0: 6c 20 29 0d 0a 09 09 09 09 09 72 65 74 75 72 6e  l ).......return
27c0: 20 6e 75 6c 6c 3b 0d 0a 09 09 09 09 72 65 74 75   null;......retu
27d0: 72 6e 20 61 73 74 32 74 61 62 6c 65 28 65 2c 20  rn ast2table(e, 
27e0: 64 65 6c 65 67 61 74 65 20 56 61 6c 75 65 20 28  delegate Value (
27f0: 41 53 54 20 5f 29 7b 0d 0a 09 09 09 09 09 69 66  AST _){.......if
2800: 28 5f 20 69 73 20 65 2e 66 75 6e 29 20 72 65 74  (_ is e.fun) ret
2810: 75 72 6e 20 28 66 20 69 73 20 6e 75 6c 6c 20 3f  urn (f is null ?
2820: 20 61 73 74 32 74 61 62 6c 65 28 65 2e 66 75 6e   ast2table(e.fun
2830: 29 20 3a 20 66 29 3b 0d 0a 09 09 09 09 09 66 6f  ) : f);.......fo
2840: 72 65 61 63 68 28 69 2c 61 3b 20 65 2e 61 72 67  reach(i,a; e.arg
2850: 73 29 20 69 66 28 5f 20 69 73 20 61 29 20 72 65  s) if(_ is a) re
2860: 74 75 72 6e 20 28 76 61 73 5b 69 5d 20 69 73 20  turn (vas[i] is 
2870: 6e 75 6c 6c 20 3f 20 61 73 74 32 74 61 62 6c 65  null ? ast2table
2880: 28 61 29 20 3a 20 76 61 73 5b 69 5d 29 3b 0d 0a  (a) : vas[i]);..
2890: 09 09 09 09 09 61 73 73 65 72 74 28 66 61 6c 73  .....assert(fals
28a0: 65 29 3b 0d 0a 09 09 09 09 7d 29 3b 0d 0a 09 09  e);......});....
28b0: 09 7d 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65 72 72  .}....}....overr
28c0: 69 64 65 20 56 61 6c 75 65 20 65 76 61 6c 5f 28  ide Value eval_(
28d0: 20 46 75 6e 20 65 2c 20 54 61 62 6c 65 20 63 74   Fun e, Table ct
28e0: 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29  x, bool ctxMod )
28f0: 0d 0a 09 09 7b 0d 0a 09 09 09 63 74 78 20 3d 20  ....{.....ctx = 
2900: 6e 65 77 20 54 61 62 6c 65 28 63 74 78 2c 20 54  new Table(ctx, T
2910: 61 62 6c 65 2e 4b 69 6e 64 2e 4e 6f 74 50 72 6f  able.Kind.NotPro
2920: 70 61 67 61 74 65 53 65 74 29 3b 0d 0a 09 09 09  pagateSet);.....
2930: 66 6f 72 65 61 63 68 28 70 3b 20 65 2e 70 61 72  foreach(p; e.par
2940: 61 6d 73 29 0d 0a 09 09 09 09 63 74 78 2e 73 65  ams)......ctx.se
2950: 74 28 70 2e 6e 61 6d 65 2c 20 4e 6f 6f 70 4c 61  t(p.name, NoopLa
2960: 79 65 72 2c 20 6e 75 6c 6c 29 3b 0d 0a 09 09 09  yer, null);.....
2970: 56 61 6c 75 65 20 61 66 20 3d 20 74 68 69 73 2e  Value af = this.
2980: 65 76 61 6c 28 65 2e 66 75 6e 62 6f 64 79 2c 20  eval(e.funbody, 
2990: 63 74 78 29 3b 0d 0a 09 09 09 69 66 28 20 61 66  ctx);.....if( af
29a0: 20 69 73 20 6e 75 6c 6c 20 29 0d 0a 09 09 09 09   is null )......
29b0: 72 65 74 75 72 6e 20 6e 75 6c 6c 3b 0d 0a 09 09  return null;....
29c0: 09 72 65 74 75 72 6e 20 61 73 74 32 74 61 62 6c  .return ast2tabl
29d0: 65 28 65 2c 20 28 41 53 54 20 5f 29 7b 69 66 28  e(e, (AST _){if(
29e0: 5f 20 69 73 20 65 2e 66 75 6e 62 6f 64 79 29 72  _ is e.funbody)r
29f0: 65 74 75 72 6e 20 61 66 3b 20 61 73 73 65 72 74  eturn af; assert
2a00: 28 66 61 6c 73 65 29 3b 7d 29 3b 0d 0a 09 09 7d  (false);});....}
2a10: 0d 0a 09 7d 0d 0a 0d 0a 09 2f 2f 2f 20 28 72 61  ...}...../// (ra
2a20: 77 6d 61 63 72 6f 29 20 6c 61 79 65 72 2e 20 61  wmacro) layer. a
2a30: 6c 6d 6f 73 74 20 73 61 6d 65 20 61 73 20 40 6d  lmost same as @m
2a40: 61 63 72 6f 2c 20 62 75 74 20 74 68 65 20 4c 61  acro, but the La
2a50: 79 65 72 20 65 78 70 72 65 73 73 69 6f 6e 20 62  yer expression b
2a60: 65 63 6f 6d 65 73 20 41 53 54 2c 20 74 6f 6f 2e  ecomes AST, too.
2a70: 0d 0a 09 63 6c 61 73 73 20 52 61 77 4d 61 63 72  ...class RawMacr
2a80: 6f 4c 61 79 65 72 45 76 61 6c 20 3a 20 4d 61 63  oLayerEval : Mac
2a90: 72 6f 4c 61 79 65 72 45 76 61 6c 0d 0a 09 7b 0d  roLayerEval...{.
2aa0: 0a 09 09 6f 76 65 72 72 69 64 65 20 56 61 6c 75  ...override Valu
2ab0: 65 20 65 76 61 6c 5f 28 20 4c 61 79 20 65 2c 20  e eval_( Lay e, 
2ac0: 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c 20  Table ctx, bool 
2ad0: 63 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d 0a 09  ctxMod )....{...
2ae0: 09 09 56 61 6c 75 65 20 61 65 20 3d 20 74 68 69  ..Value ae = thi
2af0: 73 2e 65 76 61 6c 28 65 2e 65 78 70 72 2c 20 63  s.eval(e.expr, c
2b00: 74 78 29 3b 0d 0a 09 09 09 72 65 74 75 72 6e 20  tx);.....return 
2b10: 61 65 20 69 73 20 6e 75 6c 6c 20 3f 20 6e 75 6c  ae is null ? nul
2b20: 6c 0d 0a 09 09 09 20 20 20 20 20 20 20 3a 20 61  l.....       : a
2b30: 73 74 32 74 61 62 6c 65 28 65 2c 20 64 65 6c 65  st2table(e, dele
2b40: 67 61 74 65 20 56 61 6c 75 65 20 28 41 53 54 20  gate Value (AST 
2b50: 5f 29 7b 69 66 28 5f 20 69 73 20 65 2e 65 78 70  _){if(_ is e.exp
2b60: 72 29 72 65 74 75 72 6e 20 61 65 3b 20 61 73 73  r)return ae; ass
2b70: 65 72 74 28 66 61 6c 73 65 29 3b 7d 29 3b 0d 0a  ert(false);});..
2b80: 09 09 7d 0d 0a 09 7d 0d 0a 0d 0a 70 72 69 76 61  ..}...}....priva
2b90: 74 65 3a 20 2f 2f 20 73 68 6f 72 74 20 75 74 69  te: // short uti
2ba0: 6c 73 0d 0a 0d 0a 09 73 74 72 69 6e 67 20 67 65  ls.....string ge
2bb0: 74 4e 61 6d 65 49 66 50 6f 73 73 69 62 6c 65 28  tNameIfPossible(
2bc0: 41 53 54 20 65 29 0d 0a 09 7b 0d 0a 09 09 69 66  AST e)...{....if
2bd0: 28 61 75 74 6f 20 76 20 3d 20 63 61 73 74 28 56  (auto v = cast(V
2be0: 61 72 29 65 29 0d 0a 09 09 09 72 65 74 75 72 6e  ar)e).....return
2bf0: 20 76 2e 6e 61 6d 65 3b 0d 0a 09 09 72 65 74 75   v.name;....retu
2c00: 72 6e 20 22 22 3b 0d 0a 09 7d 0d 0a 0d 0a 09 41  rn "";...}.....A
2c10: 53 54 20 72 75 6e 4d 61 63 72 6f 28 41 53 54 20  ST runMacro(AST 
2c20: 65 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 62 6f  e, Table ctx, bo
2c30: 6f 6c 20 63 74 78 4d 6f 64 3d 4c 61 79 65 72 45  ol ctxMod=LayerE
2c40: 76 61 6c 2e 43 61 73 63 61 64 65 43 74 78 29 0d  val.CascadeCtx).
2c50: 0a 09 7b 0d 0a 09 09 56 61 6c 75 65 20 76 20 3d  ..{....Value v =
2c60: 20 67 65 74 4c 61 79 65 72 45 76 61 6c 28 52 61   getLayerEval(Ra
2c70: 77 4d 61 63 72 6f 4c 61 79 65 72 29 2e 65 76 61  wMacroLayer).eva
2c80: 6c 28 65 2c 20 63 74 78 2c 20 63 74 78 4d 6f 64  l(e, ctx, ctxMod
2c90: 29 3b 0d 0a 09 09 72 65 74 75 72 6e 20 28 76 20  );....return (v 
2ca0: 69 73 20 6e 75 6c 6c 20 3f 20 65 20 3a 20 70 6f  is null ? e : po
2cb0: 6c 65 6d 79 32 64 21 28 41 53 54 29 28 76 2c 20  lemy2d!(AST)(v, 
2cc0: 65 2e 70 6f 73 29 29 3b 0d 0a 09 7d 0d 0a 0d 0a  e.pos));...}....
2cd0: 70 72 69 76 61 74 65 3a 0d 0a 09 56 61 6c 75 65  private:...Value
2ce0: 20 63 72 65 61 74 65 4e 65 77 46 75 6e 63 74 69   createNewFuncti
2cf0: 6f 6e 28 46 75 6e 20 65 2c 20 54 61 62 6c 65 20  on(Fun e, Table 
2d00: 63 74 78 29 0d 0a 09 7b 0d 0a 09 09 63 6c 61 73  ctx)...{....clas
2d10: 73 20 55 73 65 72 44 65 66 69 6e 65 64 46 75 6e  s UserDefinedFun
2d20: 56 61 6c 75 65 20 3a 20 46 75 6e 56 61 6c 75 65  Value : FunValue
2d30: 0d 0a 09 09 7b 0d 0a 09 09 09 46 75 6e 20 20 20  ....{.....Fun   
2d40: 61 73 74 3b 0d 0a 09 09 09 54 61 62 6c 65 20 64  ast;.....Table d
2d50: 65 66 43 74 78 3b 0d 0a 09 09 09 6f 76 65 72 72  efCtx;.....overr
2d60: 69 64 65 20 63 6f 6e 73 74 28 50 61 72 61 6d 65  ide const(Parame
2d70: 74 65 72 5b 5d 29 20 70 61 72 61 6d 73 28 29 20  ter[]) params() 
2d80: 7b 20 72 65 74 75 72 6e 20 61 73 74 2e 70 61 72  { return ast.par
2d90: 61 6d 73 3b 20 7d 0d 0a 09 09 09 6f 76 65 72 72  ams; }.....overr
2da0: 69 64 65 20 54 61 62 6c 65 20 64 65 66 69 6e 69  ide Table defini
2db0: 74 69 6f 6e 43 6f 6e 74 65 78 74 28 29 20 20 20  tionContext()   
2dc0: 7b 20 72 65 74 75 72 6e 20 64 65 66 43 74 78 3b  { return defCtx;
2dd0: 20 7d 0d 0a 0d 0a 09 09 09 74 68 69 73 28 46 75   }.......this(Fu
2de0: 6e 20 61 73 74 2c 20 54 61 62 6c 65 20 64 65 66  n ast, Table def
2df0: 43 74 78 29 20 7b 20 74 68 69 73 2e 61 73 74 3d  Ctx) { this.ast=
2e00: 61 73 74 3b 20 74 68 69 73 2e 64 65 66 43 74 78  ast; this.defCtx
2e10: 3d 64 65 66 43 74 78 3b 20 7d 0d 0a 09 09 09 6f  =defCtx; }.....o
2e20: 76 65 72 72 69 64 65 20 73 74 72 69 6e 67 20 74  verride string t
2e30: 6f 53 74 72 69 6e 67 28 29 20 63 6f 6e 73 74 0d  oString() const.
2e40: 0a 09 09 09 09 7b 20 72 65 74 75 72 6e 20 73 70  .....{ return sp
2e50: 72 69 6e 74 66 21 22 28 66 75 6e 63 74 69 6f 6e  rintf!"(function
2e60: 3a 25 78 3a 25 78 29 22 28 63 61 73 74 28 76 6f  :%x:%x)"(cast(vo
2e70: 69 64 2a 29 61 73 74 2c 20 63 61 73 74 28 76 6f  id*)ast, cast(vo
2e80: 69 64 2a 29 64 65 66 43 74 78 29 3b 20 7d 0d 0a  id*)defCtx); }..
2e90: 09 09 09 6f 76 65 72 72 69 64 65 20 69 6e 74 20  ...override int 
2ea0: 6f 70 43 6d 70 28 4f 62 6a 65 63 74 20 72 68 73  opCmp(Object rhs
2eb0: 29 20 7b 0d 0a 09 09 09 09 69 66 28 61 75 74 6f  ) {......if(auto
2ec0: 20 72 20 3d 20 63 61 73 74 28 55 73 65 72 44 65   r = cast(UserDe
2ed0: 66 69 6e 65 64 46 75 6e 56 61 6c 75 65 29 72 68  finedFunValue)rh
2ee0: 73 29 20 7b 0d 0a 09 09 09 09 09 69 66 28 61 75  s) {.......if(au
2ef0: 74 6f 20 63 20 3d 20 74 79 70 65 69 64 28 76 6f  to c = typeid(vo
2f00: 69 64 2a 29 2e 63 6f 6d 70 61 72 65 28 63 61 73  id*).compare(cas
2f10: 74 28 76 6f 69 64 2a 29 61 73 74 2c 20 63 61 73  t(void*)ast, cas
2f20: 74 28 76 6f 69 64 2a 29 72 2e 61 73 74 29 29 0d  t(void*)r.ast)).
2f30: 0a 09 09 09 09 09 09 72 65 74 75 72 6e 20 63 3b  .......return c;
2f40: 0d 0a 09 09 09 09 09 69 66 28 61 75 74 6f 20 63  .......if(auto c
2f50: 20 3d 20 74 79 70 65 69 64 28 76 6f 69 64 2a 29   = typeid(void*)
2f60: 2e 63 6f 6d 70 61 72 65 28 63 61 73 74 28 76 6f  .compare(cast(vo
2f70: 69 64 2a 29 64 65 66 43 74 78 2c 20 63 61 73 74  id*)defCtx, cast
2f80: 28 76 6f 69 64 2a 29 72 2e 64 65 66 43 74 78 29  (void*)r.defCtx)
2f90: 29 0d 0a 09 09 09 09 09 09 72 65 74 75 72 6e 20  )........return 
2fa0: 63 3b 0d 0a 09 09 09 09 09 72 65 74 75 72 6e 20  c;.......return 
2fb0: 30 3b 2f 2f 20 5b 54 4f 44 4f 5d 20 61 76 6f 69  0;// [TODO] avoi
2fc0: 64 20 75 73 69 6e 67 20 70 6f 69 6e 74 65 72 20  d using pointer 
2fd0: 76 61 6c 75 65 2e 2e 2e 0d 0a 09 09 09 09 7d 0d  value.........}.
2fe0: 0a 09 09 09 09 69 66 28 61 75 74 6f 20 72 20 3d  .....if(auto r =
2ff0: 20 63 61 73 74 28 56 61 6c 75 65 29 72 68 73 29   cast(Value)rhs)
3000: 20 72 65 74 75 72 6e 20 74 79 70 65 69 64 28 74   return typeid(t
3010: 68 69 73 29 2e 6f 70 43 6d 70 28 74 79 70 65 69  his).opCmp(typei
3020: 64 28 72 29 29 3b 0d 0a 09 09 09 09 74 68 72 6f  d(r));......thro
3030: 77 20 67 65 6e 65 78 21 52 75 6e 74 69 6d 65 45  w genex!RuntimeE
3040: 78 63 65 70 74 69 6f 6e 28 22 63 6f 6d 70 61 72  xception("compar
3050: 69 73 6f 6e 20 77 69 74 68 20 76 61 6c 75 65 20  ison with value 
3060: 61 6e 64 20 73 6f 6d 65 74 68 69 6e 67 20 6f 74  and something ot
3070: 68 65 72 22 29 3b 0d 0a 09 09 09 7d 0d 0a 09 09  her");.....}....
3080: 09 6f 76 65 72 72 69 64 65 20 68 61 73 68 5f 74  .override hash_t
3090: 20 74 6f 48 61 73 68 28 29 20 7b 0d 0a 09 09 09   toHash() {.....
30a0: 09 72 65 74 75 72 6e 20 28 63 61 73 74 28 68 61  .return (cast(ha
30b0: 73 68 5f 74 29 63 61 73 74 28 76 6f 69 64 2a 29  sh_t)cast(void*)
30c0: 61 73 74 29 20 2b 20 28 63 61 73 74 28 68 61 73  ast) + (cast(has
30d0: 68 5f 74 29 63 61 73 74 28 76 6f 69 64 2a 29 64  h_t)cast(void*)d
30e0: 65 66 43 74 78 29 3b 0d 0a 09 09 09 7d 0d 0a 0d  efCtx);.....}...
30f0: 0a 09 09 09 41 53 54 5b 76 6f 69 64 2a 5d 20 6d  ....AST[void*] m
3100: 61 6e 64 65 43 61 63 68 65 3b 0d 0a 09 09 09 73  andeCache;.....s
3110: 74 61 74 69 63 20 63 6c 61 73 73 20 4d 65 6d 6f  tatic class Memo
3120: 6b 65 79 54 79 70 65 0d 0a 09 09 09 7b 0d 0a 09  keyType.....{...
3130: 09 09 09 76 6f 69 64 2a 20 61 3b 20 4c 61 79 65  ...void* a; Laye
3140: 72 20 62 3b 20 54 61 62 6c 65 20 63 3b 0d 0a 09  r b; Table c;...
3150: 09 09 09 6d 69 78 69 6e 20 53 69 6d 70 6c 65 43  ...mixin SimpleC
3160: 6c 61 73 73 3b 0d 0a 09 09 09 7d 0d 0a 09 09 09  lass;.....}.....
3170: 73 74 61 74 69 63 20 54 75 70 6c 65 21 28 56 61  static Tuple!(Va
3180: 6c 75 65 2c 69 6e 74 29 5b 4d 65 6d 6f 6b 65 79  lue,int)[Memokey
3190: 54 79 70 65 5d 20 6d 65 6d 6f 3b 0d 0a 0d 0a 09  Type] memo;.....
31a0: 09 09 6f 76 65 72 72 69 64 65 20 56 61 6c 75 65  ..override Value
31b0: 20 69 6e 76 6f 6b 65 28 4c 61 79 65 72 20 6c 61   invoke(Layer la
31c0: 79 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 4c 65  y, Table ctx, Le
31d0: 78 50 6f 73 69 74 69 6f 6e 20 70 6f 73 29 0d 0a  xPosition pos)..
31e0: 09 09 09 7b 0d 0a 09 09 09 09 61 75 74 6f 20 65  ...{......auto e
31f0: 76 6c 61 79 20 3d 20 67 65 74 4c 61 79 65 72 45  vlay = getLayerE
3200: 76 61 6c 28 6c 61 79 29 3b 0d 0a 09 09 09 09 61  val(lay);......a
3210: 75 74 6f 20 6e 6f 6e 4d 65 6d 6f 69 7a 65 64 52  uto nonMemoizedR
3220: 75 6e 20 3d 20 28 29 7b 20 72 65 74 75 72 6e 20  un = (){ return 
3230: 65 76 6c 61 79 2e 6d 61 63 72 6f 41 6e 64 45 76  evlay.macroAndEv
3240: 61 6c 28 61 73 74 2e 66 75 6e 62 6f 64 79 2c 20  al(ast.funbody, 
3250: 63 74 78 29 3b 20 7d 3b 0d 0a 0d 0a 09 09 09 09  ctx); };........
3260: 69 66 28 20 21 69 73 55 73 65 72 44 65 66 69 6e  if( !isUserDefin
3270: 65 64 4c 61 79 65 72 28 6c 61 79 29 20 29 0d 0a  edLayer(lay) )..
3280: 09 09 09 09 09 72 65 74 75 72 6e 20 6e 6f 6e 4d  .....return nonM
3290: 65 6d 6f 69 7a 65 64 52 75 6e 28 29 3b 0d 0a 0d  emoizedRun();...
32a0: 0a 09 09 09 09 2f 2f 20 61 75 74 6f 6d 61 74 69  .....// automati
32b0: 63 20 6d 65 6d 6f 69 7a 65 64 20 63 6f 2d 72 65  c memoized co-re
32c0: 63 75 72 73 69 76 65 20 65 78 65 63 75 74 69 6f  cursive executio
32d0: 6e 0d 0a 09 09 09 09 4d 65 6d 6f 6b 65 79 54 79  n......MemokeyTy
32e0: 70 65 20 6d 65 6d 6f 6b 65 79 20 3d 20 6e 65 77  pe memokey = new
32f0: 20 4d 65 6d 6f 6b 65 79 54 79 70 65 28 63 61 73   MemokeyType(cas
3300: 74 28 76 6f 69 64 2a 29 61 73 74 2c 20 6c 61 79  t(void*)ast, lay
3310: 2c 20 63 74 78 29 3b 0d 0a 09 09 09 09 69 66 28  , ctx);......if(
3320: 61 75 74 6f 20 70 20 3d 20 6d 65 6d 6f 6b 65 79  auto p = memokey
3330: 20 69 6e 20 6d 65 6d 6f 29 0d 0a 09 09 09 09 7b   in memo)......{
3340: 0d 0a 09 09 09 09 09 28 2a 70 29 5b 31 5d 20 2b  .......(*p)[1] +
3350: 2b 3b 0d 0a 09 09 09 09 09 72 65 74 75 72 6e 20  +;.......return 
3360: 28 2a 70 29 5b 30 5d 3b 0d 0a 09 09 09 09 7d 0d  (*p)[0];......}.
3370: 0a 09 09 09 09 65 6c 73 65 20 7b 0d 0a 09 09 09  .....else {.....
3380: 09 09 56 61 6c 75 65 20 76 3b 0d 0a 09 09 09 09  ..Value v;......
3390: 09 74 72 79 20 7b 20 76 20 3d 20 65 76 6c 61 79  .try { v = evlay
33a0: 2e 6c 69 66 74 28 6e 65 77 20 42 6f 74 74 6f 6d  .lift(new Bottom
33b0: 56 61 6c 75 65 2c 20 63 74 78 2c 20 70 6f 73 29  Value, ctx, pos)
33c0: 3b 20 7d 20 63 61 74 63 68 20 7b 20 76 20 3d 20  ; } catch { v = 
33d0: 6e 65 77 20 42 6f 74 74 6f 6d 56 61 6c 75 65 3b  new BottomValue;
33e0: 20 7d 0d 0a 09 09 09 09 09 6d 65 6d 6f 5b 6d 65   }.......memo[me
33f0: 6d 6f 6b 65 79 5d 20 3d 20 74 75 70 6c 65 28 76  mokey] = tuple(v
3400: 2c 20 30 29 3b 0d 0a 09 09 09 09 7d 0d 0a 0d 0a  , 0);......}....
3410: 09 09 09 09 56 61 6c 75 65 20 72 20 3d 20 6e 6f  ....Value r = no
3420: 6e 4d 65 6d 6f 69 7a 65 64 52 75 6e 28 29 3b 0d  nMemoizedRun();.
3430: 0a 0d 0a 09 09 09 09 69 6e 74 20 74 6f 75 63 68  .......int touch
3440: 65 64 20 3d 20 6d 65 6d 6f 5b 6d 65 6d 6f 6b 65  ed = memo[memoke
3450: 79 5d 5b 31 5d 3b 0d 0a 09 09 09 09 6d 65 6d 6f  y][1];......memo
3460: 5b 6d 65 6d 6f 6b 65 79 5d 20 3d 20 74 75 70 6c  [memokey] = tupl
3470: 65 28 72 2c 20 31 32 33 34 35 36 37 38 29 3b 0d  e(r, 12345678);.
3480: 0a 09 09 09 09 72 65 74 75 72 6e 20 72 3b 0d 0a  .....return r;..
3490: 09 09 09 7d 0d 0a 09 09 7d 0d 0a 09 09 72 65 74  ...}....}....ret
34a0: 75 72 6e 20 6e 65 77 20 55 73 65 72 44 65 66 69  urn new UserDefi
34b0: 6e 65 64 46 75 6e 56 61 6c 75 65 28 65 2c 63 74  nedFunValue(e,ct
34c0: 78 29 3b 0d 0a 09 7d 0d 0a 0d 0a 70 75 62 6c 69  x);...}....publi
34d0: 63 3a 0d 0a 09 2f 2f 2f 20 41 64 64 20 70 72 69  c:.../// Add pri
34e0: 6d 69 74 69 76 65 20 66 75 6e 63 74 69 6f 6e 20  mitive function 
34f0: 74 6f 20 74 68 65 20 67 6c 6f 62 61 6c 20 63 6f  to the global co
3500: 6e 74 65 78 74 0d 0a 09 76 6f 69 64 20 61 64 64  ntext...void add
3510: 50 72 69 6d 69 74 69 76 65 28 52 2c 54 2e 2e 2e  Primitive(R,T...
3520: 29 28 73 74 72 69 6e 67 20 6e 61 6d 65 2c 20 4c  )(string name, L
3530: 61 79 65 72 20 64 65 66 4c 61 79 2c 20 52 20 64  ayer defLay, R d
3540: 65 6c 65 67 61 74 65 20 28 54 29 20 64 67 29 0d  elegate (T) dg).
3550: 0a 09 7b 0d 0a 09 09 63 6c 61 73 73 20 4e 61 74  ..{....class Nat
3560: 69 76 65 46 75 6e 56 61 6c 75 65 20 3a 20 46 75  iveFunValue : Fu
3570: 6e 56 61 6c 75 65 0d 0a 09 09 7b 0d 0a 09 09 09  nValue....{.....
3580: 6f 76 65 72 72 69 64 65 20 63 6f 6e 73 74 28 50  override const(P
3590: 61 72 61 6d 65 74 65 72 5b 5d 29 20 70 61 72 61  arameter[]) para
35a0: 6d 73 28 29 20 7b 20 72 65 74 75 72 6e 20 70 61  ms() { return pa
35b0: 72 61 6d 73 5f 64 61 74 61 3b 20 7d 0d 0a 09 09  rams_data; }....
35c0: 09 6f 76 65 72 72 69 64 65 20 54 61 62 6c 65 20  .override Table 
35d0: 64 65 66 69 6e 69 74 69 6f 6e 43 6f 6e 74 65 78  definitionContex
35e0: 74 28 29 20 20 20 7b 20 72 65 74 75 72 6e 20 74  t()   { return t
35f0: 68 65 43 6f 6e 74 65 78 74 3b 20 7d 0d 0a 0d 0a  heContext; }....
3600: 09 09 09 6f 76 65 72 72 69 64 65 20 73 74 72 69  ...override stri
3610: 6e 67 20 74 6f 53 74 72 69 6e 67 28 29 20 7b 20  ng toString() { 
3620: 72 65 74 75 72 6e 20 73 70 72 69 6e 74 66 21 22  return sprintf!"
3630: 28 6e 61 74 69 76 65 3a 25 78 29 22 28 64 67 2e  (native:%x)"(dg.
3640: 66 75 6e 63 70 74 72 29 3b 20 7d 0d 0a 09 09 09  funcptr); }.....
3650: 6f 76 65 72 72 69 64 65 20 69 6e 74 20 6f 70 43  override int opC
3660: 6d 70 28 4f 62 6a 65 63 74 20 72 68 73 29 20 7b  mp(Object rhs) {
3670: 0d 0a 09 09 09 09 69 66 28 61 75 74 6f 20 72 20  ......if(auto r 
3680: 3d 20 63 61 73 74 28 4e 61 74 69 76 65 46 75 6e  = cast(NativeFun
3690: 56 61 6c 75 65 29 72 68 73 29 20 72 65 74 75 72  Value)rhs) retur
36a0: 6e 20 74 79 70 65 69 64 28 74 79 70 65 6f 66 28  n typeid(typeof(
36b0: 64 67 29 29 2e 63 6f 6d 70 61 72 65 28 26 64 67  dg)).compare(&dg
36c0: 2c 26 72 2e 64 67 29 3b 0d 0a 09 09 09 09 69 66  ,&r.dg);......if
36d0: 28 61 75 74 6f 20 72 20 3d 20 63 61 73 74 28 56  (auto r = cast(V
36e0: 61 6c 75 65 29 72 68 73 29 20 20 20 20 20 20 20  alue)rhs)       
36f0: 20 20 20 72 65 74 75 72 6e 20 74 79 70 65 69 64     return typeid
3700: 28 74 68 69 73 29 2e 6f 70 43 6d 70 28 74 79 70  (this).opCmp(typ
3710: 65 69 64 28 72 29 29 3b 0d 0a 09 09 09 09 74 68  eid(r));......th
3720: 72 6f 77 20 67 65 6e 65 78 21 52 75 6e 74 69 6d  row genex!Runtim
3730: 65 45 78 63 65 70 74 69 6f 6e 28 22 63 6f 6d 70  eException("comp
3740: 61 72 69 73 6f 6e 20 77 69 74 68 20 76 61 6c 75  arison with valu
3750: 65 20 61 6e 64 20 73 6f 6d 65 74 68 69 6e 67 20  e and something 
3760: 6f 74 68 65 72 22 29 3b 0d 0a 09 09 09 7d 0d 0a  other");.....}..
3770: 09 09 09 6f 76 65 72 72 69 64 65 20 68 61 73 68  ...override hash
3780: 5f 74 20 74 6f 48 61 73 68 28 29 20 63 6f 6e 73  _t toHash() cons
3790: 74 20 7b 0d 0a 09 09 09 09 72 65 74 75 72 6e 20  t {......return 
37a0: 74 79 70 65 69 64 28 64 67 29 2e 67 65 74 48 61  typeid(dg).getHa
37b0: 73 68 28 26 64 67 29 3b 0d 0a 09 09 09 7d 0d 0a  sh(&dg);.....}..
37c0: 0d 0a 09 09 09 52 20 64 65 6c 65 67 61 74 65 28  .....R delegate(
37d0: 54 29 20 64 67 3b 0d 0a 09 09 09 50 61 72 61 6d  T) dg;.....Param
37e0: 65 74 65 72 5b 5d 20 70 61 72 61 6d 73 5f 64 61  eter[] params_da
37f0: 74 61 3b 0d 0a 0d 0a 09 09 09 74 68 69 73 28 52  ta;.......this(R
3800: 20 64 65 6c 65 67 61 74 65 28 54 29 20 64 67 29   delegate(T) dg)
3810: 0d 0a 09 09 09 7b 0d 0a 09 09 09 09 74 68 69 73  .....{......this
3820: 2e 64 67 20 3d 20 64 67 3b 0d 0a 09 09 09 09 66  .dg = dg;......f
3830: 6f 72 65 61 63 68 28 69 2c 20 54 69 3b 20 54 29  oreach(i, Ti; T)
3840: 0d 0a 09 09 09 09 09 70 61 72 61 6d 73 5f 64 61  .......params_da
3850: 74 61 20 7e 3d 20 6e 65 77 20 50 61 72 61 6d 65  ta ~= new Parame
3860: 74 65 72 28 74 65 78 74 28 69 29 2c 20 5b 5d 29  ter(text(i), [])
3870: 3b 0d 0a 09 09 09 7d 0d 0a 0d 0a 09 09 09 6f 76  ;.....}.......ov
3880: 65 72 72 69 64 65 20 56 61 6c 75 65 20 69 6e 76  erride Value inv
3890: 6f 6b 65 28 4c 61 79 65 72 20 6c 61 79 2c 20 54  oke(Layer lay, T
38a0: 61 62 6c 65 20 63 74 78 2c 20 4c 65 78 50 6f 73  able ctx, LexPos
38b0: 69 74 69 6f 6e 20 70 6f 73 29 0d 0a 09 09 09 7b  ition pos).....{
38c0: 0d 0a 09 09 09 09 69 66 28 20 6c 61 79 20 21 3d  ......if( lay !=
38d0: 20 64 65 66 4c 61 79 20 29 0d 0a 09 09 09 09 09   defLay ).......
38e0: 74 68 72 6f 77 20 67 65 6e 65 78 21 52 75 6e 74  throw genex!Runt
38f0: 69 6d 65 45 78 63 65 70 74 69 6f 6e 28 70 6f 73  imeException(pos
3900: 2c 0d 0a 09 09 09 09 09 09 74 65 78 74 28 22 6f  ,........text("o
3910: 6e 6c 79 20 22 2c 20 64 65 66 4c 61 79 2c 20 22  nly ", defLay, "
3920: 20 6c 61 79 65 72 20 63 61 6e 20 63 61 6c 6c 20   layer can call 
3930: 6e 61 74 69 76 65 20 66 75 6e 63 74 69 6f 6e 3a  native function:
3940: 20 22 2c 20 6e 61 6d 65 29 29 3b 0d 0a 09 09 09   ", name));.....
3950: 09 54 20 74 79 70 65 64 5f 61 72 67 73 3b 0d 0a  .T typed_args;..
3960: 09 09 09 09 66 6f 72 65 61 63 68 28 69 2c 20 54  ....foreach(i, T
3970: 69 3b 20 54 29 20 7b 0d 0a 09 09 09 09 09 56 61  i; T) {.......Va
3980: 6c 75 65 20 76 69 20 3d 20 63 74 78 2e 67 65 74  lue vi = ctx.get
3990: 28 74 65 78 74 28 69 29 2c 20 56 61 6c 75 65 4c  (text(i), ValueL
39a0: 61 79 65 72 2c 20 70 6f 73 29 3b 0d 0a 09 09 09  ayer, pos);.....
39b0: 09 09 74 79 70 65 64 5f 61 72 67 73 5b 69 5d 20  ..typed_args[i] 
39c0: 3d 20 63 61 73 74 28 54 69 29 20 76 69 3b 0d 0a  = cast(Ti) vi;..
39d0: 09 09 09 09 09 69 66 28 20 74 79 70 65 64 5f 61  .....if( typed_a
39e0: 72 67 73 5b 69 5d 20 69 73 20 6e 75 6c 6c 20 29  rgs[i] is null )
39f0: 0d 0a 09 09 09 09 09 09 74 68 72 6f 77 20 67 65  ........throw ge
3a00: 6e 65 78 21 52 75 6e 74 69 6d 65 45 78 63 65 70  nex!RuntimeExcep
3a10: 74 69 6f 6e 28 70 6f 73 2c 0d 0a 09 09 09 09 09  tion(pos,.......
3a20: 09 09 73 70 72 69 6e 74 66 21 22 74 79 70 65 20  ..sprintf!"type 
3a30: 6d 69 73 6d 61 74 63 68 20 6f 6e 20 74 68 65 20  mismatch on the 
3a40: 61 72 67 75 6d 65 6e 74 20 25 64 20 6f 66 20 6e  argument %d of n
3a50: 61 74 69 76 65 20 66 75 6e 63 74 69 6f 6e 20 25  ative function %
3a60: 73 2e 20 25 73 20 72 65 71 75 69 72 65 64 20 62  s. %s required b
3a70: 75 74 20 25 73 20 70 61 73 73 65 64 2e 22 28 69  ut %s passed."(i
3a80: 2b 31 2c 6e 61 6d 65 2c 74 79 70 65 69 64 28 54  +1,name,typeid(T
3a90: 69 29 2c 76 69 29 29 3b 0d 0a 09 09 09 09 7d 0d  i),vi));......}.
3aa0: 0a 09 09 09 09 74 72 79 20 7b 0d 0a 09 09 09 09  .....try {......
3ab0: 09 72 65 74 75 72 6e 20 64 67 28 74 79 70 65 64  .return dg(typed
3ac0: 5f 61 72 67 73 29 3b 0d 0a 09 09 09 09 7d 20 63  _args);......} c
3ad0: 61 74 63 68 28 20 52 75 6e 74 69 6d 65 45 78 63  atch( RuntimeExc
3ae0: 65 70 74 69 6f 6e 20 65 20 29 20 7b 0d 0a 09 09  eption e ) {....
3af0: 09 09 09 74 68 72 6f 77 20 65 2e 70 6f 73 20 69  ...throw e.pos i
3b00: 73 20 6e 75 6c 6c 20 3f 20 6e 65 77 20 52 75 6e  s null ? new Run
3b10: 74 69 6d 65 45 78 63 65 70 74 69 6f 6e 28 70 6f  timeException(po
3b20: 73 2c 20 65 2e 6d 73 67 2c 20 65 2e 66 69 6c 65  s, e.msg, e.file
3b30: 2c 20 65 2e 6c 69 6e 65 29 20 3a 20 65 3b 0d 0a  , e.line) : e;..
3b40: 09 09 09 09 7d 0d 0a 09 09 09 7d 0d 0a 09 09 7d  ....}.....}....}
3b50: 0d 0a 09 09 74 68 65 43 6f 6e 74 65 78 74 2e 73  ....theContext.s
3b60: 65 74 28 6e 61 6d 65 2c 20 64 65 66 4c 61 79 2c  et(name, defLay,
3b70: 20 6e 65 77 20 4e 61 74 69 76 65 46 75 6e 56 61   new NativeFunVa
3b80: 6c 75 65 28 64 67 29 29 3b 0d 0a 09 7d 0d 0a 7d  lue(dg));...}..}
3b90: 0d 0a 0d 0a 76 65 72 73 69 6f 6e 28 75 6e 69 74  ....version(unit
3ba0: 74 65 73 74 29 0d 0a 09 69 6d 70 6f 72 74 20 70  test)...import p
3bb0: 6f 6c 65 6d 79 2e 72 75 6e 74 69 6d 65 3b 0d 0a  olemy.runtime;..
3bc0: 0d 0a 75 6e 69 74 74 65 73 74 0d 0a 7b 0d 0a 09  ..unittest..{...
3bd0: 61 75 74 6f 20 65 20 3d 20 6e 65 77 20 45 76 61  auto e = new Eva
3be0: 6c 75 61 74 6f 72 3b 0d 0a 09 65 6e 72 6f 6c 6c  luator;...enroll
3bf0: 52 75 6e 74 69 6d 65 4c 69 62 72 61 72 79 28 65  RuntimeLibrary(e
3c00: 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20  );...assert_eq( 
3c10: 65 2e 65 76 61 6c 53 74 72 69 6e 67 28 60 0d 0a  e.evalString(`..
3c20: 40 40 66 6f 6f 28 78 29 7b 78 2a 32 7d 3b 0d 0a  @@foo(x){x*2};..
3c30: 40 66 6f 6f 20 22 2b 22 20 28 78 2c 79 29 7b 78  @foo "+" (x,y){x
3c40: 7d 3b 0d 0a 40 66 6f 6f 28 33 2b 34 29 0d 0a 60  };..@foo(3+4)..`
3c50: 29 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28  ), new IntValue(
3c60: 36 29 20 29 3b 0d 0a 7d 0d 0a 0d 0a 75 6e 69 74  6) );..}....unit
3c70: 74 65 73 74 0d 0a 7b 0d 0a 09 61 75 74 6f 20 65  test..{...auto e
3c80: 20 3d 20 6e 65 77 20 45 76 61 6c 75 61 74 6f 72   = new Evaluator
3c90: 3b 0d 0a 09 65 6e 72 6f 6c 6c 52 75 6e 74 69 6d  ;...enrollRuntim
3ca0: 65 4c 69 62 72 61 72 79 28 65 29 3b 0d 0a 09 61  eLibrary(e);...a
3cb0: 75 74 6f 20 72 20 3d 20 61 73 73 65 72 74 5f 6e  uto r = assert_n
3cc0: 6f 74 68 72 6f 77 28 20 65 2e 65 76 61 6c 53 74  othrow( e.evalSt
3cd0: 72 69 6e 67 28 60 76 61 72 20 78 20 3d 20 32 31  ring(`var x = 21
3ce0: 3b 20 78 20 2b 20 78 2a 78 3b 60 29 20 29 3b 0d  ; x + x*x;`) );.
3cf0: 0a 09 61 73 73 65 72 74 5f 65 71 28 20 72 2c 20  ..assert_eq( r, 
3d00: 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 42 69 67  new IntValue(Big
3d10: 49 6e 74 28 32 31 2b 32 31 2a 32 31 29 29 20 29  Int(21+21*21)) )
3d20: 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20 65  ;...assert_eq( e
3d30: 2e 67 6c 6f 62 61 6c 43 6f 6e 74 65 78 74 2e 67  .globalContext.g
3d40: 65 74 28 22 78 22 2c 56 61 6c 75 65 4c 61 79 65  et("x",ValueLaye
3d50: 72 29 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65  r), new IntValue
3d60: 28 42 69 67 49 6e 74 28 32 31 29 29 20 29 3b 0d  (BigInt(21)) );.
3d70: 0a 09 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77  ..assert_nothrow
3d80: 28 20 65 2e 67 6c 6f 62 61 6c 43 6f 6e 74 65 78  ( e.globalContex
3d90: 74 2e 67 65 74 28 22 78 22 2c 56 61 6c 75 65 4c  t.get("x",ValueL
3da0: 61 79 65 72 29 20 29 3b 0d 0a 09 61 73 73 65 72  ayer) );...asser
3db0: 74 5f 74 68 72 6f 77 21 52 75 6e 74 69 6d 65 45  t_throw!RuntimeE
3dc0: 78 63 65 70 74 69 6f 6e 28 20 65 2e 67 6c 6f 62  xception( e.glob
3dd0: 61 6c 43 6f 6e 74 65 78 74 2e 67 65 74 28 22 79  alContext.get("y
3de0: 22 2c 56 61 6c 75 65 4c 61 79 65 72 29 20 29 3b  ",ValueLayer) );
3df0: 0d 0a 7d 0d 0a 0d 0a 75 6e 69 74 74 65 73 74 0d  ..}....unittest.
3e00: 0a 7b 0d 0a 09 61 75 74 6f 20 65 20 3d 20 6e 65  .{...auto e = ne
3e10: 77 20 45 76 61 6c 75 61 74 6f 72 3b 0d 0a 09 65  w Evaluator;...e
3e20: 6e 72 6f 6c 6c 52 75 6e 74 69 6d 65 4c 69 62 72  nrollRuntimeLibr
3e30: 61 72 79 28 65 29 3b 0d 0a 09 61 75 74 6f 20 72  ary(e);...auto r
3e40: 20 3d 20 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f   = assert_nothro
3e50: 77 28 20 65 2e 65 76 61 6c 53 74 72 69 6e 67 28  w( e.evalString(
3e60: 60 76 61 72 20 78 20 3d 20 32 31 3b 20 76 61 72  `var x = 21; var
3e70: 20 78 20 3d 20 78 20 2b 20 78 2a 78 3b 60 29 20   x = x + x*x;`) 
3e80: 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20  );...assert_eq( 
3e90: 72 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28  r, new IntValue(
3ea0: 42 69 67 49 6e 74 28 32 31 2b 32 31 2a 32 31 29  BigInt(21+21*21)
3eb0: 29 20 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71  ) );...assert_eq
3ec0: 28 20 65 2e 67 6c 6f 62 61 6c 43 6f 6e 74 65 78  ( e.globalContex
3ed0: 74 2e 67 65 74 28 22 78 22 2c 56 61 6c 75 65 4c  t.get("x",ValueL
3ee0: 61 79 65 72 29 2c 20 6e 65 77 20 49 6e 74 56 61  ayer), new IntVa
3ef0: 6c 75 65 28 32 31 2b 32 31 2a 32 31 29 20 29 3b  lue(21+21*21) );
3f00: 0d 0a 09 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f  ...assert_nothro
3f10: 77 28 20 65 2e 67 6c 6f 62 61 6c 43 6f 6e 74 65  w( e.globalConte
3f20: 78 74 2e 67 65 74 28 22 78 22 2c 56 61 6c 75 65  xt.get("x",Value
3f30: 4c 61 79 65 72 29 20 29 3b 0d 0a 09 61 73 73 65  Layer) );...asse
3f40: 72 74 5f 74 68 72 6f 77 21 52 75 6e 74 69 6d 65  rt_throw!Runtime
3f50: 45 78 63 65 70 74 69 6f 6e 28 20 65 2e 67 6c 6f  Exception( e.glo
3f60: 62 61 6c 43 6f 6e 74 65 78 74 2e 67 65 74 28 22  balContext.get("
3f70: 79 22 2c 56 61 6c 75 65 4c 61 79 65 72 29 20 29  y",ValueLayer) )
3f80: 3b 0d 0a 7d 0d 0a 75 6e 69 74 74 65 73 74 0d 0a  ;..}..unittest..
3f90: 7b 0d 0a 09 61 75 74 6f 20 65 20 3d 20 6e 65 77  {...auto e = new
3fa0: 20 45 76 61 6c 75 61 74 6f 72 3b 0d 0a 09 65 6e   Evaluator;...en
3fb0: 72 6f 6c 6c 52 75 6e 74 69 6d 65 4c 69 62 72 61  rollRuntimeLibra
3fc0: 72 79 28 65 29 3b 0d 0a 09 61 73 73 65 72 74 5f  ry(e);...assert_
3fd0: 65 71 28 20 65 2e 65 76 61 6c 53 74 72 69 6e 67  eq( e.evalString
3fe0: 28 60 6c 65 74 20 78 3d 31 3b 20 6c 65 74 20 79  (`let x=1; let y
3ff0: 3d 28 6c 65 74 20 78 3d 32 29 3b 20 78 60 29 2c  =(let x=2); x`),
4000: 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 31 29   new IntValue(1)
4010: 20 29 3b 20 0d 0a 09 61 73 73 65 72 74 5f 65 71   ); ...assert_eq
4020: 28 20 65 2e 65 76 61 6c 53 74 72 69 6e 67 28 60  ( e.evalString(`
4030: 6c 65 74 20 78 3d 31 3b 20 6c 65 74 20 79 3d 28  let x=1; let y=(
4040: 6c 65 74 20 78 3d 32 3b 66 75 6e 28 29 7b 78 7d  let x=2;fun(){x}
4050: 29 3b 20 79 28 29 60 29 2c 20 6e 65 77 20 49 6e  ); y()`), new In
4060: 74 56 61 6c 75 65 28 32 29 20 29 3b 20 0d 0a 7d  tValue(2) ); ..}
4070: 0d 0a 0d 0a 75 6e 69 74 74 65 73 74 0d 0a 7b 0d  ....unittest..{.
4080: 0a 09 61 75 74 6f 20 65 20 3d 20 6e 65 77 20 45  ..auto e = new E
4090: 76 61 6c 75 61 74 6f 72 3b 0d 0a 09 65 6e 72 6f  valuator;...enro
40a0: 6c 6c 52 75 6e 74 69 6d 65 4c 69 62 72 61 72 79  llRuntimeLibrary
40b0: 28 65 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71  (e);...assert_eq
40c0: 28 20 65 2e 65 76 61 6c 53 74 72 69 6e 67 28 60  ( e.evalString(`
40d0: 40 61 20 78 3d 31 3b 20 40 62 20 78 3d 32 3b 20  @a x=1; @b x=2; 
40e0: 40 61 28 78 29 60 29 2c 20 6e 65 77 20 49 6e 74  @a(x)`), new Int
40f0: 56 61 6c 75 65 28 42 69 67 49 6e 74 28 31 29 29  Value(BigInt(1))
4100: 20 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28   );...assert_eq(
4110: 20 65 2e 65 76 61 6c 53 74 72 69 6e 67 28 60 40   e.evalString(`@
4120: 61 20 78 3d 31 3b 20 40 62 20 78 3d 32 3b 20 40  a x=1; @b x=2; @
4130: 62 28 78 29 60 29 2c 20 6e 65 77 20 49 6e 74 56  b(x)`), new IntV
4140: 61 6c 75 65 28 42 69 67 49 6e 74 28 32 29 29 20  alue(BigInt(2)) 
4150: 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20  );...assert_eq( 
4160: 65 2e 65 76 61 6c 53 74 72 69 6e 67 28 60 6c 65  e.evalString(`le
4170: 74 20 78 3d 31 3b 20 6c 65 74 20 5f 20 3d 20 28  t x=1; let _ = (
4180: 40 61 20 78 3d 32 3b 32 29 3b 20 78 60 29 2c 20  @a x=2;2); x`), 
4190: 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 42 69 67  new IntValue(Big
41a0: 49 6e 74 28 31 29 29 20 29 3b 0d 0a 09 65 20 3d  Int(1)) );...e =
41b0: 20 6e 65 77 20 45 76 61 6c 75 61 74 6f 72 3b 0d   new Evaluator;.
41c0: 0a 09 61 73 73 65 72 74 5f 74 68 72 6f 77 21 54  ..assert_throw!T
41d0: 68 72 6f 77 61 62 6c 65 28 20 65 2e 65 76 61 6c  hrowable( e.eval
41e0: 53 74 72 69 6e 67 28 60 6c 65 74 20 78 3d 31 3b  String(`let x=1;
41f0: 20 6c 65 74 20 5f 20 3d 20 28 40 61 20 78 3d 32   let _ = (@a x=2
4200: 3b 32 29 3b 20 40 61 28 78 29 60 29 20 29 3b 0d  ;2); @a(x)`) );.
4210: 0a 7d 0d 0a 0d 0a 75 6e 69 74 74 65 73 74 0d 0a  .}....unittest..
4220: 7b 0d 0a 09 61 75 74 6f 20 65 20 3d 20 6e 65 77  {...auto e = new
4230: 20 45 76 61 6c 75 61 74 6f 72 3b 0d 0a 09 65 6e   Evaluator;...en
4240: 72 6f 6c 6c 52 75 6e 74 69 6d 65 4c 69 62 72 61  rollRuntimeLibra
4250: 72 79 28 65 29 3b 0d 0a 09 61 73 73 65 72 74 5f  ry(e);...assert_
4260: 65 71 28 20 65 2e 65 76 61 6c 53 74 72 69 6e 67  eq( e.evalString
4270: 28 60 0d 0a 09 09 40 40 73 28 78 29 7b 78 7d 3b  (`....@@s(x){x};
4280: 0d 0a 09 09 40 73 20 22 2b 22 20 3d 20 66 75 6e  ....@s "+" = fun
4290: 28 78 2c 20 79 29 20 7b 40 76 61 6c 75 65 28 0d  (x, y) {@value(.
42a0: 0a 09 09 09 40 73 28 78 29 20 2d 20 40 73 28 79  ....@s(x) - @s(y
42b0: 29 0d 0a 09 09 29 7d 3b 0d 0a 09 09 40 73 28 31  )....)};....@s(1
42c0: 20 2b 20 32 29 0d 0a 09 60 29 2c 20 6e 65 77 20   + 2)...`), new 
42d0: 49 6e 74 56 61 6c 75 65 28 42 69 67 49 6e 74 28  IntValue(BigInt(
42e0: 2d 31 29 29 20 29 3b 0d 0a 7d 0d 0a 0d 0a 75 6e  -1)) );..}....un
42f0: 69 74 74 65 73 74 0d 0a 7b 0d 0a 09 61 75 74 6f  ittest..{...auto
4300: 20 65 20 3d 20 6e 65 77 20 45 76 61 6c 75 61 74   e = new Evaluat
4310: 6f 72 3b 0d 0a 09 65 6e 72 6f 6c 6c 52 75 6e 74  or;...enrollRunt
4320: 69 6d 65 4c 69 62 72 61 72 79 28 65 29 3b 0d 0a  imeLibrary(e);..
4330: 09 61 73 73 65 72 74 5f 65 71 28 20 65 2e 65 76  .assert_eq( e.ev
4340: 61 6c 53 74 72 69 6e 67 28 60 0d 0a 40 40 33 28  alString(`..@@3(
4350: 78 29 7b 78 7d 3b 0d 0a 64 65 66 20 69 6e 63 72  x){x};..def incr
4360: 28 78 29 20 7b 20 78 2b 31 20 7d 3b 0d 0a 40 20  (x) { x+1 };..@ 
4370: 33 20 69 6e 63 72 28 78 29 20 7b 40 76 61 6c 75  3 incr(x) {@valu
4380: 65 28 20 69 66 20 40 20 33 28 78 29 2b 31 3c 20  e( if @ 3(x)+1< 
4390: 33 20 74 68 65 6e 20 40 33 28 78 29 2b 31 20 65  3 then @3(x)+1 e
43a0: 6c 73 65 20 30 20 29 7d 3b 0d 0a 64 65 66 20 66  lse 0 )};..def f
43b0: 62 28 6e 20 40 76 61 6c 75 65 20 40 33 29 20 7b  b(n @value @3) {
43c0: 20 40 33 28 6e 29 20 7d 3b 0d 0a 66 62 28 69 6e   @3(n) };..fb(in
43d0: 63 72 28 69 6e 63 72 28 69 6e 63 72 28 30 29 29  cr(incr(incr(0))
43e0: 29 29 0d 0a 09 60 29 2c 20 6e 65 77 20 49 6e 74  ))...`), new Int
43f0: 56 61 6c 75 65 28 42 69 67 49 6e 74 28 30 29 29  Value(BigInt(0))
4400: 20 29 3b 0d 0a 7d 0d 0a 0d 0a 75 6e 69 74 74 65   );..}....unitte
4410: 73 74 0d 0a 7b 0d 0a 09 61 75 74 6f 20 65 20 3d  st..{...auto e =
4420: 20 6e 65 77 20 45 76 61 6c 75 61 74 6f 72 3b 0d   new Evaluator;.
4430: 0a 09 65 6e 72 6f 6c 6c 52 75 6e 74 69 6d 65 4c  ..enrollRuntimeL
4440: 69 62 72 61 72 79 28 65 29 3b 0d 0a 09 61 73 73  ibrary(e);...ass
4450: 65 72 74 5f 6e 6f 74 68 72 6f 77 28 20 65 2e 65  ert_nothrow( e.e
4460: 76 61 6c 53 74 72 69 6e 67 28 60 0d 0a 40 6d 61  valString(`..@ma
4470: 63 72 6f 20 74 77 69 63 65 28 78 29 20 7b 20 78  cro twice(x) { x
4480: 3b 20 78 20 7d 3b 0d 0a 64 65 66 20 6d 61 69 6e  ; x };..def main
4490: 28 29 20 7b 20 74 77 69 63 65 28 31 29 20 7d 3b  () { twice(1) };
44a0: 0d 0a 6d 61 69 6e 28 29 0d 0a 09 60 29 20 29 3b  ..main()...`) );
44b0: 0d 0a 7d 0d 0a 75 6e 69 74 74 65 73 74 0d 0a 7b  ..}..unittest..{
44c0: 0d 0a 09 61 75 74 6f 20 65 20 3d 20 6e 65 77 20  ...auto e = new 
44d0: 45 76 61 6c 75 61 74 6f 72 3b 0d 0a 09 65 6e 72  Evaluator;...enr
44e0: 6f 6c 6c 52 75 6e 74 69 6d 65 4c 69 62 72 61 72  ollRuntimeLibrar
44f0: 79 28 65 29 3b 0d 0a 09 61 73 73 65 72 74 5f 74  y(e);...assert_t
4500: 68 72 6f 77 21 52 75 6e 74 69 6d 65 45 78 63 65  hrow!RuntimeExce
4510: 70 74 69 6f 6e 28 20 65 2e 65 76 61 6c 53 74 72  ption( e.evalStr
4520: 69 6e 67 28 60 63 61 73 65 20 31 60 29 20 29 3b  ing(`case 1`) );
4530: 0d 0a 09 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f  ...assert_nothro
4540: 77 28 20 65 2e 65 76 61 6c 53 74 72 69 6e 67 28  w( e.evalString(
4550: 60 63 61 73 65 20 31 20 77 68 65 6e 20 31 3a 20  `case 1 when 1: 
4560: 32 60 29 20 29 3b 0d 0a 0d 0a 09 2f 2f 20 74 68  2`) );.....// th
4570: 69 73 20 69 73 20 61 20 73 68 6f 72 74 68 61 6e  is is a shorthan
4580: 64 20 66 6f 72 0d 0a 09 2f 2f 20 20 20 40 6d 61  d for...//   @ma
4590: 63 72 6f 20 78 20 3d 20 66 75 6e 28 29 7b 7d 20  cro x = fun(){} 
45a0: 69 6e 20 40 6d 61 63 72 6f 28 78 29 0d 0a 09 2f  in @macro(x).../
45b0: 2f 20 73 6f 20 69 74 20 69 73 20 6f 6b 20 74 6f  / so it is ok to
45c0: 20 66 61 69 6c 2c 20 62 75 74 20 69 74 20 69 73   fail, but it is
45d0: 20 72 65 61 6c 6c 79 20 69 6e 63 6f 76 65 6e 69   really incoveni
45e0: 65 6e 74 20 6f 6e 20 52 45 50 4c 0d 0a 09 61 73  ent on REPL...as
45f0: 73 65 72 74 5f 6e 6f 74 68 72 6f 77 28 20 65 2e  sert_nothrow( e.
4600: 65 76 61 6c 53 74 72 69 6e 67 28 60 40 6d 61 63  evalString(`@mac
4610: 72 6f 20 78 3d 66 75 6e 28 29 7b 7d 60 29 20 29  ro x=fun(){}`) )
4620: 3b 0d 0a 7d 0d 0a 0d 0a 75 6e 69 74 74 65 73 74  ;..}....unittest
4630: 0d 0a 7b 0d 0a 09 61 75 74 6f 20 65 20 3d 20 6e  ..{...auto e = n
4640: 65 77 20 45 76 61 6c 75 61 74 6f 72 3b 0d 0a 09  ew Evaluator;...
4650: 65 6e 72 6f 6c 6c 52 75 6e 74 69 6d 65 4c 69 62  enrollRuntimeLib
4660: 72 61 72 79 28 65 29 3b 0d 0a 09 61 73 73 65 72  rary(e);...asser
4670: 74 5f 74 68 72 6f 77 21 52 75 6e 74 69 6d 65 45  t_throw!RuntimeE
4680: 78 63 65 70 74 69 6f 6e 28 20 65 2e 65 76 61 6c  xception( e.eval
4690: 53 74 72 69 6e 67 28 60 2e 2e 2e 60 29 20 29 3b  String(`...`) );
46a0: 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20 65 2e  ...assert_eq( e.
46b0: 65 76 61 6c 53 74 72 69 6e 67 28 60 40 40 66 6f  evalString(`@@fo
46c0: 6f 28 78 29 7b 78 7d 3b 20 40 66 6f 6f 28 2e 2e  o(x){x}; @foo(..
46d0: 2e 29 60 29 2c 20 6e 65 77 20 42 6f 74 74 6f 6d  .)`), new Bottom
46e0: 56 61 6c 75 65 20 29 3b 0d 0a 7d 0d 0a           Value );..}..