reic 0.1.0

A compiler that just works
Documentation
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use crate::symtab::Identifier;
use serde_derive::Serialize;

// After lexing, combine the tokens with their following counterpart expressions
// CREDIT: matklad at https://github.com/matklad/miniml/blob/master/ast/src/exprs.rs for providing a cool base

// ------------------
// DEFINE TYPES
// ------------------

#[derive(Debug, Serialize)]
pub struct Array {
    data: Vec<Primitive>,
}

// class is just data + methods (no scripting allowed)

/// Scope type for body expressions. But I dunno, maybe we can be more lax with this in parsing phase, then use it for traversing the AST
#[derive(Debug, Serialize)]
pub enum ScopeType {
    Fn,
    Class,
    Data,
    Trait,
    Mod,
    Anonymous,
    If,
    Loop,
}

#[derive(Debug, Serialize)]
pub enum Primitive {
    // Integers
    Int32(i32),
    Int64(i64),
    Int128(i128),
    UInt32(u32),
    UInt64(u64),
    UInt128(i128),
    // Floats
    Float32(f32),
    Float64(f64),
    // Bools
    Bool(bool),
    // ASCII Chars
    Char(u8),
    // Raw Bytes
    Byte(u8),
}

#[derive(Debug, Serialize)]
pub enum ReiType {
    Primitive(Primitive),
    /// Class or Data (maybe vec too?)
    Array(Array),
}

// for rei type, maybe instead of primitive or compound, just declare you own
// with generic T

// ------------------
// CONTENT
// ------------------

#[derive(Debug)]
pub struct Content {
    pub expressions: Vec<Expr>,
}

impl Content {
    pub fn new(expressions: Vec<Expr>) -> Self {
        Self { expressions }
    }
}

impl Default for Content {
    fn default() -> Self {
        Self {
            expressions: Default::default(),
        }
    }
}

// ------------------
// DEFINE EXPRESSIONS
// ------------------

#[derive(Debug)]
pub enum Expr {
    // Primitive Expression
    Literal(Primitive),
    // Ident Expression
    Identifier(Identifier),
    // Variable Definition
    VarDef(Box<VarDef>),
    VarRedef(Box<VarRedef>),
    // Common Expressions
    UnaryOp(Box<UnaryOp>),
    BinaryOp(Box<BinaryOp>),
    // Use
    UseExpr(Box<UseExpr>),
    // If
    If(Box<If>),
    /// loop {} => while true {}. Also includes for loops
    Loop(Box<Loop>),
    // Fn
    Fn(Box<Fn>),
    ParamList(Box<ParamList>),
    ArgList(Box<ArgList>),
    CallArg(Box<CallArg>),
    FnCall(Box<FnCall>),
    // Class
    Class(Box<Class>),
    // Data
    Data(Box<Data>),
    DataFieldDef(Box<DataFieldDef>),
    // Trait
    Trait(Box<Trait>),
    // Anon scope
    AnonScope(Box<AnonScope>),
    // Meta
    ExportExpr(Box<ExportExpr>),
    ModExpr(Box<ModExpr>),
    AnnotationExpr(Box<AnnotationExpr>),
    Empty,
}

macro_rules! into_expr {
    ($id:ident) => {
        impl Into<Expr> for $id {
            fn into(self) -> Expr {
                Expr::$id(Box::new(self))
            }
        }
    };
}

// ---------------
// KEY EXPRESSIONS
// ---------------

// into_expr!(Identifier);

// what needs it own expression?
// something that will help phantasm. But we an always remove them later

// what about annotations? You are allowed 0-n annotations, which can be separated by commas but unique
// per annotatable item
#[derive(Debug)]
pub struct AnnotationExpr {
    pub annotated_expr: Expr,
}

/// You can export an item. An item is either a scope (anon or labelled) or field definition
#[derive(Debug)]
pub struct ExportExpr {
    pub item: Expr,
}

/// Like rust mod expressions, rei mods are simply labelled scopes
#[derive(Debug)]
pub struct ModExpr {
    pub body: Expr,
}

into_expr!(ModExpr);

