Hex Artifact Content
Not logged in

Artifact 969111acf2042a227ed03ae4805bfc7fb5dccb4d:


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 74 68 69 73 2e 6c 69 66 74 28 6e  turn this.lift(n
1dc0: 65 77 20 42 6f 74 74 6f 6d 56 61 6c 75 65 2c 20  ew BottomValue, 
1dd0: 63 74 78 2c 20 65 2e 70 6f 73 29 3b 0d 0a 09 09  ctx, e.pos);....
1de0: 7d 0d 0a 09 09 6f 76 65 72 72 69 64 65 20 56 61  }....override Va
1df0: 6c 75 65 20 65 76 61 6c 5f 28 20 53 74 72 20 65  lue eval_( Str e
1e00: 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f  , Table ctx, boo
1e10: 6c 20 63 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d  l ctxMod )....{.
1e20: 0a 09 09 09 72 65 74 75 72 6e 20 74 68 69 73 2e  ....return this.
1e30: 6c 69 66 74 28 73 75 70 65 72 2e 65 76 61 6c 5f  lift(super.eval_
1e40: 28 65 2c 63 74 78 2c 63 74 78 4d 6f 64 29 2c 20  (e,ctx,ctxMod), 
1e50: 63 74 78 2c 20 65 2e 70 6f 73 29 3b 0d 0a 09 09  ctx, e.pos);....
1e60: 7d 0d 0a 09 09 6f 76 65 72 72 69 64 65 20 56 61  }....override Va
1e70: 6c 75 65 20 65 76 61 6c 5f 28 20 49 6e 74 20 65  lue eval_( Int e
1e80: 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f  , Table ctx, boo
1e90: 6c 20 63 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d  l ctxMod )....{.
1ea0: 0a 09 09 09 72 65 74 75 72 6e 20 74 68 69 73 2e  ....return this.
1eb0: 6c 69 66 74 28 73 75 70 65 72 2e 65 76 61 6c 5f  lift(super.eval_
1ec0: 28 65 2c 63 74 78 2c 63 74 78 4d 6f 64 29 2c 20  (e,ctx,ctxMod), 
1ed0: 63 74 78 2c 20 65 2e 70 6f 73 29 3b 0d 0a 09 09  ctx, e.pos);....
1ee0: 7d 0d 0a 09 09 6f 76 65 72 72 69 64 65 20 56 61  }....override Va
1ef0: 6c 75 65 20 65 76 61 6c 5f 28 20 56 61 72 20 65  lue eval_( Var e
1f00: 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f  , Table ctx, boo
1f10: 6c 20 63 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d  l ctxMod )....{.
1f20: 0a 09 09 09 69 66 28 20 63 74 78 2e 68 61 73 28  ....if( ctx.has(
1f30: 65 2e 6e 61 6d 65 2c 20 6c 61 79 65 72 49 44 28  e.name, layerID(
1f40: 29 29 20 29 0d 0a 09 09 09 09 72 65 74 75 72 6e  )) )......return
1f50: 20 63 74 78 2e 67 65 74 28 65 2e 6e 61 6d 65 2c   ctx.get(e.name,
1f60: 20 6c 61 79 65 72 49 44 28 29 29 3b 0d 0a 09 09   layerID());....
1f70: 09 56 61 6c 75 65 20 76 3b 0d 0a 09 09 09 74 72  .Value v;.....tr
1f80: 79 20 7b 20 76 20 3d 20 63 74 78 2e 67 65 74 28  y { v = ctx.get(
1f90: 65 2e 6e 61 6d 65 2c 20 56 61 6c 75 65 4c 61 79  e.name, ValueLay
1fa0: 65 72 2c 20 65 2e 70 6f 73 29 3b 20 7d 0d 0a 09  er, e.pos); }...
1fb0: 09 09 63 61 74 63 68 20 28 20 52 75 6e 74 69 6d  ..catch ( Runtim
1fc0: 65 45 78 63 65 70 74 69 6f 6e 20 29 20 7b 0d 0a  eException ) {..
1fd0: 09 09 09 09 74 68 72 6f 77 20 67 65 6e 65 78 21  ....throw genex!
1fe0: 52 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e  RuntimeException
1ff0: 28 65 2e 70 6f 73 2c 20 65 2e 6e 61 6d 65 7e 22  (e.pos, e.name~"
2000: 20 6e 6f 74 20 66 6f 75 6e 64 20 69 6e 20 6c 61   not found in la
2010: 79 65 72 20 22 7e 6c 61 79 65 72 49 44 28 29 7e  yer "~layerID()~
2020: 22 20 6e 6f 72 20 22 7e 56 61 6c 75 65 4c 61 79  " nor "~ValueLay
2030: 65 72 29 3b 0d 0a 09 09 09 7d 0d 0a 09 09 09 72  er);.....}.....r
2040: 65 74 75 72 6e 20 74 68 69 73 2e 6c 69 66 74 28  eturn this.lift(
2050: 76 2c 20 63 74 78 2c 20 65 2e 70 6f 73 29 3b 0d  v, ctx, e.pos);.
2060: 0a 09 09 7d 0d 0a 09 7d 0d 0a 0d 0a 09 2f 2f 20  ...}...}.....// 
2070: 4d 61 63 72 6f 20 6c 61 79 65 72 2e 20 46 6f 72  Macro layer. For
2080: 20 6f 70 74 69 6d 69 7a 61 74 69 6f 6e 2c 20 69   optimization, i
2090: 66 20 41 53 54 20 64 69 64 6e 27 74 20 63 68 61  f AST didn't cha
20a0: 6e 67 65 20 69 74 20 72 65 74 75 72 6e 73 20 6e  nge it returns n
20b0: 75 6c 6c 0d 0a 09 63 6c 61 73 73 20 4d 61 63 72  ull...class Macr
20c0: 6f 4c 61 79 65 72 45 76 61 6c 20 3a 20 4c 61 79  oLayerEval : Lay
20d0: 65 72 45 76 61 6c 0d 0a 09 7b 0d 0a 09 09 6f 76  erEval...{....ov
20e0: 65 72 72 69 64 65 20 4c 61 79 65 72 20 6c 61 79  erride Layer lay
20f0: 65 72 49 44 28 29 0d 0a 09 09 7b 0d 0a 09 09 09  erID()....{.....
2100: 72 65 74 75 72 6e 20 4d 61 63 72 6f 4c 61 79 65  return MacroLaye
2110: 72 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65 72 72  r;....}....overr
2120: 69 64 65 20 56 61 6c 75 65 20 65 76 61 6c 54 6f  ide Value evalTo
2130: 4e 6f 6e 4e 75 6c 6c 28 20 41 53 54 20 65 2c 20  NonNull( AST e, 
2140: 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c 20  Table ctx, bool 
2150: 63 74 78 4d 6f 64 20 3d 20 43 61 73 63 61 64 65  ctxMod = Cascade
2160: 43 74 78 20 29 0d 0a 09 09 7b 0d 0a 09 09 09 56  Ctx )....{.....V
2170: 61 6c 75 65 20 76 20 3d 20 74 68 69 73 2e 65 76  alue v = this.ev
2180: 61 6c 28 65 2c 20 63 74 78 2c 20 63 74 78 4d 6f  al(e, ctx, ctxMo
2190: 64 29 3b 0d 0a 09 09 09 72 65 74 75 72 6e 20 76  d);.....return v
21a0: 20 69 73 20 6e 75 6c 6c 20 3f 20 61 73 74 32 74   is null ? ast2t
21b0: 61 62 6c 65 28 65 29 20 3a 20 76 3b 0d 0a 09 09  able(e) : v;....
21c0: 7d 0d 0a 09 09 6f 76 65 72 72 69 64 65 20 56 61  }....override Va
21d0: 6c 75 65 20 65 76 61 6c 5f 28 20 44 69 65 20 65  lue eval_( Die e
21e0: 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f  , Table ctx, boo
21f0: 6c 20 63 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d  l ctxMod )....{.
2200: 0a 09 09 09 72 65 74 75 72 6e 20 6e 75 6c 6c 3b  ....return null;
2210: 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65 72 72 69 64  ....}....overrid
2220: 65 20 56 61 6c 75 65 20 65 76 61 6c 5f 28 20 53  e Value eval_( S
2230: 74 72 20 65 2c 20 54 61 62 6c 65 20 63 74 78 2c  tr e, Table ctx,
2240: 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29 0d 0a   bool ctxMod )..
2250: 09 09 7b 0d 0a 09 09 09 72 65 74 75 72 6e 20 6e  ..{.....return n
2260: 75 6c 6c 3b 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65  ull;....}....ove
2270: 72 72 69 64 65 20 56 61 6c 75 65 20 65 76 61 6c  rride Value eval
2280: 5f 28 20 49 6e 74 20 65 2c 20 54 61 62 6c 65 20  _( Int e, Table 
2290: 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64  ctx, bool ctxMod
22a0: 20 29 0d 0a 09 09 7b 0d 0a 09 09 09 72 65 74 75   )....{.....retu
22b0: 72 6e 20 6e 75 6c 6c 3b 0d 0a 09 09 7d 0d 0a 09  rn null;....}...
22c0: 09 6f 76 65 72 72 69 64 65 20 56 61 6c 75 65 20  .override Value 
22d0: 65 76 61 6c 5f 28 20 56 61 72 20 65 2c 20 54 61  eval_( Var e, Ta
22e0: 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74  ble ctx, bool ct
22f0: 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d 0a 09 09 09  xMod )....{.....
2300: 69 66 28 20 63 74 78 2e 68 61 73 28 65 2e 6e 61  if( ctx.has(e.na
2310: 6d 65 2c 20 6c 61 79 65 72 49 44 28 29 29 20 29  me, layerID()) )
2320: 0d 0a 09 09 09 09 72 65 74 75 72 6e 20 63 74 78  ......return ctx
2330: 2e 67 65 74 28 65 2e 6e 61 6d 65 2c 20 6c 61 79  .get(e.name, lay
2340: 65 72 49 44 28 29 2c 20 65 2e 70 6f 73 29 3b 0d  erID(), e.pos);.
2350: 0a 09 09 09 72 65 74 75 72 6e 20 6e 75 6c 6c 3b  ....return null;
2360: 0d 0a 09 09 7d 0d 0a 09 09 6f 76 65 72 72 69 64  ....}....overrid
2370: 65 20 56 61 6c 75 65 20 65 76 61 6c 5f 28 20 4c  e Value eval_( L
2380: 61 79 20 65 2c 20 54 61 62 6c 65 20 63 74 78 2c  ay e, Table ctx,
2390: 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29 0d 0a   bool ctxMod )..
23a0: 09 09 7b 0d 0a 09 09 09 72 65 74 75 72 6e 20 67  ..{.....return g
23b0: 65 74 4c 61 79 65 72 45 76 61 6c 28 65 2e 6c 61  etLayerEval(e.la
23c0: 79 65 72 29 2e 65 76 61 6c 28 65 2e 65 78 70 72  yer).eval(e.expr
23d0: 2c 63 74 78 29 3b 0d 0a 09 09 7d 0d 0a 09 09 6f  ,ctx);....}....o
23e0: 76 65 72 72 69 64 65 20 56 61 6c 75 65 20 65 76  verride Value ev
23f0: 61 6c 5f 28 20 4c 65 74 20 65 2c 20 54 61 62 6c  al_( Let e, Tabl
2400: 65 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d  e ctx, bool ctxM
2410: 6f 64 20 29 0d 0a 09 09 7b 0d 0a 09 09 09 69 66  od )....{.....if
2420: 28 20 21 63 74 78 4d 6f 64 20 29 0d 0a 09 09 09  ( !ctxMod ).....
2430: 09 63 74 78 20 3d 20 6e 65 77 20 54 61 62 6c 65  .ctx = new Table
2440: 28 63 74 78 2c 20 54 61 62 6c 65 2e 4b 69 6e 64  (ctx, Table.Kind
2450: 2e 4e 6f 74 50 72 6f 70 61 67 61 74 65 53 65 74  .NotPropagateSet
2460: 29 3b 0d 0a 0d 0a 09 09 09 56 61 6c 75 65 20 61  );.......Value a
2470: 69 20 3d 20 74 68 69 73 2e 65 76 61 6c 28 65 2e  i = this.eval(e.
2480: 76 64 65 66 2c 20 63 74 78 29 3b 0d 0a 09 09 09  vdef, ctx);.....
2490: 63 74 78 2e 73 65 74 28 65 2e 6e 61 6d 65 2c 20  ctx.set(e.name, 
24a0: 4e 6f 6f 70 4c 61 79 65 72 2c 20 6e 75 6c 6c 29  NoopLayer, null)
24b0: 3b 0d 0a 09 09 09 56 61 6c 75 65 20 61 65 20 3d  ;.....Value ae =
24c0: 20 74 68 69 73 2e 65 76 61 6c 28 65 2e 65 78 70   this.eval(e.exp
24d0: 72 2c 20 63 74 78 2c 20 4f 76 65 72 77 72 69 74  r, ctx, Overwrit
24e0: 65 43 74 78 29 3b 0d 0a 0d 0a 09 09 09 69 66 28  eCtx);.......if(
24f0: 20 61 69 20 69 73 20 6e 75 6c 6c 20 26 26 20 61   ai is null && a
2500: 65 20 69 73 20 6e 75 6c 6c 20 29 20 72 65 74 75  e is null ) retu
2510: 72 6e 20 6e 75 6c 6c 3b 0d 0a 09 09 09 69 66 28  rn null;.....if(
2520: 20 61 69 20 69 73 20 6e 75 6c 6c 20 29 20 61 69   ai is null ) ai
2530: 20 3d 20 61 73 74 32 74 61 62 6c 65 28 65 2e 76   = ast2table(e.v
2540: 64 65 66 29 3b 0d 0a 09 09 09 69 66 28 20 61 65  def);.....if( ae
2550: 20 69 73 20 6e 75 6c 6c 20 29 20 61 65 20 3d 20   is null ) ae = 
2560: 61 73 74 32 74 61 62 6c 65 28 65 2e 65 78 70 72  ast2table(e.expr
2570: 29 3b 0d 0a 0d 0a 09 09 09 72 65 74 75 72 6e 20  );.......return 
2580: 61 73 74 32 74 61 62 6c 65 28 65 2c 20 64 65 6c  ast2table(e, del
2590: 65 67 61 74 65 20 56 61 6c 75 65 20 28 41 53 54  egate Value (AST
25a0: 20 5f 29 7b 0d 0a 09 09 09 09 69 66 28 5f 20 69   _){......if(_ i
25b0: 73 20 65 2e 76 64 65 66 29 20 7b 20 72 65 74 75  s e.vdef) { retu
25c0: 72 6e 20 61 69 3b 20 7d 0d 0a 09 09 09 09 69 66  rn ai; }......if
25d0: 28 5f 20 69 73 20 65 2e 65 78 70 72 29 20 7b 20  (_ is e.expr) { 
25e0: 72 65 74 75 72 6e 20 61 65 3b 20 7d 0d 0a 09 09  return ae; }....
25f0: 09 09 61 73 73 65 72 74 28 66 61 6c 73 65 29 3b  ..assert(false);
2600: 0d 0a 09 09 09 7d 29 3b 0d 0a 09 09 7d 0d 0a 09  .....});....}...
2610: 09 6f 76 65 72 72 69 64 65 20 56 61 6c 75 65 20  .override Value 
2620: 65 76 61 6c 5f 28 20 41 70 70 20 65 2c 20 54 61  eval_( App e, Ta
2630: 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c 20 63 74  ble ctx, bool ct
2640: 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d 0a 09 09 09  xMod )....{.....
2650: 56 61 6c 75 65 20 66 20 3d 20 74 68 69 73 2e 65  Value f = this.e
2660: 76 61 6c 28 20 65 2e 66 75 6e 2c 20 63 74 78 20  val( e.fun, ctx 
2670: 29 3b 0d 0a 09 09 09 69 66 28 61 75 74 6f 20 66  );.....if(auto f
2680: 66 20 3d 20 63 61 73 74 28 46 75 6e 56 61 6c 75  f = cast(FunValu
2690: 65 29 66 29 0d 0a 09 09 09 09 72 65 74 75 72 6e  e)f)......return
26a0: 20 74 68 69 73 2e 69 6e 76 6f 6b 65 46 75 6e 63   this.invokeFunc
26b0: 74 69 6f 6e 28 66 66 2c 20 65 2e 61 72 67 73 2c  tion(ff, e.args,
26c0: 20 63 74 78 2c 20 65 2e 70 6f 73 2c 20 67 65 74   ctx, e.pos, get
26d0: 4e 61 6d 65 49 66 50 6f 73 73 69 62 6c 65 28 65  NameIfPossible(e
26e0: 2e 66 75 6e 29 29 3b 0d 0a 09 09 09 65 6c 73 65  .fun));.....else
26f0: 0d 0a 09 09 09 7b 0d 0a 09 09 09 09 62 6f 6f 6c  .....{......bool
2700: 20 61 6c 6c 4e 75 6c 6c 20 3d 20 28 66 20 69 73   allNull = (f is
2710: 20 6e 75 6c 6c 29 3b 0d 0a 09 09 09 09 56 61 6c   null);......Val
2720: 75 65 5b 5d 20 76 61 73 3b 0d 0a 09 09 09 09 66  ue[] vas;......f
2730: 6f 72 65 61 63 68 28 61 3b 20 65 2e 61 72 67 73  oreach(a; e.args
2740: 29 0d 0a 09 09 09 09 7b 0d 0a 09 09 09 09 09 56  )......{.......V
2750: 61 6c 75 65 20 76 61 20 3d 20 74 68 69 73 2e 65  alue va = this.e
2760: 76 61 6c 28 61 2c 20 63 74 78 2c 20 43 61 73 63  val(a, ctx, Casc
2770: 61 64 65 43 74 78 29 3b 0d 0a 09 09 09 09 09 69  adeCtx);.......i
2780: 66 28 76 61 20 21 69 73 20 6e 75 6c 6c 29 20 61  f(va !is null) a
2790: 6c 6c 4e 75 6c 6c 20 3d 20 66 61 6c 73 65 3b 0d  llNull = false;.
27a0: 0a 09 09 09 09 09 76 61 73 20 7e 3d 20 76 61 3b  ......vas ~= va;
27b0: 0d 0a 09 09 09 09 7d 0d 0a 09 09 09 09 69 66 28  ......}......if(
27c0: 20 61 6c 6c 4e 75 6c 6c 20 29 0d 0a 09 09 09 09   allNull )......
27d0: 09 72 65 74 75 72 6e 20 6e 75 6c 6c 3b 0d 0a 09  .return null;...
27e0: 09 09 09 72 65 74 75 72 6e 20 61 73 74 32 74 61  ...return ast2ta
27f0: 62 6c 65 28 65 2c 20 64 65 6c 65 67 61 74 65 20  ble(e, delegate 
2800: 56 61 6c 75 65 20 28 41 53 54 20 5f 29 7b 0d 0a  Value (AST _){..
2810: 09 09 09 09 09 69 66 28 5f 20 69 73 20 65 2e 66  .....if(_ is e.f
2820: 75 6e 29 20 72 65 74 75 72 6e 20 28 66 20 69 73  un) return (f is
2830: 20 6e 75 6c 6c 20 3f 20 61 73 74 32 74 61 62 6c   null ? ast2tabl
2840: 65 28 65 2e 66 75 6e 29 20 3a 20 66 29 3b 0d 0a  e(e.fun) : f);..
2850: 09 09 09 09 09 66 6f 72 65 61 63 68 28 69 2c 61  .....foreach(i,a
2860: 3b 20 65 2e 61 72 67 73 29 20 69 66 28 5f 20 69  ; e.args) if(_ i
2870: 73 20 61 29 20 72 65 74 75 72 6e 20 28 76 61 73  s a) return (vas
2880: 5b 69 5d 20 69 73 20 6e 75 6c 6c 20 3f 20 61 73  [i] is null ? as
2890: 74 32 74 61 62 6c 65 28 61 29 20 3a 20 76 61 73  t2table(a) : vas
28a0: 5b 69 5d 29 3b 0d 0a 09 09 09 09 09 61 73 73 65  [i]);.......asse
28b0: 72 74 28 66 61 6c 73 65 29 3b 0d 0a 09 09 09 09  rt(false);......
28c0: 7d 29 3b 0d 0a 09 09 09 7d 0d 0a 09 09 7d 0d 0a  });.....}....}..
28d0: 09 09 6f 76 65 72 72 69 64 65 20 56 61 6c 75 65  ..override Value
28e0: 20 65 76 61 6c 5f 28 20 46 75 6e 20 65 2c 20 54   eval_( Fun e, T
28f0: 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c 20 63  able ctx, bool c
2900: 74 78 4d 6f 64 20 29 0d 0a 09 09 7b 0d 0a 09 09  txMod )....{....
2910: 09 63 74 78 20 3d 20 6e 65 77 20 54 61 62 6c 65  .ctx = new Table
2920: 28 63 74 78 2c 20 54 61 62 6c 65 2e 4b 69 6e 64  (ctx, Table.Kind
2930: 2e 4e 6f 74 50 72 6f 70 61 67 61 74 65 53 65 74  .NotPropagateSet
2940: 29 3b 0d 0a 09 09 09 66 6f 72 65 61 63 68 28 70  );.....foreach(p
2950: 3b 20 65 2e 70 61 72 61 6d 73 29 0d 0a 09 09 09  ; e.params).....
2960: 09 63 74 78 2e 73 65 74 28 70 2e 6e 61 6d 65 2c  .ctx.set(p.name,
2970: 20 4e 6f 6f 70 4c 61 79 65 72 2c 20 6e 75 6c 6c   NoopLayer, null
2980: 29 3b 0d 0a 09 09 09 56 61 6c 75 65 20 61 66 20  );.....Value af 
2990: 3d 20 74 68 69 73 2e 65 76 61 6c 28 65 2e 66 75  = this.eval(e.fu
29a0: 6e 62 6f 64 79 2c 20 63 74 78 29 3b 0d 0a 09 09  nbody, ctx);....
29b0: 09 69 66 28 20 61 66 20 69 73 20 6e 75 6c 6c 20  .if( af is null 
29c0: 29 0d 0a 09 09 09 09 72 65 74 75 72 6e 20 6e 75  )......return nu
29d0: 6c 6c 3b 0d 0a 09 09 09 72 65 74 75 72 6e 20 61  ll;.....return a
29e0: 73 74 32 74 61 62 6c 65 28 65 2c 20 28 41 53 54  st2table(e, (AST
29f0: 20 5f 29 7b 69 66 28 5f 20 69 73 20 65 2e 66 75   _){if(_ is e.fu
2a00: 6e 62 6f 64 79 29 72 65 74 75 72 6e 20 61 66 3b  nbody)return af;
2a10: 20 61 73 73 65 72 74 28 66 61 6c 73 65 29 3b 7d   assert(false);}
2a20: 29 3b 0d 0a 09 09 7d 0d 0a 09 7d 0d 0a 0d 0a 09  );....}...}.....
2a30: 2f 2f 2f 20 28 72 61 77 6d 61 63 72 6f 29 20 6c  /// (rawmacro) l
2a40: 61 79 65 72 2e 20 61 6c 6d 6f 73 74 20 73 61 6d  ayer. almost sam
2a50: 65 20 61 73 20 40 6d 61 63 72 6f 2c 20 62 75 74  e as @macro, but
2a60: 20 74 68 65 20 4c 61 79 65 72 20 65 78 70 72 65   the Layer expre
2a70: 73 73 69 6f 6e 20 62 65 63 6f 6d 65 73 20 41 53  ssion becomes AS
2a80: 54 2c 20 74 6f 6f 2e 0d 0a 09 63 6c 61 73 73 20  T, too....class 
2a90: 52 61 77 4d 61 63 72 6f 4c 61 79 65 72 45 76 61  RawMacroLayerEva
2aa0: 6c 20 3a 20 4d 61 63 72 6f 4c 61 79 65 72 45 76  l : MacroLayerEv
2ab0: 61 6c 0d 0a 09 7b 0d 0a 09 09 6f 76 65 72 72 69  al...{....overri
2ac0: 64 65 20 56 61 6c 75 65 20 65 76 61 6c 5f 28 20  de Value eval_( 
2ad0: 4c 61 79 20 65 2c 20 54 61 62 6c 65 20 63 74 78  Lay e, Table ctx
2ae0: 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64 20 29 0d  , bool ctxMod ).
2af0: 0a 09 09 7b 0d 0a 09 09 09 56 61 6c 75 65 20 61  ...{.....Value a
2b00: 65 20 3d 20 74 68 69 73 2e 65 76 61 6c 28 65 2e  e = this.eval(e.
2b10: 65 78 70 72 2c 20 63 74 78 29 3b 0d 0a 09 09 09  expr, ctx);.....
2b20: 72 65 74 75 72 6e 20 61 65 20 69 73 20 6e 75 6c  return ae is nul
2b30: 6c 20 3f 20 6e 75 6c 6c 0d 0a 09 09 09 20 20 20  l ? null.....   
2b40: 20 20 20 20 3a 20 61 73 74 32 74 61 62 6c 65 28      : ast2table(
2b50: 65 2c 20 64 65 6c 65 67 61 74 65 20 56 61 6c 75  e, delegate Valu
2b60: 65 20 28 41 53 54 20 5f 29 7b 69 66 28 5f 20 69  e (AST _){if(_ i
2b70: 73 20 65 2e 65 78 70 72 29 72 65 74 75 72 6e 20  s e.expr)return 
2b80: 61 65 3b 20 61 73 73 65 72 74 28 66 61 6c 73 65  ae; assert(false
2b90: 29 3b 7d 29 3b 0d 0a 09 09 7d 0d 0a 09 7d 0d 0a  );});....}...}..
2ba0: 0d 0a 70 72 69 76 61 74 65 3a 20 2f 2f 20 73 68  ..private: // sh
2bb0: 6f 72 74 20 75 74 69 6c 73 0d 0a 0d 0a 09 73 74  ort utils.....st
2bc0: 72 69 6e 67 20 67 65 74 4e 61 6d 65 49 66 50 6f  ring getNameIfPo
2bd0: 73 73 69 62 6c 65 28 41 53 54 20 65 29 0d 0a 09  ssible(AST e)...
2be0: 7b 0d 0a 09 09 69 66 28 61 75 74 6f 20 76 20 3d  {....if(auto v =
2bf0: 20 63 61 73 74 28 56 61 72 29 65 29 0d 0a 09 09   cast(Var)e)....
2c00: 09 72 65 74 75 72 6e 20 76 2e 6e 61 6d 65 3b 0d  .return v.name;.
2c10: 0a 09 09 72 65 74 75 72 6e 20 22 22 3b 0d 0a 09  ...return "";...
2c20: 7d 0d 0a 0d 0a 09 41 53 54 20 72 75 6e 4d 61 63  }.....AST runMac
2c30: 72 6f 28 41 53 54 20 65 2c 20 54 61 62 6c 65 20  ro(AST e, Table 
2c40: 63 74 78 2c 20 62 6f 6f 6c 20 63 74 78 4d 6f 64  ctx, bool ctxMod
2c50: 3d 4c 61 79 65 72 45 76 61 6c 2e 43 61 73 63 61  =LayerEval.Casca
2c60: 64 65 43 74 78 29 0d 0a 09 7b 0d 0a 09 09 56 61  deCtx)...{....Va
2c70: 6c 75 65 20 76 20 3d 20 67 65 74 4c 61 79 65 72  lue v = getLayer
2c80: 45 76 61 6c 28 52 61 77 4d 61 63 72 6f 4c 61 79  Eval(RawMacroLay
2c90: 65 72 29 2e 65 76 61 6c 28 65 2c 20 63 74 78 2c  er).eval(e, ctx,
2ca0: 20 63 74 78 4d 6f 64 29 3b 0d 0a 09 09 72 65 74   ctxMod);....ret
2cb0: 75 72 6e 20 28 76 20 69 73 20 6e 75 6c 6c 20 3f  urn (v is null ?
2cc0: 20 65 20 3a 20 70 6f 6c 65 6d 79 32 64 21 28 41   e : polemy2d!(A
2cd0: 53 54 29 28 76 2c 20 65 2e 70 6f 73 29 29 3b 0d  ST)(v, e.pos));.
2ce0: 0a 09 7d 0d 0a 0d 0a 70 72 69 76 61 74 65 3a 0d  ..}....private:.
2cf0: 0a 09 56 61 6c 75 65 20 63 72 65 61 74 65 4e 65  ..Value createNe
2d00: 77 46 75 6e 63 74 69 6f 6e 28 46 75 6e 20 65 2c  wFunction(Fun e,
2d10: 20 54 61 62 6c 65 20 63 74 78 29 0d 0a 09 7b 0d   Table ctx)...{.
2d20: 0a 09 09 63 6c 61 73 73 20 55 73 65 72 44 65 66  ...class UserDef
2d30: 69 6e 65 64 46 75 6e 56 61 6c 75 65 20 3a 20 46  inedFunValue : F
2d40: 75 6e 56 61 6c 75 65 0d 0a 09 09 7b 0d 0a 09 09  unValue....{....
2d50: 09 46 75 6e 20 20 20 61 73 74 3b 0d 0a 09 09 09  .Fun   ast;.....
2d60: 54 61 62 6c 65 20 64 65 66 43 74 78 3b 0d 0a 09  Table defCtx;...
2d70: 09 09 6f 76 65 72 72 69 64 65 20 63 6f 6e 73 74  ..override const
2d80: 28 50 61 72 61 6d 65 74 65 72 5b 5d 29 20 70 61  (Parameter[]) pa
2d90: 72 61 6d 73 28 29 20 7b 20 72 65 74 75 72 6e 20  rams() { return 
2da0: 61 73 74 2e 70 61 72 61 6d 73 3b 20 7d 0d 0a 09  ast.params; }...
2db0: 09 09 6f 76 65 72 72 69 64 65 20 54 61 62 6c 65  ..override Table
2dc0: 20 64 65 66 69 6e 69 74 69 6f 6e 43 6f 6e 74 65   definitionConte
2dd0: 78 74 28 29 20 20 20 7b 20 72 65 74 75 72 6e 20  xt()   { return 
2de0: 64 65 66 43 74 78 3b 20 7d 0d 0a 0d 0a 09 09 09  defCtx; }.......
2df0: 74 68 69 73 28 46 75 6e 20 61 73 74 2c 20 54 61  this(Fun ast, Ta
2e00: 62 6c 65 20 64 65 66 43 74 78 29 20 7b 20 74 68  ble defCtx) { th
2e10: 69 73 2e 61 73 74 3d 61 73 74 3b 20 74 68 69 73  is.ast=ast; this
2e20: 2e 64 65 66 43 74 78 3d 64 65 66 43 74 78 3b 20  .defCtx=defCtx; 
2e30: 7d 0d 0a 09 09 09 6f 76 65 72 72 69 64 65 20 73  }.....override s
2e40: 74 72 69 6e 67 20 74 6f 53 74 72 69 6e 67 28 29  tring toString()
2e50: 20 63 6f 6e 73 74 0d 0a 09 09 09 09 7b 20 72 65   const......{ re
2e60: 74 75 72 6e 20 73 70 72 69 6e 74 66 21 22 28 66  turn sprintf!"(f
2e70: 75 6e 63 74 69 6f 6e 3a 25 78 3a 25 78 29 22 28  unction:%x:%x)"(
2e80: 63 61 73 74 28 76 6f 69 64 2a 29 61 73 74 2c 20  cast(void*)ast, 
2e90: 63 61 73 74 28 76 6f 69 64 2a 29 64 65 66 43 74  cast(void*)defCt
2ea0: 78 29 3b 20 7d 0d 0a 09 09 09 6f 76 65 72 72 69  x); }.....overri
2eb0: 64 65 20 69 6e 74 20 6f 70 43 6d 70 28 4f 62 6a  de int opCmp(Obj
2ec0: 65 63 74 20 72 68 73 29 20 7b 0d 0a 09 09 09 09  ect rhs) {......
2ed0: 69 66 28 61 75 74 6f 20 72 20 3d 20 63 61 73 74  if(auto r = cast
2ee0: 28 55 73 65 72 44 65 66 69 6e 65 64 46 75 6e 56  (UserDefinedFunV
2ef0: 61 6c 75 65 29 72 68 73 29 20 7b 0d 0a 09 09 09  alue)rhs) {.....
2f00: 09 09 69 66 28 61 75 74 6f 20 63 20 3d 20 74 79  ..if(auto c = ty
2f10: 70 65 69 64 28 76 6f 69 64 2a 29 2e 63 6f 6d 70  peid(void*).comp
2f20: 61 72 65 28 63 61 73 74 28 76 6f 69 64 2a 29 61  are(cast(void*)a
2f30: 73 74 2c 20 63 61 73 74 28 76 6f 69 64 2a 29 72  st, cast(void*)r
2f40: 2e 61 73 74 29 29 0d 0a 09 09 09 09 09 09 72 65  .ast))........re
2f50: 74 75 72 6e 20 63 3b 0d 0a 09 09 09 09 09 69 66  turn c;.......if
2f60: 28 61 75 74 6f 20 63 20 3d 20 74 79 70 65 69 64  (auto c = typeid
2f70: 28 76 6f 69 64 2a 29 2e 63 6f 6d 70 61 72 65 28  (void*).compare(
2f80: 63 61 73 74 28 76 6f 69 64 2a 29 64 65 66 43 74  cast(void*)defCt
2f90: 78 2c 20 63 61 73 74 28 76 6f 69 64 2a 29 72 2e  x, cast(void*)r.
2fa0: 64 65 66 43 74 78 29 29 0d 0a 09 09 09 09 09 09  defCtx))........
2fb0: 72 65 74 75 72 6e 20 63 3b 0d 0a 09 09 09 09 09  return c;.......
2fc0: 72 65 74 75 72 6e 20 30 3b 2f 2f 20 5b 54 4f 44  return 0;// [TOD
2fd0: 4f 5d 20 61 76 6f 69 64 20 75 73 69 6e 67 20 70  O] avoid using p
2fe0: 6f 69 6e 74 65 72 20 76 61 6c 75 65 2e 2e 2e 0d  ointer value....
2ff0: 0a 09 09 09 09 7d 0d 0a 09 09 09 09 69 66 28 61  .....}......if(a
3000: 75 74 6f 20 72 20 3d 20 63 61 73 74 28 56 61 6c  uto r = cast(Val
3010: 75 65 29 72 68 73 29 20 72 65 74 75 72 6e 20 74  ue)rhs) return t
3020: 79 70 65 69 64 28 74 68 69 73 29 2e 6f 70 43 6d  ypeid(this).opCm
3030: 70 28 74 79 70 65 69 64 28 72 29 29 3b 0d 0a 09  p(typeid(r));...
3040: 09 09 09 74 68 72 6f 77 20 67 65 6e 65 78 21 52  ...throw genex!R
3050: 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e 28  untimeException(
3060: 22 63 6f 6d 70 61 72 69 73 6f 6e 20 77 69 74 68  "comparison with
3070: 20 76 61 6c 75 65 20 61 6e 64 20 73 6f 6d 65 74   value and somet
3080: 68 69 6e 67 20 6f 74 68 65 72 22 29 3b 0d 0a 09  hing other");...
3090: 09 09 7d 0d 0a 09 09 09 6f 76 65 72 72 69 64 65  ..}.....override
30a0: 20 68 61 73 68 5f 74 20 74 6f 48 61 73 68 28 29   hash_t toHash()
30b0: 20 7b 0d 0a 09 09 09 09 72 65 74 75 72 6e 20 28   {......return (
30c0: 63 61 73 74 28 68 61 73 68 5f 74 29 63 61 73 74  cast(hash_t)cast
30d0: 28 76 6f 69 64 2a 29 61 73 74 29 20 2b 20 28 63  (void*)ast) + (c
30e0: 61 73 74 28 68 61 73 68 5f 74 29 63 61 73 74 28  ast(hash_t)cast(
30f0: 76 6f 69 64 2a 29 64 65 66 43 74 78 29 3b 0d 0a  void*)defCtx);..
3100: 09 09 09 7d 0d 0a 0d 0a 09 09 09 41 53 54 5b 76  ...}.......AST[v
3110: 6f 69 64 2a 5d 20 6d 61 6e 64 65 43 61 63 68 65  oid*] mandeCache
3120: 3b 0d 0a 09 09 09 73 74 61 74 69 63 20 63 6c 61  ;.....static cla
3130: 73 73 20 4d 65 6d 6f 6b 65 79 54 79 70 65 0d 0a  ss MemokeyType..
3140: 09 09 09 7b 0d 0a 09 09 09 09 76 6f 69 64 2a 20  ...{......void* 
3150: 61 3b 20 4c 61 79 65 72 20 62 3b 20 54 61 62 6c  a; Layer b; Tabl
3160: 65 20 63 3b 0d 0a 09 09 09 09 6d 69 78 69 6e 20  e c;......mixin 
3170: 53 69 6d 70 6c 65 43 6c 61 73 73 3b 0d 0a 09 09  SimpleClass;....
3180: 09 7d 0d 0a 09 09 09 73 74 61 74 69 63 20 54 75  .}.....static Tu
3190: 70 6c 65 21 28 56 61 6c 75 65 2c 69 6e 74 29 5b  ple!(Value,int)[
31a0: 4d 65 6d 6f 6b 65 79 54 79 70 65 5d 20 6d 65 6d  MemokeyType] mem
31b0: 6f 3b 0d 0a 0d 0a 09 09 09 6f 76 65 72 72 69 64  o;.......overrid
31c0: 65 20 56 61 6c 75 65 20 69 6e 76 6f 6b 65 28 4c  e Value invoke(L
31d0: 61 79 65 72 20 6c 61 79 2c 20 54 61 62 6c 65 20  ayer lay, Table 
31e0: 63 74 78 2c 20 4c 65 78 50 6f 73 69 74 69 6f 6e  ctx, LexPosition
31f0: 20 70 6f 73 29 0d 0a 09 09 09 7b 0d 0a 09 09 09   pos).....{.....
3200: 09 61 75 74 6f 20 65 76 6c 61 79 20 3d 20 67 65  .auto evlay = ge
3210: 74 4c 61 79 65 72 45 76 61 6c 28 6c 61 79 29 3b  tLayerEval(lay);
3220: 0d 0a 09 09 09 09 61 75 74 6f 20 6e 6f 6e 4d 65  ......auto nonMe
3230: 6d 6f 69 7a 65 64 52 75 6e 20 3d 20 28 29 7b 20  moizedRun = (){ 
3240: 72 65 74 75 72 6e 20 65 76 6c 61 79 2e 6d 61 63  return evlay.mac
3250: 72 6f 41 6e 64 45 76 61 6c 28 61 73 74 2e 66 75  roAndEval(ast.fu
3260: 6e 62 6f 64 79 2c 20 63 74 78 29 3b 20 7d 3b 0d  nbody, ctx); };.
3270: 0a 0d 0a 09 09 09 09 69 66 28 20 21 69 73 55 73  .......if( !isUs
3280: 65 72 44 65 66 69 6e 65 64 4c 61 79 65 72 28 6c  erDefinedLayer(l
3290: 61 79 29 20 29 0d 0a 09 09 09 09 09 72 65 74 75  ay) ).......retu
32a0: 72 6e 20 6e 6f 6e 4d 65 6d 6f 69 7a 65 64 52 75  rn nonMemoizedRu
32b0: 6e 28 29 3b 0d 0a 0d 0a 09 09 09 09 2f 2f 20 61  n();........// a
32c0: 75 74 6f 6d 61 74 69 63 20 6d 65 6d 6f 69 7a 65  utomatic memoize
32d0: 64 20 63 6f 2d 72 65 63 75 72 73 69 76 65 20 65  d co-recursive e
32e0: 78 65 63 75 74 69 6f 6e 0d 0a 09 09 09 09 4d 65  xecution......Me
32f0: 6d 6f 6b 65 79 54 79 70 65 20 6d 65 6d 6f 6b 65  mokeyType memoke
3300: 79 20 3d 20 6e 65 77 20 4d 65 6d 6f 6b 65 79 54  y = new MemokeyT
3310: 79 70 65 28 63 61 73 74 28 76 6f 69 64 2a 29 61  ype(cast(void*)a
3320: 73 74 2c 20 6c 61 79 2c 20 63 74 78 29 3b 0d 0a  st, lay, ctx);..
3330: 09 09 09 09 69 66 28 61 75 74 6f 20 70 20 3d 20  ....if(auto p = 
3340: 6d 65 6d 6f 6b 65 79 20 69 6e 20 6d 65 6d 6f 29  memokey in memo)
3350: 0d 0a 09 09 09 09 7b 0d 0a 09 09 09 09 09 69 66  ......{.......if
3360: 28 20 2b 2b 28 2a 70 29 5b 31 5d 20 3e 3d 20 32  ( ++(*p)[1] >= 2
3370: 20 29 20 2f 2f 20 5b 54 4f 44 4f 5d 20 69 73 20   ) // [TODO] is 
3380: 32 20 72 65 61 6c 6c 79 20 65 6e 6f 75 67 68 3f  2 really enough?
3390: 3f 0d 0a 09 09 09 09 09 09 72 65 74 75 72 6e 20  ?........return 
33a0: 28 2a 70 29 5b 30 5d 3b 0d 0a 09 09 09 09 7d 0d  (*p)[0];......}.
33b0: 0a 09 09 09 09 65 6c 73 65 0d 0a 09 09 09 09 7b  .....else......{
33c0: 0d 0a 09 09 09 09 09 56 61 6c 75 65 20 76 3b 0d  .......Value v;.
33d0: 0a 09 09 09 09 09 74 72 79 20 7b 20 76 20 3d 20  ......try { v = 
33e0: 65 76 6c 61 79 2e 6c 69 66 74 28 6e 65 77 20 42  evlay.lift(new B
33f0: 6f 74 74 6f 6d 56 61 6c 75 65 2c 20 63 74 78 2c  ottomValue, ctx,
3400: 20 70 6f 73 29 3b 20 7d 20 63 61 74 63 68 20 7b   pos); } catch {
3410: 20 76 20 3d 20 6e 65 77 20 42 6f 74 74 6f 6d 56   v = new BottomV
3420: 61 6c 75 65 3b 20 7d 0d 0a 09 09 09 09 09 6d 65  alue; }.......me
3430: 6d 6f 5b 6d 65 6d 6f 6b 65 79 5d 20 3d 20 74 75  mo[memokey] = tu
3440: 70 6c 65 28 76 2c 20 30 29 3b 0d 0a 09 09 09 09  ple(v, 0);......
3450: 7d 0d 0a 0d 0a 09 09 09 09 56 61 6c 75 65 20 72  }........Value r
3460: 20 3d 20 6e 6f 6e 4d 65 6d 6f 69 7a 65 64 52 75   = nonMemoizedRu
3470: 6e 28 29 3b 0d 0a 09 09 09 09 6d 65 6d 6f 5b 6d  n();......memo[m
3480: 65 6d 6f 6b 65 79 5d 20 3d 20 74 75 70 6c 65 28  emokey] = tuple(
3490: 72 2c 20 39 39 39 39 29 3b 0d 0a 09 09 09 09 72  r, 9999);......r
34a0: 65 74 75 72 6e 20 72 3b 0d 0a 09 09 09 7d 0d 0a  eturn r;.....}..
34b0: 09 09 7d 0d 0a 09 09 72 65 74 75 72 6e 20 6e 65  ..}....return ne
34c0: 77 20 55 73 65 72 44 65 66 69 6e 65 64 46 75 6e  w UserDefinedFun
34d0: 56 61 6c 75 65 28 65 2c 63 74 78 29 3b 0d 0a 09  Value(e,ctx);...
34e0: 7d 0d 0a 0d 0a 70 75 62 6c 69 63 3a 0d 0a 09 2f  }....public:.../
34f0: 2f 2f 20 41 64 64 20 70 72 69 6d 69 74 69 76 65  // Add primitive
3500: 20 66 75 6e 63 74 69 6f 6e 20 74 6f 20 74 68 65   function to the
3510: 20 67 6c 6f 62 61 6c 20 63 6f 6e 74 65 78 74 0d   global context.
3520: 0a 09 76 6f 69 64 20 61 64 64 50 72 69 6d 69 74  ..void addPrimit
3530: 69 76 65 28 52 2c 54 2e 2e 2e 29 28 73 74 72 69  ive(R,T...)(stri
3540: 6e 67 20 6e 61 6d 65 2c 20 4c 61 79 65 72 20 64  ng name, Layer d
3550: 65 66 4c 61 79 2c 20 52 20 64 65 6c 65 67 61 74  efLay, R delegat
3560: 65 20 28 54 29 20 64 67 29 0d 0a 09 7b 0d 0a 09  e (T) dg)...{...
3570: 09 63 6c 61 73 73 20 4e 61 74 69 76 65 46 75 6e  .class NativeFun
3580: 56 61 6c 75 65 20 3a 20 46 75 6e 56 61 6c 75 65  Value : FunValue
3590: 0d 0a 09 09 7b 0d 0a 09 09 09 6f 76 65 72 72 69  ....{.....overri
35a0: 64 65 20 63 6f 6e 73 74 28 50 61 72 61 6d 65 74  de const(Paramet
35b0: 65 72 5b 5d 29 20 70 61 72 61 6d 73 28 29 20 7b  er[]) params() {
35c0: 20 72 65 74 75 72 6e 20 70 61 72 61 6d 73 5f 64   return params_d
35d0: 61 74 61 3b 20 7d 0d 0a 09 09 09 6f 76 65 72 72  ata; }.....overr
35e0: 69 64 65 20 54 61 62 6c 65 20 64 65 66 69 6e 69  ide Table defini
35f0: 74 69 6f 6e 43 6f 6e 74 65 78 74 28 29 20 20 20  tionContext()   
3600: 7b 20 72 65 74 75 72 6e 20 74 68 65 43 6f 6e 74  { return theCont
3610: 65 78 74 3b 20 7d 0d 0a 0d 0a 09 09 09 6f 76 65  ext; }.......ove
3620: 72 72 69 64 65 20 73 74 72 69 6e 67 20 74 6f 53  rride string toS
3630: 74 72 69 6e 67 28 29 20 7b 20 72 65 74 75 72 6e  tring() { return
3640: 20 73 70 72 69 6e 74 66 21 22 28 6e 61 74 69 76   sprintf!"(nativ
3650: 65 3a 25 78 29 22 28 64 67 2e 66 75 6e 63 70 74  e:%x)"(dg.funcpt
3660: 72 29 3b 20 7d 0d 0a 09 09 09 6f 76 65 72 72 69  r); }.....overri
3670: 64 65 20 69 6e 74 20 6f 70 43 6d 70 28 4f 62 6a  de int opCmp(Obj
3680: 65 63 74 20 72 68 73 29 20 7b 0d 0a 09 09 09 09  ect rhs) {......
3690: 69 66 28 61 75 74 6f 20 72 20 3d 20 63 61 73 74  if(auto r = cast
36a0: 28 4e 61 74 69 76 65 46 75 6e 56 61 6c 75 65 29  (NativeFunValue)
36b0: 72 68 73 29 20 72 65 74 75 72 6e 20 74 79 70 65  rhs) return type
36c0: 69 64 28 74 79 70 65 6f 66 28 64 67 29 29 2e 63  id(typeof(dg)).c
36d0: 6f 6d 70 61 72 65 28 26 64 67 2c 26 72 2e 64 67  ompare(&dg,&r.dg
36e0: 29 3b 0d 0a 09 09 09 09 69 66 28 61 75 74 6f 20  );......if(auto 
36f0: 72 20 3d 20 63 61 73 74 28 56 61 6c 75 65 29 72  r = cast(Value)r
3700: 68 73 29 20 20 20 20 20 20 20 20 20 20 72 65 74  hs)          ret
3710: 75 72 6e 20 74 79 70 65 69 64 28 74 68 69 73 29  urn typeid(this)
3720: 2e 6f 70 43 6d 70 28 74 79 70 65 69 64 28 72 29  .opCmp(typeid(r)
3730: 29 3b 0d 0a 09 09 09 09 74 68 72 6f 77 20 67 65  );......throw ge
3740: 6e 65 78 21 52 75 6e 74 69 6d 65 45 78 63 65 70  nex!RuntimeExcep
3750: 74 69 6f 6e 28 22 63 6f 6d 70 61 72 69 73 6f 6e  tion("comparison
3760: 20 77 69 74 68 20 76 61 6c 75 65 20 61 6e 64 20   with value and 
3770: 73 6f 6d 65 74 68 69 6e 67 20 6f 74 68 65 72 22  something other"
3780: 29 3b 0d 0a 09 09 09 7d 0d 0a 09 09 09 6f 76 65  );.....}.....ove
3790: 72 72 69 64 65 20 68 61 73 68 5f 74 20 74 6f 48  rride hash_t toH
37a0: 61 73 68 28 29 20 63 6f 6e 73 74 20 7b 0d 0a 09  ash() const {...
37b0: 09 09 09 72 65 74 75 72 6e 20 74 79 70 65 69 64  ...return typeid
37c0: 28 64 67 29 2e 67 65 74 48 61 73 68 28 26 64 67  (dg).getHash(&dg
37d0: 29 3b 0d 0a 09 09 09 7d 0d 0a 0d 0a 09 09 09 52  );.....}.......R
37e0: 20 64 65 6c 65 67 61 74 65 28 54 29 20 64 67 3b   delegate(T) dg;
37f0: 0d 0a 09 09 09 50 61 72 61 6d 65 74 65 72 5b 5d  .....Parameter[]
3800: 20 70 61 72 61 6d 73 5f 64 61 74 61 3b 0d 0a 0d   params_data;...
3810: 0a 09 09 09 74 68 69 73 28 52 20 64 65 6c 65 67  ....this(R deleg
3820: 61 74 65 28 54 29 20 64 67 29 0d 0a 09 09 09 7b  ate(T) dg).....{
3830: 0d 0a 09 09 09 09 74 68 69 73 2e 64 67 20 3d 20  ......this.dg = 
3840: 64 67 3b 0d 0a 09 09 09 09 66 6f 72 65 61 63 68  dg;......foreach
3850: 28 69 2c 20 54 69 3b 20 54 29 0d 0a 09 09 09 09  (i, Ti; T)......
3860: 09 70 61 72 61 6d 73 5f 64 61 74 61 20 7e 3d 20  .params_data ~= 
3870: 6e 65 77 20 50 61 72 61 6d 65 74 65 72 28 74 65  new Parameter(te
3880: 78 74 28 69 29 2c 20 5b 5d 29 3b 0d 0a 09 09 09  xt(i), []);.....
3890: 7d 0d 0a 0d 0a 09 09 09 6f 76 65 72 72 69 64 65  }.......override
38a0: 20 56 61 6c 75 65 20 69 6e 76 6f 6b 65 28 4c 61   Value invoke(La
38b0: 79 65 72 20 6c 61 79 2c 20 54 61 62 6c 65 20 63  yer lay, Table c
38c0: 74 78 2c 20 4c 65 78 50 6f 73 69 74 69 6f 6e 20  tx, LexPosition 
38d0: 70 6f 73 29 0d 0a 09 09 09 7b 0d 0a 09 09 09 09  pos).....{......
38e0: 69 66 28 20 6c 61 79 20 21 3d 20 64 65 66 4c 61  if( lay != defLa
38f0: 79 20 29 0d 0a 09 09 09 09 09 74 68 72 6f 77 20  y ).......throw 
3900: 67 65 6e 65 78 21 52 75 6e 74 69 6d 65 45 78 63  genex!RuntimeExc
3910: 65 70 74 69 6f 6e 28 70 6f 73 2c 0d 0a 09 09 09  eption(pos,.....
3920: 09 09 09 74 65 78 74 28 22 6f 6e 6c 79 20 22 2c  ...text("only ",
3930: 20 64 65 66 4c 61 79 2c 20 22 20 6c 61 79 65 72   defLay, " layer
3940: 20 63 61 6e 20 63 61 6c 6c 20 6e 61 74 69 76 65   can call native
3950: 20 66 75 6e 63 74 69 6f 6e 3a 20 22 2c 20 6e 61   function: ", na
3960: 6d 65 29 29 3b 0d 0a 09 09 09 09 54 20 74 79 70  me));......T typ
3970: 65 64 5f 61 72 67 73 3b 0d 0a 09 09 09 09 66 6f  ed_args;......fo
3980: 72 65 61 63 68 28 69 2c 20 54 69 3b 20 54 29 20  reach(i, Ti; T) 
3990: 7b 0d 0a 09 09 09 09 09 56 61 6c 75 65 20 76 69  {.......Value vi
39a0: 20 3d 20 63 74 78 2e 67 65 74 28 74 65 78 74 28   = ctx.get(text(
39b0: 69 29 2c 20 56 61 6c 75 65 4c 61 79 65 72 2c 20  i), ValueLayer, 
39c0: 70 6f 73 29 3b 0d 0a 09 09 09 09 09 74 79 70 65  pos);.......type
39d0: 64 5f 61 72 67 73 5b 69 5d 20 3d 20 63 61 73 74  d_args[i] = cast
39e0: 28 54 69 29 20 76 69 3b 0d 0a 09 09 09 09 09 69  (Ti) vi;.......i
39f0: 66 28 20 74 79 70 65 64 5f 61 72 67 73 5b 69 5d  f( typed_args[i]
3a00: 20 69 73 20 6e 75 6c 6c 20 29 0d 0a 09 09 09 09   is null )......
3a10: 09 09 74 68 72 6f 77 20 67 65 6e 65 78 21 52 75  ..throw genex!Ru
3a20: 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e 28 70  ntimeException(p
3a30: 6f 73 2c 0d 0a 09 09 09 09 09 09 09 73 70 72 69  os,.........spri
3a40: 6e 74 66 21 22 74 79 70 65 20 6d 69 73 6d 61 74  ntf!"type mismat
3a50: 63 68 20 6f 6e 20 74 68 65 20 61 72 67 75 6d 65  ch on the argume
3a60: 6e 74 20 25 64 20 6f 66 20 6e 61 74 69 76 65 20  nt %d of native 
3a70: 66 75 6e 63 74 69 6f 6e 20 25 73 2e 20 25 73 20  function %s. %s 
3a80: 72 65 71 75 69 72 65 64 20 62 75 74 20 25 73 20  required but %s 
3a90: 70 61 73 73 65 64 2e 22 28 69 2b 31 2c 6e 61 6d  passed."(i+1,nam
3aa0: 65 2c 74 79 70 65 69 64 28 54 69 29 2c 76 69 29  e,typeid(Ti),vi)
3ab0: 29 3b 0d 0a 09 09 09 09 7d 0d 0a 09 09 09 09 74  );......}......t
3ac0: 72 79 20 7b 0d 0a 09 09 09 09 09 72 65 74 75 72  ry {.......retur
3ad0: 6e 20 64 67 28 74 79 70 65 64 5f 61 72 67 73 29  n dg(typed_args)
3ae0: 3b 0d 0a 09 09 09 09 7d 20 63 61 74 63 68 28 20  ;......} catch( 
3af0: 52 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e  RuntimeException
3b00: 20 65 20 29 20 7b 0d 0a 09 09 09 09 09 74 68 72   e ) {.......thr
3b10: 6f 77 20 65 2e 70 6f 73 20 69 73 20 6e 75 6c 6c  ow e.pos is null
3b20: 20 3f 20 6e 65 77 20 52 75 6e 74 69 6d 65 45 78   ? new RuntimeEx
3b30: 63 65 70 74 69 6f 6e 28 70 6f 73 2c 20 65 2e 6d  ception(pos, e.m
3b40: 73 67 2c 20 65 2e 66 69 6c 65 2c 20 65 2e 6c 69  sg, e.file, e.li
3b50: 6e 65 29 20 3a 20 65 3b 0d 0a 09 09 09 09 7d 0d  ne) : e;......}.
3b60: 0a 09 09 09 7d 0d 0a 09 09 7d 0d 0a 09 09 74 68  ....}....}....th
3b70: 65 43 6f 6e 74 65 78 74 2e 73 65 74 28 6e 61 6d  eContext.set(nam
3b80: 65 2c 20 64 65 66 4c 61 79 2c 20 6e 65 77 20 4e  e, defLay, new N
3b90: 61 74 69 76 65 46 75 6e 56 61 6c 75 65 28 64 67  ativeFunValue(dg
3ba0: 29 29 3b 0d 0a 09 7d 0d 0a 7d 0d 0a 0d 0a 76 65  ));...}..}....ve
3bb0: 72 73 69 6f 6e 28 75 6e 69 74 74 65 73 74 29 0d  rsion(unittest).
3bc0: 0a 09 69 6d 70 6f 72 74 20 70 6f 6c 65 6d 79 2e  ..import polemy.
3bd0: 72 75 6e 74 69 6d 65 3b 0d 0a 0d 0a 75 6e 69 74  runtime;....unit
3be0: 74 65 73 74 0d 0a 7b 0d 0a 09 61 75 74 6f 20 65  test..{...auto e
3bf0: 20 3d 20 6e 65 77 20 45 76 61 6c 75 61 74 6f 72   = new Evaluator
3c00: 3b 0d 0a 09 65 6e 72 6f 6c 6c 52 75 6e 74 69 6d  ;...enrollRuntim
3c10: 65 4c 69 62 72 61 72 79 28 65 29 3b 0d 0a 09 61  eLibrary(e);...a
3c20: 73 73 65 72 74 5f 65 71 28 20 65 2e 65 76 61 6c  ssert_eq( e.eval
3c30: 53 74 72 69 6e 67 28 60 0d 0a 40 40 66 6f 6f 28  String(`..@@foo(
3c40: 78 29 7b 78 2a 32 7d 3b 0d 0a 40 66 6f 6f 20 22  x){x*2};..@foo "
3c50: 2b 22 20 28 78 2c 79 29 7b 78 7d 3b 0d 0a 40 66  +" (x,y){x};..@f
3c60: 6f 6f 28 33 2b 34 29 0d 0a 60 29 2c 20 6e 65 77  oo(3+4)..`), new
3c70: 20 49 6e 74 56 61 6c 75 65 28 36 29 20 29 3b 0d   IntValue(6) );.
3c80: 0a 7d 0d 0a 0d 0a 75 6e 69 74 74 65 73 74 0d 0a  .}....unittest..
3c90: 7b 0d 0a 09 61 75 74 6f 20 65 20 3d 20 6e 65 77  {...auto e = new
3ca0: 20 45 76 61 6c 75 61 74 6f 72 3b 0d 0a 09 65 6e   Evaluator;...en
3cb0: 72 6f 6c 6c 52 75 6e 74 69 6d 65 4c 69 62 72 61  rollRuntimeLibra
3cc0: 72 79 28 65 29 3b 0d 0a 09 61 75 74 6f 20 72 20  ry(e);...auto r 
3cd0: 3d 20 61 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77  = assert_nothrow
3ce0: 28 20 65 2e 65 76 61 6c 53 74 72 69 6e 67 28 60  ( e.evalString(`
3cf0: 76 61 72 20 78 20 3d 20 32 31 3b 20 78 20 2b 20  var x = 21; x + 
3d00: 78 2a 78 3b 60 29 20 29 3b 0d 0a 09 61 73 73 65  x*x;`) );...asse
3d10: 72 74 5f 65 71 28 20 72 2c 20 6e 65 77 20 49 6e  rt_eq( r, new In
3d20: 74 56 61 6c 75 65 28 42 69 67 49 6e 74 28 32 31  tValue(BigInt(21
3d30: 2b 32 31 2a 32 31 29 29 20 29 3b 0d 0a 09 61 73  +21*21)) );...as
3d40: 73 65 72 74 5f 65 71 28 20 65 2e 67 6c 6f 62 61  sert_eq( e.globa
3d50: 6c 43 6f 6e 74 65 78 74 2e 67 65 74 28 22 78 22  lContext.get("x"
3d60: 2c 56 61 6c 75 65 4c 61 79 65 72 29 2c 20 6e 65  ,ValueLayer), ne
3d70: 77 20 49 6e 74 56 61 6c 75 65 28 42 69 67 49 6e  w IntValue(BigIn
3d80: 74 28 32 31 29 29 20 29 3b 0d 0a 09 61 73 73 65  t(21)) );...asse
3d90: 72 74 5f 6e 6f 74 68 72 6f 77 28 20 65 2e 67 6c  rt_nothrow( e.gl
3da0: 6f 62 61 6c 43 6f 6e 74 65 78 74 2e 67 65 74 28  obalContext.get(
3db0: 22 78 22 2c 56 61 6c 75 65 4c 61 79 65 72 29 20  "x",ValueLayer) 
3dc0: 29 3b 0d 0a 09 61 73 73 65 72 74 5f 74 68 72 6f  );...assert_thro
3dd0: 77 21 52 75 6e 74 69 6d 65 45 78 63 65 70 74 69  w!RuntimeExcepti
3de0: 6f 6e 28 20 65 2e 67 6c 6f 62 61 6c 43 6f 6e 74  on( e.globalCont
3df0: 65 78 74 2e 67 65 74 28 22 79 22 2c 56 61 6c 75  ext.get("y",Valu
3e00: 65 4c 61 79 65 72 29 20 29 3b 0d 0a 7d 0d 0a 0d  eLayer) );..}...
3e10: 0a 75 6e 69 74 74 65 73 74 0d 0a 7b 0d 0a 09 61  .unittest..{...a
3e20: 75 74 6f 20 65 20 3d 20 6e 65 77 20 45 76 61 6c  uto e = new Eval
3e30: 75 61 74 6f 72 3b 0d 0a 09 65 6e 72 6f 6c 6c 52  uator;...enrollR
3e40: 75 6e 74 69 6d 65 4c 69 62 72 61 72 79 28 65 29  untimeLibrary(e)
3e50: 3b 0d 0a 09 61 75 74 6f 20 72 20 3d 20 61 73 73  ;...auto r = ass
3e60: 65 72 74 5f 6e 6f 74 68 72 6f 77 28 20 65 2e 65  ert_nothrow( e.e
3e70: 76 61 6c 53 74 72 69 6e 67 28 60 76 61 72 20 78  valString(`var x
3e80: 20 3d 20 32 31 3b 20 76 61 72 20 78 20 3d 20 78   = 21; var x = x
3e90: 20 2b 20 78 2a 78 3b 60 29 20 29 3b 0d 0a 09 61   + x*x;`) );...a
3ea0: 73 73 65 72 74 5f 65 71 28 20 72 2c 20 6e 65 77  ssert_eq( r, new
3eb0: 20 49 6e 74 56 61 6c 75 65 28 42 69 67 49 6e 74   IntValue(BigInt
3ec0: 28 32 31 2b 32 31 2a 32 31 29 29 20 29 3b 0d 0a  (21+21*21)) );..
3ed0: 09 61 73 73 65 72 74 5f 65 71 28 20 65 2e 67 6c  .assert_eq( e.gl
3ee0: 6f 62 61 6c 43 6f 6e 74 65 78 74 2e 67 65 74 28  obalContext.get(
3ef0: 22 78 22 2c 56 61 6c 75 65 4c 61 79 65 72 29 2c  "x",ValueLayer),
3f00: 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 32 31   new IntValue(21
3f10: 2b 32 31 2a 32 31 29 20 29 3b 0d 0a 09 61 73 73  +21*21) );...ass
3f20: 65 72 74 5f 6e 6f 74 68 72 6f 77 28 20 65 2e 67  ert_nothrow( e.g
3f30: 6c 6f 62 61 6c 43 6f 6e 74 65 78 74 2e 67 65 74  lobalContext.get
3f40: 28 22 78 22 2c 56 61 6c 75 65 4c 61 79 65 72 29  ("x",ValueLayer)
3f50: 20 29 3b 0d 0a 09 61 73 73 65 72 74 5f 74 68 72   );...assert_thr
3f60: 6f 77 21 52 75 6e 74 69 6d 65 45 78 63 65 70 74  ow!RuntimeExcept
3f70: 69 6f 6e 28 20 65 2e 67 6c 6f 62 61 6c 43 6f 6e  ion( e.globalCon
3f80: 74 65 78 74 2e 67 65 74 28 22 79 22 2c 56 61 6c  text.get("y",Val
3f90: 75 65 4c 61 79 65 72 29 20 29 3b 0d 0a 7d 0d 0a  ueLayer) );..}..
3fa0: 75 6e 69 74 74 65 73 74 0d 0a 7b 0d 0a 09 61 75  unittest..{...au
3fb0: 74 6f 20 65 20 3d 20 6e 65 77 20 45 76 61 6c 75  to e = new Evalu
3fc0: 61 74 6f 72 3b 0d 0a 09 65 6e 72 6f 6c 6c 52 75  ator;...enrollRu
3fd0: 6e 74 69 6d 65 4c 69 62 72 61 72 79 28 65 29 3b  ntimeLibrary(e);
3fe0: 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20 65 2e  ...assert_eq( e.
3ff0: 65 76 61 6c 53 74 72 69 6e 67 28 60 6c 65 74 20  evalString(`let 
4000: 78 3d 31 3b 20 6c 65 74 20 79 3d 28 6c 65 74 20  x=1; let y=(let 
4010: 78 3d 32 29 3b 20 78 60 29 2c 20 6e 65 77 20 49  x=2); x`), new I
4020: 6e 74 56 61 6c 75 65 28 31 29 20 29 3b 20 0d 0a  ntValue(1) ); ..
4030: 09 61 73 73 65 72 74 5f 65 71 28 20 65 2e 65 76  .assert_eq( e.ev
4040: 61 6c 53 74 72 69 6e 67 28 60 6c 65 74 20 78 3d  alString(`let x=
4050: 31 3b 20 6c 65 74 20 79 3d 28 6c 65 74 20 78 3d  1; let y=(let x=
4060: 32 3b 66 75 6e 28 29 7b 78 7d 29 3b 20 79 28 29  2;fun(){x}); y()
4070: 60 29 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65  `), new IntValue
4080: 28 32 29 20 29 3b 20 0d 0a 7d 0d 0a 0d 0a 75 6e  (2) ); ..}....un
4090: 69 74 74 65 73 74 0d 0a 7b 0d 0a 09 61 75 74 6f  ittest..{...auto
40a0: 20 65 20 3d 20 6e 65 77 20 45 76 61 6c 75 61 74   e = new Evaluat
40b0: 6f 72 3b 0d 0a 09 65 6e 72 6f 6c 6c 52 75 6e 74  or;...enrollRunt
40c0: 69 6d 65 4c 69 62 72 61 72 79 28 65 29 3b 0d 0a  imeLibrary(e);..
40d0: 09 61 73 73 65 72 74 5f 65 71 28 20 65 2e 65 76  .assert_eq( e.ev
40e0: 61 6c 53 74 72 69 6e 67 28 60 40 61 20 78 3d 31  alString(`@a x=1
40f0: 3b 20 40 62 20 78 3d 32 3b 20 40 61 28 78 29 60  ; @b x=2; @a(x)`
4100: 29 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28  ), new IntValue(
4110: 42 69 67 49 6e 74 28 31 29 29 20 29 3b 0d 0a 09  BigInt(1)) );...
4120: 61 73 73 65 72 74 5f 65 71 28 20 65 2e 65 76 61  assert_eq( e.eva
4130: 6c 53 74 72 69 6e 67 28 60 40 61 20 78 3d 31 3b  lString(`@a x=1;
4140: 20 40 62 20 78 3d 32 3b 20 40 62 28 78 29 60 29   @b x=2; @b(x)`)
4150: 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 42  , new IntValue(B
4160: 69 67 49 6e 74 28 32 29 29 20 29 3b 0d 0a 09 61  igInt(2)) );...a
4170: 73 73 65 72 74 5f 65 71 28 20 65 2e 65 76 61 6c  ssert_eq( e.eval
4180: 53 74 72 69 6e 67 28 60 6c 65 74 20 78 3d 31 3b  String(`let x=1;
4190: 20 6c 65 74 20 5f 20 3d 20 28 40 61 20 78 3d 32   let _ = (@a x=2
41a0: 3b 32 29 3b 20 78 60 29 2c 20 6e 65 77 20 49 6e  ;2); x`), new In
41b0: 74 56 61 6c 75 65 28 42 69 67 49 6e 74 28 31 29  tValue(BigInt(1)
41c0: 29 20 29 3b 0d 0a 09 65 20 3d 20 6e 65 77 20 45  ) );...e = new E
41d0: 76 61 6c 75 61 74 6f 72 3b 0d 0a 09 61 73 73 65  valuator;...asse
41e0: 72 74 5f 74 68 72 6f 77 21 54 68 72 6f 77 61 62  rt_throw!Throwab
41f0: 6c 65 28 20 65 2e 65 76 61 6c 53 74 72 69 6e 67  le( e.evalString
4200: 28 60 6c 65 74 20 78 3d 31 3b 20 6c 65 74 20 5f  (`let x=1; let _
4210: 20 3d 20 28 40 61 20 78 3d 32 3b 32 29 3b 20 40   = (@a x=2;2); @
4220: 61 28 78 29 60 29 20 29 3b 0d 0a 7d 0d 0a 0d 0a  a(x)`) );..}....
4230: 75 6e 69 74 74 65 73 74 0d 0a 7b 0d 0a 09 61 75  unittest..{...au
4240: 74 6f 20 65 20 3d 20 6e 65 77 20 45 76 61 6c 75  to e = new Evalu
4250: 61 74 6f 72 3b 0d 0a 09 65 6e 72 6f 6c 6c 52 75  ator;...enrollRu
4260: 6e 74 69 6d 65 4c 69 62 72 61 72 79 28 65 29 3b  ntimeLibrary(e);
4270: 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20 65 2e  ...assert_eq( e.
4280: 65 76 61 6c 53 74 72 69 6e 67 28 60 0d 0a 09 09  evalString(`....
4290: 40 40 73 28 78 29 7b 78 7d 3b 0d 0a 09 09 40 73  @@s(x){x};....@s
42a0: 20 22 2b 22 20 3d 20 66 75 6e 28 78 2c 20 79 29   "+" = fun(x, y)
42b0: 20 7b 40 76 61 6c 75 65 28 0d 0a 09 09 09 40 73   {@value(.....@s
42c0: 28 78 29 20 2d 20 40 73 28 79 29 0d 0a 09 09 29  (x) - @s(y)....)
42d0: 7d 3b 0d 0a 09 09 40 73 28 31 20 2b 20 32 29 0d  };....@s(1 + 2).
42e0: 0a 09 60 29 2c 20 6e 65 77 20 49 6e 74 56 61 6c  ..`), new IntVal
42f0: 75 65 28 42 69 67 49 6e 74 28 2d 31 29 29 20 29  ue(BigInt(-1)) )
4300: 3b 0d 0a 7d 0d 0a 0d 0a 75 6e 69 74 74 65 73 74  ;..}....unittest
4310: 0d 0a 7b 0d 0a 09 61 75 74 6f 20 65 20 3d 20 6e  ..{...auto e = n
4320: 65 77 20 45 76 61 6c 75 61 74 6f 72 3b 0d 0a 09  ew Evaluator;...
4330: 65 6e 72 6f 6c 6c 52 75 6e 74 69 6d 65 4c 69 62  enrollRuntimeLib
4340: 72 61 72 79 28 65 29 3b 0d 0a 09 61 73 73 65 72  rary(e);...asser
4350: 74 5f 65 71 28 20 65 2e 65 76 61 6c 53 74 72 69  t_eq( e.evalStri
4360: 6e 67 28 60 0d 0a 40 40 33 28 78 29 7b 78 7d 3b  ng(`..@@3(x){x};
4370: 0d 0a 64 65 66 20 69 6e 63 72 28 78 29 20 7b 20  ..def incr(x) { 
4380: 78 2b 31 20 7d 3b 0d 0a 40 20 33 20 69 6e 63 72  x+1 };..@ 3 incr
4390: 28 78 29 20 7b 40 76 61 6c 75 65 28 20 69 66 20  (x) {@value( if 
43a0: 40 20 33 28 78 29 2b 31 3c 20 33 20 74 68 65 6e  @ 3(x)+1< 3 then
43b0: 20 40 33 28 78 29 2b 31 20 65 6c 73 65 20 30 20   @3(x)+1 else 0 
43c0: 29 7d 3b 0d 0a 64 65 66 20 66 62 28 6e 20 40 76  )};..def fb(n @v
43d0: 61 6c 75 65 20 40 33 29 20 7b 20 40 33 28 6e 29  alue @3) { @3(n)
43e0: 20 7d 3b 0d 0a 66 62 28 69 6e 63 72 28 69 6e 63   };..fb(incr(inc
43f0: 72 28 69 6e 63 72 28 30 29 29 29 29 0d 0a 09 60  r(incr(0))))...`
4400: 29 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28  ), new IntValue(
4410: 42 69 67 49 6e 74 28 30 29 29 20 29 3b 0d 0a 7d  BigInt(0)) );..}
4420: 0d 0a 0d 0a 75 6e 69 74 74 65 73 74 0d 0a 7b 0d  ....unittest..{.
4430: 0a 09 61 75 74 6f 20 65 20 3d 20 6e 65 77 20 45  ..auto e = new E
4440: 76 61 6c 75 61 74 6f 72 3b 0d 0a 09 65 6e 72 6f  valuator;...enro
4450: 6c 6c 52 75 6e 74 69 6d 65 4c 69 62 72 61 72 79  llRuntimeLibrary
4460: 28 65 29 3b 0d 0a 09 61 73 73 65 72 74 5f 6e 6f  (e);...assert_no
4470: 74 68 72 6f 77 28 20 65 2e 65 76 61 6c 53 74 72  throw( e.evalStr
4480: 69 6e 67 28 60 0d 0a 40 6d 61 63 72 6f 20 74 77  ing(`..@macro tw
4490: 69 63 65 28 78 29 20 7b 20 78 3b 20 78 20 7d 3b  ice(x) { x; x };
44a0: 0d 0a 64 65 66 20 6d 61 69 6e 28 29 20 7b 20 74  ..def main() { t
44b0: 77 69 63 65 28 31 29 20 7d 3b 0d 0a 6d 61 69 6e  wice(1) };..main
44c0: 28 29 0d 0a 09 60 29 20 29 3b 0d 0a 7d 0d 0a 75  ()...`) );..}..u
44d0: 6e 69 74 74 65 73 74 0d 0a 7b 0d 0a 09 61 75 74  nittest..{...aut
44e0: 6f 20 65 20 3d 20 6e 65 77 20 45 76 61 6c 75 61  o e = new Evalua
44f0: 74 6f 72 3b 0d 0a 09 65 6e 72 6f 6c 6c 52 75 6e  tor;...enrollRun
4500: 74 69 6d 65 4c 69 62 72 61 72 79 28 65 29 3b 0d  timeLibrary(e);.
4510: 0a 09 61 73 73 65 72 74 5f 74 68 72 6f 77 21 52  ..assert_throw!R
4520: 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e 28  untimeException(
4530: 20 65 2e 65 76 61 6c 53 74 72 69 6e 67 28 60 63   e.evalString(`c
4540: 61 73 65 20 31 60 29 20 29 3b 0d 0a 09 61 73 73  ase 1`) );...ass
4550: 65 72 74 5f 6e 6f 74 68 72 6f 77 28 20 65 2e 65  ert_nothrow( e.e
4560: 76 61 6c 53 74 72 69 6e 67 28 60 63 61 73 65 20  valString(`case 
4570: 31 20 77 68 65 6e 20 31 3a 20 32 60 29 20 29 3b  1 when 1: 2`) );
4580: 0d 0a 0d 0a 09 2f 2f 20 74 68 69 73 20 69 73 20  .....// this is 
4590: 61 20 73 68 6f 72 74 68 61 6e 64 20 66 6f 72 0d  a shorthand for.
45a0: 0a 09 2f 2f 20 20 20 40 6d 61 63 72 6f 20 78 20  ..//   @macro x 
45b0: 3d 20 66 75 6e 28 29 7b 7d 20 69 6e 20 40 6d 61  = fun(){} in @ma
45c0: 63 72 6f 28 78 29 0d 0a 09 2f 2f 20 73 6f 20 69  cro(x)...// so i
45d0: 74 20 69 73 20 6f 6b 20 74 6f 20 66 61 69 6c 2c  t is ok to fail,
45e0: 20 62 75 74 20 69 74 20 69 73 20 72 65 61 6c 6c   but it is reall
45f0: 79 20 69 6e 63 6f 76 65 6e 69 65 6e 74 20 6f 6e  y incovenient on
4600: 20 52 45 50 4c 0d 0a 09 61 73 73 65 72 74 5f 6e   REPL...assert_n
4610: 6f 74 68 72 6f 77 28 20 65 2e 65 76 61 6c 53 74  othrow( e.evalSt
4620: 72 69 6e 67 28 60 40 6d 61 63 72 6f 20 78 3d 66  ring(`@macro x=f
4630: 75 6e 28 29 7b 7d 60 29 20 29 3b 0d 0a 7d 0d 0a  un(){}`) );..}..
4640: 0d 0a 75 6e 69 74 74 65 73 74 0d 0a 7b 0d 0a 09  ..unittest..{...
4650: 61 75 74 6f 20 65 20 3d 20 6e 65 77 20 45 76 61  auto e = new Eva
4660: 6c 75 61 74 6f 72 3b 0d 0a 09 65 6e 72 6f 6c 6c  luator;...enroll
4670: 52 75 6e 74 69 6d 65 4c 69 62 72 61 72 79 28 65  RuntimeLibrary(e
4680: 29 3b 0d 0a 09 61 73 73 65 72 74 5f 74 68 72 6f  );...assert_thro
4690: 77 21 52 75 6e 74 69 6d 65 45 78 63 65 70 74 69  w!RuntimeExcepti
46a0: 6f 6e 28 20 65 2e 65 76 61 6c 53 74 72 69 6e 67  on( e.evalString
46b0: 28 60 2e 2e 2e 60 29 20 29 3b 0d 0a 09 61 73 73  (`...`) );...ass
46c0: 65 72 74 5f 65 71 28 20 65 2e 65 76 61 6c 53 74  ert_eq( e.evalSt
46d0: 72 69 6e 67 28 60 40 40 66 6f 6f 28 78 29 7b 78  ring(`@@foo(x){x
46e0: 7d 3b 20 40 66 6f 6f 28 2e 2e 2e 29 60 29 2c 20  }; @foo(...)`), 
46f0: 6e 65 77 20 42 6f 74 74 6f 6d 56 61 6c 75 65 20  new BottomValue 
4700: 29 3b 0d 0a 7d 0d 0a                             );..}..