1
+ use std:: cell:: RefCell ;
1
2
use std:: fmt:: { Debug , Formatter } ;
3
+ use std:: path:: PathBuf ;
4
+ use std:: rc:: { Rc , Weak } ;
2
5
use crate :: compiler:: lexer:: Token ;
3
6
4
- #[ derive( Clone ) ]
7
+ #[ derive( Clone , PartialEq ) ]
5
8
pub enum Stmt {
6
9
Template {
7
10
name : Token ,
@@ -14,6 +17,18 @@ pub enum Stmt {
14
17
expr : Expr ,
15
18
} ,
16
19
20
+ Module {
21
+ name : Token ,
22
+ statements : Vec < Stmt > ,
23
+ } ,
24
+ Include {
25
+ path : Token ,
26
+ } ,
27
+ Import {
28
+ path : Vec < Token > ,
29
+ selector : Option < Vec < Token > > ,
30
+ } ,
31
+
17
32
Error ,
18
33
}
19
34
@@ -28,12 +43,21 @@ impl Debug for Stmt {
28
43
Stmt :: Function { name, expr } =>
29
44
write ! ( f, "function {} = {:?};" , name. source( ) , expr) ,
30
45
46
+ Stmt :: Module { name, statements } =>
47
+ write ! ( f, "module {} {{ {} }}" , name. source( ) , statements. iter( ) . map( |stmt| format!( "{:?}" , stmt) )
48
+ . collect:: <Vec <String >>( ) . join( " " ) ) ,
49
+ Stmt :: Include { path } => write ! ( f, "include \" {}\" ;" , path. source( ) ) ,
50
+ Stmt :: Import { path, selector } => write ! ( f, "{}.{}" ,
51
+ path. iter( ) . map( |name| name. source( ) . to_owned( ) ) . collect:: <Vec <String >>( ) . join( "." ) ,
52
+ selector. as_ref( ) . map( |names| names. iter( ) . map( |name| name. source( ) . to_owned( ) ) . collect:: <Vec <String >>( ) . join( ", " ) )
53
+ . map( |names| format!( "{{{}}}" , names) ) . unwrap_or_else( || String :: from( "*" ) ) ) ,
54
+
31
55
Stmt :: Error => write ! ( f, "Error;" )
32
56
}
33
57
}
34
58
}
35
59
36
- #[ derive( Clone ) ]
60
+ #[ derive( Clone , PartialEq ) ]
37
61
pub enum Expr {
38
62
ConstantFloat ( f32 ) ,
39
63
ConstantInt ( i32 ) ,
@@ -103,6 +127,29 @@ impl Debug for Expr {
103
127
}
104
128
}
105
129
130
+ #[ derive( Debug ) ]
131
+ pub struct OutputFunction {
132
+ pub name : String ,
133
+ pub path : PathBuf ,
134
+ pub json : JsonElement ,
135
+ }
136
+
137
+ #[ derive( Debug ) ]
138
+ pub struct Template {
139
+ pub name : String , // Can be identifiers or operator names (like `+`)
140
+ pub args : Vec < String > ,
141
+ pub expr : Expr ,
142
+ pub current_modules : Vec < Weak < RefCell < Module > > > ,
143
+ }
144
+
145
+ #[ derive( Debug ) ]
146
+ pub struct Module {
147
+ pub name : String ,
148
+ pub sub_modules : Vec < Rc < RefCell < Module > > > ,
149
+ pub templates : Vec < Rc < RefCell < Template > > > ,
150
+ pub output_functions : Vec < Rc < RefCell < OutputFunction > > > ,
151
+ }
152
+
106
153
#[ derive( Clone ) ]
107
154
pub enum JsonElement {
108
155
ConstantFloat ( f32 ) ,
@@ -112,6 +159,9 @@ pub enum JsonElement {
112
159
Object ( Vec < ( String , JsonElement ) > ) ,
113
160
Array ( Vec < JsonElement > ) ,
114
161
162
+ Module ( Rc < RefCell < Module > > ) ,
163
+ Template ( Rc < RefCell < Template > > ) ,
164
+
115
165
Error ,
116
166
}
117
167
@@ -127,6 +177,9 @@ impl Debug for JsonElement {
127
177
JsonElement :: Array ( elements) => write ! ( f, "[{}]" , elements. iter( )
128
178
. map( |element| format!( "{:?}" , element) ) . collect:: <Vec <String >>( )
129
179
. join( ", " ) ) ,
180
+
181
+ JsonElement :: Module ( module) => write ! ( f, "<module {}>" , & module. borrow( ) . name) ,
182
+ JsonElement :: Template ( template) => write ! ( f, "<template {}>" , & template. borrow( ) . name) ,
130
183
JsonElement :: Error => write ! ( f, "Error" ) ,
131
184
}
132
185
}
0 commit comments