#[derive(Debug, Clone, Copy)]
pub enum TypeModifier {
    Let,
    Mut,
    Const,
    Static,
}

/// A variable definition must have a type modifier and an lhs + rhs
/// The lhs can be as simple as an ident, and rhs can be a literal
#[derive(Debug)]
pub struct VarDef {
    pub var_type_modifier: TypeModifier,
    pub lhs: Expr,
    pub rhs: Expr,
}

// a variable redef is simply lhs = rhs without a type modifier
// technically can be overloaded like c++ but uhh
#[derive(Debug)]
pub struct VarRedef {
    pub lhs: Expr,
    pub rhs: Expr,
}

into_expr!(VarRedef);

// we are only looking for 'assignable' expressions
// or maybe non = expressions

#[derive(Debug)]
pub struct PartialExpr {
    pub expr: Expr,
}

// what about an anonymous scope? just dont give it a label, but allow it to have access to its super namespace
#[derive(Debug)]
pub struct AnonScope {
    pub expr: Expr,
}

into_expr!(AnonScope);

// some reserved 'operators' like #, // are simply ignored and cut out from the lexer
// hash comments may be passed on here to document the documentable field
// scope operators {} are parsed into the expr itself
// strings should be extracted into literals

// make an annotation expr too I'd say
// what about $ in a string literal?
// i dunno, maybe handle that in the next stage

// Reserved
#[derive(Debug, Clone, Copy)]
pub enum ReservedOperator {
    DollarSign,
    Annotation,
    Hash,
    /// Equals (=) always has to do with var def/redef
    Eq,
    Dot,
    Comma,
    QuestionMark,
    Colon,
    Semicolon,
    /// Only used for namespace deref
    DoubleColon,
}

// Most overloadable unary operator methods can be derived from their binary counterparts
// e.g. ++ => += 1
// A function call can be seen as a pseudo unary operation involving one term and multiple inputs
// But that is better implemented in the stdlib I think
// What about indexing? [], maybe have a core trait that impls it?

#[derive(Debug, Clone, Copy)]
pub enum OverloableUnaryOperator {
    PlusPlus,
    MinusMinus,
    Exclamation,
}

#[derive(Debug, Clone, Copy)]
pub enum OverloableBinaryOperator {
    // Comparison
    /// ==
    Equiv,
    Lt,
    Gt,
    Gte,
    Lte,
    /// &&, and
    And,
    /// ||, or
    Or,

    // Arithmetic
    Mult,
    Div,
    Add,
    Sub,
    Modulo,

    // Shorthands
    PlusEq,
    MinusEq,
    MultEq,
    DivEq,
    DoubleMult,
    DoubleDiv,

    // Usually Bitwise, as impl'd by core lib for primitive types
    LeftShift,
    RightShift,
    BitwiseAnd,
    BitwiseOr,
    BitwiseNot,
    BitwiseXor,
}

#[derive(Debug)]
pub struct UnaryOp {
    pub kind: OverloableUnaryOperator,
    pub lhs: Expr,
}

into_expr!(UnaryOp);

#[derive(Debug)]
pub struct BinaryOp {
    pub kind: OverloableBinaryOperator,
    pub lhs: Expr,
    pub rhs: Expr,
}

into_expr!(BinaryOp);

#[derive(Debug)]
pub struct Fn {
    pub export: bool,
    pub fn_name: Identifier,
    pub fn_return_type: ReiType,
    pub args: ParamList,
    pub body: Expr,
}

pub type DefaultParam = Option<Identifier>;
pub type ParamName = Identifier;
pub type ParamType = Identifier;

// ... would simply be considered Identifier Name ...
// and parsed as such. With a std::any type

#[derive(Debug)]
pub struct ParamList {
    pub params: Vec<(ParamName, ParamType, DefaultParam)>,
}

#[derive(Debug)]
pub struct ArgList {
    pub args: Vec<Identifier>,
}

into_expr!(ArgList);

into_expr!(ParamList);

into_expr!(Fn);

