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