Hex Artifact Content
Not logged in

Artifact 2eb228662dba6e2da3fc4fc62e55c4a1e62075ba:


0000: ef bb bf 2f 2a 2a 0d 0a 20 2a 20 41 75 74 68 6f  .../**.. * Autho
0010: 72 73 3a 20 6b 2e 69 6e 61 62 61 0d 0a 20 2a 20  rs: k.inaba.. * 
0020: 4c 69 63 65 6e 73 65 3a 20 4e 59 53 4c 20 30 2e  License: NYSL 0.
0030: 39 39 38 32 20 68 74 74 70 3a 2f 2f 77 77 77 2e  9982 http://www.
0040: 6b 6d 6f 6e 6f 73 2e 6e 65 74 2f 6e 79 73 6c 2f  kmonos.net/nysl/
0050: 0d 0a 20 2a 0d 0a 20 2a 20 52 75 6e 74 69 6d 65  .. *.. * Runtime
0060: 20 64 61 74 61 20 73 74 72 75 63 74 75 72 65 73   data structures
0070: 20 66 6f 72 20 50 6f 6c 65 6d 79 20 70 72 6f 67   for Polemy prog
0080: 72 61 6d 6d 69 6e 67 20 6c 61 6e 67 75 61 67 65  ramming language
0090: 2e 0d 0a 20 2a 2f 0d 0a 6d 6f 64 75 6c 65 20 70  ... */..module p
00a0: 6f 6c 65 6d 79 2e 72 75 6e 74 69 6d 65 3b 0d 0a  olemy.runtime;..
00b0: 69 6d 70 6f 72 74 20 70 6f 6c 65 6d 79 2e 5f 63  import polemy._c
00c0: 6f 6d 6d 6f 6e 3b 0d 0a 69 6d 70 6f 72 74 20 70  ommon;..import p
00d0: 6f 6c 65 6d 79 2e 6c 65 78 20 3a 20 4c 65 78 50  olemy.lex : LexP
00e0: 6f 73 69 74 69 6f 6e 3b 0d 0a 69 6d 70 6f 72 74  osition;..import
00f0: 20 73 74 64 2e 73 74 64 69 6f 3b 0d 0a 0d 0a 63   std.stdio;....c
0100: 6c 61 73 73 20 50 6f 6c 65 6d 79 52 75 6e 74 69  lass PolemyRunti
0110: 6d 65 45 78 63 65 70 74 69 6f 6e 20 3a 20 45 78  meException : Ex
0120: 63 65 70 74 69 6f 6e 0d 0a 7b 0d 0a 09 74 68 69  ception..{...thi
0130: 73 28 73 74 72 69 6e 67 20 6d 73 67 29 20 7b 20  s(string msg) { 
0140: 73 75 70 65 72 28 6d 73 67 29 3b 20 7d 0d 0a 7d  super(msg); }..}
0150: 0d 0a 0d 0a 61 62 73 74 72 61 63 74 20 63 6c 61  ....abstract cla
0160: 73 73 20 56 61 6c 75 65 0d 0a 7b 0d 0a 7d 0d 0a  ss Value..{..}..
0170: 0d 0a 63 6c 61 73 73 20 55 6e 64 65 66 69 6e 65  ..class Undefine
0180: 64 56 61 6c 75 65 20 3a 20 56 61 6c 75 65 0d 0a  dValue : Value..
0190: 7b 0d 0a 09 6d 69 78 69 6e 20 53 69 6d 70 6c 65  {...mixin Simple
01a0: 43 6f 6e 73 74 72 75 63 74 6f 72 3b 0d 0a 09 6d  Constructor;...m
01b0: 69 78 69 6e 20 53 69 6d 70 6c 65 43 6f 6d 70 61  ixin SimpleCompa
01c0: 72 65 3b 0d 0a 09 6f 76 65 72 72 69 64 65 20 73  re;...override s
01d0: 74 72 69 6e 67 20 74 6f 53 74 72 69 6e 67 28 29  tring toString()
01e0: 20 63 6f 6e 73 74 20 7b 20 72 65 74 75 72 6e 20   const { return 
01f0: 22 28 75 6e 64 65 66 69 6e 65 64 29 22 3b 20 7d  "(undefined)"; }
0200: 0d 0a 7d 0d 0a 0d 0a 63 6c 61 73 73 20 49 6e 74  ..}....class Int
0210: 56 61 6c 75 65 20 3a 20 56 61 6c 75 65 0d 0a 7b  Value : Value..{
0220: 0d 0a 09 42 69 67 49 6e 74 20 64 61 74 61 3b 0d  ...BigInt data;.
0230: 0a 09 6d 69 78 69 6e 20 53 69 6d 70 6c 65 43 6f  ..mixin SimpleCo
0240: 6e 73 74 72 75 63 74 6f 72 3b 0d 0a 09 6d 69 78  nstructor;...mix
0250: 69 6e 20 53 69 6d 70 6c 65 43 6f 6d 70 61 72 65  in SimpleCompare
0260: 3b 0d 0a 09 6f 76 65 72 72 69 64 65 20 73 74 72  ;...override str
0270: 69 6e 67 20 74 6f 53 74 72 69 6e 67 28 29 20 63  ing toString() c
0280: 6f 6e 73 74 20 7b 0d 0a 09 09 63 6f 6e 73 74 28  onst {....const(
0290: 63 68 61 72 29 5b 5d 20 63 73 3b 20 64 61 74 61  char)[] cs; data
02a0: 2e 74 6f 53 74 72 69 6e 67 28 28 63 6f 6e 73 74  .toString((const
02b0: 28 63 68 61 72 29 5b 5d 20 73 29 7b 63 73 3d 73  (char)[] s){cs=s
02c0: 3b 7d 2c 20 6e 75 6c 6c 29 3b 0d 0a 09 09 72 65  ;}, null);....re
02d0: 74 75 72 6e 20 74 6f 21 73 74 72 69 6e 67 28 63  turn to!string(c
02e0: 73 29 3b 0d 0a 09 7d 0d 0a 7d 0d 0a 0d 0a 63 6c  s);...}..}....cl
02f0: 61 73 73 20 53 74 72 56 61 6c 75 65 20 3a 20 56  ass StrValue : V
0300: 61 6c 75 65 0d 0a 7b 0d 0a 09 73 74 72 69 6e 67  alue..{...string
0310: 20 64 61 74 61 3b 0d 0a 09 6d 69 78 69 6e 20 53   data;...mixin S
0320: 69 6d 70 6c 65 43 6f 6e 73 74 72 75 63 74 6f 72  impleConstructor
0330: 3b 0d 0a 09 6d 69 78 69 6e 20 53 69 6d 70 6c 65  ;...mixin Simple
0340: 43 6f 6d 70 61 72 65 3b 0d 0a 09 6f 76 65 72 72  Compare;...overr
0350: 69 64 65 20 73 74 72 69 6e 67 20 74 6f 53 74 72  ide string toStr
0360: 69 6e 67 28 29 20 63 6f 6e 73 74 20 7b 20 72 65  ing() const { re
0370: 74 75 72 6e 20 64 61 74 61 3b 20 7d 0d 0a 7d 0d  turn data; }..}.
0380: 0a 0d 0a 63 6c 61 73 73 20 46 75 6e 56 61 6c 75  ...class FunValu
0390: 65 20 3a 20 56 61 6c 75 65 0d 0a 7b 0d 0a 09 56  e : Value..{...V
03a0: 61 6c 75 65 20 64 65 6c 65 67 61 74 65 28 69 6d  alue delegate(im
03b0: 6d 75 74 61 62 6c 65 20 4c 65 78 50 6f 73 69 74  mutable LexPosit
03c0: 69 6f 6e 20 70 6f 73 2c 20 56 61 6c 75 65 5b 5d  ion pos, Value[]
03d0: 29 20 64 61 74 61 3b 0d 0a 09 6d 69 78 69 6e 20  ) data;...mixin 
03e0: 53 69 6d 70 6c 65 43 6f 6e 73 74 72 75 63 74 6f  SimpleConstructo
03f0: 72 3b 0d 0a 09 56 61 6c 75 65 20 63 61 6c 6c 28  r;...Value call(
0400: 69 6d 6d 75 74 61 62 6c 65 20 4c 65 78 50 6f 73  immutable LexPos
0410: 69 74 69 6f 6e 20 70 6f 73 2c 20 56 61 6c 75 65  ition pos, Value
0420: 5b 5d 20 61 72 67 73 29 20 7b 20 72 65 74 75 72  [] args) { retur
0430: 6e 20 64 61 74 61 28 70 6f 73 2c 61 72 67 73 29  n data(pos,args)
0440: 3b 20 7d 0d 0a 09 6f 76 65 72 72 69 64 65 20 73  ; }...override s
0450: 74 72 69 6e 67 20 74 6f 53 74 72 69 6e 67 28 29  tring toString()
0460: 20 63 6f 6e 73 74 20 7b 20 72 65 74 75 72 6e 20   const { return 
0470: 73 70 72 69 6e 74 66 21 22 28 66 75 6e 63 74 69  sprintf!"(functi
0480: 6f 6e 3a 25 73 3a 25 73 29 22 28 64 61 74 61 2e  on:%s:%s)"(data.
0490: 70 74 72 2c 64 61 74 61 2e 66 75 6e 63 70 74 72  ptr,data.funcptr
04a0: 29 3b 20 7d 0d 0a 7d 0d 0a 69 6d 70 6f 72 74 20  ); }..}..import 
04b0: 73 74 64 2e 73 74 64 69 6f 3b 0d 0a 63 6c 61 73  std.stdio;..clas
04c0: 73 20 43 6f 6e 74 65 78 74 0d 0a 7b 0d 0a 09 43  s Context..{...C
04d0: 6f 6e 74 65 78 74 20 20 20 20 20 20 20 70 61 72  ontext       par
04e0: 65 6e 74 3b 0d 0a 09 56 61 6c 75 65 5b 73 74 72  ent;...Value[str
04f0: 69 6e 67 5d 20 74 61 62 6c 65 3b 0d 0a 09 74 68  ing] table;...th
0500: 69 73 28 43 6f 6e 74 65 78 74 20 70 61 72 65 6e  is(Context paren
0510: 74 20 3d 20 6e 75 6c 6c 29 20 7b 20 74 68 69 73  t = null) { this
0520: 2e 70 61 72 65 6e 74 20 3d 20 70 61 72 65 6e 74  .parent = parent
0530: 3b 20 7d 0d 0a 0d 0a 09 76 6f 69 64 20 61 64 64  ; }.....void add
0540: 28 73 74 72 69 6e 67 20 69 2c 20 56 61 6c 75 65  (string i, Value
0550: 20 76 29 0d 0a 09 7b 0d 0a 09 09 74 61 62 6c 65   v)...{....table
0560: 5b 69 5d 20 3d 20 76 3b 0d 0a 09 7d 0d 0a 09 0d  [i] = v;...}....
0570: 0a 09 56 61 6c 75 65 20 6f 70 49 6e 64 65 78 28  ..Value opIndex(
0580: 73 74 72 69 6e 67 20 69 29 0d 0a 09 7b 0d 0a 09  string i)...{...
0590: 09 69 66 28 20 69 20 69 6e 20 74 61 62 6c 65 20  .if( i in table 
05a0: 29 0d 0a 09 09 09 72 65 74 75 72 6e 20 74 61 62  ).....return tab
05b0: 6c 65 5b 69 5d 3b 0d 0a 09 09 69 66 28 20 70 61  le[i];....if( pa
05c0: 72 65 6e 74 20 69 73 20 6e 75 6c 6c 20 29 0d 0a  rent is null )..
05d0: 09 09 09 74 68 72 6f 77 20 6e 65 77 20 50 6f 6c  ...throw new Pol
05e0: 65 6d 79 52 75 6e 74 69 6d 65 45 78 63 65 70 74  emyRuntimeExcept
05f0: 69 6f 6e 28 73 70 72 69 6e 74 66 21 22 76 61 72  ion(sprintf!"var
0600: 69 61 62 6c 65 20 25 73 20 6e 6f 74 20 66 6f 75  iable %s not fou
0610: 6e 64 22 28 69 29 29 3b 0d 0a 09 09 72 65 74 75  nd"(i));....retu
0620: 72 6e 20 70 61 72 65 6e 74 5b 69 5d 3b 0d 0a 09  rn parent[i];...
0630: 7d 0d 0a 0d 0a 09 76 6f 69 64 20 6f 70 49 6e 64  }.....void opInd
0640: 65 78 41 73 73 69 67 6e 28 56 61 6c 75 65 20 76  exAssign(Value v
0650: 2c 20 73 74 72 69 6e 67 20 69 29 0d 0a 09 7b 0d  , string i)...{.
0660: 0a 09 09 69 66 28 20 69 20 69 6e 20 74 61 62 6c  ...if( i in tabl
0670: 65 20 29 0d 0a 09 09 09 72 65 74 75 72 6e 20 74  e ).....return t
0680: 61 62 6c 65 5b 69 5d 20 3d 20 76 3b 0d 0a 09 09  able[i] = v;....
0690: 69 66 28 20 70 61 72 65 6e 74 20 69 73 20 6e 75  if( parent is nu
06a0: 6c 6c 20 29 0d 0a 09 09 09 74 68 72 6f 77 20 6e  ll ).....throw n
06b0: 65 77 20 50 6f 6c 65 6d 79 52 75 6e 74 69 6d 65  ew PolemyRuntime
06c0: 45 78 63 65 70 74 69 6f 6e 28 73 70 72 69 6e 74  Exception(sprint
06d0: 66 21 22 76 61 72 69 61 62 6c 65 20 25 73 20 6e  f!"variable %s n
06e0: 6f 74 20 66 6f 75 6e 64 22 28 69 29 29 3b 0d 0a  ot found"(i));..
06f0: 09 09 72 65 74 75 72 6e 20 70 61 72 65 6e 74 5b  ..return parent[
0700: 69 5d 20 3d 20 76 3b 0d 0a 09 7d 0d 0a 7d 0d 0a  i] = v;...}..}..