Artifact 55043f61f436485acee029c5fc311b61b366ea16
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 }
19
20 /// AST node for integer literal
21 class Int : AST
22 {
23 BigInt data; ///
24
25 mixin SimpleClass;
26 this(LexPosition pos, int n) {super(pos); data = n;}
27 this(LexPosition pos, long n) {super(pos); data = n;}
28 this(LexPosition pos, BigInt n) {super(pos); data = n;}
29 this(LexPosition pos, string n) {super(pos); data = BigInt(n);}
30 }
31
32 /// AST node for string literal
33 class Str : AST
34 {
35 string data; ///
36
37 mixin SimpleClass;
38 }
39
40 /// AST node for variable reference
41 class Var : AST
42 {
43 string name; ///
44
45 mixin SimpleClass;
46 }
47
48 /// AST node for @layered(expression)
49 class Lay : AST
50 {
51 Layer layer; ///
52 AST expr; ///
53
54 mixin SimpleClass;
55 }
56
57 /// AST node for variable declaration
58 class Let : AST
59 {
60 string name; ///
61 Layer layer; ///
62 AST init; ///
63 AST expr; ///
64
65 mixin SimpleClass;
66 }
67
68 /// AST node for function application
69 class App : AST
70 {
71 AST fun; ///
72 AST[] args; ///
73
74 mixin SimpleClass;
75 this(LexPosition pos, AST fun, AST[] args...) { super(pos); this.fun=fun; this.args=args.dup; }
76 }
77
78 ///
79 class Parameter
80 {
81 string name; ///
82 Layer[] layers; ///
83
84 mixin SimpleClass;
85 }
86
87 /// AST node for function literal
88 class Fun : AST
89 {
90 Parameter[] params; ///
91 AST funbody; ///
92
93 mixin SimpleClass;
94 }
95
96 /// List of AST Types
97
98 alias TypeTuple!(Int,Str,Var,Lay,Let,App,Fun) ListOfASTTypes;
99
100 /// Handy Generator for AST nodes. To use this, mixin EasyAst;
101
102 /*mixin*/
103 template EasyAST()
104 {
105 ///
106 template genEast(T)
107 { T genEast(P...)(P ps) { return new T(LexPosition.dummy, ps); } }
108
109 alias genEast!Str strl; ///
110 alias genEast!Int intl; ///
111 auto fun(string[] xs, AST ps) {
112 return genEast!Fun(array(map!((string x){return new Parameter(x,[]);})(xs)),ps); }
113 auto funp(Parameter[] xs, AST ps) { return genEast!Fun(xs,ps); } ///
114 alias genEast!Var var; ///
115 alias genEast!Lay lay; ///
116 alias genEast!Let let; ///
117 alias genEast!App call; ///
118 auto param(string name, string[] lay...) { return new Parameter(name, lay); } ///
119 }