/// type X = Y
/// Y should be a typeable Expr, leave for now (an expr that evals to a Type)
#[derive(Debug)]
pub struct Type {
    pub export: bool,
    pub ident: Identifier,
    pub rhs: Expr,
}

// maybe to add a new expr, use a macro. That makes everything pub and #[derive(debug)]
// or maybe derive new

#[derive(Debug)]
pub struct If {
    pub condition: Expr,
    pub true_body: Expr,
    // an else if, else or normal (parent scope)
    // technically, an else if is just another If
    // else is simply an anoymous scope without a condition
    pub false_body: Expr,
}

into_expr!(If);

// Maybe allow some expressions?
// or make that an error with travelling the tree afterwards -> semantic err??
// Better to give a pointer or ref instead? Yea I like that, but then we have to deal with lifetimes
// so maybe just a pointer

// inheritance is good for cases where you would otherwise do https://stackoverflow.com/questions/65380698/trait-with-default-implementation-and-required-struct-member
// and you can simply overload the function

/// A class. Can also impl traits on inherit classes
#[derive(Debug)]
pub struct Class {
    pub export: bool,
    pub class_name: Identifier,
    pub body: Expr,
    pub implements: Vec<*mut Trait>,
    pub inherits: Vec<*mut Class>,
}

into_expr!(Class);

/// Is it possible to reference another 'scope' or expr body?. Also depends what is being used, the whole thing, or a list of things?. If using the whole thing, can maybe copy and paste.. nah
/// Maybe do it at phantasm stage. But make it a bit easier. Like give it a pointer to the body/scope instead of just reiterating the name
pub type Namespace = String;
pub type UseItems = String;

// No such thing as 'pub use x' or 'export use x', instead use the reexport keyword, 'reexport x'
// or maybe just export 'x' again

#[derive(Debug)]
pub struct UseExpr {
    pub namespace: Namespace,
    pub items: Vec<UseItems>,
}

into_expr!(UseExpr);

// should actually be Vec<CallArg>
// with keyword args as an optional
#[derive(Debug)]
pub struct CallArg {
    pub arg: Identifier,
    pub keyword_param: Option<Identifier>,
}

into_expr!(CallArg);

#[derive(Debug)]
pub struct FnCall {
    pub fn_name: Identifier,
    pub args: ArgList,
}

into_expr!(FnCall);

// we can actually give data a default val if we just make it like any other
// vardef
// maybe we can further simplify that to mut/const/etc? no but const is rust-like..

#[derive(Debug)]
pub struct Data {
    pub export: bool,
    pub data_name: Identifier,
    pub fields: Vec<DataFieldDef>,
}

// maybe make a new expr for data fields?

#[derive(Debug)]
pub struct DataFieldDef {
    pub ident: Identifier,
    pub def: Expr,
}

into_expr!(Data);
into_expr!(DataFieldDef);

#[derive(Debug)]
pub struct Trait {
    pub export: bool,
    pub trait_name: Identifier,
    pub fields: Vec<TraitExpr>,
}

// What about export? visible flag?
// another level of indirection? uhh, idk, tree can get quite big, when you just repeat it a few times

/// Either function declarations or type declarations
#[derive(Debug)]
pub enum TraitExpr {
    TraitFnDec(Fn),
    Type(Type),
}

// what about default traits?
// uhh, make a trait called Default or just use the default constructor
// default trait functions

// #[derive(Debug)]
// pub struct TraitFnDec {}

// prefer to use lambda, map, reduce, etc. iters when possible instead of loops
// although where possible, can also try to parallelise and unroll a loop (no jumps due to branch pred and overhead)
// maybe depending on the hardware (phantasm analysis)

#[derive(Debug)]
pub struct Loop {
    /// The condition should eval to a bool
    pub condition: BoolExpr,
    /// The body also includes implicit logic for 'while', 'for' and 'do'
    pub body: Expr,
}

// maybe implemented by core lib?
// since the comparison ops are overloadable and dont need to return a bool per se? but they prob should
// ok maybe have a subset called BoolOp from Binary expr or unary expr, specific things
#[derive(Debug)]
pub struct BoolExpr {
    pub boolean_expr: Expr,
}

into_expr!(Loop);