Hex Artifact Content
Not logged in

Artifact 239622603e376c1100930f9cc2c912b788b7caff:


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 53 79 6e 74 61 78 20 74 72 65 65 20 66 6f  * Syntax tree fo
0060: 72 20 50 6f 6c 65 6d 79 20 70 72 6f 67 72 61 6d  r Polemy program
0070: 6d 69 6e 67 20 6c 61 6e 67 75 61 67 65 2e 0a 20  ming language.. 
0080: 2a 2f 0a 6d 6f 64 75 6c 65 20 70 6f 6c 65 6d 79  */.module polemy
0090: 2e 61 73 74 3b 0a 69 6d 70 6f 72 74 20 70 6f 6c  .ast;.import pol
00a0: 65 6d 79 2e 5f 63 6f 6d 6d 6f 6e 3b 0a 69 6d 70  emy._common;.imp
00b0: 6f 72 74 20 70 6f 6c 65 6d 79 2e 6c 65 78 3b 0d  ort polemy.lex;.
00c0: 0a 0d 0a 2f 2f 2f 0d 0a 61 62 73 74 72 61 63 74  ...///..abstract
00d0: 20 63 6c 61 73 73 20 41 53 54 0a 7b 0a 09 69 6d   class AST.{..im
00e0: 6d 75 74 61 62 6c 65 20 4c 65 78 50 6f 73 69 74  mutable LexPosit
00f0: 69 6f 6e 20 70 6f 73 3b 0a 09 6d 69 78 69 6e 20  ion pos;..mixin 
0100: 53 69 6d 70 6c 65 43 6f 6e 73 74 72 75 63 74 6f  SimpleConstructo
0110: 72 3b 0a 7d 0a 0a 2f 2f 2f 0d 0a 63 6c 61 73 73  r;.}..///..class
0120: 20 53 74 72 4c 69 74 65 72 61 6c 20 3a 20 41 53   StrLiteral : AS
0130: 54 0a 7b 0a 09 73 74 72 69 6e 67 20 64 61 74 61  T.{..string data
0140: 3b 0a 09 6d 69 78 69 6e 20 53 69 6d 70 6c 65 43  ;..mixin SimpleC
0150: 6c 61 73 73 3b 0d 0a 7d 0a 0a 2f 2f 2f 0d 0a 63  lass;..}..///..c
0160: 6c 61 73 73 20 49 6e 74 4c 69 74 65 72 61 6c 20  lass IntLiteral 
0170: 3a 20 41 53 54 0a 7b 0a 09 42 69 67 49 6e 74 20  : AST.{..BigInt 
0180: 64 61 74 61 3b 0d 0a 09 6d 69 78 69 6e 20 53 69  data;...mixin Si
0190: 6d 70 6c 65 43 6c 61 73 73 3b 0d 0a 09 74 68 69  mpleClass;...thi
01a0: 73 28 69 6d 6d 75 74 61 62 6c 65 20 4c 65 78 50  s(immutable LexP
01b0: 6f 73 69 74 69 6f 6e 20 70 6f 73 2c 20 6c 6f 6e  osition pos, lon
01c0: 67 20 6e 29 20 7b 73 75 70 65 72 28 70 6f 73 29  g n) {super(pos)
01d0: 3b 20 64 61 74 61 20 3d 20 6e 3b 7d 0d 0a 09 74  ; data = n;}...t
01e0: 68 69 73 28 69 6d 6d 75 74 61 62 6c 65 20 4c 65  his(immutable Le
01f0: 78 50 6f 73 69 74 69 6f 6e 20 70 6f 73 2c 20 42  xPosition pos, B
0200: 69 67 49 6e 74 20 6e 29 20 7b 73 75 70 65 72 28  igInt n) {super(
0210: 70 6f 73 29 3b 20 64 61 74 61 20 3d 20 6e 3b 7d  pos); data = n;}
0220: 0d 0a 09 74 68 69 73 28 69 6d 6d 75 74 61 62 6c  ...this(immutabl
0230: 65 20 4c 65 78 50 6f 73 69 74 69 6f 6e 20 70 6f  e LexPosition po
0240: 73 2c 20 73 74 72 69 6e 67 20 6e 29 20 7b 73 75  s, string n) {su
0250: 70 65 72 28 70 6f 73 29 3b 20 64 61 74 61 20 3d  per(pos); data =
0260: 20 42 69 67 49 6e 74 28 6e 29 3b 7d 0d 0a 7d 0a   BigInt(n);}..}.
0270: 0a 2f 2f 2f 0d 0a 63 6c 61 73 73 20 56 61 72 45  .///..class VarE
0280: 78 70 72 65 73 73 69 6f 6e 20 3a 20 41 53 54 0d  xpression : AST.
0290: 0a 7b 0d 0a 09 73 74 72 69 6e 67 20 76 61 72 3b  .{...string var;
02a0: 0d 0a 09 6d 69 78 69 6e 20 53 69 6d 70 6c 65 43  ...mixin SimpleC
02b0: 6c 61 73 73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2f 2f 0d  lass;..}....///.
02c0: 0a 63 6c 61 73 73 20 4c 61 79 65 72 65 64 45 78  .class LayeredEx
02d0: 70 72 65 73 73 69 6f 6e 20 3a 20 41 53 54 0d 0a  pression : AST..
02e0: 7b 0d 0a 09 73 74 72 69 6e 67 20 6c 61 79 3b 0d  {...string lay;.
02f0: 0a 09 41 53 54 20 20 20 20 65 78 70 72 3b 0d 0a  ..AST    expr;..
0300: 09 6d 69 78 69 6e 20 53 69 6d 70 6c 65 43 6c 61  .mixin SimpleCla
0310: 73 73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2f 2f 0d 0a 63  ss;..}....///..c
0320: 6c 61 73 73 20 4c 65 74 45 78 70 72 65 73 73 69  lass LetExpressi
0330: 6f 6e 20 3a 20 41 53 54 0d 0a 7b 0d 0a 09 73 74  on : AST..{...st
0340: 72 69 6e 67 20 76 61 72 3b 0d 0a 09 73 74 72 69  ring var;...stri
0350: 6e 67 20 6c 61 79 65 72 3b 0d 0a 09 41 53 54 20  ng layer;...AST 
0360: 20 20 20 69 6e 69 74 3b 0d 0a 09 41 53 54 20 20     init;...AST  
0370: 20 20 65 78 70 72 3b 0d 0a 09 6d 69 78 69 6e 20    expr;...mixin 
0380: 53 69 6d 70 6c 65 43 6c 61 73 73 3b 0d 0a 7d 0d  SimpleClass;..}.
0390: 0a 0d 0a 2f 2f 2f 0d 0a 63 6c 61 73 73 20 46 75  ...///..class Fu
03a0: 6e 63 61 6c 6c 45 78 70 72 65 73 73 69 6f 6e 20  ncallExpression 
03b0: 3a 20 41 53 54 0a 7b 0a 09 41 53 54 20 20 20 66  : AST.{..AST   f
03c0: 75 6e 3b 0a 09 41 53 54 5b 5d 20 61 72 67 73 3b  un;..AST[] args;
03d0: 0a 09 74 68 69 73 28 69 6d 6d 75 74 61 62 6c 65  ..this(immutable
03e0: 20 4c 65 78 50 6f 73 69 74 69 6f 6e 20 70 6f 73   LexPosition pos
03f0: 2c 20 41 53 54 20 66 75 6e 2c 20 41 53 54 5b 5d  , AST fun, AST[]
0400: 20 61 72 67 73 2e 2e 2e 29 0a 09 09 7b 20 73 75   args...)...{ su
0410: 70 65 72 28 70 6f 73 29 3b 20 74 68 69 73 2e 66  per(pos); this.f
0420: 75 6e 3d 66 75 6e 3b 20 74 68 69 73 2e 61 72 67  un=fun; this.arg
0430: 73 3d 61 72 67 73 2e 64 75 70 3b 20 7d 0a 09 6d  s=args.dup; }..m
0440: 69 78 69 6e 20 53 69 6d 70 6c 65 43 6c 61 73 73  ixin SimpleClass
0450: 3b 0d 0a 7d 0a 0d 0a 2f 2f 2f 0d 0a 63 6c 61 73  ;..}...///..clas
0460: 73 20 50 61 72 61 6d 65 74 65 72 0d 0a 7b 0d 0a  s Parameter..{..
0470: 09 73 74 72 69 6e 67 20 20 20 6e 61 6d 65 3b 0d  .string   name;.
0480: 0a 09 73 74 72 69 6e 67 5b 5d 20 6c 61 79 65 72  ..string[] layer
0490: 73 3b 0d 0a 09 6d 69 78 69 6e 20 53 69 6d 70 6c  s;...mixin Simpl
04a0: 65 43 6c 61 73 73 3b 0d 0a 7d 0d 0a 0d 0a 2f 2f  eClass;..}....//
04b0: 2f 0d 0a 63 6c 61 73 73 20 46 75 6e 4c 69 74 65  /..class FunLite
04c0: 72 61 6c 20 3a 20 41 53 54 0d 0a 7b 0d 0a 09 50  ral : AST..{...P
04d0: 61 72 61 6d 65 74 65 72 5b 5d 20 70 61 72 61 6d  arameter[] param
04e0: 73 3b 0d 0a 09 41 53 54 20 20 20 20 20 20 20 20  s;...AST        
04f0: 20 66 75 6e 62 6f 64 79 3b 0d 0a 09 6d 69 78 69   funbody;...mixi
0500: 6e 20 53 69 6d 70 6c 65 43 6c 61 73 73 3b 0d 0a  n SimpleClass;..
0510: 7d 0d 0a 0d 0a 2f 2f 2f 20 48 61 6e 64 79 20 47  }..../// Handy G
0520: 65 6e 65 72 61 74 6f 72 20 66 6f 72 20 41 53 54  enerator for AST
0530: 20 6e 6f 64 65 73 2e 20 54 6f 20 75 73 65 20 74   nodes. To use t
0540: 68 69 73 2c 20 6d 69 78 69 6e 20 45 61 73 79 41  his, mixin EasyA
0550: 73 74 3b 0d 0a 0d 0a 2f 2a 6d 69 78 69 6e 2a 2f  st;..../*mixin*/
0560: 0d 0a 74 65 6d 70 6c 61 74 65 20 45 61 73 79 41  ..template EasyA
0570: 53 54 28 29 0d 0a 7b 0d 0a 09 2f 2f 2f 0d 0a 09  ST()..{...///...
0580: 74 65 6d 70 6c 61 74 65 20 67 65 6e 45 61 73 74  template genEast
0590: 28 54 29 0d 0a 09 09 7b 20 54 20 67 65 6e 45 61  (T)....{ T genEa
05a0: 73 74 28 50 2e 2e 2e 29 28 50 20 70 73 29 20 7b  st(P...)(P ps) {
05b0: 20 72 65 74 75 72 6e 20 6e 65 77 20 54 28 4c 65   return new T(Le
05c0: 78 50 6f 73 69 74 69 6f 6e 2e 64 75 6d 6d 79 2c  xPosition.dummy,
05d0: 20 70 73 29 3b 20 7d 20 7d 0d 0a 0d 0a 09 61 6c   ps); } }.....al
05e0: 69 61 73 20 67 65 6e 45 61 73 74 21 53 74 72 4c  ias genEast!StrL
05f0: 69 74 65 72 61 6c 20 73 74 72 6c 3b 20 2f 2f 2f  iteral strl; ///
0600: 0d 0a 09 61 6c 69 61 73 20 67 65 6e 45 61 73 74  ...alias genEast
0610: 21 49 6e 74 4c 69 74 65 72 61 6c 20 69 6e 74 6c  !IntLiteral intl
0620: 3b 20 2f 2f 2f 0d 0a 09 61 75 74 6f 20 66 75 6e  ; ///...auto fun
0630: 28 73 74 72 69 6e 67 5b 5d 20 78 73 2c 20 41 53  (string[] xs, AS
0640: 54 20 70 73 29 20 7b 0d 0a 09 09 72 65 74 75 72  T ps) {....retur
0650: 6e 20 67 65 6e 45 61 73 74 21 46 75 6e 4c 69 74  n genEast!FunLit
0660: 65 72 61 6c 28 61 72 72 61 79 28 6d 61 70 21 28  eral(array(map!(
0670: 28 73 74 72 69 6e 67 20 78 29 7b 72 65 74 75 72  (string x){retur
0680: 6e 20 6e 65 77 20 50 61 72 61 6d 65 74 65 72 28  n new Parameter(
0690: 78 2c 5b 5d 29 3b 7d 29 28 78 73 29 29 2c 70 73  x,[]);})(xs)),ps
06a0: 29 3b 20 7d 0d 0a 09 61 75 74 6f 20 66 75 6e 70  ); }...auto funp
06b0: 28 50 61 72 61 6d 65 74 65 72 5b 5d 20 78 73 2c  (Parameter[] xs,
06c0: 20 41 53 54 20 70 73 29 20 7b 20 72 65 74 75 72   AST ps) { retur
06d0: 6e 20 67 65 6e 45 61 73 74 21 46 75 6e 4c 69 74  n genEast!FunLit
06e0: 65 72 61 6c 28 78 73 2c 70 73 29 3b 20 7d 20 2f  eral(xs,ps); } /
06f0: 2f 2f 0d 0a 09 61 6c 69 61 73 20 67 65 6e 45 61  //...alias genEa
0700: 73 74 21 56 61 72 45 78 70 72 65 73 73 69 6f 6e  st!VarExpression
0710: 20 76 61 72 3b 20 2f 2f 2f 0d 0a 09 61 6c 69 61   var; ///...alia
0720: 73 20 67 65 6e 45 61 73 74 21 4c 61 79 65 72 65  s genEast!Layere
0730: 64 45 78 70 72 65 73 73 69 6f 6e 20 6c 61 79 3b  dExpression lay;
0740: 20 2f 2f 2f 0d 0a 09 61 6c 69 61 73 20 67 65 6e   ///...alias gen
0750: 45 61 73 74 21 4c 65 74 45 78 70 72 65 73 73 69  East!LetExpressi
0760: 6f 6e 20 6c 65 74 3b 20 2f 2f 2f 0d 0a 09 61 6c  on let; ///...al
0770: 69 61 73 20 67 65 6e 45 61 73 74 21 46 75 6e 63  ias genEast!Func
0780: 61 6c 6c 45 78 70 72 65 73 73 69 6f 6e 20 63 61  allExpression ca
0790: 6c 6c 3b 20 2f 2f 2f 0d 0a 09 61 75 74 6f 20 70  ll; ///...auto p
07a0: 61 72 61 6d 28 73 74 72 69 6e 67 20 6e 61 6d 65  aram(string name
07b0: 2c 20 73 74 72 69 6e 67 5b 5d 20 6c 61 79 2e 2e  , string[] lay..
07c0: 2e 29 20 7b 20 72 65 74 75 72 6e 20 6e 65 77 20  .) { return new 
07d0: 50 61 72 61 6d 65 74 65 72 28 6e 61 6d 65 2c 20  Parameter(name, 
07e0: 6c 61 79 29 3b 20 7d 20 2f 2f 2f 0d 0a 7d 0d 0a  lay); } ///..}..