Hex Artifact Content
Not logged in

Artifact 9460508c714f0a1b06b1ec6fe740029c6cbecc1a:


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 69 6e 69 74 2c 20  is.eval(e.init, 
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 69 6e 69 74 2c  unMacro(le.init,
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 72 65 74 75 72 6e  D());.....return
1f60: 20 74 68 69 73 2e 6c 69 66 74 28 63 74 78 2e 67   this.lift(ctx.g
1f70: 65 74 28 65 2e 6e 61 6d 65 2c 20 56 61 6c 75 65  et(e.name, Value
1f80: 4c 61 79 65 72 2c 20 65 2e 70 6f 73 29 2c 20 63  Layer, e.pos), c
1f90: 74 78 2c 20 65 2e 70 6f 73 29 3b 0d 0a 09 09 7d  tx, e.pos);....}
1fa0: 0d 0a 09 7d 0d 0a 0d 0a 09 2f 2f 20 4d 61 63 72  ...}.....// Macr
1fb0: 6f 20 6c 61 79 65 72 2e 20 46 6f 72 20 6f 70 74  o layer. For opt
1fc0: 69 6d 69 7a 61 74 69 6f 6e 2c 20 69 66 20 41 53  imization, if AS
1fd0: 54 20 64 69 64 6e 27 74 20 63 68 61 6e 67 65 20  T didn't change 
1fe0: 69 74 20 72 65 74 75 72 6e 73 20 6e 75 6c 6c 0d  it returns null.
1ff0: 0a 09 63 6c 61 73 73 20 4d 61 63 72 6f 4c 61 79  ..class MacroLay
2000: 65 72 45 76 61 6c 20 3a 20 4c 61 79 65 72 45 76  erEval : LayerEv
2010: 61 6c 0d 0a 09 7b 0d 0a 09 09 6f 76 65 72 72 69  al...{....overri
2020: 64 65 20 4c 61 79 65 72 20 6c 61 79 65 72 49 44  de Layer layerID
2030: 28 29 0d 0a 09 09 7b 0d 0a 09 09 09 72 65 74 75  ()....{.....retu
2040: 72 6e 20 4d 61 63 72 6f 4c 61 79 65 72 3b 0d 0a  rn MacroLayer;..
2050: 09 09 7d 0d 0a 09 09 6f 76 65 72 72 69 64 65 20  ..}....override 
2060: 56 61 6c 75 65 20 65 76 61 6c 54 6f 4e 6f 6e 4e  Value evalToNonN
2070: 75 6c 6c 28 20 41 53 54 20 65 2c 20 54 61 62 6c  ull( AST e, Tabl
2080: 65 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d  e ctx, bool ctxM
2090: 6f 64 20 3d 20 43 61 73 63 61 64 65 43 74 78 20  od = CascadeCtx 
20a0: 29 0d 0a 09 09 7b 0d 0a 09 09 09 56 61 6c 75 65  )....{.....Value
20b0: 20 76 20 3d 20 74 68 69 73 2e 65 76 61 6c 28 65   v = this.eval(e
20c0: 2c 20 63 74 78 2c 20 63 74 78 4d 6f 64 29 3b 0d  , ctx, ctxMod);.
20d0: 0a 09 09 09 72 65 74 75 72 6e 20 76 20 69 73 20  ....return v is 
20e0: 6e 75 6c 6c 20 3f 20 61 73 74 32 74 61 62 6c 65  null ? ast2table
20f0: 28 65 29 20 3a 20 76 3b 0d 0a 09 09 7d 0d 0a 09  (e) : v;....}...
2100: 09 6f 76 65 72 72 69 64 65 20 56 61 6c 75 65 20  .override Value 
2110: 65 76 61 6c 5f 28 20 44 69 65 20 65 2c 20 54 61  eval_( Die e, Ta
2120: 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74  ble ctx, bool ct
2130: 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d 0a 09 09 09  xMod )....{.....
2140: 72 65 74 75 72 6e 20 6e 75 6c 6c 3b 0d 0a 09 09  return null;....
2150: 7d 0d 0a 09 09 6f 76 65 72 72 69 64 65 20 56 61  }....override Va
2160: 6c 75 65 20 65 76 61 6c 5f 28 20 53 74 72 20 65  lue eval_( Str e
2170: 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f  , Table ctx, boo
2180: 6c 20 63 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d  l ctxMod )....{.
2190: 0a 09 09 09 72 65 74 75 72 6e 20 6e 75 6c 6c 3b  ....return null;
21a0: 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65 72 72 69 64  ....}....overrid
21b0: 65 20 56 61 6c 75 65 20 65 76 61 6c 5f 28 20 49  e Value eval_( I
21c0: 6e 74 20 65 2c 20 54 61 62 6c 65 20 63 74 78 2c  nt e, Table ctx,
21d0: 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29 0d 0a   bool ctxMod )..
21e0: 09 09 7b 0d 0a 09 09 09 72 65 74 75 72 6e 20 6e  ..{.....return n
21f0: 75 6c 6c 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65  ull;....}....ove
2200: 72 72 69 64 65 20 56 61 6c 75 65 20 65 76 61 6c  rride Value eval
2210: 5f 28 20 56 61 72 20 65 2c 20 54 61 62 6c 65 20  _( Var e, Table 
2220: 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64  ctx, bool ctxMod
2230: 20 29 0d 0a 09 09 7b 0d 0a 09 09 09 69 66 28 20   )....{.....if( 
2240: 63 74 78 2e 68 61 73 28 65 2e 6e 61 6d 65 2c 20  ctx.has(e.name, 
2250: 6c 61 79 65 72 49 44 28 29 29 20 29 0d 0a 09 09  layerID()) )....
2260: 09 09 72 65 74 75 72 6e 20 63 74 78 2e 67 65 74  ..return ctx.get
2270: 28 65 2e 6e 61 6d 65 2c 20 6c 61 79 65 72 49 44  (e.name, layerID
2280: 28 29 2c 20 65 2e 70 6f 73 29 3b 0d 0a 09 09 09  (), e.pos);.....
2290: 72 65 74 75 72 6e 20 6e 75 6c 6c 3b 0d 0a 09 09  return null;....
22a0: 7d 0d 0a 09 09 6f 76 65 72 72 69 64 65 20 56 61  }....override Va
22b0: 6c 75 65 20 65 76 61 6c 5f 28 20 4c 61 79 20 65  lue eval_( Lay e
22c0: 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f  , Table ctx, boo
22d0: 6c 20 63 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d  l ctxMod )....{.
22e0: 0a 09 09 09 72 65 74 75 72 6e 20 67 65 74 4c 61  ....return getLa
22f0: 79 65 72 45 76 61 6c 28 65 2e 6c 61 79 65 72 29  yerEval(e.layer)
2300: 2e 65 76 61 6c 28 65 2e 65 78 70 72 2c 63 74 78  .eval(e.expr,ctx
2310: 29 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65 72 72  );....}....overr
2320: 69 64 65 20 56 61 6c 75 65 20 65 76 61 6c 5f 28  ide Value eval_(
2330: 20 4c 65 74 20 65 2c 20 54 61 62 6c 65 20 63 74   Let e, Table ct
2340: 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29  x, bool ctxMod )
2350: 0d 0a 09 09 7b 0d 0a 09 09 09 69 66 28 20 21 63  ....{.....if( !c
2360: 74 78 4d 6f 64 20 29 0d 0a 09 09 09 09 63 74 78  txMod )......ctx
2370: 20 3d 20 6e 65 77 20 54 61 62 6c 65 28 63 74 78   = new Table(ctx
2380: 2c 20 54 61 62 6c 65 2e 4b 69 6e 64 2e 4e 6f 74  , Table.Kind.Not
2390: 50 72 6f 70 61 67 61 74 65 53 65 74 29 3b 0d 0a  PropagateSet);..
23a0: 0d 0a 09 09 09 56 61 6c 75 65 20 61 69 20 3d 20  .....Value ai = 
23b0: 74 68 69 73 2e 65 76 61 6c 28 65 2e 69 6e 69 74  this.eval(e.init
23c0: 2c 20 63 74 78 29 3b 0d 0a 09 09 09 63 74 78 2e  , ctx);.....ctx.
23d0: 73 65 74 28 65 2e 6e 61 6d 65 2c 20 4e 6f 6f 70  set(e.name, Noop
23e0: 4c 61 79 65 72 2c 20 6e 75 6c 6c 29 3b 0d 0a 09  Layer, null);...
23f0: 09 09 56 61 6c 75 65 20 61 65 20 3d 20 74 68 69  ..Value ae = thi
2400: 73 2e 65 76 61 6c 28 65 2e 65 78 70 72 2c 20 63  s.eval(e.expr, c
2410: 74 78 2c 20 4f 76 65 72 77 72 69 74 65 43 74 78  tx, OverwriteCtx
2420: 29 3b 0d 0a 0d 0a 09 09 09 69 66 28 20 61 69 20  );.......if( ai 
2430: 69 73 20 6e 75 6c 6c 20 26 26 20 61 65 20 69 73  is null && ae is
2440: 20 6e 75 6c 6c 20 29 20 72 65 74 75 72 6e 20 6e   null ) return n
2450: 75 6c 6c 3b 0d 0a 09 09 09 69 66 28 20 61 69 20  ull;.....if( ai 
2460: 69 73 20 6e 75 6c 6c 20 29 20 61 69 20 3d 20 61  is null ) ai = a
2470: 73 74 32 74 61 62 6c 65 28 65 2e 69 6e 69 74 29  st2table(e.init)
2480: 3b 0d 0a 09 09 09 69 66 28 20 61 65 20 69 73 20  ;.....if( ae is 
2490: 6e 75 6c 6c 20 29 20 61 65 20 3d 20 61 73 74 32  null ) ae = ast2
24a0: 74 61 62 6c 65 28 65 2e 65 78 70 72 29 3b 0d 0a  table(e.expr);..
24b0: 0d 0a 09 09 09 72 65 74 75 72 6e 20 61 73 74 32  .....return ast2
24c0: 74 61 62 6c 65 28 65 2c 20 64 65 6c 65 67 61 74  table(e, delegat
24d0: 65 20 56 61 6c 75 65 20 28 41 53 54 20 5f 29 7b  e Value (AST _){
24e0: 0d 0a 09 09 09 09 69 66 28 5f 20 69 73 20 65 2e  ......if(_ is e.
24f0: 69 6e 69 74 29 20 7b 20 72 65 74 75 72 6e 20 61  init) { return a
2500: 69 3b 20 7d 0d 0a 09 09 09 09 69 66 28 5f 20 69  i; }......if(_ i
2510: 73 20 65 2e 65 78 70 72 29 20 7b 20 72 65 74 75  s e.expr) { retu
2520: 72 6e 20 61 65 3b 20 7d 0d 0a 09 09 09 09 61 73  rn ae; }......as
2530: 73 65 72 74 28 66 61 6c 73 65 29 3b 0d 0a 09 09  sert(false);....
2540: 09 7d 29 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65  .});....}....ove
2550: 72 72 69 64 65 20 56 61 6c 75 65 20 65 76 61 6c  rride Value eval
2560: 5f 28 20 41 70 70 20 65 2c 20 54 61 62 6c 65 20  _( App e, Table 
2570: 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64  ctx, bool ctxMod
2580: 20 29 0d 0a 09 09 7b 0d 0a 09 09 09 56 61 6c 75   )....{.....Valu
2590: 65 20 66 20 3d 20 74 68 69 73 2e 65 76 61 6c 28  e f = this.eval(
25a0: 20 65 2e 66 75 6e 2c 20 63 74 78 20 29 3b 0d 0a   e.fun, ctx );..
25b0: 09 09 09 69 66 28 61 75 74 6f 20 66 66 20 3d 20  ...if(auto ff = 
25c0: 63 61 73 74 28 46 75 6e 56 61 6c 75 65 29 66 29  cast(FunValue)f)
25d0: 0d 0a 09 09 09 09 72 65 74 75 72 6e 20 74 68 69  ......return thi
25e0: 73 2e 69 6e 76 6f 6b 65 46 75 6e 63 74 69 6f 6e  s.invokeFunction
25f0: 28 66 66 2c 20 65 2e 61 72 67 73 2c 20 63 74 78  (ff, e.args, ctx
2600: 2c 20 65 2e 70 6f 73 2c 20 67 65 74 4e 61 6d 65  , e.pos, getName
2610: 49 66 50 6f 73 73 69 62 6c 65 28 65 2e 66 75 6e  IfPossible(e.fun
2620: 29 29 3b 0d 0a 09 09 09 65 6c 73 65 0d 0a 09 09  ));.....else....
2630: 09 7b 0d 0a 09 09 09 09 62 6f 6f 6c 20 61 6c 6c  .{......bool all
2640: 4e 75 6c 6c 20 3d 20 28 66 20 69 73 20 6e 75 6c  Null = (f is nul
2650: 6c 29 3b 0d 0a 09 09 09 09 56 61 6c 75 65 5b 5d  l);......Value[]
2660: 20 76 61 73 3b 0d 0a 09 09 09 09 66 6f 72 65 61   vas;......forea
2670: 63 68 28 61 3b 20 65 2e 61 72 67 73 29 0d 0a 09  ch(a; e.args)...
2680: 09 09 09 7b 0d 0a 09 09 09 09 09 56 61 6c 75 65  ...{.......Value
2690: 20 76 61 20 3d 20 74 68 69 73 2e 65 76 61 6c 28   va = this.eval(
26a0: 61 2c 20 63 74 78 2c 20 43 61 73 63 61 64 65 43  a, ctx, CascadeC
26b0: 74 78 29 3b 0d 0a 09 09 09 09 09 69 66 28 76 61  tx);.......if(va
26c0: 20 21 69 73 20 6e 75 6c 6c 29 20 61 6c 6c 4e 75   !is null) allNu
26d0: 6c 6c 20 3d 20 66 61 6c 73 65 3b 0d 0a 09 09 09  ll = false;.....
26e0: 09 09 76 61 73 20 7e 3d 20 76 61 3b 0d 0a 09 09  ..vas ~= va;....
26f0: 09 09 7d 0d 0a 09 09 09 09 69 66 28 20 61 6c 6c  ..}......if( all
2700: 4e 75 6c 6c 20 29 0d 0a 09 09 09 09 09 72 65 74  Null ).......ret
2710: 75 72 6e 20 6e 75 6c 6c 3b 0d 0a 09 09 09 09 72  urn null;......r
2720: 65 74 75 72 6e 20 61 73 74 32 74 61 62 6c 65 28  eturn ast2table(
2730: 65 2c 20 64 65 6c 65 67 61 74 65 20 56 61 6c 75  e, delegate Valu
2740: 65 20 28 41 53 54 20 5f 29 7b 0d 0a 09 09 09 09  e (AST _){......
2750: 09 69 66 28 5f 20 69 73 20 65 2e 66 75 6e 29 20  .if(_ is e.fun) 
2760: 72 65 74 75 72 6e 20 28 66 20 69 73 20 6e 75 6c  return (f is nul
2770: 6c 20 3f 20 61 73 74 32 74 61 62 6c 65 28 65 2e  l ? ast2table(e.
2780: 66 75 6e 29 20 3a 20 66 29 3b 0d 0a 09 09 09 09  fun) : f);......
2790: 09 66 6f 72 65 61 63 68 28 69 2c 61 3b 20 65 2e  .foreach(i,a; e.
27a0: 61 72 67 73 29 20 69 66 28 5f 20 69 73 20 61 29  args) if(_ is a)
27b0: 20 72 65 74 75 72 6e 20 28 76 61 73 5b 69 5d 20   return (vas[i] 
27c0: 69 73 20 6e 75 6c 6c 20 3f 20 61 73 74 32 74 61  is null ? ast2ta
27d0: 62 6c 65 28 61 29 20 3a 20 76 61 73 5b 69 5d 29  ble(a) : vas[i])
27e0: 3b 0d 0a 09 09 09 09 09 61 73 73 65 72 74 28 66  ;.......assert(f
27f0: 61 6c 73 65 29 3b 0d 0a 09 09 09 09 7d 29 3b 0d  alse);......});.
2800: 0a 09 09 09 7d 0d 0a 09 09 7d 0d 0a 09 09 6f 76  ....}....}....ov
2810: 65 72 72 69 64 65 20 56 61 6c 75 65 20 65 76 61  erride Value eva
2820: 6c 5f 28 20 46 75 6e 20 65 2c 20 54 61 62 6c 65  l_( Fun e, Table
2830: 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f   ctx, bool ctxMo
2840: 64 20 29 0d 0a 09 09 7b 0d 0a 09 09 09 63 74 78  d )....{.....ctx
2850: 20 3d 20 6e 65 77 20 54 61 62 6c 65 28 63 74 78   = new Table(ctx
2860: 2c 20 54 61 62 6c 65 2e 4b 69 6e 64 2e 4e 6f 74  , Table.Kind.Not
2870: 50 72 6f 70 61 67 61 74 65 53 65 74 29 3b 0d 0a  PropagateSet);..
2880: 09 09 09 66 6f 72 65 61 63 68 28 70 3b 20 65 2e  ...foreach(p; e.
2890: 70 61 72 61 6d 73 29 0d 0a 09 09 09 09 63 74 78  params)......ctx
28a0: 2e 73 65 74 28 70 2e 6e 61 6d 65 2c 20 4e 6f 6f  .set(p.name, Noo
28b0: 70 4c 61 79 65 72 2c 20 6e 75 6c 6c 29 3b 0d 0a  pLayer, null);..
28c0: 09 09 09 56 61 6c 75 65 20 61 66 20 3d 20 74 68  ...Value af = th
28d0: 69 73 2e 65 76 61 6c 28 65 2e 66 75 6e 62 6f 64  is.eval(e.funbod
28e0: 79 2c 20 63 74 78 29 3b 0d 0a 09 09 09 69 66 28  y, ctx);.....if(
28f0: 20 61 66 20 69 73 20 6e 75 6c 6c 20 29 0d 0a 09   af is null )...
2900: 09 09 09 72 65 74 75 72 6e 20 6e 75 6c 6c 3b 0d  ...return null;.
2910: 0a 09 09 09 72 65 74 75 72 6e 20 61 73 74 32 74  ....return ast2t
2920: 61 62 6c 65 28 65 2c 20 28 41 53 54 20 5f 29 7b  able(e, (AST _){
2930: 69 66 28 5f 20 69 73 20 65 2e 66 75 6e 62 6f 64  if(_ is e.funbod
2940: 79 29 72 65 74 75 72 6e 20 61 66 3b 20 61 73 73  y)return af; ass
2950: 65 72 74 28 66 61 6c 73 65 29 3b 7d 29 3b 0d 0a  ert(false);});..
2960: 09 09 7d 0d 0a 09 7d 0d 0a 0d 0a 09 2f 2f 2f 20  ..}...}...../// 
2970: 28 72 61 77 6d 61 63 72 6f 29 20 6c 61 79 65 72  (rawmacro) layer
2980: 2e 20 61 6c 6d 6f 73 74 20 73 61 6d 65 20 61 73  . almost same as
2990: 20 40 6d 61 63 72 6f 2c 20 62 75 74 20 74 68 65   @macro, but the
29a0: 20 4c 61 79 65 72 20 65 78 70 72 65 73 73 69 6f   Layer expressio
29b0: 6e 20 62 65 63 6f 6d 65 73 20 41 53 54 2c 20 74  n becomes AST, t
29c0: 6f 6f 2e 0d 0a 09 63 6c 61 73 73 20 52 61 77 4d  oo....class RawM
29d0: 61 63 72 6f 4c 61 79 65 72 45 76 61 6c 20 3a 20  acroLayerEval : 
29e0: 4d 61 63 72 6f 4c 61 79 65 72 45 76 61 6c 0d 0a  MacroLayerEval..
29f0: 09 7b 0d 0a 09 09 6f 76 65 72 72 69 64 65 20 56  .{....override V
2a00: 61 6c 75 65 20 65 76 61 6c 5f 28 20 4c 61 79 20  alue eval_( Lay 
2a10: 65 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 62 6f  e, Table ctx, bo
2a20: 6f 6c 20 63 74 78 4d 6f 64 20 29 0d 0a 09 09 7b  ol ctxMod )....{
2a30: 0d 0a 09 09 09 56 61 6c 75 65 20 61 65 20 3d 20  .....Value ae = 
2a40: 74 68 69 73 2e 65 76 61 6c 28 65 2e 65 78 70 72  this.eval(e.expr
2a50: 2c 20 63 74 78 29 3b 0d 0a 09 09 09 72 65 74 75  , ctx);.....retu
2a60: 72 6e 20 61 65 20 69 73 20 6e 75 6c 6c 20 3f 20  rn ae is null ? 
2a70: 6e 75 6c 6c 0d 0a 09 09 09 20 20 20 20 20 20 20  null.....       
2a80: 3a 20 61 73 74 32 74 61 62 6c 65 28 65 2c 20 64  : ast2table(e, d
2a90: 65 6c 65 67 61 74 65 20 56 61 6c 75 65 20 28 41  elegate Value (A
2aa0: 53 54 20 5f 29 7b 69 66 28 5f 20 69 73 20 65 2e  ST _){if(_ is e.
2ab0: 65 78 70 72 29 72 65 74 75 72 6e 20 61 65 3b 20  expr)return ae; 
2ac0: 61 73 73 65 72 74 28 66 61 6c 73 65 29 3b 7d 29  assert(false);})
2ad0: 3b 0d 0a 09 09 7d 0d 0a 09 7d 0d 0a 0d 0a 70 72  ;....}...}....pr
2ae0: 69 76 61 74 65 3a 20 2f 2f 20 73 68 6f 72 74 20  ivate: // short 
2af0: 75 74 69 6c 73 0d 0a 0d 0a 09 73 74 72 69 6e 67  utils.....string
2b00: 20 67 65 74 4e 61 6d 65 49 66 50 6f 73 73 69 62   getNameIfPossib
2b10: 6c 65 28 41 53 54 20 65 29 0d 0a 09 7b 0d 0a 09  le(AST e)...{...
2b20: 09 69 66 28 61 75 74 6f 20 76 20 3d 20 63 61 73  .if(auto v = cas
2b30: 74 28 56 61 72 29 65 29 0d 0a 09 09 09 72 65 74  t(Var)e).....ret
2b40: 75 72 6e 20 76 2e 6e 61 6d 65 3b 0d 0a 09 09 72  urn v.name;....r
2b50: 65 74 75 72 6e 20 22 22 3b 0d 0a 09 7d 0d 0a 0d  eturn "";...}...
2b60: 0a 09 41 53 54 20 72 75 6e 4d 61 63 72 6f 28 41  ..AST runMacro(A
2b70: 53 54 20 65 2c 20 54 61 62 6c 65 20 63 74 78 2c  ST e, Table ctx,
2b80: 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 3d 4c 61 79   bool ctxMod=Lay
2b90: 65 72 45 76 61 6c 2e 43 61 73 63 61 64 65 43 74  erEval.CascadeCt
2ba0: 78 29 0d 0a 09 7b 0d 0a 09 09 56 61 6c 75 65 20  x)...{....Value 
2bb0: 76 20 3d 20 67 65 74 4c 61 79 65 72 45 76 61 6c  v = getLayerEval
2bc0: 28 52 61 77 4d 61 63 72 6f 4c 61 79 65 72 29 2e  (RawMacroLayer).
2bd0: 65 76 61 6c 28 65 2c 20 63 74 78 2c 20 63 74 78  eval(e, ctx, ctx
2be0: 4d 6f 64 29 3b 0d 0a 09 09 72 65 74 75 72 6e 20  Mod);....return 
2bf0: 28 76 20 69 73 20 6e 75 6c 6c 20 3f 20 65 20 3a  (v is null ? e :
2c00: 20 70 6f 6c 65 6d 79 32 64 21 28 41 53 54 29 28   polemy2d!(AST)(
2c10: 76 2c 20 65 2e 70 6f 73 29 29 3b 0d 0a 09 7d 0d  v, e.pos));...}.
2c20: 0a 0d 0a 70 72 69 76 61 74 65 3a 0d 0a 09 56 61  ...private:...Va
2c30: 6c 75 65 20 63 72 65 61 74 65 4e 65 77 46 75 6e  lue createNewFun
2c40: 63 74 69 6f 6e 28 46 75 6e 20 65 2c 20 54 61 62  ction(Fun e, Tab
2c50: 6c 65 20 63 74 78 29 0d 0a 09 7b 0d 0a 09 09 63  le ctx)...{....c
2c60: 6c 61 73 73 20 55 73 65 72 44 65 66 69 6e 65 64  lass UserDefined
2c70: 46 75 6e 56 61 6c 75 65 20 3a 20 46 75 6e 56 61  FunValue : FunVa
2c80: 6c 75 65 0d 0a 09 09 7b 0d 0a 09 09 09 46 75 6e  lue....{.....Fun
2c90: 20 20 20 61 73 74 3b 0d 0a 09 09 09 54 61 62 6c     ast;.....Tabl
2ca0: 65 20 64 65 66 43 74 78 3b 0d 0a 09 09 09 6f 76  e defCtx;.....ov
2cb0: 65 72 72 69 64 65 20 63 6f 6e 73 74 28 50 61 72  erride const(Par
2cc0: 61 6d 65 74 65 72 5b 5d 29 20 70 61 72 61 6d 73  ameter[]) params
2cd0: 28 29 20 7b 20 72 65 74 75 72 6e 20 61 73 74 2e  () { return ast.
2ce0: 70 61 72 61 6d 73 3b 20 7d 0d 0a 09 09 09 6f 76  params; }.....ov
2cf0: 65 72 72 69 64 65 20 54 61 62 6c 65 20 64 65 66  erride Table def
2d00: 69 6e 69 74 69 6f 6e 43 6f 6e 74 65 78 74 28 29  initionContext()
2d10: 20 20 20 7b 20 72 65 74 75 72 6e 20 64 65 66 43     { return defC
2d20: 74 78 3b 20 7d 0d 0a 0d 0a 09 09 09 74 68 69 73  tx; }.......this
2d30: 28 46 75 6e 20 61 73 74 2c 20 54 61 62 6c 65 20  (Fun ast, Table 
2d40: 64 65 66 43 74 78 29 20 7b 20 74 68 69 73 2e 61  defCtx) { this.a
2d50: 73 74 3d 61 73 74 3b 20 74 68 69 73 2e 64 65 66  st=ast; this.def
2d60: 43 74 78 3d 64 65 66 43 74 78 3b 20 7d 0d 0a 09  Ctx=defCtx; }...
2d70: 09 09 6f 76 65 72 72 69 64 65 20 73 74 72 69 6e  ..override strin
2d80: 67 20 74 6f 53 74 72 69 6e 67 28 29 20 63 6f 6e  g toString() con
2d90: 73 74 0d 0a 09 09 09 09 7b 20 72 65 74 75 72 6e  st......{ return
2da0: 20 73 70 72 69 6e 74 66 21 22 28 66 75 6e 63 74   sprintf!"(funct
2db0: 69 6f 6e 3a 25 78 3a 25 78 29 22 28 63 61 73 74  ion:%x:%x)"(cast
2dc0: 28 76 6f 69 64 2a 29 61 73 74 2c 20 63 61 73 74  (void*)ast, cast
2dd0: 28 76 6f 69 64 2a 29 64 65 66 43 74 78 29 3b 20  (void*)defCtx); 
2de0: 7d 0d 0a 09 09 09 6f 76 65 72 72 69 64 65 20 69  }.....override i
2df0: 6e 74 20 6f 70 43 6d 70 28 4f 62 6a 65 63 74 20  nt opCmp(Object 
2e00: 72 68 73 29 20 7b 0d 0a 09 09 09 09 69 66 28 61  rhs) {......if(a
2e10: 75 74 6f 20 72 20 3d 20 63 61 73 74 28 55 73 65  uto r = cast(Use
2e20: 72 44 65 66 69 6e 65 64 46 75 6e 56 61 6c 75 65  rDefinedFunValue
2e30: 29 72 68 73 29 20 7b 0d 0a 09 09 09 09 09 69 66  )rhs) {.......if
2e40: 28 61 75 74 6f 20 63 20 3d 20 74 79 70 65 69 64  (auto c = typeid
2e50: 28 76 6f 69 64 2a 29 2e 63 6f 6d 70 61 72 65 28  (void*).compare(
2e60: 63 61 73 74 28 76 6f 69 64 2a 29 61 73 74 2c 20  cast(void*)ast, 
2e70: 63 61 73 74 28 76 6f 69 64 2a 29 72 2e 61 73 74  cast(void*)r.ast
2e80: 29 29 0d 0a 09 09 09 09 09 09 72 65 74 75 72 6e  ))........return
2e90: 20 63 3b 0d 0a 09 09 09 09 09 69 66 28 61 75 74   c;.......if(aut
2ea0: 6f 20 63 20 3d 20 74 79 70 65 69 64 28 76 6f 69  o c = typeid(voi
2eb0: 64 2a 29 2e 63 6f 6d 70 61 72 65 28 63 61 73 74  d*).compare(cast
2ec0: 28 76 6f 69 64 2a 29 64 65 66 43 74 78 2c 20 63  (void*)defCtx, c
2ed0: 61 73 74 28 76 6f 69 64 2a 29 72 2e 64 65 66 43  ast(void*)r.defC
2ee0: 74 78 29 29 0d 0a 09 09 09 09 09 09 72 65 74 75  tx))........retu
2ef0: 72 6e 20 63 3b 0d 0a 09 09 09 09 09 72 65 74 75  rn c;.......retu
2f00: 72 6e 20 30 3b 2f 2f 20 5b 54 4f 44 4f 5d 20 61  rn 0;// [TODO] a
2f10: 76 6f 69 64 20 75 73 69 6e 67 20 70 6f 69 6e 74  void using point
2f20: 65 72 20 76 61 6c 75 65 2e 2e 2e 0d 0a 09 09 09  er value........
2f30: 09 7d 0d 0a 09 09 09 09 69 66 28 61 75 74 6f 20  .}......if(auto 
2f40: 72 20 3d 20 63 61 73 74 28 56 61 6c 75 65 29 72  r = cast(Value)r
2f50: 68 73 29 20 72 65 74 75 72 6e 20 74 79 70 65 69  hs) return typei
2f60: 64 28 74 68 69 73 29 2e 6f 70 43 6d 70 28 74 79  d(this).opCmp(ty
2f70: 70 65 69 64 28 72 29 29 3b 0d 0a 09 09 09 09 74  peid(r));......t
2f80: 68 72 6f 77 20 67 65 6e 65 78 21 52 75 6e 74 69  hrow genex!Runti
2f90: 6d 65 45 78 63 65 70 74 69 6f 6e 28 22 63 6f 6d  meException("com
2fa0: 70 61 72 69 73 6f 6e 20 77 69 74 68 20 76 61 6c  parison with val
2fb0: 75 65 20 61 6e 64 20 73 6f 6d 65 74 68 69 6e 67  ue and something
2fc0: 20 6f 74 68 65 72 22 29 3b 0d 0a 09 09 09 7d 0d   other");.....}.
2fd0: 0a 09 09 09 6f 76 65 72 72 69 64 65 20 68 61 73  ....override has
2fe0: 68 5f 74 20 74 6f 48 61 73 68 28 29 20 7b 0d 0a  h_t toHash() {..
2ff0: 09 09 09 09 72 65 74 75 72 6e 20 28 63 61 73 74  ....return (cast
3000: 28 68 61 73 68 5f 74 29 63 61 73 74 28 76 6f 69  (hash_t)cast(voi
3010: 64 2a 29 61 73 74 29 20 2b 20 28 63 61 73 74 28  d*)ast) + (cast(
3020: 68 61 73 68 5f 74 29 63 61 73 74 28 76 6f 69 64  hash_t)cast(void
3030: 2a 29 64 65 66 43 74 78 29 3b 0d 0a 09 09 09 7d  *)defCtx);.....}
3040: 0d 0a 0d 0a 09 09 09 41 53 54 5b 76 6f 69 64 2a  .......AST[void*
3050: 5d 20 6d 61 6e 64 65 43 61 63 68 65 3b 0d 0a 09  ] mandeCache;...
3060: 09 09 73 74 61 74 69 63 20 63 6c 61 73 73 20 4d  ..static class M
3070: 65 6d 6f 6b 65 79 54 79 70 65 0d 0a 09 09 09 7b  emokeyType.....{
3080: 0d 0a 09 09 09 09 76 6f 69 64 2a 20 61 3b 20 4c  ......void* a; L
3090: 61 79 65 72 20 62 3b 20 54 61 62 6c 65 20 63 3b  ayer b; Table c;
30a0: 0d 0a 09 09 09 09 6d 69 78 69 6e 20 53 69 6d 70  ......mixin Simp
30b0: 6c 65 43 6c 61 73 73 3b 0d 0a 09 09 09 7d 0d 0a  leClass;.....}..
30c0: 09 09 09 73 74 61 74 69 63 20 54 75 70 6c 65 21  ...static Tuple!
30d0: 28 56 61 6c 75 65 2c 69 6e 74 29 5b 4d 65 6d 6f  (Value,int)[Memo
30e0: 6b 65 79 54 79 70 65 5d 20 6d 65 6d 6f 3b 0d 0a  keyType] memo;..
30f0: 0d 0a 09 09 09 6f 76 65 72 72 69 64 65 20 56 61  .....override Va
3100: 6c 75 65 20 69 6e 76 6f 6b 65 28 4c 61 79 65 72  lue invoke(Layer
3110: 20 6c 61 79 2c 20 54 61 62 6c 65 20 63 74 78 2c   lay, Table ctx,
3120: 20 4c 65 78 50 6f 73 69 74 69 6f 6e 20 70 6f 73   LexPosition pos
3130: 29 0d 0a 09 09 09 7b 0d 0a 09 09 09 09 61 75 74  ).....{......aut
3140: 6f 20 65 76 6c 61 79 20 3d 20 67 65 74 4c 61 79  o evlay = getLay
3150: 65 72 45 76 61 6c 28 6c 61 79 29 3b 0d 0a 09 09  erEval(lay);....
3160: 09 09 61 75 74 6f 20 6e 6f 6e 4d 65 6d 6f 69 7a  ..auto nonMemoiz
3170: 65 64 52 75 6e 20 3d 20 28 29 7b 20 72 65 74 75  edRun = (){ retu
3180: 72 6e 20 65 76 6c 61 79 2e 6d 61 63 72 6f 41 6e  rn evlay.macroAn
3190: 64 45 76 61 6c 28 61 73 74 2e 66 75 6e 62 6f 64  dEval(ast.funbod
31a0: 79 2c 20 63 74 78 29 3b 20 7d 3b 0d 0a 0d 0a 09  y, ctx); };.....
31b0: 09 09 09 69 66 28 20 21 69 73 55 73 65 72 44 65  ...if( !isUserDe
31c0: 66 69 6e 65 64 4c 61 79 65 72 28 6c 61 79 29 20  finedLayer(lay) 
31d0: 29 0d 0a 09 09 09 09 09 72 65 74 75 72 6e 20 6e  ).......return n
31e0: 6f 6e 4d 65 6d 6f 69 7a 65 64 52 75 6e 28 29 3b  onMemoizedRun();
31f0: 0d 0a 0d 0a 09 09 09 09 2f 2f 20 61 75 74 6f 6d  ........// autom
3200: 61 74 69 63 20 6d 65 6d 6f 69 7a 65 64 20 63 6f  atic memoized co
3210: 2d 72 65 63 75 72 73 69 76 65 20 65 78 65 63 75  -recursive execu
3220: 74 69 6f 6e 0d 0a 09 09 09 09 4d 65 6d 6f 6b 65  tion......Memoke
3230: 79 54 79 70 65 20 6d 65 6d 6f 6b 65 79 20 3d 20  yType memokey = 
3240: 6e 65 77 20 4d 65 6d 6f 6b 65 79 54 79 70 65 28  new MemokeyType(
3250: 63 61 73 74 28 76 6f 69 64 2a 29 61 73 74 2c 20  cast(void*)ast, 
3260: 6c 61 79 2c 20 63 74 78 29 3b 0d 0a 09 09 09 09  lay, ctx);......
3270: 69 66 28 61 75 74 6f 20 70 20 3d 20 6d 65 6d 6f  if(auto p = memo
3280: 6b 65 79 20 69 6e 20 6d 65 6d 6f 29 0d 0a 09 09  key in memo)....
3290: 09 09 7b 0d 0a 09 09 09 09 09 28 2a 70 29 5b 31  ..{.......(*p)[1
32a0: 5d 20 2b 2b 3b 0d 0a 09 09 09 09 09 72 65 74 75  ] ++;.......retu
32b0: 72 6e 20 28 2a 70 29 5b 30 5d 3b 0d 0a 09 09 09  rn (*p)[0];.....
32c0: 09 7d 0d 0a 09 09 09 09 65 6c 73 65 0d 0a 09 09  .}......else....
32d0: 09 09 09 6d 65 6d 6f 5b 6d 65 6d 6f 6b 65 79 5d  ...memo[memokey]
32e0: 20 3d 20 74 75 70 6c 65 28 65 76 6c 61 79 2e 6c   = tuple(evlay.l
32f0: 69 66 74 28 6e 65 77 20 42 6f 74 74 6f 6d 56 61  ift(new BottomVa
3300: 6c 75 65 2c 20 63 74 78 2c 20 70 6f 73 29 2c 20  lue, ctx, pos), 
3310: 30 29 3b 0d 0a 0d 0a 09 09 09 09 56 61 6c 75 65  0);........Value
3320: 20 72 20 3d 20 6e 6f 6e 4d 65 6d 6f 69 7a 65 64   r = nonMemoized
3330: 52 75 6e 28 29 3b 0d 0a 0d 0a 09 09 09 09 69 6e  Run();........in
3340: 74 20 74 6f 75 63 68 65 64 20 3d 20 6d 65 6d 6f  t touched = memo
3350: 5b 6d 65 6d 6f 6b 65 79 5d 5b 31 5d 3b 0d 0a 09  [memokey][1];...
3360: 09 09 09 6d 65 6d 6f 5b 6d 65 6d 6f 6b 65 79 5d  ...memo[memokey]
3370: 20 3d 20 74 75 70 6c 65 28 72 2c 20 31 32 33 34   = tuple(r, 1234
3380: 35 36 37 38 29 3b 0d 0a 09 09 09 09 72 65 74 75  5678);......retu
3390: 72 6e 20 72 3b 0d 0a 09 09 09 7d 0d 0a 09 09 7d  rn r;.....}....}
33a0: 0d 0a 09 09 72 65 74 75 72 6e 20 6e 65 77 20 55  ....return new U
33b0: 73 65 72 44 65 66 69 6e 65 64 46 75 6e 56 61 6c  serDefinedFunVal
33c0: 75 65 28 65 2c 63 74 78 29 3b 0d 0a 09 7d 0d 0a  ue(e,ctx);...}..
33d0: 0d 0a 70 75 62 6c 69 63 3a 0d 0a 09 2f 2f 2f 20  ..public:.../// 
33e0: 41 64 64 20 70 72 69 6d 69 74 69 76 65 20 66 75  Add primitive fu
33f0: 6e 63 74 69 6f 6e 20 74 6f 20 74 68 65 20 67 6c  nction to the gl
3400: 6f 62 61 6c 20 63 6f 6e 74 65 78 74 0d 0a 09 76  obal context...v
3410: 6f 69 64 20 61 64 64 50 72 69 6d 69 74 69 76 65  oid addPrimitive
3420: 28 52 2c 54 2e 2e 2e 29 28 73 74 72 69 6e 67 20  (R,T...)(string 
3430: 6e 61 6d 65 2c 20 4c 61 79 65 72 20 64 65 66 4c  name, Layer defL
3440: 61 79 2c 20 52 20 64 65 6c 65 67 61 74 65 20 28  ay, R delegate (
3450: 54 29 20 64 67 29 0d 0a 09 7b 0d 0a 09 09 63 6c  T) dg)...{....cl
3460: 61 73 73 20 4e 61 74 69 76 65 46 75 6e 56 61 6c  ass NativeFunVal
3470: 75 65 20 3a 20 46 75 6e 56 61 6c 75 65 0d 0a 09  ue : FunValue...
3480: 09 7b 0d 0a 09 09 09 6f 76 65 72 72 69 64 65 20  .{.....override 
3490: 63 6f 6e 73 74 28 50 61 72 61 6d 65 74 65 72 5b  const(Parameter[
34a0: 5d 29 20 70 61 72 61 6d 73 28 29 20 7b 20 72 65  ]) params() { re
34b0: 74 75 72 6e 20 70 61 72 61 6d 73 5f 64 61 74 61  turn params_data
34c0: 3b 20 7d 0d 0a 09 09 09 6f 76 65 72 72 69 64 65  ; }.....override
34d0: 20 54 61 62 6c 65 20 64 65 66 69 6e 69 74 69 6f   Table definitio
34e0: 6e 43 6f 6e 74 65 78 74 28 29 20 20 20 7b 20 72  nContext()   { r
34f0: 65 74 75 72 6e 20 74 68 65 43 6f 6e 74 65 78 74  eturn theContext
3500: 3b 20 7d 0d 0a 0d 0a 09 09 09 6f 76 65 72 72 69  ; }.......overri
3510: 64 65 20 73 74 72 69 6e 67 20 74 6f 53 74 72 69  de string toStri
3520: 6e 67 28 29 20 7b 20 72 65 74 75 72 6e 20 73 70  ng() { return sp
3530: 72 69 6e 74 66 21 22 28 6e 61 74 69 76 65 3a 25  rintf!"(native:%
3540: 78 29 22 28 64 67 2e 66 75 6e 63 70 74 72 29 3b  x)"(dg.funcptr);
3550: 20 7d 0d 0a 09 09 09 6f 76 65 72 72 69 64 65 20   }.....override 
3560: 69 6e 74 20 6f 70 43 6d 70 28 4f 62 6a 65 63 74  int opCmp(Object
3570: 20 72 68 73 29 20 7b 0d 0a 09 09 09 09 69 66 28   rhs) {......if(
3580: 61 75 74 6f 20 72 20 3d 20 63 61 73 74 28 4e 61  auto r = cast(Na
3590: 74 69 76 65 46 75 6e 56 61 6c 75 65 29 72 68 73  tiveFunValue)rhs
35a0: 29 20 72 65 74 75 72 6e 20 74 79 70 65 69 64 28  ) return typeid(
35b0: 74 79 70 65 6f 66 28 64 67 29 29 2e 63 6f 6d 70  typeof(dg)).comp
35c0: 61 72 65 28 26 64 67 2c 26 72 2e 64 67 29 3b 0d  are(&dg,&r.dg);.
35d0: 0a 09 09 09 09 69 66 28 61 75 74 6f 20 72 20 3d  .....if(auto r =
35e0: 20 63 61 73 74 28 56 61 6c 75 65 29 72 68 73 29   cast(Value)rhs)
35f0: 20 20 20 20 20 20 20 20 20 20 72 65 74 75 72 6e            return
3600: 20 74 79 70 65 69 64 28 74 68 69 73 29 2e 6f 70   typeid(this).op
3610: 43 6d 70 28 74 79 70 65 69 64 28 72 29 29 3b 0d  Cmp(typeid(r));.
3620: 0a 09 09 09 09 74 68 72 6f 77 20 67 65 6e 65 78  .....throw genex
3630: 21 52 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f  !RuntimeExceptio
3640: 6e 28 22 63 6f 6d 70 61 72 69 73 6f 6e 20 77 69  n("comparison wi
3650: 74 68 20 76 61 6c 75 65 20 61 6e 64 20 73 6f 6d  th value and som
3660: 65 74 68 69 6e 67 20 6f 74 68 65 72 22 29 3b 0d  ething other");.
3670: 0a 09 09 09 7d 0d 0a 09 09 09 6f 76 65 72 72 69  ....}.....overri
3680: 64 65 20 68 61 73 68 5f 74 20 74 6f 48 61 73 68  de hash_t toHash
3690: 28 29 20 63 6f 6e 73 74 20 7b 0d 0a 09 09 09 09  () const {......
36a0: 72 65 74 75 72 6e 20 74 79 70 65 69 64 28 64 67  return typeid(dg
36b0: 29 2e 67 65 74 48 61 73 68 28 26 64 67 29 3b 0d  ).getHash(&dg);.
36c0: 0a 09 09 09 7d 0d 0a 0d 0a 09 09 09 52 20 64 65  ....}.......R de
36d0: 6c 65 67 61 74 65 28 54 29 20 64 67 3b 0d 0a 09  legate(T) dg;...
36e0: 09 09 50 61 72 61 6d 65 74 65 72 5b 5d 20 70 61  ..Parameter[] pa
36f0: 72 61 6d 73 5f 64 61 74 61 3b 0d 0a 0d 0a 09 09  rams_data;......
3700: 09 74 68 69 73 28 52 20 64 65 6c 65 67 61 74 65  .this(R delegate
3710: 28 54 29 20 64 67 29 0d 0a 09 09 09 7b 0d 0a 09  (T) dg).....{...
3720: 09 09 09 74 68 69 73 2e 64 67 20 3d 20 64 67 3b  ...this.dg = dg;
3730: 0d 0a 09 09 09 09 66 6f 72 65 61 63 68 28 69 2c  ......foreach(i,
3740: 20 54 69 3b 20 54 29 0d 0a 09 09 09 09 09 70 61   Ti; T).......pa
3750: 72 61 6d 73 5f 64 61 74 61 20 7e 3d 20 6e 65 77  rams_data ~= new
3760: 20 50 61 72 61 6d 65 74 65 72 28 74 65 78 74 28   Parameter(text(
3770: 69 29 2c 20 5b 5d 29 3b 0d 0a 09 09 09 7d 0d 0a  i), []);.....}..
3780: 0d 0a 09 09 09 6f 76 65 72 72 69 64 65 20 56 61  .....override Va
3790: 6c 75 65 20 69 6e 76 6f 6b 65 28 4c 61 79 65 72  lue invoke(Layer
37a0: 20 6c 61 79 2c 20 54 61 62 6c 65 20 63 74 78 2c   lay, Table ctx,
37b0: 20 4c 65 78 50 6f 73 69 74 69 6f 6e 20 70 6f 73   LexPosition pos
37c0: 29 0d 0a 09 09 09 7b 0d 0a 09 09 09 09 69 66 28  ).....{......if(
37d0: 20 6c 61 79 20 21 3d 20 64 65 66 4c 61 79 20 29   lay != defLay )
37e0: 0d 0a 09 09 09 09 09 74 68 72 6f 77 20 67 65 6e  .......throw gen
37f0: 65 78 21 52 75 6e 74 69 6d 65 45 78 63 65 70 74  ex!RuntimeExcept
3800: 69 6f 6e 28 70 6f 73 2c 0d 0a 09 09 09 09 09 09  ion(pos,........
3810: 74 65 78 74 28 22 6f 6e 6c 79 20 22 2c 20 64 65  text("only ", de
3820: 66 4c 61 79 2c 20 22 20 6c 61 79 65 72 20 63 61  fLay, " layer ca
3830: 6e 20 63 61 6c 6c 20 6e 61 74 69 76 65 20 66 75  n call native fu
3840: 6e 63 74 69 6f 6e 3a 20 22 2c 20 6e 61 6d 65 29  nction: ", name)
3850: 29 3b 0d 0a 09 09 09 09 54 20 74 79 70 65 64 5f  );......T typed_
3860: 61 72 67 73 3b 0d 0a 09 09 09 09 66 6f 72 65 61  args;......forea
3870: 63 68 28 69 2c 20 54 69 3b 20 54 29 20 7b 0d 0a  ch(i, Ti; T) {..
3880: 09 09 09 09 09 74 79 70 65 64 5f 61 72 67 73 5b  .....typed_args[
3890: 69 5d 20 3d 20 63 61 73 74 28 54 69 29 20 63 74  i] = cast(Ti) ct
38a0: 78 2e 67 65 74 28 74 65 78 74 28 69 29 2c 20 56  x.get(text(i), V
38b0: 61 6c 75 65 4c 61 79 65 72 2c 20 70 6f 73 29 3b  alueLayer, pos);
38c0: 0d 0a 09 09 09 09 09 69 66 28 20 74 79 70 65 64  .......if( typed
38d0: 5f 61 72 67 73 5b 69 5d 20 69 73 20 6e 75 6c 6c  _args[i] is null
38e0: 20 29 0d 0a 09 09 09 09 09 09 74 68 72 6f 77 20   )........throw 
38f0: 67 65 6e 65 78 21 52 75 6e 74 69 6d 65 45 78 63  genex!RuntimeExc
3900: 65 70 74 69 6f 6e 28 70 6f 73 2c 0d 0a 09 09 09  eption(pos,.....
3910: 09 09 09 09 73 70 72 69 6e 74 66 21 22 74 79 70  ....sprintf!"typ
3920: 65 20 6d 69 73 6d 61 74 63 68 20 6f 6e 20 74 68  e mismatch on th
3930: 65 20 61 72 67 75 6d 65 6e 74 20 25 64 20 6f 66  e argument %d of
3940: 20 6e 61 74 69 76 65 20 66 75 6e 63 74 69 6f 6e   native function
3950: 3a 20 25 73 22 28 69 2b 31 2c 6e 61 6d 65 29 29  : %s"(i+1,name))
3960: 3b 0d 0a 09 09 09 09 7d 0d 0a 09 09 09 09 74 72  ;......}......tr
3970: 79 20 7b 0d 0a 09 09 09 09 09 72 65 74 75 72 6e  y {.......return
3980: 20 64 67 28 74 79 70 65 64 5f 61 72 67 73 29 3b   dg(typed_args);
3990: 0d 0a 09 09 09 09 7d 20 63 61 74 63 68 28 20 52  ......} catch( R
39a0: 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e 20  untimeException 
39b0: 65 20 29 20 7b 0d 0a 09 09 09 09 09 74 68 72 6f  e ) {.......thro
39c0: 77 20 65 2e 70 6f 73 20 69 73 20 6e 75 6c 6c 20  w e.pos is null 
39d0: 3f 20 6e 65 77 20 52 75 6e 74 69 6d 65 45 78 63  ? new RuntimeExc
39e0: 65 70 74 69 6f 6e 28 70 6f 73 2c 20 65 2e 6d 73  eption(pos, e.ms
39f0: 67 2c 20 65 2e 66 69 6c 65 2c 20 65 2e 6c 69 6e  g, e.file, e.lin
3a00: 65 29 20 3a 20 65 3b 0d 0a 09 09 09 09 7d 0d 0a  e) : e;......}..
3a10: 09 09 09 7d 0d 0a 09 09 7d 0d 0a 09 09 74 68 65  ...}....}....the
3a20: 43 6f 6e 74 65 78 74 2e 73 65 74 28 6e 61 6d 65  Context.set(name
3a30: 2c 20 64 65 66 4c 61 79 2c 20 6e 65 77 20 4e 61  , defLay, new Na
3a40: 74 69 76 65 46 75 6e 56 61 6c 75 65 28 64 67 29  tiveFunValue(dg)
3a50: 29 3b 0d 0a 09 7d 0d 0a 7d 0d 0a 0d 0a 76 65 72  );...}..}....ver
3a60: 73 69 6f 6e 28 75 6e 69 74 74 65 73 74 29 0d 0a  sion(unittest)..
3a70: 09 69 6d 70 6f 72 74 20 70 6f 6c 65 6d 79 2e 72  .import polemy.r
3a80: 75 6e 74 69 6d 65 3b 0d 0a 0d 0a 75 6e 69 74 74  untime;....unitt
3a90: 65 73 74 0d 0a 7b 0d 0a 09 61 75 74 6f 20 65 20  est..{...auto e 
3aa0: 3d 20 6e 65 77 20 45 76 61 6c 75 61 74 6f 72 3b  = new Evaluator;
3ab0: 0d 0a 09 65 6e 72 6f 6c 6c 52 75 6e 74 69 6d 65  ...enrollRuntime
3ac0: 4c 69 62 72 61 72 79 28 65 29 3b 0d 0a 09 61 75  Library(e);...au
3ad0: 74 6f 20 72 20 3d 20 61 73 73 65 72 74 5f 6e 6f  to r = assert_no
3ae0: 74 68 72 6f 77 28 20 65 2e 65 76 61 6c 53 74 72  throw( e.evalStr
3af0: 69 6e 67 28 60 76 61 72 20 78 20 3d 20 32 31 3b  ing(`var x = 21;
3b00: 20 78 20 2b 20 78 2a 78 3b 60 29 20 29 3b 0d 0a   x + x*x;`) );..
3b10: 09 61 73 73 65 72 74 5f 65 71 28 20 72 2c 20 6e  .assert_eq( r, n
3b20: 65 77 20 49 6e 74 56 61 6c 75 65 28 42 69 67 49  ew IntValue(BigI
3b30: 6e 74 28 32 31 2b 32 31 2a 32 31 29 29 20 29 3b  nt(21+21*21)) );
3b40: 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20 65 2e  ...assert_eq( e.
3b50: 67 6c 6f 62 61 6c 43 6f 6e 74 65 78 74 2e 67 65  globalContext.ge
3b60: 74 28 22 78 22 2c 56 61 6c 75 65 4c 61 79 65 72  t("x",ValueLayer
3b70: 29 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28  ), new IntValue(
3b80: 42 69 67 49 6e 74 28 32 31 29 29 20 29 3b 0d 0a  BigInt(21)) );..
3b90: 09 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77 28  .assert_nothrow(
3ba0: 20 65 2e 67 6c 6f 62 61 6c 43 6f 6e 74 65 78 74   e.globalContext
3bb0: 2e 67 65 74 28 22 78 22 2c 56 61 6c 75 65 4c 61  .get("x",ValueLa
3bc0: 79 65 72 29 20 29 3b 0d 0a 09 61 73 73 65 72 74  yer) );...assert
3bd0: 5f 74 68 72 6f 77 21 52 75 6e 74 69 6d 65 45 78  _throw!RuntimeEx
3be0: 63 65 70 74 69 6f 6e 28 20 65 2e 67 6c 6f 62 61  ception( e.globa
3bf0: 6c 43 6f 6e 74 65 78 74 2e 67 65 74 28 22 79 22  lContext.get("y"
3c00: 2c 56 61 6c 75 65 4c 61 79 65 72 29 20 29 3b 0d  ,ValueLayer) );.
3c10: 0a 7d 0d 0a 0d 0a 75 6e 69 74 74 65 73 74 0d 0a  .}....unittest..
3c20: 7b 0d 0a 09 61 75 74 6f 20 65 20 3d 20 6e 65 77  {...auto e = new
3c30: 20 45 76 61 6c 75 61 74 6f 72 3b 0d 0a 09 65 6e   Evaluator;...en
3c40: 72 6f 6c 6c 52 75 6e 74 69 6d 65 4c 69 62 72 61  rollRuntimeLibra
3c50: 72 79 28 65 29 3b 0d 0a 09 61 75 74 6f 20 72 20  ry(e);...auto r 
3c60: 3d 20 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77  = assert_nothrow
3c70: 28 20 65 2e 65 76 61 6c 53 74 72 69 6e 67 28 60  ( e.evalString(`
3c80: 76 61 72 20 78 20 3d 20 32 31 3b 20 76 61 72 20  var x = 21; var 
3c90: 78 20 3d 20 78 20 2b 20 78 2a 78 3b 60 29 20 29  x = x + x*x;`) )
3ca0: 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20 72  ;...assert_eq( r
3cb0: 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 42  , new IntValue(B
3cc0: 69 67 49 6e 74 28 32 31 2b 32 31 2a 32 31 29 29  igInt(21+21*21))
3cd0: 20 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28   );...assert_eq(
3ce0: 20 65 2e 67 6c 6f 62 61 6c 43 6f 6e 74 65 78 74   e.globalContext
3cf0: 2e 67 65 74 28 22 78 22 2c 56 61 6c 75 65 4c 61  .get("x",ValueLa
3d00: 79 65 72 29 2c 20 6e 65 77 20 49 6e 74 56 61 6c  yer), new IntVal
3d10: 75 65 28 32 31 2b 32 31 2a 32 31 29 20 29 3b 0d  ue(21+21*21) );.
3d20: 0a 09 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77  ..assert_nothrow
3d30: 28 20 65 2e 67 6c 6f 62 61 6c 43 6f 6e 74 65 78  ( e.globalContex
3d40: 74 2e 67 65 74 28 22 78 22 2c 56 61 6c 75 65 4c  t.get("x",ValueL
3d50: 61 79 65 72 29 20 29 3b 0d 0a 09 61 73 73 65 72  ayer) );...asser
3d60: 74 5f 74 68 72 6f 77 21 52 75 6e 74 69 6d 65 45  t_throw!RuntimeE
3d70: 78 63 65 70 74 69 6f 6e 28 20 65 2e 67 6c 6f 62  xception( e.glob
3d80: 61 6c 43 6f 6e 74 65 78 74 2e 67 65 74 28 22 79  alContext.get("y
3d90: 22 2c 56 61 6c 75 65 4c 61 79 65 72 29 20 29 3b  ",ValueLayer) );
3da0: 0d 0a 7d 0d 0a 75 6e 69 74 74 65 73 74 0d 0a 7b  ..}..unittest..{
3db0: 0d 0a 09 61 75 74 6f 20 65 20 3d 20 6e 65 77 20  ...auto e = new 
3dc0: 45 76 61 6c 75 61 74 6f 72 3b 0d 0a 09 65 6e 72  Evaluator;...enr
3dd0: 6f 6c 6c 52 75 6e 74 69 6d 65 4c 69 62 72 61 72  ollRuntimeLibrar
3de0: 79 28 65 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65  y(e);...assert_e
3df0: 71 28 20 65 2e 65 76 61 6c 53 74 72 69 6e 67 28  q( e.evalString(
3e00: 60 6c 65 74 20 78 3d 31 3b 20 6c 65 74 20 79 3d  `let x=1; let y=
3e10: 28 6c 65 74 20 78 3d 32 29 3b 20 78 60 29 2c 20  (let x=2); x`), 
3e20: 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 31 29 20  new IntValue(1) 
3e30: 29 3b 20 0d 0a 09 61 73 73 65 72 74 5f 65 71 28  ); ...assert_eq(
3e40: 20 65 2e 65 76 61 6c 53 74 72 69 6e 67 28 60 6c   e.evalString(`l
3e50: 65 74 20 78 3d 31 3b 20 6c 65 74 20 79 3d 28 6c  et x=1; let y=(l
3e60: 65 74 20 78 3d 32 3b 66 75 6e 28 29 7b 78 7d 29  et x=2;fun(){x})
3e70: 3b 20 79 28 29 60 29 2c 20 6e 65 77 20 49 6e 74  ; y()`), new Int
3e80: 56 61 6c 75 65 28 32 29 20 29 3b 20 0d 0a 7d 0d  Value(2) ); ..}.
3e90: 0a 0d 0a 75 6e 69 74 74 65 73 74 0d 0a 7b 0d 0a  ...unittest..{..
3ea0: 09 61 75 74 6f 20 65 20 3d 20 6e 65 77 20 45 76  .auto e = new Ev
3eb0: 61 6c 75 61 74 6f 72 3b 0d 0a 09 65 6e 72 6f 6c  aluator;...enrol
3ec0: 6c 52 75 6e 74 69 6d 65 4c 69 62 72 61 72 79 28  lRuntimeLibrary(
3ed0: 65 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28  e);...assert_eq(
3ee0: 20 65 2e 65 76 61 6c 53 74 72 69 6e 67 28 60 40   e.evalString(`@
3ef0: 61 20 78 3d 31 3b 20 40 62 20 78 3d 32 3b 20 40  a x=1; @b x=2; @
3f00: 61 28 78 29 60 29 2c 20 6e 65 77 20 49 6e 74 56  a(x)`), new IntV
3f10: 61 6c 75 65 28 42 69 67 49 6e 74 28 31 29 29 20  alue(BigInt(1)) 
3f20: 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20  );...assert_eq( 
3f30: 65 2e 65 76 61 6c 53 74 72 69 6e 67 28 60 40 61  e.evalString(`@a
3f40: 20 78 3d 31 3b 20 40 62 20 78 3d 32 3b 20 40 62   x=1; @b x=2; @b
3f50: 28 78 29 60 29 2c 20 6e 65 77 20 49 6e 74 56 61  (x)`), new IntVa
3f60: 6c 75 65 28 42 69 67 49 6e 74 28 32 29 29 20 29  lue(BigInt(2)) )
3f70: 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20 65  ;...assert_eq( e
3f80: 2e 65 76 61 6c 53 74 72 69 6e 67 28 60 6c 65 74  .evalString(`let
3f90: 20 78 3d 31 3b 20 6c 65 74 20 5f 20 3d 20 28 40   x=1; let _ = (@
3fa0: 61 20 78 3d 32 3b 32 29 3b 20 78 60 29 2c 20 6e  a x=2;2); x`), n
3fb0: 65 77 20 49 6e 74 56 61 6c 75 65 28 42 69 67 49  ew IntValue(BigI
3fc0: 6e 74 28 31 29 29 20 29 3b 0d 0a 09 65 20 3d 20  nt(1)) );...e = 
3fd0: 6e 65 77 20 45 76 61 6c 75 61 74 6f 72 3b 0d 0a  new Evaluator;..
3fe0: 09 61 73 73 65 72 74 5f 74 68 72 6f 77 21 54 68  .assert_throw!Th
3ff0: 72 6f 77 61 62 6c 65 28 20 65 2e 65 76 61 6c 53  rowable( e.evalS
4000: 74 72 69 6e 67 28 60 6c 65 74 20 78 3d 31 3b 20  tring(`let x=1; 
4010: 6c 65 74 20 5f 20 3d 20 28 40 61 20 78 3d 32 3b  let _ = (@a x=2;
4020: 32 29 3b 20 40 61 28 78 29 60 29 20 29 3b 0d 0a  2); @a(x)`) );..
4030: 7d 0d 0a 0d 0a 75 6e 69 74 74 65 73 74 0d 0a 7b  }....unittest..{
4040: 0d 0a 09 61 75 74 6f 20 65 20 3d 20 6e 65 77 20  ...auto e = new 
4050: 45 76 61 6c 75 61 74 6f 72 3b 0d 0a 09 65 6e 72  Evaluator;...enr
4060: 6f 6c 6c 52 75 6e 74 69 6d 65 4c 69 62 72 61 72  ollRuntimeLibrar
4070: 79 28 65 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65  y(e);...assert_e
4080: 71 28 20 65 2e 65 76 61 6c 53 74 72 69 6e 67 28  q( e.evalString(
4090: 60 0d 0a 09 09 40 40 73 28 78 29 7b 78 7d 3b 0d  `....@@s(x){x};.
40a0: 0a 09 09 40 73 20 22 2b 22 20 3d 20 66 75 6e 28  ...@s "+" = fun(
40b0: 78 2c 20 79 29 20 7b 40 76 61 6c 75 65 28 0d 0a  x, y) {@value(..
40c0: 09 09 09 40 73 28 78 29 20 2d 20 40 73 28 79 29  ...@s(x) - @s(y)
40d0: 0d 0a 09 09 29 7d 3b 0d 0a 09 09 40 73 28 31 20  ....)};....@s(1 
40e0: 2b 20 32 29 0d 0a 09 60 29 2c 20 6e 65 77 20 49  + 2)...`), new I
40f0: 6e 74 56 61 6c 75 65 28 42 69 67 49 6e 74 28 2d  ntValue(BigInt(-
4100: 31 29 29 20 29 3b 0d 0a 7d 0d 0a 0d 0a 75 6e 69  1)) );..}....uni
4110: 74 74 65 73 74 0d 0a 7b 0d 0a 09 61 75 74 6f 20  ttest..{...auto 
4120: 65 20 3d 20 6e 65 77 20 45 76 61 6c 75 61 74 6f  e = new Evaluato
4130: 72 3b 0d 0a 09 65 6e 72 6f 6c 6c 52 75 6e 74 69  r;...enrollRunti
4140: 6d 65 4c 69 62 72 61 72 79 28 65 29 3b 0d 0a 09  meLibrary(e);...
4150: 61 73 73 65 72 74 5f 65 71 28 20 65 2e 65 76 61  assert_eq( e.eva
4160: 6c 53 74 72 69 6e 67 28 60 0d 0a 40 40 33 28 78  lString(`..@@3(x
4170: 29 7b 78 7d 3b 0d 0a 64 65 66 20 69 6e 63 72 28  ){x};..def incr(
4180: 78 29 20 7b 20 78 2b 31 20 7d 3b 0d 0a 40 20 33  x) { x+1 };..@ 3
4190: 20 69 6e 63 72 28 78 29 20 7b 40 76 61 6c 75 65   incr(x) {@value
41a0: 28 20 69 66 20 40 20 33 28 78 29 2b 31 3c 20 33  ( if @ 3(x)+1< 3
41b0: 20 74 68 65 6e 20 40 33 28 78 29 2b 31 20 65 6c   then @3(x)+1 el
41c0: 73 65 20 30 20 29 7d 3b 0d 0a 64 65 66 20 66 62  se 0 )};..def fb
41d0: 28 6e 20 40 76 61 6c 75 65 20 40 33 29 20 7b 20  (n @value @3) { 
41e0: 40 33 28 6e 29 20 7d 3b 0d 0a 66 62 28 69 6e 63  @3(n) };..fb(inc
41f0: 72 28 69 6e 63 72 28 69 6e 63 72 28 30 29 29 29  r(incr(incr(0)))
4200: 29 0d 0a 09 60 29 2c 20 6e 65 77 20 49 6e 74 56  )...`), new IntV
4210: 61 6c 75 65 28 42 69 67 49 6e 74 28 30 29 29 20  alue(BigInt(0)) 
4220: 29 3b 0d 0a 7d 0d 0a 0d 0a 75 6e 69 74 74 65 73  );..}....unittes
4230: 74 0d 0a 7b 0d 0a 09 61 75 74 6f 20 65 20 3d 20  t..{...auto e = 
4240: 6e 65 77 20 45 76 61 6c 75 61 74 6f 72 3b 0d 0a  new Evaluator;..
4250: 09 65 6e 72 6f 6c 6c 52 75 6e 74 69 6d 65 4c 69  .enrollRuntimeLi
4260: 62 72 61 72 79 28 65 29 3b 0d 0a 09 61 73 73 65  brary(e);...asse
4270: 72 74 5f 6e 6f 74 68 72 6f 77 28 20 65 2e 65 76  rt_nothrow( e.ev
4280: 61 6c 53 74 72 69 6e 67 28 60 0d 0a 40 6d 61 63  alString(`..@mac
4290: 72 6f 20 74 77 69 63 65 28 78 29 20 7b 20 78 3b  ro twice(x) { x;
42a0: 20 78 20 7d 3b 0d 0a 64 65 66 20 6d 61 69 6e 28   x };..def main(
42b0: 29 20 7b 20 74 77 69 63 65 28 31 29 20 7d 3b 0d  ) { twice(1) };.
42c0: 0a 6d 61 69 6e 28 29 0d 0a 09 60 29 20 29 3b 0d  .main()...`) );.
42d0: 0a 7d 0d 0a 75 6e 69 74 74 65 73 74 0d 0a 7b 0d  .}..unittest..{.
42e0: 0a 09 61 75 74 6f 20 65 20 3d 20 6e 65 77 20 45  ..auto e = new E
42f0: 76 61 6c 75 61 74 6f 72 3b 0d 0a 09 65 6e 72 6f  valuator;...enro
4300: 6c 6c 52 75 6e 74 69 6d 65 4c 69 62 72 61 72 79  llRuntimeLibrary
4310: 28 65 29 3b 0d 0a 09 61 73 73 65 72 74 5f 74 68  (e);...assert_th
4320: 72 6f 77 21 52 75 6e 74 69 6d 65 45 78 63 65 70  row!RuntimeExcep
4330: 74 69 6f 6e 28 20 65 2e 65 76 61 6c 53 74 72 69  tion( e.evalStri
4340: 6e 67 28 60 63 61 73 65 20 31 60 29 20 29 3b 0d  ng(`case 1`) );.
4350: 0a 09 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77  ..assert_nothrow
4360: 28 20 65 2e 65 76 61 6c 53 74 72 69 6e 67 28 60  ( e.evalString(`
4370: 63 61 73 65 20 31 20 77 68 65 6e 20 31 3a 20 32  case 1 when 1: 2
4380: 60 29 20 29 3b 0d 0a 0d 0a 09 2f 2f 20 74 68 69  `) );.....// thi
4390: 73 20 69 73 20 61 20 73 68 6f 72 74 68 61 6e 64  s is a shorthand
43a0: 20 66 6f 72 0d 0a 09 2f 2f 20 20 20 40 6d 61 63   for...//   @mac
43b0: 72 6f 20 78 20 3d 20 66 75 6e 28 29 7b 7d 20 69  ro x = fun(){} i
43c0: 6e 20 40 6d 61 63 72 6f 28 78 29 0d 0a 09 2f 2f  n @macro(x)...//
43d0: 20 73 6f 20 69 74 20 69 73 20 6f 6b 20 74 6f 20   so it is ok to 
43e0: 66 61 69 6c 2c 20 62 75 74 20 69 74 20 69 73 20  fail, but it is 
43f0: 72 65 61 6c 6c 79 20 69 6e 63 6f 76 65 6e 69 65  really incovenie
4400: 6e 74 20 6f 6e 20 52 45 50 4c 0d 0a 09 61 73 73  nt on REPL...ass
4410: 65 72 74 5f 6e 6f 74 68 72 6f 77 28 20 65 2e 65  ert_nothrow( e.e
4420: 76 61 6c 53 74 72 69 6e 67 28 60 40 6d 61 63 72  valString(`@macr
4430: 6f 20 78 3d 66 75 6e 28 29 7b 7d 60 29 20 29 3b  o x=fun(){}`) );
4440: 0d 0a 7d 0d 0a 0d 0a 75 6e 69 74 74 65 73 74 0d  ..}....unittest.
4450: 0a 7b 0d 0a 09 61 75 74 6f 20 65 20 3d 20 6e 65  .{...auto e = ne
4460: 77 20 45 76 61 6c 75 61 74 6f 72 3b 0d 0a 09 65  w Evaluator;...e
4470: 6e 72 6f 6c 6c 52 75 6e 74 69 6d 65 4c 69 62 72  nrollRuntimeLibr
4480: 61 72 79 28 65 29 3b 0d 0a 09 61 73 73 65 72 74  ary(e);...assert
4490: 5f 74 68 72 6f 77 21 52 75 6e 74 69 6d 65 45 78  _throw!RuntimeEx
44a0: 63 65 70 74 69 6f 6e 28 20 65 2e 65 76 61 6c 53  ception( e.evalS
44b0: 74 72 69 6e 67 28 60 2e 2e 2e 60 29 20 29 3b 0d  tring(`...`) );.
44c0: 0a 09 61 73 73 65 72 74 5f 65 71 28 20 65 2e 65  ..assert_eq( e.e
44d0: 76 61 6c 53 74 72 69 6e 67 28 60 40 40 66 6f 6f  valString(`@@foo
44e0: 28 78 29 7b 78 7d 3b 20 40 66 6f 6f 28 2e 2e 2e  (x){x}; @foo(...
44f0: 29 60 29 2c 20 6e 65 77 20 42 6f 74 74 6f 6d 56  )`), new BottomV
4500: 61 6c 75 65 20 29 3b 0d 0a 7d 0d 0a              alue );..}..