1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
use serde_json;

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstModule {
    pub shebang: Option<String>,
    pub instructions: Vec<AstInstruction>,
}

impl AstModule {
    pub fn from_json(source: &str) -> serde_json::Result<Self> {
        serde_json::from_str(source)
    }

    pub fn to_json(&self, pretty: bool) -> serde_json::Result<String> {
        if pretty {
            serde_json::to_string_pretty(self)
        } else {
            serde_json::to_string(self)
        }
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstIdentifier(pub String);

impl Default for AstIdentifier {
    fn default() -> Self {
        AstIdentifier("".to_owned())
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstString(pub String, pub AstType);

impl Default for AstString {
    fn default() -> Self {
        AstString("".to_owned(), AstType::default())
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstFloat(pub f64, pub AstType);

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstInteger(pub i64, pub AstType);

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum AstNumber {
    Float(AstFloat),
    Integer(AstInteger),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum AstValue {
    Ref(Box<AstValue>, Option<Box<AstAccess>>),
    Deref(Box<AstValue>, Option<Box<AstAccess>>),
    FunctionCall(AstIdentifier, Vec<AstValue>, Option<Box<AstAccess>>),
    Tuple(Vec<AstValue>, Option<Box<AstAccess>>),
    String(AstString),
    Number(AstNumber),
    OperationInline(AstIdentifier, Vec<AstValue>, Option<Box<AstAccess>>),
    Variable(AstIdentifier, Option<Box<AstAccess>>),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum AstAccess {
    Tuple(AstInteger, Option<Box<AstAccess>>),
    Variable(AstIdentifier, Option<Box<AstAccess>>),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstVariable {
    pub id: AstIdentifier,
    pub typeid: AstType,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum AstType {
    Tuple(Vec<AstType>),
    Pointer(Box<AstType>),
    Identifier(AstIdentifier),
}

impl Default for AstType {
    fn default() -> Self {
        AstType::Identifier(AstIdentifier::default())
    }
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum AstInstruction {
    Meta(AstMeta),
    Import(AstImport),
    Globals(Vec<AstVariable>),
    Extern(AstExtern),
    Struct(AstStruct),
    Function(AstFunction),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstMeta(pub Vec<AstMetaField>);

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstMetaField {
    pub id: AstIdentifier,
    pub args: Vec<AstMetaValue>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum AstMetaValue {
    Named(AstIdentifier, Box<AstMetaValue>),
    Field(AstMetaField),
    String(AstString),
    Number(AstNumber),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstImport {
    pub meta: Vec<AstMeta>,
    pub names: Vec<AstIdentifier>,
    pub module: AstString,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstExtern {
    pub meta: Vec<AstMeta>,
    pub item: AstFunctionHeader,
    pub location_module: AstIdentifier,
    pub location_function: AstIdentifier,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstStruct {
    pub meta: Vec<AstMeta>,
    pub export: bool,
    pub id: AstIdentifier,
    pub fields: Vec<AstVariable>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstFunction {
    pub meta: Vec<AstMeta>,
    pub export: bool,
    pub header: AstFunctionHeader,
    pub locals: Vec<AstVariable>,
    pub ops: Vec<AstBlockOp>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstFunctionHeader {
    pub id: AstIdentifier,
    pub params: Vec<AstVariable>,
    pub typeid: Option<AstType>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub enum AstBlockOp {
    Label(AstIdentifier),
    Operation(AstOperation),
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstOperation {
    pub meta: Vec<AstMeta>,
    pub id: AstIdentifier,
    pub params: Vec<AstValue>,
    pub targets: Vec<AstValue>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstOpsDescriptor {
    pub meta: Vec<AstMeta>,
    pub rules: Vec<AstOpRule>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstOpRule {
    pub meta: Vec<AstMeta>,
    pub id: AstIdentifier,
    pub params: Vec<AstOpParam>,
    pub targets: Vec<AstType>,
    pub definition: Vec<AstOpRuleDef>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstOpParam {
    pub id: AstIdentifier,
    pub typeid: AstType,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstOpRuleDef {
    pub id: AstIdentifier,
    pub description: Vec<AstOpRuleDefDesc>,
}

#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct AstOpRuleDefDesc {
    pub id: AstIdentifier,
    pub value: AstString,
}