-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.rs
More file actions
564 lines (533 loc) · 16.9 KB
/
lib.rs
File metadata and controls
564 lines (533 loc) · 16.9 KB
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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
use core::fmt;
use std::rc::Rc;
#[derive(Debug)]
pub struct FileContainer {
pub top_items: Vec<Rc<Box<TypeTree>>>,
pub curried: Vec<Ty>,
}
#[derive(Debug)]
pub struct SigInfo {
pub left: Option<Ty>,
pub err: Option<Ty>,
pub undefined: Option<Ty>,
pub right: Ty,
}
#[derive(Debug)]
pub struct ErrorInfo {
pub message: String,
pub code: usize,
pub curried: Ty,
}
#[derive(Debug)]
pub struct TagInfo {
pub name: String,
pub props: Vec<Rc<Box<TypeTree>>>,
pub types: Vec<Ty>,
pub curried: Ty,
}
#[derive(Debug)]
pub struct StructInfo {
pub props: Vec<String>,
pub types: Vec<Ty>,
pub curried: Ty,
}
#[derive(Debug)]
pub struct ArgInfo {
pub curried: Ty,
}
#[derive(Debug)]
pub struct DeclaratorInfo {
pub name: String,
pub curried: Ty,
}
#[derive(Debug)]
pub struct MatchOp {
pub expr: Rc<Box<TypeTree>>,
pub curried: Ty,
pub arms: Vec<Rc<Box<TypeTree>>>,
pub curried_arms: Ty,
}
#[derive(Debug)]
pub struct ForOp {
pub in_expr: Rc<Box<TypeTree>>,
pub in_curried: Ty,
pub body: Rc<Box<TypeTree>>,
pub body_curried: Ty,
}
#[derive(Debug)]
pub struct BinaryOp {
pub left: Rc<Box<TypeTree>>,
pub right: Rc<Box<TypeTree>>,
pub curried: Ty,
}
#[derive(Debug)]
pub struct UnaryOp {
pub val: Rc<Box<TypeTree>>,
pub curried: Ty,
}
#[derive(Debug)]
pub struct NoOp {
pub curried: Ty,
}
#[derive(Debug)]
pub struct Invoke {
pub args: Vec<Rc<Box<TypeTree>>>,
pub args_curried: Vec<Ty>,
pub ident: Rc<Box<TypeTree>>,
pub curried: Ty,
}
#[derive(Debug)]
pub struct Initialization {
pub left: String,
pub right: Rc<Box<TypeTree>>,
pub curried: Ty,
}
#[derive(Debug)]
pub struct Reassignment {
pub left: Rc<Box<TypeTree>>,
pub right: Rc<Box<TypeTree>>,
pub curried: Ty,
}
#[derive(Debug)]
pub struct PropAccess {
pub prev: Rc<Box<TypeTree>>,
pub ident: String,
pub curried: Ty,
}
#[derive(Debug)]
pub struct SymbolAccess {
pub ident: String,
pub curried: Ty,
}
#[derive(Debug)]
pub struct ArrayAccess {
pub prev: Rc<Box<TypeTree>>,
pub inner: Rc<Box<TypeTree>>,
pub curried: Ty,
}
#[derive(Debug)]
pub struct StructInitialize {
pub idents: Vec<String>,
pub vals: Vec<Rc<Box<TypeTree>>>,
pub vals_curried: Vec<Ty>,
pub curried: Ty,
}
#[derive(Debug)]
pub struct ArrayInitialize {
pub vals: Vec<Rc<Box<TypeTree>>>,
pub vals_curried: Vec<Ty>,
pub curried: Ty,
}
#[derive(Debug)]
pub struct FunctionInitialize {
pub name: String,
pub args: Vec<Rc<Box<TypeTree>>>,
pub args_curried: Vec<Ty>,
pub block: Rc<Box<TypeTree>>,
pub block_curried: Ty,
}
#[derive(Debug)]
pub struct Block {
pub exprs: Vec<Rc<Box<TypeTree>>>,
pub curried: Ty,
}
#[derive(Debug)]
pub enum TypeTree {
// info
StructInfo(StructInfo),
DeclaratorInfo(DeclaratorInfo),
TagInfo(TagInfo),
ErrorInfo(ErrorInfo),
SigInfo(SigInfo),
// flow
For(ForOp),
Invoke(Invoke),
Match(MatchOp),
Arm(BinaryOp),
Block(Block),
Return(UnaryOp),
ReturnVoid(NoOp),
Never(NoOp),
Break(UnaryOp),
BreakVoid(NoOp),
// binops
Plus(BinaryOp),
Minus(BinaryOp),
Divide(BinaryOp),
Multiply(BinaryOp),
Modulo(BinaryOp),
Range(BinaryOp),
CastAs(BinaryOp),
BubbleUndef(BinaryOp),
BubbleError(BinaryOp),
// unops
ReadBorrow(UnaryOp),
MutBorrow(UnaryOp),
Copy(UnaryOp),
Clone(UnaryOp),
Negate(UnaryOp),
Not(UnaryOp),
// values
PropAccess(PropAccess),
SymbolAccess(SymbolAccess),
RestAccess(NoOp),
SelfAccess(NoOp),
// data types
ArgInit(NoOp),
SelfInit(NoOp),
SymbolInit(SymbolAccess),
StructInit(StructInitialize),
PropInit(Initialization),
ArrayInit(ArrayInitialize),
FuncInit(FunctionInitialize),
AnonFuncInit(FunctionInitialize),
ConstInit(Initialization),
MutInit(Initialization),
StringInit(ArrayInitialize),
// reassignments
As(Reassignment),
PlusAs(Reassignment),
MinusAs(Reassignment),
MultiplyAs(Reassignment),
DivideAs(Reassignment),
ModAs(Reassignment),
OrAs(Reassignment),
NotAs(Reassignment),
XorAs(Reassignment),
LShiftAs(Reassignment),
RShiftAs(Reassignment),
// value types
UndefinedValue,
UnknownValue,
BoolValue(bool),
I64(i64),
Char(char),
I32(i32),
U64(u64),
U32(u32),
F64(f64),
}
impl TypeTree {
pub fn get_curried(&self) -> Ty {
match self {
TypeTree::DeclaratorInfo(x) => x.curried.clone(),
TypeTree::StructInfo(x) => x.curried.clone(),
TypeTree::TagInfo(x) => x.curried.clone(),
TypeTree::SigInfo(x) => x.right.clone(),
TypeTree::ErrorInfo(x) => x.curried.clone(),
TypeTree::For(x) => x.body_curried.clone(),
TypeTree::Invoke(x) => x.curried.clone(),
TypeTree::Match(x) => x.curried_arms.clone(),
TypeTree::Arm(x) => x.curried.clone(),
TypeTree::Block(x) => x.curried.clone(),
TypeTree::Return(x) => x.curried.clone(),
TypeTree::ReturnVoid(_) => Ty::Void,
TypeTree::Never(_) => Ty::Never,
TypeTree::Break(x) => x.curried.clone(),
TypeTree::BreakVoid(x) => x.curried.clone(),
TypeTree::Plus(x) => x.curried.clone(),
TypeTree::Minus(x) => x.curried.clone(),
TypeTree::Divide(x) => x.curried.clone(),
TypeTree::Multiply(x) => x.curried.clone(),
TypeTree::Modulo(x) => x.curried.clone(),
TypeTree::Range(x) => x.curried.clone(),
TypeTree::CastAs(x) => x.curried.clone(),
TypeTree::BubbleUndef(x) => x.curried.clone(),
TypeTree::BubbleError(x) => x.curried.clone(),
TypeTree::ReadBorrow(x) => x.curried.clone(),
TypeTree::MutBorrow(x) => x.curried.clone(),
TypeTree::Copy(x) => x.curried.clone(),
TypeTree::Clone(x) => x.curried.clone(),
TypeTree::Negate(x) => x.curried.clone(),
TypeTree::Not(x) => x.curried.clone(),
TypeTree::PropAccess(x) => x.curried.clone(),
TypeTree::SymbolAccess(x) => x.curried.clone(),
TypeTree::RestAccess(x) => x.curried.clone(),
TypeTree::SelfAccess(x) => x.curried.clone(),
TypeTree::StructInit(x) => x.curried.clone(),
TypeTree::PropInit(x) => x.curried.clone(),
TypeTree::ArrayInit(x) => x.curried.clone(),
TypeTree::FuncInit(x) => x.block_curried.clone(),
TypeTree::AnonFuncInit(x) => x.block_curried.clone(),
TypeTree::ConstInit(x) => x.curried.clone(),
TypeTree::MutInit(x) => x.curried.clone(),
TypeTree::StringInit(x) => x.curried.clone(),
TypeTree::As(x) => x.curried.clone(),
TypeTree::PlusAs(x) => x.curried.clone(),
TypeTree::MinusAs(x) => x.curried.clone(),
TypeTree::MultiplyAs(x) => x.curried.clone(),
TypeTree::DivideAs(x) => x.curried.clone(),
TypeTree::ModAs(x) => x.curried.clone(),
TypeTree::OrAs(x) => x.curried.clone(),
TypeTree::NotAs(x) => x.curried.clone(),
TypeTree::XorAs(x) => x.curried.clone(),
TypeTree::LShiftAs(x) => x.curried.clone(),
TypeTree::RShiftAs(x) => x.curried.clone(),
TypeTree::UndefinedValue => Ty::Undefined,
TypeTree::BoolValue(_) => Ty::Bool,
TypeTree::I64(_) => Ty::I64,
TypeTree::I32(_) => Ty::I32,
TypeTree::U64(_) => Ty::U64,
TypeTree::U32(_) => Ty::U32,
TypeTree::F64(_) => Ty::F64,
TypeTree::Char(_) => Ty::Char,
TypeTree::UnknownValue => Ty::Unknown,
TypeTree::ArgInit(x) => x.curried.clone(),
TypeTree::SelfInit(x) => x.curried.clone(),
TypeTree::SymbolInit(x) => x.curried.clone(),
}
}
pub fn into_init(&self) -> &Initialization {
match self {
TypeTree::ConstInit(x) => x,
_ => panic!("type lang issue, failed into init"),
}
}
pub fn into_data(&self) -> Vec<u8> {
let mut v = vec![];
match self {
TypeTree::U64(x) => {
v.extend_from_slice(&u64::to_ne_bytes(x.clone()));
}
TypeTree::I64(x) => {
v.extend_from_slice(&i64::to_ne_bytes(x.clone()));
}
_ => panic!("type lang issue, failed into data"),
}
return v;
}
pub fn into_declarator(&self) -> &DeclaratorInfo {
match self {
TypeTree::DeclaratorInfo(x) => x,
_ => panic!("issue declarator not found"),
}
}
pub fn into_func_init(&self) -> &FunctionInitialize {
match self {
TypeTree::FuncInit(x) => x,
_ => panic!("issue function not found"),
}
}
pub fn into_symbol_access(&self) -> &SymbolAccess {
match self {
TypeTree::SymbolAccess(x) => x,
_ => panic!("issue symbol not found"),
}
}
pub fn into_prop_init(&self) -> &Initialization {
match self {
TypeTree::PropInit(x) => x,
_ => panic!("issue property not found"),
}
}
pub fn into_binary_op(&self) -> &BinaryOp {
match self {
TypeTree::Arm(x) => x,
_ => panic!("issue binary op not found"),
}
}
pub fn whatami(&self) -> &'static str {
match self {
TypeTree::StructInfo(_) => "struct declaration",
TypeTree::DeclaratorInfo(_) => "property declaration",
TypeTree::TagInfo(_) => "tag declaration",
TypeTree::SigInfo(_) => "type signature",
TypeTree::ErrorInfo(_) => "error declaration",
TypeTree::For(_) => "for loop",
TypeTree::Invoke(_) => "function invocation",
TypeTree::Match(_) => "match",
TypeTree::Arm(_) => "pattern match arm",
TypeTree::Block(_) => "block of statements",
TypeTree::Return(_) => "return expression",
TypeTree::ReturnVoid(_) => "return",
TypeTree::Never(_) => "never",
TypeTree::Break(_) => "break expression",
TypeTree::BreakVoid(_) => "break",
TypeTree::Plus(_) => "addition",
TypeTree::Minus(_) => "subtraction",
TypeTree::Divide(_) => "division",
TypeTree::Multiply(_) => "multiplication",
TypeTree::Modulo(_) => "modulus",
TypeTree::Range(_) => "range",
TypeTree::CastAs(_) => "cast",
TypeTree::BubbleUndef(_) => "undefinded bubble",
TypeTree::BubbleError(_) => "error bubble",
TypeTree::ReadBorrow(_) => "read borrow",
TypeTree::MutBorrow(_) => "mutable borrow",
TypeTree::Copy(_) => "unsized copy",
TypeTree::Clone(_) => "sized clone",
TypeTree::Negate(_) => "negation",
TypeTree::Not(_) => "boolean negatation",
TypeTree::PropAccess(_) => "property access",
TypeTree::SymbolAccess(_) => "symbol reference",
TypeTree::RestAccess(_) => "rest access",
TypeTree::SelfAccess(_) => "self reference",
TypeTree::StructInit(_) => "struct initialization",
TypeTree::PropInit(_) => "property assignment",
TypeTree::ArrayInit(_) => "array initialization",
TypeTree::FuncInit(_) => "function initialization",
TypeTree::AnonFuncInit(_) => "anonymous function initialization",
TypeTree::ConstInit(_) => "constant initialization",
TypeTree::MutInit(_) => "mutable initialization",
TypeTree::StringInit(_) => "string initialization",
TypeTree::As(_) => "reassignment",
TypeTree::PlusAs(_) => "addition reassignment",
TypeTree::MinusAs(_) => "subtraction reassignment",
TypeTree::MultiplyAs(_) => "multiplication reassignment",
TypeTree::DivideAs(_) => "division reassignment",
TypeTree::ModAs(_) => "modulus reassignemnt",
TypeTree::OrAs(_) => "or reassignment",
TypeTree::NotAs(_) => "logical not reassignment",
TypeTree::XorAs(_) => "xor reassignment",
TypeTree::LShiftAs(_) => "left shift reassignment",
TypeTree::RShiftAs(_) => "right shift reassignment",
TypeTree::UndefinedValue => "undefined",
TypeTree::BoolValue(_) => "boolean value",
TypeTree::I64(_) => "integer 64 bit",
TypeTree::I32(_) => "integer 32 bit",
TypeTree::U64(_) => "unsigned integer 64 bit",
TypeTree::U32(_) => "unsigned integer 32 bit",
TypeTree::F64(_) => "floating point double precision 64 bit",
TypeTree::Char(_) => "ascii character",
TypeTree::UnknownValue => "unknown value",
TypeTree::ArgInit(_) => "function argument",
TypeTree::SelfInit(_) => "self as function argument",
TypeTree::SymbolInit(_) => "symbol definition or initialization",
}
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Ty {
I64,
I32,
U64,
U32,
F64,
Unknown,
Rest,
Undefined,
Void,
Never,
Bool,
Char,
String,
Const(Box<Ty>),
Mut(Box<Ty>),
MutBorrow(Box<Ty>),
ReadBorrow(Box<Ty>),
Frame(Vec<Ty>),
Struct(Vec<Ty>),
Error,
Tag(Vec<Ty>),
Function(Vec<Ty>, Box<Ty>),
Custom(String),
Trait(String),
TSelf,
Array(Box<Ty>),
}
impl fmt::Display for Ty {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
Ty::I64 => write!(f, "i64"),
Ty::I32 => write!(f, "i32"),
Ty::U64 => write!(f, "u64"),
Ty::U32 => write!(f, "u32"),
Ty::F64 => write!(f, "f64"),
Ty::Unknown => write!(f, "unknown"),
Ty::Rest => write!(f, "_"),
Ty::Undefined => write!(f, "undefined"),
Ty::Void => write!(f, "void"),
Ty::Never => write!(f, "never"),
Ty::Bool => write!(f, "bool"),
Ty::Char => write!(f, "char"),
// might want to just make this an Array
Ty::String => write!(f, "[char]"),
Ty::Const(x) => write!(f, "const {}", x),
Ty::Mut(x) => write!(f, "let {}", x),
Ty::ReadBorrow(x) => write!(f, "&{}", x),
Ty::MutBorrow(x) => write!(f, "*{}", x),
Ty::Frame(x) => {
write!(f, "frame(").unwrap();
for a in x {
write!(f, "{},", a).unwrap();
}
write!(f, ")").unwrap();
Ok(())
}
Ty::Struct(x) => {
write!(f, "struct {{").unwrap();
for a in x {
write!(f, "{},", a).unwrap();
}
write!(f, "}}").unwrap();
Ok(())
}
Ty::Error => write!(f, "error"),
Ty::Tag(x) => {
write!(f, "tag ").unwrap();
for a in x {
write!(f, "| {}", a).unwrap();
}
Ok(())
}
Ty::Function(x, y) => {
write!(f, "function(").unwrap();
for a in x {
write!(f, "{},", a).unwrap();
}
write!(f, ") {}", y).unwrap();
Ok(())
}
Ty::Custom(x) => write!(f, "type {}", x),
Ty::Array(x) => write!(f, "[{}]", x),
Ty::Trait(x) => write!(f, "trait {}", x),
Ty::TSelf => write!(f, "self"),
}
}
}
impl Ty {
pub fn ensure_mut(&self) -> Result<(), Ty> {
match self {
Ty::I64 => Err(Ty::I64),
Ty::I32 => Err(Ty::I32),
Ty::U64 => Err(Ty::U64),
Ty::Const(val) => Err(Ty::Const(val.to_owned())),
Ty::Mut(_) => Ok(()),
Ty::ReadBorrow(val) => Err(Ty::ReadBorrow(val.to_owned())),
Ty::MutBorrow(_) => Ok(()),
Ty::Void => Err(Ty::Void),
Ty::Error => Ok(()),
_ => panic!("type lang issue. type not able to be associated to const"),
}
}
pub fn into_vec(&mut self) -> &mut Vec<Ty> {
match self {
Ty::Tag(x) => x,
_ => panic!("type lang issue. unhandled match arm"),
}
}
}
#[macro_export]
macro_rules! simple_tree {
($val:ident) => {
Rc::new(Box::new(TypeTree::$val))
};
}
#[macro_export]
macro_rules! ok_simple_tree {
($val:ident, $curried:ident) => {
Ok((Rc::new(Box::new(TypeTree::$val)), $curried))
};
}
#[macro_export]
macro_rules! ok_tree {
($val:ident, $op:ident, $curried:ident) => {
Ok((Rc::new(Box::new(TypeTree::$val($op))), $curried))
};
}
#[macro_export]
macro_rules! tree {
($val:ident, $op:ident) => {
Rc::new(Box::new(TypeTree::$val($op)))
};
}