Hex Artifact Content
Not logged in

Artifact 62b3543f769fc9c997f7d8825040e9b312009566:


0000: 2f 2a 2a 0a 20 2a 20 41 75 74 68 6f 72 73 3a 20  /**. * Authors: 
0010: 6b 2e 69 6e 61 62 61 0a 20 2a 20 4c 69 63 65 6e  k.inaba. * Licen
0020: 73 65 3a 20 4e 59 53 4c 20 30 2e 39 39 38 32 20  se: NYSL 0.9982 
0030: 68 74 74 70 3a 2f 2f 77 77 77 2e 6b 6d 6f 6e 6f  http://www.kmono
0040: 73 2e 6e 65 74 2f 6e 79 73 6c 2f 0a 20 2a 0a 20  s.net/nysl/. *. 
0050: 2a 20 45 76 61 6c 75 61 74 6f 72 20 66 6f 72 20  * Evaluator for 
0060: 50 6f 6c 65 6d 79 20 70 72 6f 67 72 61 6d 6d 69  Polemy programmi
0070: 6e 67 20 6c 61 6e 67 75 61 67 65 2e 0a 20 2a 2f  ng language.. */
0080: 0a 6d 6f 64 75 6c 65 20 70 6f 6c 65 6d 79 2e 65  .module polemy.e
0090: 76 61 6c 3b 0a 69 6d 70 6f 72 74 20 70 6f 6c 65  val;.import pole
00a0: 6d 79 2e 5f 63 6f 6d 6d 6f 6e 3b 0a 69 6d 70 6f  my._common;.impo
00b0: 72 74 20 70 6f 6c 65 6d 79 2e 6c 65 78 20 3a 20  rt polemy.lex : 
00c0: 4c 65 78 50 6f 73 69 74 69 6f 6e 3b 0a 69 6d 70  LexPosition;.imp
00d0: 6f 72 74 20 70 6f 6c 65 6d 79 2e 61 73 74 3b 0a  ort 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 0a 69 6d 70 6f 72 74 20 70 6f 6c 65  rse;.import pole
0100: 6d 79 2e 76 61 6c 75 65 3b 0a 69 6d 70 6f 72 74  my.value;.import
0110: 20 73 74 64 2e 74 79 70 65 63 6f 6e 73 3b 0a 69   std.typecons;.i
0120: 6d 70 6f 72 74 20 73 74 64 2e 73 74 64 69 6f 3b  mport std.stdio;
0130: 0a 0a 54 61 62 6c 65 20 63 72 65 61 74 65 47 6c  ..Table createGl
0140: 6f 62 61 6c 43 6f 6e 74 65 78 74 28 29 0a 7b 0d  obalContext().{.
0150: 0a 09 61 75 74 6f 20 63 74 78 20 3d 20 6e 65 77  ..auto ctx = new
0160: 20 54 61 62 6c 65 3b 0d 0a 09 2f 2f 20 5b 54 4f   Table;...// [TO
0170: 44 4f 5d 20 61 75 74 6f 67 65 6e 65 72 61 74 65  DO] autogenerate
0180: 20 74 68 65 73 65 20 74 79 70 65 63 68 65 63 6b   these typecheck
0190: 73 0a 09 63 74 78 2e 73 65 74 28 22 2b 22 2c 20  s..ctx.set("+", 
01a0: 22 40 76 61 6c 22 2c 20 6e 65 77 20 46 75 6e 56  "@val", new FunV
01b0: 61 6c 75 65 28 64 65 6c 65 67 61 74 65 20 56 61  alue(delegate Va
01c0: 6c 75 65 28 69 6d 6d 75 74 61 62 6c 65 20 4c 65  lue(immutable Le
01d0: 78 50 6f 73 69 74 69 6f 6e 20 70 6f 73 2c 20 56  xPosition pos, V
01e0: 61 6c 75 65 5b 5d 20 61 72 67 73 29 7b 0a 09 09  alue[] args){...
01f0: 69 66 28 20 61 72 67 73 2e 6c 65 6e 67 74 68 20  if( args.length 
0200: 21 3d 20 32 20 29 0a 09 09 09 74 68 72 6f 77 20  != 2 )....throw 
0210: 6e 65 77 20 52 75 6e 74 69 6d 65 45 78 63 65 70  new RuntimeExcep
0220: 74 69 6f 6e 28 70 6f 73 2c 20 22 2b 20 74 61 6b  tion(pos, "+ tak
0230: 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73  es two arguments
0240: 21 21 22 29 3b 0a 09 09 69 66 28 20 61 75 74 6f  !!");...if( auto
0250: 20 78 20 3d 20 63 61 73 74 28 49 6e 74 56 61 6c   x = cast(IntVal
0260: 75 65 29 61 72 67 73 5b 30 5d 20 29 0a 09 09 09  ue)args[0] )....
0270: 69 66 28 20 61 75 74 6f 20 79 20 3d 20 63 61 73  if( auto y = cas
0280: 74 28 49 6e 74 56 61 6c 75 65 29 61 72 67 73 5b  t(IntValue)args[
0290: 31 5d 20 29 0a 09 09 09 09 72 65 74 75 72 6e 20  1] ).....return 
02a0: 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 78 2e 64  new IntValue(x.d
02b0: 61 74 61 2b 79 2e 64 61 74 61 29 3b 0a 09 09 74  ata+y.data);...t
02c0: 68 72 6f 77 20 6e 65 77 20 52 75 6e 74 69 6d 65  hrow new Runtime
02d0: 45 78 63 65 70 74 69 6f 6e 28 70 6f 73 2c 20 22  Exception(pos, "
02e0: 63 61 6e 6e 6f 74 20 61 64 64 20 6e 6f 6e 2d 69  cannot add non-i
02f0: 6e 74 65 67 65 72 73 22 29 3b 0a 09 7d 29 29 3b  ntegers");..}));
0300: 0a 09 63 74 78 2e 73 65 74 28 22 2d 22 2c 20 22  ..ctx.set("-", "
0310: 40 76 61 6c 22 2c 20 6e 65 77 20 46 75 6e 56 61  @val", new FunVa
0320: 6c 75 65 28 64 65 6c 65 67 61 74 65 20 56 61 6c  lue(delegate Val
0330: 75 65 28 69 6d 6d 75 74 61 62 6c 65 20 4c 65 78  ue(immutable Lex
0340: 50 6f 73 69 74 69 6f 6e 20 70 6f 73 2c 20 56 61  Position pos, Va
0350: 6c 75 65 5b 5d 20 61 72 67 73 29 7b 0a 09 09 69  lue[] args){...i
0360: 66 28 20 61 72 67 73 2e 6c 65 6e 67 74 68 20 21  f( args.length !
0370: 3d 20 32 20 29 0a 09 09 09 74 68 72 6f 77 20 6e  = 2 )....throw n
0380: 65 77 20 52 75 6e 74 69 6d 65 45 78 63 65 70 74  ew RuntimeExcept
0390: 69 6f 6e 28 70 6f 73 2c 20 22 2d 20 74 61 6b 65  ion(pos, "- take
03a0: 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 21  s two arguments!
03b0: 21 22 29 3b 0d 0a 09 09 69 66 28 20 61 75 74 6f  !");....if( auto
03c0: 20 78 20 3d 20 63 61 73 74 28 49 6e 74 56 61 6c   x = cast(IntVal
03d0: 75 65 29 61 72 67 73 5b 30 5d 20 29 0a 09 09 09  ue)args[0] )....
03e0: 69 66 28 20 61 75 74 6f 20 79 20 3d 20 63 61 73  if( auto y = cas
03f0: 74 28 49 6e 74 56 61 6c 75 65 29 61 72 67 73 5b  t(IntValue)args[
0400: 31 5d 20 29 0a 09 09 09 09 72 65 74 75 72 6e 20  1] ).....return 
0410: 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 78 2e 64  new IntValue(x.d
0420: 61 74 61 2d 79 2e 64 61 74 61 29 3b 0a 09 09 74  ata-y.data);...t
0430: 68 72 6f 77 20 6e 65 77 20 52 75 6e 74 69 6d 65  hrow new Runtime
0440: 45 78 63 65 70 74 69 6f 6e 28 70 6f 73 2c 20 22  Exception(pos, "
0450: 63 61 6e 6e 6f 74 20 73 75 62 74 72 61 63 74 20  cannot subtract 
0460: 6e 6f 6e 2d 69 6e 74 65 67 65 72 73 22 29 3b 0d  non-integers");.
0470: 0a 09 7d 29 29 3b 0a 09 63 74 78 2e 73 65 74 28  ..}));..ctx.set(
0480: 22 2a 22 2c 20 22 40 76 61 6c 22 2c 20 6e 65 77  "*", "@val", new
0490: 20 46 75 6e 56 61 6c 75 65 28 64 65 6c 65 67 61   FunValue(delega
04a0: 74 65 20 56 61 6c 75 65 28 69 6d 6d 75 74 61 62  te Value(immutab
04b0: 6c 65 20 4c 65 78 50 6f 73 69 74 69 6f 6e 20 70  le LexPosition p
04c0: 6f 73 2c 20 56 61 6c 75 65 5b 5d 20 61 72 67 73  os, Value[] args
04d0: 29 7b 0a 09 09 69 66 28 20 61 72 67 73 2e 6c 65  ){...if( args.le
04e0: 6e 67 74 68 20 21 3d 20 32 20 29 0a 09 09 09 74  ngth != 2 )....t
04f0: 68 72 6f 77 20 6e 65 77 20 52 75 6e 74 69 6d 65  hrow new Runtime
0500: 45 78 63 65 70 74 69 6f 6e 28 70 6f 73 2c 20 22  Exception(pos, "
0510: 2a 20 74 61 6b 65 73 20 74 77 6f 20 61 72 67 75  * takes two argu
0520: 6d 65 6e 74 73 21 21 22 29 3b 0d 0a 09 09 69 66  ments!!");....if
0530: 28 20 61 75 74 6f 20 78 20 3d 20 63 61 73 74 28  ( auto x = cast(
0540: 49 6e 74 56 61 6c 75 65 29 61 72 67 73 5b 30 5d  IntValue)args[0]
0550: 20 29 0a 09 09 09 69 66 28 20 61 75 74 6f 20 79   )....if( auto y
0560: 20 3d 20 63 61 73 74 28 49 6e 74 56 61 6c 75 65   = cast(IntValue
0570: 29 61 72 67 73 5b 31 5d 20 29 0a 09 09 09 09 72  )args[1] ).....r
0580: 65 74 75 72 6e 20 6e 65 77 20 49 6e 74 56 61 6c  eturn new IntVal
0590: 75 65 28 78 2e 64 61 74 61 2a 79 2e 64 61 74 61  ue(x.data*y.data
05a0: 29 3b 0a 09 09 74 68 72 6f 77 20 6e 65 77 20 52  );...throw new R
05b0: 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e 28  untimeException(
05c0: 70 6f 73 2c 20 22 63 61 6e 6e 6f 74 20 6d 75 6c  pos, "cannot mul
05d0: 74 69 70 6c 79 20 6e 6f 6e 2d 69 6e 74 65 67 65  tiply non-intege
05e0: 72 73 22 29 3b 0d 0a 09 7d 29 29 3b 0a 09 63 74  rs");...}));..ct
05f0: 78 2e 73 65 74 28 22 2f 22 2c 20 22 40 76 61 6c  x.set("/", "@val
0600: 22 2c 20 6e 65 77 20 46 75 6e 56 61 6c 75 65 28  ", new FunValue(
0610: 64 65 6c 65 67 61 74 65 20 56 61 6c 75 65 28 69  delegate Value(i
0620: 6d 6d 75 74 61 62 6c 65 20 4c 65 78 50 6f 73 69  mmutable LexPosi
0630: 74 69 6f 6e 20 70 6f 73 2c 20 56 61 6c 75 65 5b  tion pos, Value[
0640: 5d 20 61 72 67 73 29 7b 0a 09 09 69 66 28 20 61  ] args){...if( a
0650: 72 67 73 2e 6c 65 6e 67 74 68 20 21 3d 20 32 20  rgs.length != 2 
0660: 29 0a 09 09 09 74 68 72 6f 77 20 6e 65 77 20 52  )....throw new R
0670: 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e 28  untimeException(
0680: 70 6f 73 2c 20 22 2f 20 74 61 6b 65 73 20 74 77  pos, "/ takes tw
0690: 6f 20 61 72 67 75 6d 65 6e 74 73 21 21 22 29 3b  o arguments!!");
06a0: 0d 0a 09 09 69 66 28 20 61 75 74 6f 20 78 20 3d  ....if( auto x =
06b0: 20 63 61 73 74 28 49 6e 74 56 61 6c 75 65 29 61   cast(IntValue)a
06c0: 72 67 73 5b 30 5d 20 29 0a 09 09 09 69 66 28 20  rgs[0] )....if( 
06d0: 61 75 74 6f 20 79 20 3d 20 63 61 73 74 28 49 6e  auto y = cast(In
06e0: 74 56 61 6c 75 65 29 61 72 67 73 5b 31 5d 20 29  tValue)args[1] )
06f0: 0a 09 09 09 09 72 65 74 75 72 6e 20 6e 65 77 20  .....return new 
0700: 49 6e 74 56 61 6c 75 65 28 78 2e 64 61 74 61 2f  IntValue(x.data/
0710: 79 2e 64 61 74 61 29 3b 0a 09 09 74 68 72 6f 77  y.data);...throw
0720: 20 6e 65 77 20 52 75 6e 74 69 6d 65 45 78 63 65   new RuntimeExce
0730: 70 74 69 6f 6e 28 70 6f 73 2c 20 22 63 61 6e 6e  ption(pos, "cann
0740: 6f 74 20 64 69 76 69 64 65 20 6e 6f 6e 2d 69 6e  ot divide non-in
0750: 74 65 67 65 72 73 22 29 3b 0d 0a 09 7d 29 29 3b  tegers");...}));
0760: 0a 09 63 74 78 2e 73 65 74 28 22 3c 22 2c 20 22  ..ctx.set("<", "
0770: 40 76 61 6c 22 2c 20 6e 65 77 20 46 75 6e 56 61  @val", new FunVa
0780: 6c 75 65 28 64 65 6c 65 67 61 74 65 20 56 61 6c  lue(delegate Val
0790: 75 65 28 69 6d 6d 75 74 61 62 6c 65 20 4c 65 78  ue(immutable Lex
07a0: 50 6f 73 69 74 69 6f 6e 20 70 6f 73 2c 20 56 61  Position pos, Va
07b0: 6c 75 65 5b 5d 20 61 72 67 73 29 7b 0a 09 09 69  lue[] args){...i
07c0: 66 28 20 61 72 67 73 2e 6c 65 6e 67 74 68 20 21  f( args.length !
07d0: 3d 20 32 20 29 0a 09 09 09 74 68 72 6f 77 20 6e  = 2 )....throw n
07e0: 65 77 20 52 75 6e 74 69 6d 65 45 78 63 65 70 74  ew RuntimeExcept
07f0: 69 6f 6e 28 70 6f 73 2c 20 22 3c 20 74 61 6b 65  ion(pos, "< take
0800: 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 21  s two arguments!
0810: 21 22 29 3b 0d 0a 09 09 69 66 28 20 61 75 74 6f  !");....if( auto
0820: 20 78 20 3d 20 63 61 73 74 28 49 6e 74 56 61 6c   x = cast(IntVal
0830: 75 65 29 61 72 67 73 5b 30 5d 20 29 0a 09 09 09  ue)args[0] )....
0840: 69 66 28 20 61 75 74 6f 20 79 20 3d 20 63 61 73  if( auto y = cas
0850: 74 28 49 6e 74 56 61 6c 75 65 29 61 72 67 73 5b  t(IntValue)args[
0860: 31 5d 20 29 0a 09 09 09 09 72 65 74 75 72 6e 20  1] ).....return 
0870: 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 42 69 67  new IntValue(Big
0880: 49 6e 74 28 74 6f 21 69 6e 74 28 78 2e 64 61 74  Int(to!int(x.dat
0890: 61 20 3c 20 79 2e 64 61 74 61 29 29 29 3b 0a 09  a < y.data)));..
08a0: 09 74 68 72 6f 77 20 6e 65 77 20 52 75 6e 74 69  .throw new Runti
08b0: 6d 65 45 78 63 65 70 74 69 6f 6e 28 70 6f 73 2c  meException(pos,
08c0: 20 22 63 61 6e 6e 6f 74 20 63 6f 6d 70 61 72 65   "cannot compare
08d0: 20 6e 6f 6e 2d 69 6e 74 65 67 65 72 73 22 29 3b   non-integers");
08e0: 0d 0a 09 7d 29 29 3b 0a 09 63 74 78 2e 73 65 74  ...}));..ctx.set
08f0: 28 22 3e 22 2c 20 22 40 76 61 6c 22 2c 20 6e 65  (">", "@val", ne
0900: 77 20 46 75 6e 56 61 6c 75 65 28 64 65 6c 65 67  w FunValue(deleg
0910: 61 74 65 20 56 61 6c 75 65 28 69 6d 6d 75 74 61  ate Value(immuta
0920: 62 6c 65 20 4c 65 78 50 6f 73 69 74 69 6f 6e 20  ble LexPosition 
0930: 70 6f 73 2c 20 56 61 6c 75 65 5b 5d 20 61 72 67  pos, Value[] arg
0940: 73 29 7b 0a 09 09 69 66 28 20 61 72 67 73 2e 6c  s){...if( args.l
0950: 65 6e 67 74 68 20 21 3d 20 32 20 29 0a 09 09 09  ength != 2 )....
0960: 74 68 72 6f 77 20 6e 65 77 20 52 75 6e 74 69 6d  throw new Runtim
0970: 65 45 78 63 65 70 74 69 6f 6e 28 70 6f 73 2c 20  eException(pos, 
0980: 22 3e 20 74 61 6b 65 73 20 74 77 6f 20 61 72 67  "> takes two arg
0990: 75 6d 65 6e 74 73 21 21 22 29 3b 0d 0a 09 09 69  uments!!");....i
09a0: 66 28 20 61 75 74 6f 20 78 20 3d 20 63 61 73 74  f( auto x = cast
09b0: 28 49 6e 74 56 61 6c 75 65 29 61 72 67 73 5b 30  (IntValue)args[0
09c0: 5d 20 29 0a 09 09 09 69 66 28 20 61 75 74 6f 20  ] )....if( auto 
09d0: 79 20 3d 20 63 61 73 74 28 49 6e 74 56 61 6c 75  y = cast(IntValu
09e0: 65 29 61 72 67 73 5b 31 5d 20 29 0a 09 09 09 09  e)args[1] ).....
09f0: 72 65 74 75 72 6e 20 6e 65 77 20 49 6e 74 56 61  return new IntVa
0a00: 6c 75 65 28 42 69 67 49 6e 74 28 74 6f 21 69 6e  lue(BigInt(to!in
0a10: 74 28 78 2e 64 61 74 61 3e 79 2e 64 61 74 61 29  t(x.data>y.data)
0a20: 29 29 3b 0a 09 09 74 68 72 6f 77 20 6e 65 77 20  ));...throw new 
0a30: 52 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e  RuntimeException
0a40: 28 70 6f 73 2c 20 22 63 61 6e 6e 6f 74 20 63 6f  (pos, "cannot co
0a50: 6d 70 61 72 65 20 6e 6f 6e 2d 69 6e 74 65 67 65  mpare non-intege
0a60: 72 73 22 29 3b 0d 0a 09 7d 29 29 3b 0a 09 63 74  rs");...}));..ct
0a70: 78 2e 73 65 74 28 22 70 72 69 6e 74 22 2c 20 22  x.set("print", "
0a80: 40 76 61 6c 22 2c 20 6e 65 77 20 46 75 6e 56 61  @val", new FunVa
0a90: 6c 75 65 28 64 65 6c 65 67 61 74 65 20 56 61 6c  lue(delegate Val
0aa0: 75 65 28 69 6d 6d 75 74 61 62 6c 65 20 4c 65 78  ue(immutable Lex
0ab0: 50 6f 73 69 74 69 6f 6e 20 70 6f 73 2c 20 56 61  Position pos, Va
0ac0: 6c 75 65 5b 5d 20 61 72 67 73 29 7b 0a 09 09 66  lue[] args){...f
0ad0: 6f 72 65 61 63 68 28 61 3b 20 61 72 67 73 29 0a  oreach(a; args).
0ae0: 09 09 09 77 72 69 74 65 28 61 29 3b 0a 09 09 77  ...write(a);...w
0af0: 72 69 74 65 6c 6e 28 22 22 29 3b 0a 09 09 72 65  riteln("");...re
0b00: 74 75 72 6e 20 6e 65 77 20 49 6e 74 56 61 6c 75  turn new IntValu
0b10: 65 28 42 69 67 49 6e 74 28 31 37 38 29 29 3b 0a  e(BigInt(178));.
0b20: 09 7d 29 29 3b 0a 09 63 74 78 2e 73 65 74 28 22  .}));..ctx.set("
0b30: 69 66 22 2c 20 22 40 76 61 6c 22 2c 20 6e 65 77  if", "@val", new
0b40: 20 46 75 6e 56 61 6c 75 65 28 64 65 6c 65 67 61   FunValue(delega
0b50: 74 65 20 56 61 6c 75 65 28 69 6d 6d 75 74 61 62  te Value(immutab
0b60: 6c 65 20 4c 65 78 50 6f 73 69 74 69 6f 6e 20 70  le LexPosition p
0b70: 6f 73 2c 20 56 61 6c 75 65 5b 5d 20 61 72 67 73  os, Value[] args
0b80: 29 7b 0a 09 09 69 66 28 20 61 72 67 73 2e 6c 65  ){...if( args.le
0b90: 6e 67 74 68 20 21 3d 20 33 20 29 0a 09 09 09 74  ngth != 3 )....t
0ba0: 68 72 6f 77 20 6e 65 77 20 52 75 6e 74 69 6d 65  hrow new Runtime
0bb0: 45 78 63 65 70 74 69 6f 6e 28 70 6f 73 2c 20 22  Exception(pos, "
0bc0: 69 66 20 74 61 6b 65 73 20 74 68 72 65 65 20 61  if takes three a
0bd0: 72 67 75 6d 65 6e 74 73 21 21 22 29 3b 0a 09 09  rguments!!");...
0be0: 69 66 28 20 61 75 74 6f 20 78 20 3d 20 63 61 73  if( auto x = cas
0bf0: 74 28 49 6e 74 56 61 6c 75 65 29 61 72 67 73 5b  t(IntValue)args[
0c00: 30 5d 20 29 0a 09 09 69 66 28 20 61 75 74 6f 20  0] )...if( auto 
0c10: 66 74 20 3d 20 63 61 73 74 28 46 75 6e 56 61 6c  ft = cast(FunVal
0c20: 75 65 29 61 72 67 73 5b 31 5d 20 29 0a 09 09 69  ue)args[1] )...i
0c30: 66 28 20 61 75 74 6f 20 66 65 20 3d 20 63 61 73  f( auto fe = cas
0c40: 74 28 46 75 6e 56 61 6c 75 65 29 61 72 67 73 5b  t(FunValue)args[
0c50: 32 5d 20 29 0a 09 09 09 72 65 74 75 72 6e 20 28  2] )....return (
0c60: 78 2e 64 61 74 61 20 3d 3d 20 30 20 3f 20 66 65  x.data == 0 ? fe
0c70: 20 3a 20 66 74 29 2e 63 61 6c 6c 28 70 6f 73 2c   : ft).call(pos,
0c80: 5b 5d 29 3b 0a 09 09 74 68 72 6f 77 20 6e 65 77  []);...throw new
0c90: 20 52 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f   RuntimeExceptio
0ca0: 6e 28 70 6f 73 2c 20 22 74 79 70 65 20 6d 69 73  n(pos, "type mis
0cb0: 6d 61 74 63 68 20 69 6e 20 69 66 22 29 3b 0d 0a  match in if");..
0cc0: 09 7d 29 29 3b 0a 09 72 65 74 75 72 6e 20 63 74  .}));..return ct
0cd0: 78 3b 0a 7d 0a 0a 2f 2f 2f 20 45 6e 74 72 79 20  x;.}../// Entry 
0ce0: 70 6f 69 6e 74 20 6f 66 20 74 68 69 73 20 6d 6f  point of this mo
0cf0: 64 75 6c 65 0d 0a 0d 0a 54 75 70 6c 65 21 28 56  dule....Tuple!(V
0d00: 61 6c 75 65 2c 22 76 61 6c 22 2c 54 61 62 6c 65  alue,"val",Table
0d10: 2c 22 63 74 78 22 29 20 65 76 61 6c 53 74 72 69  ,"ctx") evalStri
0d20: 6e 67 28 53 2c 54 2e 2e 2e 29 28 53 20 73 74 72  ng(S,T...)(S str
0d30: 2c 20 54 20 66 6e 5f 6c 6e 5f 63 6e 29 0a 7b 0a  , T fn_ln_cn).{.
0d40: 09 72 65 74 75 72 6e 20 65 76 61 6c 28 20 70 6f  .return eval( po
0d50: 6c 65 6d 79 2e 70 61 72 73 65 2e 70 61 72 73 65  lemy.parse.parse
0d60: 53 74 72 69 6e 67 28 73 74 72 2c 20 66 6e 5f 6c  String(str, fn_l
0d70: 6e 5f 63 6e 29 20 29 3b 0a 7d 0a 0a 54 75 70 6c  n_cn) );.}..Tupl
0d80: 65 21 28 56 61 6c 75 65 2c 22 76 61 6c 22 2c 54  e!(Value,"val",T
0d90: 61 62 6c 65 2c 22 63 74 78 22 29 20 65 76 61 6c  able,"ctx") eval
0da0: 46 69 6c 65 28 53 2c 20 54 2e 2e 2e 29 28 53 20  File(S, T...)(S 
0db0: 66 69 6c 65 6e 61 6d 65 2c 20 54 20 6c 6e 5f 63  filename, T ln_c
0dc0: 6e 29 0a 7b 0a 09 72 65 74 75 72 6e 20 65 76 61  n).{..return eva
0dd0: 6c 28 20 70 6f 6c 65 6d 79 2e 70 61 72 73 65 2e  l( polemy.parse.
0de0: 70 61 72 73 65 46 69 6c 65 28 66 69 6c 65 6e 61  parseFile(filena
0df0: 6d 65 2c 20 6c 6e 5f 63 6e 29 20 29 3b 0a 7d 0a  me, ln_cn) );.}.
0e00: 0a 54 75 70 6c 65 21 28 56 61 6c 75 65 2c 22 76  .Tuple!(Value,"v
0e10: 61 6c 22 2c 54 61 62 6c 65 2c 22 63 74 78 22 29  al",Table,"ctx")
0e20: 20 65 76 61 6c 28 41 53 54 20 65 29 0a 7b 0a 09   eval(AST e).{..
0e30: 54 61 62 6c 65 20 63 74 78 20 3d 20 63 72 65 61  Table ctx = crea
0e40: 74 65 47 6c 6f 62 61 6c 43 6f 6e 74 65 78 74 28  teGlobalContext(
0e50: 29 3b 0a 09 72 65 74 75 72 6e 20 74 79 70 65 6f  );..return typeo
0e60: 66 28 72 65 74 75 72 6e 29 28 65 76 61 6c 28 65  f(return)(eval(e
0e70: 2c 20 63 74 78 29 2c 20 63 74 78 29 3b 0a 7d 0a  , ctx), ctx);.}.
0e80: 0a 56 61 6c 75 65 20 65 76 61 6c 28 41 53 54 20  .Value eval(AST 
0e90: 5f 65 2c 20 54 61 62 6c 65 20 63 74 78 2c 20 62  _e, Table ctx, b
0ea0: 6f 6f 6c 20 73 70 6c 69 74 43 74 78 20 3d 20 66  ool splitCtx = f
0eb0: 61 6c 73 65 2c 20 4c 61 79 65 72 20 6c 61 79 3d  alse, Layer lay=
0ec0: 22 40 76 61 6c 22 29 0a 7b 0a 09 69 66 28 20 61  "@val").{..if( a
0ed0: 75 74 6f 20 65 20 3d 20 63 61 73 74 28 53 74 72  uto e = cast(Str
0ee0: 4c 69 74 65 72 61 6c 29 5f 65 20 29 0a 09 7b 0a  Literal)_e )..{.
0ef0: 09 09 72 65 74 75 72 6e 20 6e 65 77 20 53 74 72  ..return new Str
0f00: 56 61 6c 75 65 28 65 2e 64 61 74 61 29 3b 0a 09  Value(e.data);..
0f10: 7d 0a 09 65 6c 73 65 0a 09 69 66 28 20 61 75 74  }..else..if( aut
0f20: 6f 20 65 20 3d 20 63 61 73 74 28 49 6e 74 4c 69  o e = cast(IntLi
0f30: 74 65 72 61 6c 29 5f 65 20 29 0a 09 7b 0a 09 09  teral)_e )..{...
0f40: 72 65 74 75 72 6e 20 6e 65 77 20 49 6e 74 56 61  return new IntVa
0f50: 6c 75 65 28 65 2e 64 61 74 61 29 3b 0a 09 7d 0a  lue(e.data);..}.
0f60: 09 65 6c 73 65 0d 0a 09 69 66 28 20 61 75 74 6f  .else...if( auto
0f70: 20 65 20 3d 20 63 61 73 74 28 56 61 72 45 78 70   e = cast(VarExp
0f80: 72 65 73 73 69 6f 6e 29 5f 65 20 29 0d 0a 09 7b  ression)_e )...{
0f90: 0d 0a 09 09 72 65 74 75 72 6e 20 63 74 78 2e 67  ....return ctx.g
0fa0: 65 74 28 65 2e 76 61 72 2c 20 6c 61 79 2c 20 65  et(e.var, lay, e
0fb0: 2e 70 6f 73 29 3b 0d 0a 09 7d 0d 0a 09 65 6c 73  .pos);...}...els
0fc0: 65 0d 0a 09 69 66 28 20 61 75 74 6f 20 65 20 3d  e...if( auto e =
0fd0: 20 63 61 73 74 28 4c 61 79 65 72 65 64 45 78 70   cast(LayeredExp
0fe0: 72 65 73 73 69 6f 6e 29 5f 65 20 29 0d 0a 09 7b  ression)_e )...{
0ff0: 0d 0a 09 09 72 65 74 75 72 6e 20 65 76 61 6c 28  ....return eval(
1000: 65 2e 65 78 70 72 2c 20 63 74 78 2c 20 66 61 6c  e.expr, ctx, fal
1010: 73 65 2c 20 65 2e 6c 61 79 29 3b 0d 0a 09 7d 0d  se, e.lay);...}.
1020: 0a 09 65 6c 73 65 0d 0a 09 69 66 28 20 61 75 74  ..else...if( aut
1030: 6f 20 65 20 3d 20 63 61 73 74 28 4c 65 74 45 78  o e = cast(LetEx
1040: 70 72 65 73 73 69 6f 6e 29 5f 65 20 29 0d 0a 09  pression)_e )...
1050: 7b 0d 0a 09 09 2f 2f 20 66 6f 72 20 6c 65 74 72  {....// for letr
1060: 65 63 2c 20 77 65 20 6e 65 65 64 20 74 68 69 73  ec, we need this
1070: 2c 20 62 75 74 20 73 68 6f 75 6c 64 20 61 76 6f  , but should avo
1080: 69 64 20 6f 76 65 72 77 72 69 74 69 6e 67 3f 3f  id overwriting??
1090: 3f 3f 0d 0a 09 09 2f 2f 20 63 74 78 2e 73 65 74  ??....// ctx.set
10a0: 28 65 2e 76 61 72 2c 20 22 40 76 61 6c 22 2c 20  (e.var, "@val", 
10b0: 6e 65 77 20 55 6e 64 65 66 69 6e 65 64 56 61 6c  new UndefinedVal
10c0: 75 65 2c 20 65 2e 70 6f 73 29 3b 0d 0a 09 09 56  ue, e.pos);....V
10d0: 61 6c 75 65 20 76 20 3d 20 65 76 61 6c 28 65 2e  alue v = eval(e.
10e0: 69 6e 69 74 2c 20 63 74 78 2c 20 74 72 75 65 29  init, ctx, true)
10f0: 3b 0d 0a 09 09 69 66 28 73 70 6c 69 74 43 74 78  ;....if(splitCtx
1100: 29 0d 0a 09 09 09 63 74 78 20 3d 20 6e 65 77 20  ).....ctx = new 
1110: 54 61 62 6c 65 28 63 74 78 2c 20 54 61 62 6c 65  Table(ctx, Table
1120: 2e 4b 69 6e 64 2e 4e 6f 74 50 72 6f 70 61 67 61  .Kind.NotPropaga
1130: 74 65 53 65 74 29 3b 0d 0a 09 09 63 74 78 2e 73  teSet);....ctx.s
1140: 65 74 28 65 2e 76 61 72 2c 20 28 65 2e 6c 61 79  et(e.var, (e.lay
1150: 65 72 2e 6c 65 6e 67 74 68 20 3f 20 65 2e 6c 61  er.length ? e.la
1160: 79 65 72 20 3a 20 6c 61 79 29 2c 20 76 2c 20 65  yer : lay), v, e
1170: 2e 70 6f 73 29 3b 0d 0a 09 09 72 65 74 75 72 6e  .pos);....return
1180: 20 65 76 61 6c 28 65 2e 65 78 70 72 2c 20 63 74   eval(e.expr, ct
1190: 78 29 3b 0d 0a 09 7d 0d 0a 09 65 6c 73 65 0a 09  x);...}...else..
11a0: 69 66 28 20 61 75 74 6f 20 65 20 3d 20 63 61 73  if( auto e = cas
11b0: 74 28 46 75 6e 63 61 6c 6c 45 78 70 72 65 73 73  t(FuncallExpress
11c0: 69 6f 6e 29 5f 65 20 29 0a 09 7b 0a 09 09 56 61  ion)_e )..{...Va
11d0: 6c 75 65 20 5f 66 20 3d 20 65 76 61 6c 28 65 2e  lue _f = eval(e.
11e0: 66 75 6e 2c 20 63 74 78 29 3b 0a 09 09 69 66 28  fun, ctx);...if(
11f0: 20 61 75 74 6f 20 66 20 3d 20 63 61 73 74 28 46   auto f = cast(F
1200: 75 6e 56 61 6c 75 65 29 5f 66 20 29 20 7b 0a 09  unValue)_f ) {..
1210: 09 09 56 61 6c 75 65 5b 5d 20 61 72 67 73 3b 0a  ..Value[] args;.
1220: 09 09 09 66 6f 72 65 61 63 68 28 61 3b 20 65 2e  ...foreach(a; e.
1230: 61 72 67 73 29 0a 09 09 09 09 61 72 67 73 20 7e  args).....args ~
1240: 3d 20 65 76 61 6c 28 61 2c 20 63 74 78 29 3b 0a  = eval(a, ctx);.
1250: 09 09 09 72 65 74 75 72 6e 20 66 2e 63 61 6c 6c  ...return f.call
1260: 28 65 2e 70 6f 73 2c 20 61 72 67 73 29 3b 0a 09  (e.pos, args);..
1270: 09 7d 20 65 6c 73 65 0a 09 09 09 74 68 72 6f 77  .} else....throw
1280: 20 6e 65 77 20 52 75 6e 74 69 6d 65 45 78 63 65   new RuntimeExce
1290: 70 74 69 6f 6e 28 65 2e 70 6f 73 2c 20 22 4e 6f  ption(e.pos, "No
12a0: 6e 2d 66 75 6e 63 69 6f 6e 20 69 73 20 61 70 70  n-funcion is app
12b0: 6c 69 65 64 22 29 3b 0a 09 7d 0a 09 65 6c 73 65  lied");..}..else
12c0: 0a 09 69 66 28 20 61 75 74 6f 20 65 20 3d 20 63  ..if( auto e = c
12d0: 61 73 74 28 46 75 6e 4c 69 74 65 72 61 6c 29 5f  ast(FunLiteral)_
12e0: 65 20 29 0a 09 7b 0a 09 09 72 65 74 75 72 6e 20  e )..{...return 
12f0: 6e 65 77 20 46 75 6e 56 61 6c 75 65 28 64 65 6c  new FunValue(del
1300: 65 67 61 74 65 20 56 61 6c 75 65 28 69 6d 6d 75  egate Value(immu
1310: 74 61 62 6c 65 20 4c 65 78 50 6f 73 69 74 69 6f  table LexPositio
1320: 6e 20 70 6f 73 2c 20 56 61 6c 75 65 5b 5d 20 61  n pos, Value[] a
1330: 72 67 73 29 7b 0a 09 09 09 69 66 28 20 65 2e 70  rgs){....if( e.p
1340: 61 72 61 6d 73 2e 6c 65 6e 67 74 68 20 21 3d 20  arams.length != 
1350: 61 72 67 73 2e 6c 65 6e 67 74 68 20 29 0a 09 09  args.length )...
1360: 09 09 74 68 72 6f 77 20 6e 65 77 20 52 75 6e 74  ..throw new Runt
1370: 69 6d 65 45 78 63 65 70 74 69 6f 6e 28 65 2e 70  imeException(e.p
1380: 6f 73 2c 20 73 70 72 69 6e 74 66 21 22 41 72 67  os, sprintf!"Arg
1390: 75 6d 65 6e 74 20 4e 75 6d 62 65 72 20 4d 69 73  ument Number Mis
13a0: 6d 61 74 63 68 20 28 25 64 20 72 65 71 75 69 72  match (%d requir
13b0: 65 64 20 62 75 74 20 25 64 20 67 69 76 65 6e 29  ed but %d given)
13c0: 22 0a 09 09 09 09 09 28 65 2e 70 61 72 61 6d 73  "......(e.params
13d0: 2e 6c 65 6e 67 74 68 2c 20 61 72 67 73 2e 6c 65  .length, args.le
13e0: 6e 67 74 68 29 29 3b 0a 09 09 09 54 61 62 6c 65  ngth));....Table
13f0: 20 63 74 78 4e 65 6f 20 3d 20 6e 65 77 20 54 61   ctxNeo = new Ta
1400: 62 6c 65 28 63 74 78 2c 20 54 61 62 6c 65 2e 4b  ble(ctx, Table.K
1410: 69 6e 64 2e 4e 6f 74 50 72 6f 70 61 67 61 74 65  ind.NotPropagate
1420: 53 65 74 29 3b 0a 09 09 09 66 6f 72 65 61 63 68  Set);....foreach
1430: 28 69 2c 70 3b 20 65 2e 70 61 72 61 6d 73 29 0a  (i,p; e.params).
1440: 09 09 09 09 63 74 78 4e 65 6f 2e 73 65 74 28 70  ....ctxNeo.set(p
1450: 2c 20 22 40 76 61 6c 22 2c 20 61 72 67 73 5b 69  , "@val", args[i
1460: 5d 29 3b 0a 09 09 09 72 65 74 75 72 6e 20 65 76  ]);....return ev
1470: 61 6c 28 65 2e 66 75 6e 62 6f 64 79 2c 20 63 74  al(e.funbody, ct
1480: 78 4e 65 6f 29 3b 0a 09 09 7d 29 3b 0a 09 7d 0a  xNeo);...});..}.
1490: 09 74 68 72 6f 77 20 6e 65 77 20 52 75 6e 74 69  .throw new Runti
14a0: 6d 65 45 78 63 65 70 74 69 6f 6e 28 5f 65 2e 70  meException(_e.p
14b0: 6f 73 2c 20 73 70 72 69 6e 74 66 21 22 55 6e 6b  os, sprintf!"Unk
14c0: 6e 6f 77 6e 20 4b 69 6e 64 20 6f 66 20 45 78 70  nown Kind of Exp
14d0: 72 65 73 73 69 6f 6e 20 25 73 22 28 74 79 70 65  ression %s"(type
14e0: 69 64 28 5f 65 29 29 29 3b 0a 7d 0a 0d 0a 75 6e  id(_e)));.}...un
14f0: 69 74 74 65 73 74 0a 7b 0d 0a 09 61 75 74 6f 20  ittest.{...auto 
1500: 72 20 3d 20 61 73 73 65 72 74 5f 6e 6f 74 68 72  r = assert_nothr
1510: 6f 77 28 20 65 76 61 6c 53 74 72 69 6e 67 28 60  ow( evalString(`
1520: 76 61 72 20 78 20 3d 20 32 31 3b 20 78 20 2b 20  var x = 21; x + 
1530: 78 2a 78 3b 60 29 20 29 3b 0a 09 61 73 73 65 72  x*x;`) );..asser
1540: 74 5f 65 71 28 20 72 2e 76 61 6c 2c 20 6e 65 77  t_eq( r.val, new
1550: 20 49 6e 74 56 61 6c 75 65 28 42 69 67 49 6e 74   IntValue(BigInt
1560: 28 32 31 2b 32 31 2a 32 31 29 29 20 29 3b 0a 09  (21+21*21)) );..
1570: 61 73 73 65 72 74 5f 65 71 28 20 72 2e 63 74 78  assert_eq( r.ctx
1580: 2e 67 65 74 28 22 78 22 2c 22 40 76 61 6c 22 29  .get("x","@val")
1590: 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 42  , new IntValue(B
15a0: 69 67 49 6e 74 28 32 31 29 29 20 29 3b 0a 09 61  igInt(21)) );..a
15b0: 73 73 65 72 74 5f 6e 6f 74 68 72 6f 77 28 20 72  ssert_nothrow( r
15c0: 2e 63 74 78 2e 67 65 74 28 22 78 22 2c 22 40 76  .ctx.get("x","@v
15d0: 61 6c 22 29 20 29 3b 0a 09 61 73 73 65 72 74 5f  al") );..assert_
15e0: 74 68 72 6f 77 21 52 75 6e 74 69 6d 65 45 78 63  throw!RuntimeExc
15f0: 65 70 74 69 6f 6e 28 20 72 2e 63 74 78 2e 67 65  eption( r.ctx.ge
1600: 74 28 22 79 22 2c 22 40 76 61 6c 22 29 20 29 3b  t("y","@val") );
1610: 0a 7d 0a 75 6e 69 74 74 65 73 74 0d 0a 7b 0d 0a  .}.unittest..{..
1620: 09 61 75 74 6f 20 72 20 3d 20 61 73 73 65 72 74  .auto r = assert
1630: 5f 6e 6f 74 68 72 6f 77 28 20 65 76 61 6c 53 74  _nothrow( evalSt
1640: 72 69 6e 67 28 60 76 61 72 20 78 20 3d 20 32 31  ring(`var x = 21
1650: 3b 20 76 61 72 20 78 20 3d 20 78 20 2b 20 78 2a  ; var x = x + x*
1660: 78 3b 60 29 20 29 3b 0d 0a 09 61 73 73 65 72 74  x;`) );...assert
1670: 5f 65 71 28 20 72 2e 76 61 6c 2c 20 6e 65 77 20  _eq( r.val, new 
1680: 49 6e 74 56 61 6c 75 65 28 42 69 67 49 6e 74 28  IntValue(BigInt(
1690: 32 31 2b 32 31 2a 32 31 29 29 20 29 3b 0d 0a 09  21+21*21)) );...
16a0: 61 73 73 65 72 74 5f 65 71 28 20 72 2e 63 74 78  assert_eq( r.ctx
16b0: 2e 67 65 74 28 22 78 22 2c 22 40 76 61 6c 22 29  .get("x","@val")
16c0: 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 42  , new IntValue(B
16d0: 69 67 49 6e 74 28 32 31 2b 32 31 2a 32 31 29 29  igInt(21+21*21))
16e0: 20 29 3b 0d 0a 09 61 73 73 65 72 74 5f 6e 6f 74   );...assert_not
16f0: 68 72 6f 77 28 20 72 2e 63 74 78 2e 67 65 74 28  hrow( r.ctx.get(
1700: 22 78 22 2c 22 40 76 61 6c 22 29 20 29 3b 0d 0a  "x","@val") );..
1710: 09 61 73 73 65 72 74 5f 74 68 72 6f 77 21 52 75  .assert_throw!Ru
1720: 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e 28 20  ntimeException( 
1730: 72 2e 63 74 78 2e 67 65 74 28 22 79 22 2c 22 40  r.ctx.get("y","@
1740: 76 61 6c 22 29 20 29 3b 0d 0a 7d 0d 0a 75 6e 69  val") );..}..uni
1750: 74 74 65 73 74 0d 0a 7b 0d 0a 09 61 73 73 65 72  ttest..{...asser
1760: 74 5f 65 71 28 20 65 76 61 6c 53 74 72 69 6e 67  t_eq( evalString
1770: 28 60 6c 65 74 20 78 3d 31 3b 20 6c 65 74 20 79  (`let x=1; let y
1780: 3d 28 6c 65 74 20 78 3d 32 29 3b 20 78 60 29 2e  =(let x=2); x`).
1790: 76 61 6c 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75  val, new IntValu
17a0: 65 28 42 69 67 49 6e 74 28 31 29 29 20 29 3b 20  e(BigInt(1)) ); 
17b0: 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20 65 76  ...assert_eq( ev
17c0: 61 6c 53 74 72 69 6e 67 28 60 6c 65 74 20 78 3d  alString(`let x=
17d0: 31 3b 20 6c 65 74 20 79 3d 28 6c 65 74 20 78 3d  1; let y=(let x=
17e0: 32 3b 66 75 6e 28 29 7b 78 7d 29 3b 20 79 28 29  2;fun(){x}); y()
17f0: 60 29 2e 76 61 6c 2c 20 6e 65 77 20 49 6e 74 56  `).val, new IntV
1800: 61 6c 75 65 28 42 69 67 49 6e 74 28 32 29 29 20  alue(BigInt(2)) 
1810: 29 3b 20 0d 0a 7d 0d 0a 75 6e 69 74 74 65 73 74  ); ..}..unittest
1820: 0d 0a 7b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28  ..{...assert_eq(
1830: 20 65 76 61 6c 53 74 72 69 6e 67 28 60 40 61 20   evalString(`@a 
1840: 78 3d 31 3b 20 40 62 20 78 3d 32 3b 20 40 61 28  x=1; @b x=2; @a(
1850: 78 29 60 29 2e 76 61 6c 2c 20 6e 65 77 20 49 6e  x)`).val, new In
1860: 74 56 61 6c 75 65 28 42 69 67 49 6e 74 28 31 29  tValue(BigInt(1)
1870: 29 20 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71  ) );...assert_eq
1880: 28 20 65 76 61 6c 53 74 72 69 6e 67 28 60 40 61  ( evalString(`@a
1890: 20 78 3d 31 3b 20 40 62 20 78 3d 32 3b 20 40 62   x=1; @b x=2; @b
18a0: 28 78 29 60 29 2e 76 61 6c 2c 20 6e 65 77 20 49  (x)`).val, new I
18b0: 6e 74 56 61 6c 75 65 28 42 69 67 49 6e 74 28 32  ntValue(BigInt(2
18c0: 29 29 20 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65  )) );...assert_e
18d0: 71 28 20 65 76 61 6c 53 74 72 69 6e 67 28 60 6c  q( evalString(`l
18e0: 65 74 20 78 3d 31 3b 20 6c 65 74 20 5f 20 3d 20  et x=1; let _ = 
18f0: 28 40 61 20 78 3d 32 3b 32 29 3b 20 78 60 29 2e  (@a x=2;2); x`).
1900: 76 61 6c 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75  val, new IntValu
1910: 65 28 42 69 67 49 6e 74 28 31 29 29 20 29 3b 0d  e(BigInt(1)) );.
1920: 0a 09 61 73 73 65 72 74 5f 74 68 72 6f 77 21 45  ..assert_throw!E
1930: 72 72 6f 72 28 20 65 76 61 6c 53 74 72 69 6e 67  rror( evalString
1940: 28 60 6c 65 74 20 78 3d 31 3b 20 6c 65 74 20 5f  (`let x=1; let _
1950: 20 3d 20 28 40 61 20 78 3d 32 3b 32 29 3b 20 40   = (@a x=2;2); @
1960: 61 28 78 29 60 29 20 29 3b 0d 0a 7d 0d 0a 0a 75  a(x)`) );..}...u
1970: 6e 69 74 74 65 73 74 0a 7b 0a 09 61 73 73 65 72  nittest.{..asser
1980: 74 5f 65 71 28 20 65 76 61 6c 53 74 72 69 6e 67  t_eq( evalString
1990: 28 60 76 61 72 20 66 61 63 20 3d 20 66 75 6e 28  (`var fac = fun(
19a0: 78 29 7b 0a 09 09 69 66 28 78 29 0a 09 09 09 7b  x){...if(x)....{
19b0: 20 78 2a 66 61 63 28 78 2d 31 29 3b 20 7d 0a 09   x*fac(x-1); }..
19c0: 09 65 6c 73 65 0a 09 09 09 7b 20 31 3b 20 7d 3b  .else....{ 1; };
19d0: 0a 09 7d 3b 0a 09 66 61 63 28 31 30 29 3b 60 29  ..};..fac(10);`)
19e0: 2e 76 61 6c 2c 20 6e 65 77 20 49 6e 74 56 61 6c  .val, new IntVal
19f0: 75 65 28 42 69 67 49 6e 74 28 31 30 2a 39 2a 38  ue(BigInt(10*9*8
1a00: 2a 35 30 34 30 29 29 29 3b 0a 09 61 73 73 65 72  *5040)));..asser
1a10: 74 5f 65 71 28 20 65 76 61 6c 53 74 72 69 6e 67  t_eq( evalString
1a20: 28 60 76 61 72 20 66 69 62 20 3d 20 66 75 6e 28  (`var fib = fun(
1a30: 78 29 7b 0a 09 09 69 66 28 78 3c 32 29 0a 09 09  x){...if(x<2)...
1a40: 09 7b 20 31 3b 20 7d 0a 09 09 65 6c 73 65 0a 09  .{ 1; }...else..
1a50: 09 09 7b 20 66 69 62 28 78 2d 31 29 20 2b 20 66  ..{ fib(x-1) + f
1a60: 69 62 28 78 2d 32 29 3b 20 7d 3b 0a 09 7d 3b 0a  ib(x-2); };..};.
1a70: 09 66 69 62 28 31 30 29 3b 60 29 2e 76 61 6c 2c  .fib(10);`).val,
1a80: 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 42 69   new IntValue(Bi
1a90: 67 49 6e 74 28 38 39 29 29 29 3b 0a 7d 0a        gInt(89)));.}.