Hex Artifact Content
Not logged in

Artifact 9d70fd93390357134b95b2f63b83b56345798440:


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 0d 0a 2f 2f 2f 0a 54 61 62 6c 65 20 63 72 65  ...///.Table cre
0140: 61 74 65 47 6c 6f 62 61 6c 43 6f 6e 74 65 78 74  ateGlobalContext
0150: 28 29 0a 7b 0d 0a 09 61 75 74 6f 20 63 74 78 20  ().{...auto ctx 
0160: 3d 20 6e 65 77 20 54 61 62 6c 65 3b 0d 0a 09 2f  = new Table;.../
0170: 2f 20 5b 54 4f 44 4f 5d 20 61 75 74 6f 67 65 6e  / [TODO] autogen
0180: 65 72 61 74 65 20 74 68 65 73 65 20 74 79 70 65  erate these type
0190: 63 68 65 63 6b 73 0a 09 63 74 78 2e 73 65 74 28  checks..ctx.set(
01a0: 22 2b 22 2c 20 22 40 76 22 2c 20 6e 65 77 20 46  "+", "@v", new F
01b0: 75 6e 56 61 6c 75 65 28 64 65 6c 65 67 61 74 65  unValue(delegate
01c0: 20 56 61 6c 75 65 28 69 6d 6d 75 74 61 62 6c 65   Value(immutable
01d0: 20 4c 65 78 50 6f 73 69 74 69 6f 6e 20 70 6f 73   LexPosition pos
01e0: 2c 20 4c 61 79 65 72 20 6c 61 79 2c 20 56 61 6c  , Layer lay, Val
01f0: 75 65 5b 5d 20 61 72 67 73 29 7b 0a 09 09 69 66  ue[] args){...if
0200: 28 20 61 72 67 73 2e 6c 65 6e 67 74 68 20 21 3d  ( args.length !=
0210: 20 32 20 29 0a 09 09 09 74 68 72 6f 77 20 67 65   2 )....throw ge
0220: 6e 65 78 21 52 75 6e 74 69 6d 65 45 78 63 65 70  nex!RuntimeExcep
0230: 74 69 6f 6e 28 70 6f 73 2c 20 22 2b 20 74 61 6b  tion(pos, "+ tak
0240: 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73  es two arguments
0250: 21 21 22 29 3b 0a 09 09 69 66 28 20 61 75 74 6f  !!");...if( auto
0260: 20 78 20 3d 20 63 61 73 74 28 49 6e 74 56 61 6c   x = cast(IntVal
0270: 75 65 29 61 72 67 73 5b 30 5d 20 29 0a 09 09 09  ue)args[0] )....
0280: 69 66 28 20 61 75 74 6f 20 79 20 3d 20 63 61 73  if( auto y = cas
0290: 74 28 49 6e 74 56 61 6c 75 65 29 61 72 67 73 5b  t(IntValue)args[
02a0: 31 5d 20 29 0a 09 09 09 09 72 65 74 75 72 6e 20  1] ).....return 
02b0: 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 78 2e 64  new IntValue(x.d
02c0: 61 74 61 2b 79 2e 64 61 74 61 29 3b 0a 09 09 74  ata+y.data);...t
02d0: 68 72 6f 77 20 67 65 6e 65 78 21 52 75 6e 74 69  hrow genex!Runti
02e0: 6d 65 45 78 63 65 70 74 69 6f 6e 28 70 6f 73 2c  meException(pos,
02f0: 20 22 63 61 6e 6e 6f 74 20 61 64 64 20 6e 6f 6e   "cannot add non
0300: 2d 69 6e 74 65 67 65 72 73 22 29 3b 0a 09 7d 29  -integers");..})
0310: 29 3b 0a 09 63 74 78 2e 73 65 74 28 22 2d 22 2c  );..ctx.set("-",
0320: 20 22 40 76 22 2c 20 6e 65 77 20 46 75 6e 56 61   "@v", new FunVa
0330: 6c 75 65 28 64 65 6c 65 67 61 74 65 20 56 61 6c  lue(delegate Val
0340: 75 65 28 69 6d 6d 75 74 61 62 6c 65 20 4c 65 78  ue(immutable Lex
0350: 50 6f 73 69 74 69 6f 6e 20 70 6f 73 2c 20 4c 61  Position pos, La
0360: 79 65 72 20 6c 61 79 2c 20 56 61 6c 75 65 5b 5d  yer lay, Value[]
0370: 20 61 72 67 73 29 7b 0a 09 09 69 66 28 20 61 72   args){...if( ar
0380: 67 73 2e 6c 65 6e 67 74 68 20 21 3d 20 32 20 29  gs.length != 2 )
0390: 0a 09 09 09 74 68 72 6f 77 20 67 65 6e 65 78 21  ....throw genex!
03a0: 52 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e  RuntimeException
03b0: 28 70 6f 73 2c 20 22 2d 20 74 61 6b 65 73 20 74  (pos, "- takes t
03c0: 77 6f 20 61 72 67 75 6d 65 6e 74 73 21 21 22 29  wo arguments!!")
03d0: 3b 0d 0a 09 09 69 66 28 20 61 75 74 6f 20 78 20  ;....if( auto x 
03e0: 3d 20 63 61 73 74 28 49 6e 74 56 61 6c 75 65 29  = cast(IntValue)
03f0: 61 72 67 73 5b 30 5d 20 29 0a 09 09 09 69 66 28  args[0] )....if(
0400: 20 61 75 74 6f 20 79 20 3d 20 63 61 73 74 28 49   auto y = cast(I
0410: 6e 74 56 61 6c 75 65 29 61 72 67 73 5b 31 5d 20  ntValue)args[1] 
0420: 29 0a 09 09 09 09 72 65 74 75 72 6e 20 6e 65 77  ).....return new
0430: 20 49 6e 74 56 61 6c 75 65 28 78 2e 64 61 74 61   IntValue(x.data
0440: 2d 79 2e 64 61 74 61 29 3b 0a 09 09 74 68 72 6f  -y.data);...thro
0450: 77 20 67 65 6e 65 78 21 52 75 6e 74 69 6d 65 45  w genex!RuntimeE
0460: 78 63 65 70 74 69 6f 6e 28 70 6f 73 2c 20 22 63  xception(pos, "c
0470: 61 6e 6e 6f 74 20 73 75 62 74 72 61 63 74 20 6e  annot subtract n
0480: 6f 6e 2d 69 6e 74 65 67 65 72 73 22 29 3b 0d 0a  on-integers");..
0490: 09 7d 29 29 3b 0a 09 63 74 78 2e 73 65 74 28 22  .}));..ctx.set("
04a0: 2a 22 2c 20 22 40 76 22 2c 20 6e 65 77 20 46 75  *", "@v", new Fu
04b0: 6e 56 61 6c 75 65 28 64 65 6c 65 67 61 74 65 20  nValue(delegate 
04c0: 56 61 6c 75 65 28 69 6d 6d 75 74 61 62 6c 65 20  Value(immutable 
04d0: 4c 65 78 50 6f 73 69 74 69 6f 6e 20 70 6f 73 2c  LexPosition pos,
04e0: 20 4c 61 79 65 72 20 6c 61 79 2c 20 56 61 6c 75   Layer lay, Valu
04f0: 65 5b 5d 20 61 72 67 73 29 7b 0a 09 09 69 66 28  e[] args){...if(
0500: 20 61 72 67 73 2e 6c 65 6e 67 74 68 20 21 3d 20   args.length != 
0510: 32 20 29 0a 09 09 09 74 68 72 6f 77 20 67 65 6e  2 )....throw gen
0520: 65 78 21 52 75 6e 74 69 6d 65 45 78 63 65 70 74  ex!RuntimeExcept
0530: 69 6f 6e 28 70 6f 73 2c 20 22 2a 20 74 61 6b 65  ion(pos, "* take
0540: 73 20 74 77 6f 20 61 72 67 75 6d 65 6e 74 73 21  s two arguments!
0550: 21 22 29 3b 0d 0a 09 09 69 66 28 20 61 75 74 6f  !");....if( auto
0560: 20 78 20 3d 20 63 61 73 74 28 49 6e 74 56 61 6c   x = cast(IntVal
0570: 75 65 29 61 72 67 73 5b 30 5d 20 29 0a 09 09 09  ue)args[0] )....
0580: 69 66 28 20 61 75 74 6f 20 79 20 3d 20 63 61 73  if( auto y = cas
0590: 74 28 49 6e 74 56 61 6c 75 65 29 61 72 67 73 5b  t(IntValue)args[
05a0: 31 5d 20 29 0a 09 09 09 09 72 65 74 75 72 6e 20  1] ).....return 
05b0: 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 78 2e 64  new IntValue(x.d
05c0: 61 74 61 2a 79 2e 64 61 74 61 29 3b 0a 09 09 74  ata*y.data);...t
05d0: 68 72 6f 77 20 67 65 6e 65 78 21 52 75 6e 74 69  hrow genex!Runti
05e0: 6d 65 45 78 63 65 70 74 69 6f 6e 28 70 6f 73 2c  meException(pos,
05f0: 20 22 63 61 6e 6e 6f 74 20 6d 75 6c 74 69 70 6c   "cannot multipl
0600: 79 20 6e 6f 6e 2d 69 6e 74 65 67 65 72 73 22 29  y non-integers")
0610: 3b 0d 0a 09 7d 29 29 3b 0a 09 63 74 78 2e 73 65  ;...}));..ctx.se
0620: 74 28 22 2f 22 2c 20 22 40 76 22 2c 20 6e 65 77  t("/", "@v", new
0630: 20 46 75 6e 56 61 6c 75 65 28 64 65 6c 65 67 61   FunValue(delega
0640: 74 65 20 56 61 6c 75 65 28 69 6d 6d 75 74 61 62  te Value(immutab
0650: 6c 65 20 4c 65 78 50 6f 73 69 74 69 6f 6e 20 70  le LexPosition p
0660: 6f 73 2c 20 4c 61 79 65 72 20 6c 61 79 2c 20 56  os, Layer lay, V
0670: 61 6c 75 65 5b 5d 20 61 72 67 73 29 7b 0a 09 09  alue[] args){...
0680: 69 66 28 20 61 72 67 73 2e 6c 65 6e 67 74 68 20  if( args.length 
0690: 21 3d 20 32 20 29 0a 09 09 09 74 68 72 6f 77 20  != 2 )....throw 
06a0: 67 65 6e 65 78 21 52 75 6e 74 69 6d 65 45 78 63  genex!RuntimeExc
06b0: 65 70 74 69 6f 6e 28 70 6f 73 2c 20 22 2f 20 74  eption(pos, "/ t
06c0: 61 6b 65 73 20 74 77 6f 20 61 72 67 75 6d 65 6e  akes two argumen
06d0: 74 73 21 21 22 29 3b 0d 0a 09 09 69 66 28 20 61  ts!!");....if( a
06e0: 75 74 6f 20 78 20 3d 20 63 61 73 74 28 49 6e 74  uto x = cast(Int
06f0: 56 61 6c 75 65 29 61 72 67 73 5b 30 5d 20 29 0a  Value)args[0] ).
0700: 09 09 09 69 66 28 20 61 75 74 6f 20 79 20 3d 20  ...if( auto y = 
0710: 63 61 73 74 28 49 6e 74 56 61 6c 75 65 29 61 72  cast(IntValue)ar
0720: 67 73 5b 31 5d 20 29 0a 09 09 09 09 72 65 74 75  gs[1] ).....retu
0730: 72 6e 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28  rn new IntValue(
0740: 78 2e 64 61 74 61 2f 79 2e 64 61 74 61 29 3b 0a  x.data/y.data);.
0750: 09 09 74 68 72 6f 77 20 67 65 6e 65 78 21 52 75  ..throw genex!Ru
0760: 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e 28 70  ntimeException(p
0770: 6f 73 2c 20 22 63 61 6e 6e 6f 74 20 64 69 76 69  os, "cannot divi
0780: 64 65 20 6e 6f 6e 2d 69 6e 74 65 67 65 72 73 22  de non-integers"
0790: 29 3b 0d 0a 09 7d 29 29 3b 0a 09 63 74 78 2e 73  );...}));..ctx.s
07a0: 65 74 28 22 3c 22 2c 20 22 40 76 22 2c 20 6e 65  et("<", "@v", ne
07b0: 77 20 46 75 6e 56 61 6c 75 65 28 64 65 6c 65 67  w FunValue(deleg
07c0: 61 74 65 20 56 61 6c 75 65 28 69 6d 6d 75 74 61  ate Value(immuta
07d0: 62 6c 65 20 4c 65 78 50 6f 73 69 74 69 6f 6e 20  ble LexPosition 
07e0: 70 6f 73 2c 20 4c 61 79 65 72 20 6c 61 79 2c 20  pos, Layer lay, 
07f0: 56 61 6c 75 65 5b 5d 20 61 72 67 73 29 7b 0a 09  Value[] args){..
0800: 09 69 66 28 20 61 72 67 73 2e 6c 65 6e 67 74 68  .if( args.length
0810: 20 21 3d 20 32 20 29 0a 09 09 09 74 68 72 6f 77   != 2 )....throw
0820: 20 67 65 6e 65 78 21 52 75 6e 74 69 6d 65 45 78   genex!RuntimeEx
0830: 63 65 70 74 69 6f 6e 28 70 6f 73 2c 20 22 3c 20  ception(pos, "< 
0840: 74 61 6b 65 73 20 74 77 6f 20 61 72 67 75 6d 65  takes two argume
0850: 6e 74 73 21 21 22 29 3b 0d 0a 09 09 69 66 28 20  nts!!");....if( 
0860: 61 75 74 6f 20 78 20 3d 20 63 61 73 74 28 49 6e  auto x = cast(In
0870: 74 56 61 6c 75 65 29 61 72 67 73 5b 30 5d 20 29  tValue)args[0] )
0880: 0a 09 09 09 69 66 28 20 61 75 74 6f 20 79 20 3d  ....if( auto y =
0890: 20 63 61 73 74 28 49 6e 74 56 61 6c 75 65 29 61   cast(IntValue)a
08a0: 72 67 73 5b 31 5d 20 29 0a 09 09 09 09 72 65 74  rgs[1] ).....ret
08b0: 75 72 6e 20 6e 65 77 20 49 6e 74 56 61 6c 75 65  urn new IntValue
08c0: 28 42 69 67 49 6e 74 28 74 6f 21 69 6e 74 28 78  (BigInt(to!int(x
08d0: 2e 64 61 74 61 20 3c 20 79 2e 64 61 74 61 29 29  .data < y.data))
08e0: 29 3b 0a 09 09 74 68 72 6f 77 20 67 65 6e 65 78  );...throw genex
08f0: 21 52 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f  !RuntimeExceptio
0900: 6e 28 70 6f 73 2c 20 22 63 61 6e 6e 6f 74 20 63  n(pos, "cannot c
0910: 6f 6d 70 61 72 65 20 6e 6f 6e 2d 69 6e 74 65 67  ompare non-integ
0920: 65 72 73 22 29 3b 0d 0a 09 7d 29 29 3b 0a 09 63  ers");...}));..c
0930: 74 78 2e 73 65 74 28 22 3e 22 2c 20 22 40 76 22  tx.set(">", "@v"
0940: 2c 20 6e 65 77 20 46 75 6e 56 61 6c 75 65 28 64  , new FunValue(d
0950: 65 6c 65 67 61 74 65 20 56 61 6c 75 65 28 69 6d  elegate Value(im
0960: 6d 75 74 61 62 6c 65 20 4c 65 78 50 6f 73 69 74  mutable LexPosit
0970: 69 6f 6e 20 70 6f 73 2c 20 4c 61 79 65 72 20 6c  ion pos, Layer l
0980: 61 79 2c 20 56 61 6c 75 65 5b 5d 20 61 72 67 73  ay, Value[] args
0990: 29 7b 0a 09 09 69 66 28 20 61 72 67 73 2e 6c 65  ){...if( args.le
09a0: 6e 67 74 68 20 21 3d 20 32 20 29 0a 09 09 09 74  ngth != 2 )....t
09b0: 68 72 6f 77 20 67 65 6e 65 78 21 52 75 6e 74 69  hrow genex!Runti
09c0: 6d 65 45 78 63 65 70 74 69 6f 6e 28 70 6f 73 2c  meException(pos,
09d0: 20 22 3e 20 74 61 6b 65 73 20 74 77 6f 20 61 72   "> takes two ar
09e0: 67 75 6d 65 6e 74 73 21 21 22 29 3b 0d 0a 09 09  guments!!");....
09f0: 69 66 28 20 61 75 74 6f 20 78 20 3d 20 63 61 73  if( auto x = cas
0a00: 74 28 49 6e 74 56 61 6c 75 65 29 61 72 67 73 5b  t(IntValue)args[
0a10: 30 5d 20 29 0a 09 09 09 69 66 28 20 61 75 74 6f  0] )....if( auto
0a20: 20 79 20 3d 20 63 61 73 74 28 49 6e 74 56 61 6c   y = cast(IntVal
0a30: 75 65 29 61 72 67 73 5b 31 5d 20 29 0a 09 09 09  ue)args[1] )....
0a40: 09 72 65 74 75 72 6e 20 6e 65 77 20 49 6e 74 56  .return new IntV
0a50: 61 6c 75 65 28 42 69 67 49 6e 74 28 74 6f 21 69  alue(BigInt(to!i
0a60: 6e 74 28 78 2e 64 61 74 61 3e 79 2e 64 61 74 61  nt(x.data>y.data
0a70: 29 29 29 3b 0a 09 09 74 68 72 6f 77 20 67 65 6e  )));...throw gen
0a80: 65 78 21 52 75 6e 74 69 6d 65 45 78 63 65 70 74  ex!RuntimeExcept
0a90: 69 6f 6e 28 70 6f 73 2c 20 22 63 61 6e 6e 6f 74  ion(pos, "cannot
0aa0: 20 63 6f 6d 70 61 72 65 20 6e 6f 6e 2d 69 6e 74   compare non-int
0ab0: 65 67 65 72 73 22 29 3b 0d 0a 09 7d 29 29 3b 0a  egers");...}));.
0ac0: 09 63 74 78 2e 73 65 74 28 22 70 72 69 6e 74 22  .ctx.set("print"
0ad0: 2c 20 22 40 76 22 2c 20 6e 65 77 20 46 75 6e 56  , "@v", new FunV
0ae0: 61 6c 75 65 28 64 65 6c 65 67 61 74 65 20 56 61  alue(delegate Va
0af0: 6c 75 65 28 69 6d 6d 75 74 61 62 6c 65 20 4c 65  lue(immutable Le
0b00: 78 50 6f 73 69 74 69 6f 6e 20 70 6f 73 2c 20 4c  xPosition pos, L
0b10: 61 79 65 72 20 6c 61 79 2c 20 56 61 6c 75 65 5b  ayer lay, Value[
0b20: 5d 20 61 72 67 73 29 7b 0a 09 09 66 6f 72 65 61  ] args){...forea
0b30: 63 68 28 61 3b 20 61 72 67 73 29 0a 09 09 09 77  ch(a; args)....w
0b40: 72 69 74 65 28 61 29 3b 0a 09 09 77 72 69 74 65  rite(a);...write
0b50: 6c 6e 28 22 22 29 3b 0a 09 09 72 65 74 75 72 6e  ln("");...return
0b60: 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 42 69   new IntValue(Bi
0b70: 67 49 6e 74 28 31 37 38 29 29 3b 0a 09 7d 29 29  gInt(178));..}))
0b80: 3b 0a 09 63 74 78 2e 73 65 74 28 22 69 66 22 2c  ;..ctx.set("if",
0b90: 20 22 40 76 22 2c 20 6e 65 77 20 46 75 6e 56 61   "@v", new FunVa
0ba0: 6c 75 65 28 64 65 6c 65 67 61 74 65 20 56 61 6c  lue(delegate Val
0bb0: 75 65 28 69 6d 6d 75 74 61 62 6c 65 20 4c 65 78  ue(immutable Lex
0bc0: 50 6f 73 69 74 69 6f 6e 20 70 6f 73 2c 20 4c 61  Position pos, La
0bd0: 79 65 72 20 6c 61 79 2c 20 56 61 6c 75 65 5b 5d  yer lay, Value[]
0be0: 20 61 72 67 73 29 7b 0a 09 09 69 66 28 20 61 72   args){...if( ar
0bf0: 67 73 2e 6c 65 6e 67 74 68 20 21 3d 20 33 20 29  gs.length != 3 )
0c00: 0a 09 09 09 74 68 72 6f 77 20 67 65 6e 65 78 21  ....throw genex!
0c10: 52 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e  RuntimeException
0c20: 28 70 6f 73 2c 20 22 69 66 20 74 61 6b 65 73 20  (pos, "if takes 
0c30: 74 68 72 65 65 20 61 72 67 75 6d 65 6e 74 73 21  three arguments!
0c40: 21 22 29 3b 0a 09 09 69 66 28 20 61 75 74 6f 20  !");...if( auto 
0c50: 78 20 3d 20 63 61 73 74 28 49 6e 74 56 61 6c 75  x = cast(IntValu
0c60: 65 29 61 72 67 73 5b 30 5d 20 29 0a 09 09 69 66  e)args[0] )...if
0c70: 28 20 61 75 74 6f 20 66 74 20 3d 20 63 61 73 74  ( auto ft = cast
0c80: 28 46 75 6e 56 61 6c 75 65 29 61 72 67 73 5b 31  (FunValue)args[1
0c90: 5d 20 29 0a 09 09 69 66 28 20 61 75 74 6f 20 66  ] )...if( auto f
0ca0: 65 20 3d 20 63 61 73 74 28 46 75 6e 56 61 6c 75  e = cast(FunValu
0cb0: 65 29 61 72 67 73 5b 32 5d 20 29 0a 09 09 09 72  e)args[2] )....r
0cc0: 65 74 75 72 6e 20 28 78 2e 64 61 74 61 20 3d 3d  eturn (x.data ==
0cd0: 20 30 20 3f 20 66 65 20 3a 20 66 74 29 2e 63 61   0 ? fe : ft).ca
0ce0: 6c 6c 28 70 6f 73 2c 6c 61 79 2c 5b 5d 29 3b 0a  ll(pos,lay,[]);.
0cf0: 09 09 74 68 72 6f 77 20 67 65 6e 65 78 21 52 75  ..throw genex!Ru
0d00: 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e 28 70  ntimeException(p
0d10: 6f 73 2c 20 22 74 79 70 65 20 6d 69 73 6d 61 74  os, "type mismat
0d20: 63 68 20 69 6e 20 69 66 22 29 3b 0d 0a 09 7d 29  ch in if");...})
0d30: 29 3b 0a 09 72 65 74 75 72 6e 20 63 74 78 3b 0a  );..return ctx;.
0d40: 7d 0a 0a 2f 2f 2f 20 45 6e 74 72 79 20 70 6f 69  }../// Entry poi
0d50: 6e 74 20 6f 66 20 74 68 69 73 20 6d 6f 64 75 6c  nt of this modul
0d60: 65 0d 0a 0d 0a 54 75 70 6c 65 21 28 56 61 6c 75  e....Tuple!(Valu
0d70: 65 2c 22 76 61 6c 22 2c 54 61 62 6c 65 2c 22 63  e,"val",Table,"c
0d80: 74 78 22 29 20 65 76 61 6c 53 74 72 69 6e 67 28  tx") evalString(
0d90: 53 2c 54 2e 2e 2e 29 28 53 20 73 74 72 2c 20 54  S,T...)(S str, T
0da0: 20 66 6e 5f 6c 6e 5f 63 6e 29 0a 7b 0a 09 72 65   fn_ln_cn).{..re
0db0: 74 75 72 6e 20 65 76 61 6c 28 20 70 6f 6c 65 6d  turn eval( polem
0dc0: 79 2e 70 61 72 73 65 2e 70 61 72 73 65 53 74 72  y.parse.parseStr
0dd0: 69 6e 67 28 73 74 72 2c 20 66 6e 5f 6c 6e 5f 63  ing(str, fn_ln_c
0de0: 6e 29 20 29 3b 0a 7d 0a 0a 2f 2f 2f 20 45 6e 74  n) );.}../// Ent
0df0: 72 79 20 70 6f 69 6e 74 20 6f 66 20 74 68 69 73  ry point of this
0e00: 20 6d 6f 64 75 6c 65 0d 0a 0d 0a 54 75 70 6c 65   module....Tuple
0e10: 21 28 56 61 6c 75 65 2c 22 76 61 6c 22 2c 54 61  !(Value,"val",Ta
0e20: 62 6c 65 2c 22 63 74 78 22 29 20 65 76 61 6c 46  ble,"ctx") evalF
0e30: 69 6c 65 28 53 2c 20 54 2e 2e 2e 29 28 53 20 66  ile(S, T...)(S f
0e40: 69 6c 65 6e 61 6d 65 2c 20 54 20 6c 6e 5f 63 6e  ilename, T ln_cn
0e50: 29 0a 7b 0a 09 72 65 74 75 72 6e 20 65 76 61 6c  ).{..return eval
0e60: 28 20 70 6f 6c 65 6d 79 2e 70 61 72 73 65 2e 70  ( polemy.parse.p
0e70: 61 72 73 65 46 69 6c 65 28 66 69 6c 65 6e 61 6d  arseFile(filenam
0e80: 65 2c 20 6c 6e 5f 63 6e 29 20 29 3b 0a 7d 0a 0a  e, ln_cn) );.}..
0e90: 2f 2f 2f 20 45 6e 74 72 79 20 70 6f 69 6e 74 20  /// Entry point 
0ea0: 6f 66 20 74 68 69 73 20 6d 6f 64 75 6c 65 0d 0a  of this module..
0eb0: 0d 0a 54 75 70 6c 65 21 28 56 61 6c 75 65 2c 22  ..Tuple!(Value,"
0ec0: 76 61 6c 22 2c 54 61 62 6c 65 2c 22 63 74 78 22  val",Table,"ctx"
0ed0: 29 20 65 76 61 6c 28 41 53 54 20 65 29 0a 7b 0a  ) eval(AST e).{.
0ee0: 09 54 61 62 6c 65 20 63 74 78 20 3d 20 63 72 65  .Table ctx = cre
0ef0: 61 74 65 47 6c 6f 62 61 6c 43 6f 6e 74 65 78 74  ateGlobalContext
0f00: 28 29 3b 0a 09 72 65 74 75 72 6e 20 74 79 70 65  ();..return type
0f10: 6f 66 28 72 65 74 75 72 6e 29 28 65 76 61 6c 28  of(return)(eval(
0f20: 65 2c 20 63 74 78 2c 20 66 61 6c 73 65 2c 20 22  e, ctx, false, "
0f30: 40 76 22 29 2c 20 63 74 78 29 3b 0a 7d 0a 0a 2f  @v"), ctx);.}../
0f40: 2f 2f 20 45 6e 74 72 79 20 70 6f 69 6e 74 20 6f  // Entry point o
0f50: 66 20 74 68 69 73 20 6d 6f 64 75 6c 65 0d 0a 2f  f this module../
0f60: 2f 2f 20 49 66 20 73 70 6c 69 74 43 74 78 20 3d  // If splitCtx =
0f70: 20 74 72 75 65 2c 20 74 68 65 6e 20 69 6e 6e 65   true, then inne
0f80: 72 20 76 61 72 69 61 62 6c 65 20 64 65 63 6c 61  r variable decla
0f90: 72 61 74 69 6f 6e 20 64 6f 20 6e 6f 74 20 6f 76  ration do not ov
0fa0: 65 72 77 72 69 74 65 20 63 74 78 2e 0d 0a 2f 2f  erwrite ctx...//
0fb0: 2f 20 6c 61 79 20 69 73 20 74 68 65 20 6c 61 79  / lay is the lay
0fc0: 65 72 20 49 44 20 66 6f 72 20 65 76 61 6c 75 61  er ID for evalua
0fd0: 74 69 6f 6e 20 28 73 74 61 6e 64 61 72 64 20 76  tion (standard v
0fe0: 61 6c 75 65 20 73 65 6d 61 6e 74 69 63 73 20 75  alue semantics u
0ff0: 73 65 73 20 22 40 76 22 29 2e 0d 0a 0d 0a 56 61  ses "@v").....Va
1000: 6c 75 65 20 65 76 61 6c 28 41 53 54 20 5f 65 2c  lue eval(AST _e,
1010: 20 54 61 62 6c 65 20 63 74 78 2c 20 62 6f 6f 6c   Table ctx, bool
1020: 20 73 70 6c 69 74 43 74 78 2c 20 4c 61 79 65 72   splitCtx, Layer
1030: 20 6c 61 79 29 0a 7b 0a 09 69 66 28 20 61 75 74   lay).{..if( aut
1040: 6f 20 65 20 3d 20 63 61 73 74 28 53 74 72 4c 69  o e = cast(StrLi
1050: 74 65 72 61 6c 29 5f 65 20 29 0a 09 7b 0a 09 09  teral)_e )..{...
1060: 72 65 74 75 72 6e 20 6e 65 77 20 53 74 72 56 61  return new StrVa
1070: 6c 75 65 28 65 2e 64 61 74 61 29 3b 0a 09 7d 0a  lue(e.data);..}.
1080: 09 65 6c 73 65 0a 09 69 66 28 20 61 75 74 6f 20  .else..if( auto 
1090: 65 20 3d 20 63 61 73 74 28 49 6e 74 4c 69 74 65  e = cast(IntLite
10a0: 72 61 6c 29 5f 65 20 29 0a 09 7b 0a 09 09 72 65  ral)_e )..{...re
10b0: 74 75 72 6e 20 6e 65 77 20 49 6e 74 56 61 6c 75  turn new IntValu
10c0: 65 28 65 2e 64 61 74 61 29 3b 0a 09 7d 0a 09 65  e(e.data);..}..e
10d0: 6c 73 65 0d 0a 09 69 66 28 20 61 75 74 6f 20 65  lse...if( auto e
10e0: 20 3d 20 63 61 73 74 28 56 61 72 45 78 70 72 65   = cast(VarExpre
10f0: 73 73 69 6f 6e 29 5f 65 20 29 0d 0a 09 7b 0d 0a  ssion)_e )...{..
1100: 09 09 72 65 74 75 72 6e 20 63 74 78 2e 67 65 74  ..return ctx.get
1110: 28 65 2e 76 61 72 2c 20 6c 61 79 2c 20 65 2e 70  (e.var, lay, e.p
1120: 6f 73 29 3b 0d 0a 09 7d 0d 0a 09 65 6c 73 65 0d  os);...}...else.
1130: 0a 09 69 66 28 20 61 75 74 6f 20 65 20 3d 20 63  ..if( auto e = c
1140: 61 73 74 28 4c 61 79 65 72 65 64 45 78 70 72 65  ast(LayeredExpre
1150: 73 73 69 6f 6e 29 5f 65 20 29 0d 0a 09 7b 0d 0a  ssion)_e )...{..
1160: 09 09 72 65 74 75 72 6e 20 65 76 61 6c 28 65 2e  ..return eval(e.
1170: 65 78 70 72 2c 20 63 74 78 2c 20 66 61 6c 73 65  expr, ctx, false
1180: 2c 20 65 2e 6c 61 79 29 3b 0d 0a 09 7d 0d 0a 09  , e.lay);...}...
1190: 65 6c 73 65 0d 0a 09 69 66 28 20 61 75 74 6f 20  else...if( auto 
11a0: 65 20 3d 20 63 61 73 74 28 4c 65 74 45 78 70 72  e = cast(LetExpr
11b0: 65 73 73 69 6f 6e 29 5f 65 20 29 0d 0a 09 7b 0d  ession)_e )...{.
11c0: 0a 09 09 2f 2f 20 66 6f 72 20 6c 65 74 72 65 63  ...// for letrec
11d0: 2c 20 77 65 20 6e 65 65 64 20 74 68 69 73 2c 20  , we need this, 
11e0: 62 75 74 20 73 68 6f 75 6c 64 20 61 76 6f 69 64  but should avoid
11f0: 20 6f 76 65 72 77 72 69 74 69 6e 67 3f 3f 3f 3f   overwriting????
1200: 0d 0a 09 09 2f 2f 20 63 74 78 2e 73 65 74 28 65  ....// ctx.set(e
1210: 2e 76 61 72 2c 20 22 40 76 22 2c 20 6e 65 77 20  .var, "@v", new 
1220: 55 6e 64 65 66 69 6e 65 64 56 61 6c 75 65 2c 20  UndefinedValue, 
1230: 65 2e 70 6f 73 29 3b 0d 0a 09 09 56 61 6c 75 65  e.pos);....Value
1240: 20 76 20 3d 20 65 76 61 6c 28 65 2e 69 6e 69 74   v = eval(e.init
1250: 2c 20 63 74 78 2c 20 74 72 75 65 2c 20 6c 61 79  , ctx, true, lay
1260: 29 3b 0d 0a 09 09 69 66 28 73 70 6c 69 74 43 74  );....if(splitCt
1270: 78 29 0d 0a 09 09 09 63 74 78 20 3d 20 6e 65 77  x).....ctx = new
1280: 20 54 61 62 6c 65 28 63 74 78 2c 20 54 61 62 6c   Table(ctx, Tabl
1290: 65 2e 4b 69 6e 64 2e 4e 6f 74 50 72 6f 70 61 67  e.Kind.NotPropag
12a0: 61 74 65 53 65 74 29 3b 0d 0a 09 09 63 74 78 2e  ateSet);....ctx.
12b0: 73 65 74 28 65 2e 76 61 72 2c 20 28 65 2e 6c 61  set(e.var, (e.la
12c0: 79 65 72 2e 6c 65 6e 67 74 68 20 3f 20 65 2e 6c  yer.length ? e.l
12d0: 61 79 65 72 20 3a 20 6c 61 79 29 2c 20 76 2c 20  ayer : lay), v, 
12e0: 65 2e 70 6f 73 29 3b 0d 0a 09 09 72 65 74 75 72  e.pos);....retur
12f0: 6e 20 65 76 61 6c 28 65 2e 65 78 70 72 2c 20 63  n eval(e.expr, c
1300: 74 78 2c 20 66 61 6c 73 65 2c 20 6c 61 79 29 3b  tx, false, lay);
1310: 0d 0a 09 7d 0d 0a 09 65 6c 73 65 0a 09 69 66 28  ...}...else..if(
1320: 20 61 75 74 6f 20 65 20 3d 20 63 61 73 74 28 46   auto e = cast(F
1330: 75 6e 63 61 6c 6c 45 78 70 72 65 73 73 69 6f 6e  uncallExpression
1340: 29 5f 65 20 29 0a 09 7b 0a 09 09 56 61 6c 75 65  )_e )..{...Value
1350: 20 5f 66 20 3d 20 65 76 61 6c 28 65 2e 66 75 6e   _f = eval(e.fun
1360: 2c 20 63 74 78 2c 20 74 72 75 65 2c 20 6c 61 79  , ctx, true, lay
1370: 29 3b 0a 09 09 69 66 28 20 61 75 74 6f 20 66 20  );...if( auto f 
1380: 3d 20 63 61 73 74 28 46 75 6e 56 61 6c 75 65 29  = cast(FunValue)
1390: 5f 66 20 29 20 7b 0a 09 09 09 56 61 6c 75 65 5b  _f ) {....Value[
13a0: 5d 20 61 72 67 73 3b 0a 09 09 09 66 6f 72 65 61  ] args;....forea
13b0: 63 68 28 61 3b 20 65 2e 61 72 67 73 29 0a 09 09  ch(a; e.args)...
13c0: 09 09 61 72 67 73 20 7e 3d 20 65 76 61 6c 28 61  ..args ~= eval(a
13d0: 2c 20 63 74 78 2c 20 74 72 75 65 2c 20 6c 61 79  , ctx, true, lay
13e0: 29 3b 0a 09 09 09 72 65 74 75 72 6e 20 66 2e 63  );....return f.c
13f0: 61 6c 6c 28 65 2e 70 6f 73 2c 20 6c 61 79 2c 20  all(e.pos, lay, 
1400: 61 72 67 73 29 3b 0a 09 09 7d 20 65 6c 73 65 0a  args);...} else.
1410: 09 09 09 74 68 72 6f 77 20 67 65 6e 65 78 21 52  ...throw genex!R
1420: 75 6e 74 69 6d 65 45 78 63 65 70 74 69 6f 6e 28  untimeException(
1430: 65 2e 70 6f 73 2c 20 22 4e 6f 6e 2d 66 75 6e 63  e.pos, "Non-func
1440: 69 6f 6e 20 69 73 20 61 70 70 6c 69 65 64 22 29  ion is applied")
1450: 3b 0a 09 7d 0a 09 65 6c 73 65 0a 09 69 66 28 20  ;..}..else..if( 
1460: 61 75 74 6f 20 65 20 3d 20 63 61 73 74 28 46 75  auto e = cast(Fu
1470: 6e 4c 69 74 65 72 61 6c 29 5f 65 20 29 0a 09 7b  nLiteral)_e )..{
1480: 0a 09 09 72 65 74 75 72 6e 20 6e 65 77 20 46 75  ...return new Fu
1490: 6e 56 61 6c 75 65 28 64 65 6c 65 67 61 74 65 20  nValue(delegate 
14a0: 56 61 6c 75 65 28 69 6d 6d 75 74 61 62 6c 65 20  Value(immutable 
14b0: 4c 65 78 50 6f 73 69 74 69 6f 6e 20 70 6f 73 2c  LexPosition pos,
14c0: 20 73 74 72 69 6e 67 20 6c 61 79 2c 20 56 61 6c   string lay, Val
14d0: 75 65 5b 5d 20 61 72 67 73 29 7b 0a 09 09 09 69  ue[] args){....i
14e0: 66 28 20 65 2e 70 61 72 61 6d 73 2e 6c 65 6e 67  f( e.params.leng
14f0: 74 68 20 21 3d 20 61 72 67 73 2e 6c 65 6e 67 74  th != args.lengt
1500: 68 20 29 0a 09 09 09 09 74 68 72 6f 77 20 67 65  h ).....throw ge
1510: 6e 65 78 21 52 75 6e 74 69 6d 65 45 78 63 65 70  nex!RuntimeExcep
1520: 74 69 6f 6e 28 65 2e 70 6f 73 2c 20 73 70 72 69  tion(e.pos, spri
1530: 6e 74 66 21 22 41 72 67 75 6d 65 6e 74 20 4e 75  ntf!"Argument Nu
1540: 6d 62 65 72 20 4d 69 73 6d 61 74 63 68 20 28 25  mber Mismatch (%
1550: 64 20 72 65 71 75 69 72 65 64 20 62 75 74 20 25  d required but %
1560: 64 20 67 69 76 65 6e 29 22 0a 09 09 09 09 09 28  d given)"......(
1570: 65 2e 70 61 72 61 6d 73 2e 6c 65 6e 67 74 68 2c  e.params.length,
1580: 20 61 72 67 73 2e 6c 65 6e 67 74 68 29 29 3b 0a   args.length));.
1590: 09 09 09 54 61 62 6c 65 20 63 74 78 4e 65 6f 20  ...Table ctxNeo 
15a0: 3d 20 6e 65 77 20 54 61 62 6c 65 28 63 74 78 2c  = new Table(ctx,
15b0: 20 54 61 62 6c 65 2e 4b 69 6e 64 2e 4e 6f 74 50   Table.Kind.NotP
15c0: 72 6f 70 61 67 61 74 65 53 65 74 29 3b 0a 09 09  ropagateSet);...
15d0: 09 66 6f 72 65 61 63 68 28 69 2c 70 3b 20 65 2e  .foreach(i,p; e.
15e0: 70 61 72 61 6d 73 29 0a 09 09 09 09 63 74 78 4e  params).....ctxN
15f0: 65 6f 2e 73 65 74 28 70 2e 6e 61 6d 65 2c 20 6c  eo.set(p.name, l
1600: 61 79 2c 20 61 72 67 73 5b 69 5d 29 3b 0d 0a 09  ay, args[i]);...
1610: 09 09 72 65 74 75 72 6e 20 65 76 61 6c 28 65 2e  ..return eval(e.
1620: 66 75 6e 62 6f 64 79 2c 20 63 74 78 4e 65 6f 2c  funbody, ctxNeo,
1630: 20 74 72 75 65 2c 20 6c 61 79 29 3b 0a 09 09 7d   true, lay);...}
1640: 29 3b 0a 09 7d 0a 09 74 68 72 6f 77 20 67 65 6e  );..}..throw gen
1650: 65 78 21 52 75 6e 74 69 6d 65 45 78 63 65 70 74  ex!RuntimeExcept
1660: 69 6f 6e 28 5f 65 2e 70 6f 73 2c 20 73 70 72 69  ion(_e.pos, spri
1670: 6e 74 66 21 22 55 6e 6b 6e 6f 77 6e 20 4b 69 6e  ntf!"Unknown Kin
1680: 64 20 6f 66 20 45 78 70 72 65 73 73 69 6f 6e 20  d of Expression 
1690: 25 73 22 28 74 79 70 65 69 64 28 5f 65 29 29 29  %s"(typeid(_e)))
16a0: 3b 0a 7d 0a 0d 0a 75 6e 69 74 74 65 73 74 0a 7b  ;.}...unittest.{
16b0: 0d 0a 09 61 75 74 6f 20 72 20 3d 20 61 73 73 65  ...auto r = asse
16c0: 72 74 5f 6e 6f 74 68 72 6f 77 28 20 65 76 61 6c  rt_nothrow( eval
16d0: 53 74 72 69 6e 67 28 60 76 61 72 20 78 20 3d 20  String(`var x = 
16e0: 32 31 3b 20 78 20 2b 20 78 2a 78 3b 60 29 20 29  21; x + x*x;`) )
16f0: 3b 0a 09 61 73 73 65 72 74 5f 65 71 28 20 72 2e  ;..assert_eq( r.
1700: 76 61 6c 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75  val, new IntValu
1710: 65 28 42 69 67 49 6e 74 28 32 31 2b 32 31 2a 32  e(BigInt(21+21*2
1720: 31 29 29 20 29 3b 0a 09 61 73 73 65 72 74 5f 65  1)) );..assert_e
1730: 71 28 20 72 2e 63 74 78 2e 67 65 74 28 22 78 22  q( r.ctx.get("x"
1740: 2c 22 40 76 22 29 2c 20 6e 65 77 20 49 6e 74 56  ,"@v"), new IntV
1750: 61 6c 75 65 28 42 69 67 49 6e 74 28 32 31 29 29  alue(BigInt(21))
1760: 20 29 3b 0a 09 61 73 73 65 72 74 5f 6e 6f 74 68   );..assert_noth
1770: 72 6f 77 28 20 72 2e 63 74 78 2e 67 65 74 28 22  row( r.ctx.get("
1780: 78 22 2c 22 40 76 22 29 20 29 3b 0a 09 61 73 73  x","@v") );..ass
1790: 65 72 74 5f 74 68 72 6f 77 21 52 75 6e 74 69 6d  ert_throw!Runtim
17a0: 65 45 78 63 65 70 74 69 6f 6e 28 20 72 2e 63 74  eException( r.ct
17b0: 78 2e 67 65 74 28 22 79 22 2c 22 40 76 22 29 20  x.get("y","@v") 
17c0: 29 3b 0a 7d 0a 75 6e 69 74 74 65 73 74 0d 0a 7b  );.}.unittest..{
17d0: 0d 0a 09 61 75 74 6f 20 72 20 3d 20 61 73 73 65  ...auto r = asse
17e0: 72 74 5f 6e 6f 74 68 72 6f 77 28 20 65 76 61 6c  rt_nothrow( eval
17f0: 53 74 72 69 6e 67 28 60 76 61 72 20 78 20 3d 20  String(`var x = 
1800: 32 31 3b 20 76 61 72 20 78 20 3d 20 78 20 2b 20  21; var x = x + 
1810: 78 2a 78 3b 60 29 20 29 3b 0d 0a 09 61 73 73 65  x*x;`) );...asse
1820: 72 74 5f 65 71 28 20 72 2e 76 61 6c 2c 20 6e 65  rt_eq( r.val, ne
1830: 77 20 49 6e 74 56 61 6c 75 65 28 42 69 67 49 6e  w IntValue(BigIn
1840: 74 28 32 31 2b 32 31 2a 32 31 29 29 20 29 3b 0d  t(21+21*21)) );.
1850: 0a 09 61 73 73 65 72 74 5f 65 71 28 20 72 2e 63  ..assert_eq( r.c
1860: 74 78 2e 67 65 74 28 22 78 22 2c 22 40 76 22 29  tx.get("x","@v")
1870: 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 42  , new IntValue(B
1880: 69 67 49 6e 74 28 32 31 2b 32 31 2a 32 31 29 29  igInt(21+21*21))
1890: 20 29 3b 0d 0a 09 61 73 73 65 72 74 5f 6e 6f 74   );...assert_not
18a0: 68 72 6f 77 28 20 72 2e 63 74 78 2e 67 65 74 28  hrow( r.ctx.get(
18b0: 22 78 22 2c 22 40 76 22 29 20 29 3b 0d 0a 09 61  "x","@v") );...a
18c0: 73 73 65 72 74 5f 74 68 72 6f 77 21 52 75 6e 74  ssert_throw!Runt
18d0: 69 6d 65 45 78 63 65 70 74 69 6f 6e 28 20 72 2e  imeException( r.
18e0: 63 74 78 2e 67 65 74 28 22 79 22 2c 22 40 76 22  ctx.get("y","@v"
18f0: 29 20 29 3b 0d 0a 7d 0d 0a 75 6e 69 74 74 65 73  ) );..}..unittes
1900: 74 0d 0a 7b 0d 0a 09 61 73 73 65 72 74 5f 65 71  t..{...assert_eq
1910: 28 20 65 76 61 6c 53 74 72 69 6e 67 28 60 6c 65  ( evalString(`le
1920: 74 20 78 3d 31 3b 20 6c 65 74 20 79 3d 28 6c 65  t x=1; let y=(le
1930: 74 20 78 3d 32 29 3b 20 78 60 29 2e 76 61 6c 2c  t x=2); x`).val,
1940: 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 42 69   new IntValue(Bi
1950: 67 49 6e 74 28 31 29 29 20 29 3b 20 0d 0a 09 61  gInt(1)) ); ...a
1960: 73 73 65 72 74 5f 65 71 28 20 65 76 61 6c 53 74  ssert_eq( evalSt
1970: 72 69 6e 67 28 60 6c 65 74 20 78 3d 31 3b 20 6c  ring(`let x=1; l
1980: 65 74 20 79 3d 28 6c 65 74 20 78 3d 32 3b 66 75  et y=(let x=2;fu
1990: 6e 28 29 7b 78 7d 29 3b 20 79 28 29 60 29 2e 76  n(){x}); y()`).v
19a0: 61 6c 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65  al, new IntValue
19b0: 28 42 69 67 49 6e 74 28 32 29 29 20 29 3b 20 0d  (BigInt(2)) ); .
19c0: 0a 7d 0d 0a 75 6e 69 74 74 65 73 74 0d 0a 7b 0d  .}..unittest..{.
19d0: 0a 09 61 73 73 65 72 74 5f 65 71 28 20 65 76 61  ..assert_eq( eva
19e0: 6c 53 74 72 69 6e 67 28 60 40 61 20 78 3d 31 3b  lString(`@a x=1;
19f0: 20 40 62 20 78 3d 32 3b 20 40 61 28 78 29 60 29   @b x=2; @a(x)`)
1a00: 2e 76 61 6c 2c 20 6e 65 77 20 49 6e 74 56 61 6c  .val, new IntVal
1a10: 75 65 28 42 69 67 49 6e 74 28 31 29 29 20 29 3b  ue(BigInt(1)) );
1a20: 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20 65 76  ...assert_eq( ev
1a30: 61 6c 53 74 72 69 6e 67 28 60 40 61 20 78 3d 31  alString(`@a x=1
1a40: 3b 20 40 62 20 78 3d 32 3b 20 40 62 28 78 29 60  ; @b x=2; @b(x)`
1a50: 29 2e 76 61 6c 2c 20 6e 65 77 20 49 6e 74 56 61  ).val, new IntVa
1a60: 6c 75 65 28 42 69 67 49 6e 74 28 32 29 29 20 29  lue(BigInt(2)) )
1a70: 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28 20 65  ;...assert_eq( e
1a80: 76 61 6c 53 74 72 69 6e 67 28 60 6c 65 74 20 78  valString(`let x
1a90: 3d 31 3b 20 6c 65 74 20 5f 20 3d 20 28 40 61 20  =1; let _ = (@a 
1aa0: 78 3d 32 3b 32 29 3b 20 78 60 29 2e 76 61 6c 2c  x=2;2); x`).val,
1ab0: 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 42 69   new IntValue(Bi
1ac0: 67 49 6e 74 28 31 29 29 20 29 3b 0d 0a 09 61 73  gInt(1)) );...as
1ad0: 73 65 72 74 5f 74 68 72 6f 77 21 45 72 72 6f 72  sert_throw!Error
1ae0: 28 20 65 76 61 6c 53 74 72 69 6e 67 28 60 6c 65  ( evalString(`le
1af0: 74 20 78 3d 31 3b 20 6c 65 74 20 5f 20 3d 20 28  t x=1; let _ = (
1b00: 40 61 20 78 3d 32 3b 32 29 3b 20 40 61 28 78 29  @a x=2;2); @a(x)
1b10: 60 29 20 29 3b 0d 0a 7d 0d 0a 0a 75 6e 69 74 74  `) );..}...unitt
1b20: 65 73 74 0a 7b 0a 09 61 73 73 65 72 74 5f 65 71  est.{..assert_eq
1b30: 28 20 65 76 61 6c 53 74 72 69 6e 67 28 60 76 61  ( evalString(`va
1b40: 72 20 66 61 63 20 3d 20 66 75 6e 28 78 29 7b 0a  r fac = fun(x){.
1b50: 09 09 69 66 28 78 29 0a 09 09 09 7b 20 78 2a 66  ..if(x)....{ x*f
1b60: 61 63 28 78 2d 31 29 3b 20 7d 0a 09 09 65 6c 73  ac(x-1); }...els
1b70: 65 0a 09 09 09 7b 20 31 3b 20 7d 3b 0a 09 7d 3b  e....{ 1; };..};
1b80: 0a 09 66 61 63 28 31 30 29 3b 60 29 2e 76 61 6c  ..fac(10);`).val
1b90: 2c 20 6e 65 77 20 49 6e 74 56 61 6c 75 65 28 42  , new IntValue(B
1ba0: 69 67 49 6e 74 28 31 30 2a 39 2a 38 2a 35 30 34  igInt(10*9*8*504
1bb0: 30 29 29 29 3b 0a 09 61 73 73 65 72 74 5f 65 71  0)));..assert_eq
1bc0: 28 20 65 76 61 6c 53 74 72 69 6e 67 28 60 76 61  ( evalString(`va
1bd0: 72 20 66 69 62 20 3d 20 66 75 6e 28 78 29 7b 0a  r fib = fun(x){.
1be0: 09 09 69 66 28 78 3c 32 29 0a 09 09 09 7b 20 31  ..if(x<2)....{ 1
1bf0: 3b 20 7d 0a 09 09 65 6c 73 65 0a 09 09 09 7b 20  ; }...else....{ 
1c00: 66 69 62 28 78 2d 31 29 20 2b 20 66 69 62 28 78  fib(x-1) + fib(x
1c10: 2d 32 29 3b 20 7d 3b 0a 09 7d 3b 0a 09 66 69 62  -2); };..};..fib
1c20: 28 31 30 29 3b 60 29 2e 76 61 6c 2c 20 6e 65 77  (10);`).val, new
1c30: 20 49 6e 74 56 61 6c 75 65 28 42 69 67 49 6e 74   IntValue(BigInt
1c40: 28 38 39 29 29 29 3b 0a 7d 0a 0d 0a 75 6e 69 74  (89)));.}...unit
1c50: 74 65 73 74 0d 0a 7b 0d 0a 09 61 73 73 65 72 74  test..{...assert
1c60: 5f 74 68 72 6f 77 21 54 68 72 6f 77 61 62 6c 65  _throw!Throwable
1c70: 28 20 65 76 61 6c 53 74 72 69 6e 67 28 60 40 73  ( evalString(`@s
1c80: 20 22 2b 22 3d 66 75 6e 28 78 2c 79 29 7b 78 2d   "+"=fun(x,y){x-
1c90: 79 7d 3b 40 73 28 31 2b 32 29 60 29 20 29 3b 0d  y};@s(1+2)`) );.
1ca0: 0a 09 61 73 73 65 72 74 5f 65 71 28 20 65 76 61  ..assert_eq( eva
1cb0: 6c 53 74 72 69 6e 67 28 60 40 73 20 22 2b 22 3d  lString(`@s "+"=
1cc0: 66 75 6e 28 78 2c 79 29 7b 78 2d 79 7d 3b 31 2b  fun(x,y){x-y};1+
1cd0: 32 60 29 2e 76 61 6c 2c 20 6e 65 77 20 49 6e 74  2`).val, new Int
1ce0: 56 61 6c 75 65 28 42 69 67 49 6e 74 28 33 29 29  Value(BigInt(3))
1cf0: 20 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28   );...assert_eq(
1d00: 20 65 76 61 6c 53 74 72 69 6e 67 28 60 40 73 20   evalString(`@s 
1d10: 22 2b 22 3d 66 75 6e 28 78 2c 79 29 7b 40 76 28  "+"=fun(x,y){@v(
1d20: 40 73 28 78 29 2d 40 73 28 79 29 29 7d 3b 31 2b  @s(x)-@s(y))};1+
1d30: 32 60 29 2e 76 61 6c 2c 20 6e 65 77 20 49 6e 74  2`).val, new Int
1d40: 56 61 6c 75 65 28 42 69 67 49 6e 74 28 33 29 29  Value(BigInt(3))
1d50: 20 29 3b 0d 0a 09 61 73 73 65 72 74 5f 65 71 28   );...assert_eq(
1d60: 20 65 76 61 6c 53 74 72 69 6e 67 28 60 40 73 20   evalString(`@s 
1d70: 22 2b 22 3d 66 75 6e 28 78 2c 79 29 7b 40 76 28  "+"=fun(x,y){@v(
1d80: 40 73 28 78 29 2d 40 73 28 79 29 29 7d 3b 40 73  @s(x)-@s(y))};@s
1d90: 28 31 2b 32 29 60 29 2e 76 61 6c 2c 20 6e 65 77  (1+2)`).val, new
1da0: 20 49 6e 74 56 61 6c 75 65 28 42 69 67 49 6e 74   IntValue(BigInt
1db0: 28 2d 31 29 29 20 29 3b 0d 0a 7d                 (-1)) );..}