Artifact 17cd075ee7ebe8fb8439134c7e6ea26ec0041b79
1 /**
2 * Authors: k.inaba
3 * License: NYSL 0.9982 http://www.kmonos.net/nysl/
4 *
5 * Syntax tree for Polemy programming language.
6 */
7 module polemy.ast;
8 import polemy._common;
9 import polemy.failure;
10 import polemy.layer;
11
12 ///
13 abstract class AST
14 {
15 LexPosition pos; ///
16
17 mixin SimpleConstructor;
18 invariant(){ assert(pos !is null);}
19 }
20
21 /// AST node for integer literal
22 class Int : AST
23 {
24 BigInt data; ///
25
26 mixin SimpleClass;
27 this(LexPosition pos, int n) {super(pos); data = n;}
28 this(LexPosition pos, long n) {super(pos); data = n;}
29 this(LexPosition pos, BigInt n) {super(pos); data = n;}
30 this(LexPosition pos, string n) {super(pos); data = BigInt(n);}
31 }
32
33 /// AST node for string literal
34 class Str : AST
35 {
36 string data; ///
37
38 mixin SimpleClass;
39 }
40
41 /// AST node for variable reference
42 class Var : AST
43 {
44 string name; ///
45
46 mixin SimpleClass;
47 }
48
49 /// AST node for @layered(expression)
50 class Lay : AST
51 {
52 Layer layer; ///
53 AST expr; ///
54
55 mixin SimpleClass;
56 }
57
58 /// AST node for variable declaration
59 class Let : AST
60 {
61 string name; ///
62 Layer layer; ///
63 AST vdef; ///
64 AST expr; ///
65
66 mixin SimpleClass;
67 }
68
69 /// AST node for function application
70 class App : AST
71 {
72 AST fun; ///
73 AST[] args; ///
74
75 mixin SimpleClass;
76 this(LexPosition pos, AST fun, AST[] args...) { super(pos); this.fun=fun; this.args=args.dup; }
77 }
78
79 ///
80 class Parameter
81 {
82 string name; ///
83 Layer[] layers; ///
84
85 mixin SimpleClass;
86 }
87
88 /// AST node for function literal
89 class Fun : AST
90 {
91 Parameter[] params; ///
92 AST funbody; ///
93
94 mixin SimpleClass;
95 }
96
97 /// AST node for deadend
98 class Die : AST
99 {
100 mixin SimpleClass;
101 }
102
103 /// List of AST Types
104
105 alias TypeTuple!(Int,Str,Var,Lay,Let,App,Fun,Die) ListOfASTTypes;
106
107 /// Handy Generator for AST nodes. To use this, mixin EasyAst;
108
109 /*mixin*/
110 template EasyAST()
111 {
112 ///
113 template genEast(T)
114 { T genEast(P...)(P ps) { return new T(LexPosition.dummy, ps); } }
115
116 alias genEast!Str strl; ///
117 alias genEast!Int intl; ///
118 auto fun(string[] xs, AST ps) {
119 return genEast!Fun(array(map!((string x){return new Parameter(x,[]);})(xs)),ps); }
120 auto funp(Parameter[] xs, AST ps) { return genEast!Fun(xs,ps); } ///
121 alias genEast!Var var; ///
122 alias genEast!Lay lay; ///
123 alias genEast!Let let; ///
124 alias genEast!App call; ///
125 auto param(string name, string[] lay...) { return new Parameter(name, lay); } ///
126 alias genEast!Die dieast; ///
127 }