1use serde_json;
2
3#[derive(Debug, Serialize, Deserialize, Clone)]
4pub struct AstModule {
5 pub shebang: Option<String>,
6 pub instructions: Vec<AstInstruction>,
7}
8
9impl AstModule {
10 pub fn from_json(source: &str) -> serde_json::Result<Self> {
11 serde_json::from_str(source)
12 }
13
14 pub fn to_json(&self, pretty: bool) -> serde_json::Result<String> {
15 if pretty {
16 serde_json::to_string_pretty(self)
17 } else {
18 serde_json::to_string(self)
19 }
20 }
21}
22
23#[derive(Debug, Serialize, Deserialize, Clone)]
24pub struct AstIdentifier(pub String);
25
26impl Default for AstIdentifier {
27 fn default() -> Self {
28 AstIdentifier("".to_owned())
29 }
30}
31
32#[derive(Debug, Serialize, Deserialize, Clone)]
33pub struct AstString(pub String, pub AstType);
34
35impl Default for AstString {
36 fn default() -> Self {
37 AstString("".to_owned(), AstType::default())
38 }
39}
40
41#[derive(Debug, Serialize, Deserialize, Clone)]
42pub struct AstFloat(pub f64, pub AstType);
43
44#[derive(Debug, Serialize, Deserialize, Clone)]
45pub struct AstInteger(pub i64, pub AstType);
46
47#[derive(Debug, Serialize, Deserialize, Clone)]
48pub enum AstNumber {
49 Float(AstFloat),
50 Integer(AstInteger),
51}
52
53#[derive(Debug, Serialize, Deserialize, Clone)]
54pub enum AstValue {
55 Ref(Box<AstValue>, Option<Box<AstAccess>>),
56 Deref(Box<AstValue>, Option<Box<AstAccess>>),
57 FunctionCall(AstIdentifier, Vec<AstValue>, Option<Box<AstAccess>>),
58 Tuple(Vec<AstValue>, Option<Box<AstAccess>>),
59 String(AstString),
60 Number(AstNumber),
61 OperationInline(AstIdentifier, Vec<AstValue>, Option<Box<AstAccess>>),
62 Variable(AstIdentifier, Option<Box<AstAccess>>),
63}
64
65#[derive(Debug, Serialize, Deserialize, Clone)]
66pub enum AstAccess {
67 Tuple(AstInteger, Option<Box<AstAccess>>),
68 Variable(AstIdentifier, Option<Box<AstAccess>>),
69}
70
71#[derive(Debug, Serialize, Deserialize, Clone)]
72pub struct AstVariable {
73 pub id: AstIdentifier,
74 pub typeid: AstType,
75}
76
77#[derive(Debug, Serialize, Deserialize, Clone)]
78pub enum AstType {
79 Tuple(Vec<AstType>),
80 Pointer(Box<AstType>),
81 Identifier(AstIdentifier),
82}
83
84impl Default for AstType {
85 fn default() -> Self {
86 AstType::Identifier(AstIdentifier::default())
87 }
88}
89
90#[derive(Debug, Serialize, Deserialize, Clone)]
91pub enum AstInstruction {
92 Meta(AstMeta),
93 Import(AstImport),
94 Globals(Vec<AstVariable>),
95 Extern(AstExtern),
96 Struct(AstStruct),
97 Function(AstFunction),
98}
99
100#[derive(Debug, Serialize, Deserialize, Clone)]
101pub struct AstMeta(pub Vec<AstMetaField>);
102
103#[derive(Debug, Serialize, Deserialize, Clone)]
104pub struct AstMetaField {
105 pub id: AstIdentifier,
106 pub args: Vec<AstMetaValue>,
107}
108
109#[derive(Debug, Serialize, Deserialize, Clone)]
110pub enum AstMetaValue {
111 Named(AstIdentifier, Box<AstMetaValue>),
112 Field(AstMetaField),
113 String(AstString),
114 Number(AstNumber),
115}
116
117#[derive(Debug, Serialize, Deserialize, Clone)]
118pub struct AstImport {
119 pub meta: Vec<AstMeta>,
120 pub names: Vec<AstIdentifier>,
121 pub module: AstString,
122}
123
124#[derive(Debug, Serialize, Deserialize, Clone)]
125pub struct AstExtern {
126 pub meta: Vec<AstMeta>,
127 pub item: AstFunctionHeader,
128 pub location_module: AstIdentifier,
129 pub location_function: AstIdentifier,
130}
131
132#[derive(Debug, Serialize, Deserialize, Clone)]
133pub struct AstStruct {
134 pub meta: Vec<AstMeta>,
135 pub export: bool,
136 pub id: AstIdentifier,
137 pub fields: Vec<AstVariable>,
138}
139
140#[derive(Debug, Serialize, Deserialize, Clone)]
141pub struct AstFunction {
142 pub meta: Vec<AstMeta>,
143 pub export: bool,
144 pub header: AstFunctionHeader,
145 pub locals: Vec<AstVariable>,
146 pub ops: Vec<AstBlockOp>,
147}
148
149#[derive(Debug, Serialize, Deserialize, Clone)]
150pub struct AstFunctionHeader {
151 pub id: AstIdentifier,
152 pub params: Vec<AstVariable>,
153 pub typeid: Option<AstType>,
154}
155
156#[derive(Debug, Serialize, Deserialize, Clone)]
157pub enum AstBlockOp {
158 Label(AstIdentifier),
159 Operation(AstOperation),
160}
161
162#[derive(Debug, Serialize, Deserialize, Clone)]
163pub struct AstOperation {
164 pub meta: Vec<AstMeta>,
165 pub id: AstIdentifier,
166 pub params: Vec<AstValue>,
167 pub targets: Vec<AstValue>,
168}
169
170#[derive(Debug, Serialize, Deserialize, Clone)]
171pub struct AstOpsDescriptor {
172 pub meta: Vec<AstMeta>,
173 pub rules: Vec<AstOpRule>,
174}
175
176#[derive(Debug, Serialize, Deserialize, Clone)]
177pub struct AstOpRule {
178 pub meta: Vec<AstMeta>,
179 pub id: AstIdentifier,
180 pub params: Vec<AstOpParam>,
181 pub targets: Vec<AstType>,
182 pub definition: Vec<AstOpRuleDef>,
183}
184
185#[derive(Debug, Serialize, Deserialize, Clone)]
186pub struct AstOpParam {
187 pub id: AstIdentifier,
188 pub typeid: AstType,
189}
190
191#[derive(Debug, Serialize, Deserialize, Clone)]
192pub struct AstOpRuleDef {
193 pub id: AstIdentifier,
194 pub description: Vec<AstOpRuleDefDesc>,
195}
196
197#[derive(Debug, Serialize, Deserialize, Clone)]
198pub struct AstOpRuleDefDesc {
199 pub id: AstIdentifier,
200 pub value: AstString,
201}