Artifact b8324439b8512940d0ecba64c1dd40f491fc3f6b
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.lex;
10
11 ///
12 abstract class AST
13 {
14 immutable LexPosition pos;
15 mixin SimpleConstructor;
16 }
17
18 ///
19 class StrLiteral : AST
20 {
21 string data;
22 mixin SimpleClass;
23 }
24
25 ///
26 class IntLiteral : AST
27 {
28 BigInt data;
29 mixin SimpleClass;
30 this(immutable LexPosition pos, long n) {super(pos); data = n;}
31 this(immutable LexPosition pos, BigInt n) {super(pos); data = n;}
32 this(immutable LexPosition pos, string n) {super(pos); data = BigInt(n);}
33 }
34
35 ///
36 class VarExpression : AST
37 {
38 string var;
39 mixin SimpleClass;
40 }
41
42 ///
43 class LayeredExpression : AST
44 {
45 string lay;
46 AST expr;
47 mixin SimpleClass;
48 }
49
50 ///
51 class LetExpression : AST
52 {
53 string var;
54 string layer;
55 AST init;
56 AST expr;
57 mixin SimpleClass;
58 }
59
60 ///
61 class FuncallExpression : AST
62 {
63 AST fun;
64 AST[] args;
65 this(immutable LexPosition pos, AST fun, AST[] args...)
66 { super(pos); this.fun=fun; this.args=args.dup; }
67 mixin SimpleClass;
68 }
69
70 ///
71 class FunLiteral : AST
72 {
73 string[] params;
74 AST funbody;
75 mixin SimpleClass;
76 }
77
78 /// Handy Generator for AST nodes. To use this, mixin EasyAst;
79
80 /*mixin*/
81 template EasyAST()
82 {
83 ///
84 template genEast(T)
85 { T genEast(P...)(P ps) { return new T(LexPosition.dummy, ps); } }
86
87 alias genEast!StrLiteral strl; ///
88 alias genEast!IntLiteral intl; ///
89 auto fun(string[] xs, AST ps) { return genEast!FunLiteral(xs,ps); } ///
90 alias genEast!VarExpression var; ///
91 alias genEast!LayeredExpression lay; ///
92 alias genEast!LetExpression let; ///
93 alias genEast!FuncallExpression call; ///
94 }