Differences From Artifact [f03af1208bc8c434]:
- File
polemy/ast.d
- 2010-11-08 12:26:39 - part of checkin [80ff567c75] on branch trunk - Testing easyAST. (user: kinaba) [annotate]
To Artifact [4e37845b6ce5021a]:
- File
polemy/ast.d
- 2010-11-08 14:59:30 - part of checkin [b985f3bf91] on branch trunk - refactored parser to change AST to ML-like one. (user: kinaba) [annotate]
4 * 4 *
5 * Syntax tree for Polemy programming language. 5 * Syntax tree for Polemy programming language.
6 */ 6 */
7 module polemy.ast; 7 module polemy.ast;
8 import polemy._common; 8 import polemy._common;
9 import polemy.lex; 9 import polemy.lex;
10 10
11 alias Statement[] Program; <
12 <
13 abstract class Statement | 11 abstract class AST
14 { 12 {
15 immutable LexPosition pos; 13 immutable LexPosition pos;
16 mixin SimpleConstructor; 14 mixin SimpleConstructor;
17 } 15 }
18 16
19 class DeclStatement : Statement <
20 { <
21 string var; <
22 Expression expr; <
23 mixin SimpleClass; <
24 } <
25 <
26 class ExprStatement : Statement <
27 { <
28 Expression expr; <
29 mixin SimpleClass; <
30 } <
31 <
32 abstract class Expression <
33 { <
34 immutable LexPosition pos; <
35 mixin SimpleConstructor; <
36 } <
37 <
38 class StrLiteralExpression : Expression | 17 class StrLiteral : AST
39 { 18 {
40 string data; 19 string data;
41 mixin SimpleClass; 20 mixin SimpleClass;
42 } 21 }
43 22
44 class IntLiteralExpression : Expression | 23 class IntLiteral : AST
45 { 24 {
46 BigInt data; 25 BigInt data;
47 mixin SimpleClass; 26 mixin SimpleClass;
> 27 this(immutable LexPosition pos, long n) {super(pos); data = n;}
> 28 this(immutable LexPosition pos, BigInt n) {super(pos); data = n;}
> 29 this(immutable LexPosition pos, string n) {super(pos); data = BigInt(n);
48 } 30 }
49 31
50 class VarExpression : Expression | 32 class VarExpression : AST
51 { 33 {
52 string var; 34 string var;
53 mixin SimpleClass; 35 mixin SimpleClass;
54 } 36 }
55 37
56 class AssignExpression : Expression | 38 class LetExpression : AST
57 { 39 {
58 Expression lhs; | 40 string var;
59 Expression rhs; | 41 AST init;
> 42 AST expr;
60 mixin SimpleClass; 43 mixin SimpleClass;
61 } 44 }
62 45
63 class FuncallExpression : Expression | 46 class FuncallExpression : AST
64 { 47 {
65 Expression fun; | 48 AST fun;
66 Expression[] args; | 49 AST[] args;
67 this(immutable LexPosition pos, Expression fun, Expression[] args...) | 50 this(immutable LexPosition pos, AST fun, AST[] args...)
68 { super(pos); this.fun=fun; this.args=args.dup; } 51 { super(pos); this.fun=fun; this.args=args.dup; }
69 mixin SimpleClass; 52 mixin SimpleClass;
70 } 53 }
71 54
72 class FunLiteralExpression : Expression | 55 class FunLiteral : AST
73 { 56 {
74 string[] params; 57 string[] params;
75 Program funbody; | 58 AST funbody;
76 mixin SimpleClass; 59 mixin SimpleClass;
77 } 60 }
78 61
79 /// Handy Generator for AST nodes. To use this, mixin EasyAst; 62 /// Handy Generator for AST nodes. To use this, mixin EasyAst;
80 63
81 /*mixin*/ 64 /*mixin*/
82 template EasyAst() | 65 template EasyAST()
83 { 66 {
84 template genEast(T) 67 template genEast(T)
85 { T genEast(P...)(P ps) { return new T(LexPosition.dummy, ps); } 68 { T genEast(P...)(P ps) { return new T(LexPosition.dummy, ps); }
86 69
87 alias genEast!DeclStatement decl; | 70 alias genEast!StrLiteral strl;
88 alias genEast!ExprStatement expr; | 71 alias genEast!IntLiteral intl;
> 72 auto fun(string[] xs, AST ps) { return genEast!FunLiteral(xs,ps); }
89 alias genEast!VarExpression var; 73 alias genEast!VarExpression var;
> 74 alias genEast!LetExpression let;
90 alias genEast!FuncallExpression funcall; | 75 alias genEast!FuncallExpression call;
91 alias genEast!IntLiteralExpression intl; <
92 alias genEast!StrLiteralExpression strl; <
93 alias genEast!FunLiteralExpression fun; <
94 } 76 }