Artifact 98d6a7a3304f704349de8bacefe88e76ea6922f7
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 init; ///
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 /// List of AST Types
98
99 alias TypeTuple!(Int,Str,Var,Lay,Let,App,Fun) ListOfASTTypes;
100
101 /// Handy Generator for AST nodes. To use this, mixin EasyAst;
102
103 /*mixin*/
104 template EasyAST()
105 {
106 ///
107 template genEast(T)
108 { T genEast(P...)(P ps) { return new T(LexPosition.dummy, ps); } }
109
110 alias genEast!Str strl; ///
111 alias genEast!Int intl; ///
112 auto fun(string[] xs, AST ps) {
113 return genEast!Fun(array(map!((string x){return new Parameter(x,[]);})(xs)),ps); }
114 auto funp(Parameter[] xs, AST ps) { return genEast!Fun(xs,ps); } ///
115 alias genEast!Var var; ///
116 alias genEast!Lay lay; ///
117 alias genEast!Let let; ///
118 alias genEast!App call; ///
119 auto param(string name, string[] lay...) { return new Parameter(name, lay); } ///
120 }