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
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
use super::ast;

use std::collections::HashMap;

const DEFAULT_NUM_PRIMITIVE_SIGNATURE: ast::PrimitiveType = ast::PrimitiveType::I32;
const DEFAULT_NUM_TYPE_SIGNATURE: ast::TypeSignature =
    ast::TypeSignature::Primitive(DEFAULT_NUM_PRIMITIVE_SIGNATURE);
const NIL_TYPE_SIGNATURE: ast::TypeSignature =
    ast::TypeSignature::Primitive(ast::PrimitiveType::Nil);
const BOOL_TYPE_SIGNATURE: ast::TypeSignature =
    ast::TypeSignature::Primitive(ast::PrimitiveType::Bool);

#[derive(Debug, Clone)]
pub struct SemanticError {
    pub msg: String,
    pub pos: super::Position,
    pub file: Option<String>,
}

#[derive(Debug, Clone)]
pub struct SemanticStdLib {
    variables: HashMap<String, (bool, ast::TypeSignature)>,
}

impl SemanticStdLib {
    pub fn new() -> SemanticStdLib {
        SemanticStdLib {
            variables: HashMap::new(),
        }
    }

    pub fn add_fn(&mut self, name: String, sig: ast::FunctionSignature) {
        self.variables
            .insert(name, (false, ast::TypeSignature::Function(sig)));
    }
}

#[derive(Debug, Clone)]
struct Scope {
    variables: HashMap<String, (bool, ast::TypeSignature)>,
}

pub struct SemanticAnalyzer {
    scopes: Vec<Scope>,
    errors: Vec<SemanticError>,
    in_function_block: bool,
    file: Option<String>,
}

impl<'a> SemanticAnalyzer {
    fn check_if_var_in_scopes(&self, var: &String) -> Option<&(bool, ast::TypeSignature)> {
        for s in self.scopes.iter().rev() {
            match s.variables.get(var) {
                Some(ts) => return Some(ts),
                None => continue,
            }
        }
        None
    }

    fn make_err(&mut self, pos: &super::Position, msg: String) -> SemanticError {
        let e = SemanticError {
            msg: msg,
            pos: pos.clone(),
            file: self.file.clone(),
        };
        self.errors.push(e.clone());
        e
    }

    fn new_scope(&mut self) {
        self.scopes.push(Scope {
            variables: HashMap::new(),
        });
    }

    fn pop_scope(&mut self, pos: &super::Position) {
        if self.scopes.len() == 1 {
            self.make_err(pos, format!("Cannot pop global scope"));
        } else {
            self.scopes.pop();
        }
    }

    fn last_scope(&mut self) -> &mut Scope {
        self.scopes.last_mut().unwrap()
    }

    fn check_if_type_is_defined(
        &mut self,
        type_: &ast::TypeSignature,
    ) -> Option<ast::TypeSignature> {
        match type_ {
            ast::TypeSignature::Primitive(_) => Some(type_.clone()),
            ast::TypeSignature::Function(_) => Some(type_.clone()),
            ast::TypeSignature::Custom(s) if s == "String" => Some(type_.clone()),
            _ => None,
        }
    }

    pub fn analyze(
        ast: &mut ast::AstNode,
        filename: Option<String>,
        stdlib: Option<SemanticStdLib>,
    ) -> Result<(), Vec<SemanticError>> {
        let mut sa = SemanticAnalyzer {
            scopes: vec![Scope {
                variables: HashMap::new(),
            }],
            errors: Vec::new(),
            in_function_block: false,
            file: filename,
        };

        if let Some(lib) = stdlib {
            sa.last_scope().variables.extend(lib.variables);
        }

        analyze(&mut sa, ast);

        if sa.errors.len() > 0 {
            Err(sa.errors)
        } else {
            Ok(())
        }
    }
}

pub fn analyze(sa: &mut SemanticAnalyzer, ast: &mut ast::AstNode) -> ast::TypeSignature {
    let node_type = match ast.node {
        ast::Ast::Module(ref mut exprs) => {
            let mut idx = 1;
            let len = exprs.len();
            let mut return_type: Option<ast::TypeSignature> = None;
            for expr in (&mut **exprs).iter_mut() {
                let mut e_error = false;
                match expr.node {
                    ast::Ast::Statement(_) => {
                        analyze(sa, expr);
                    }
                    _ => {
                        if idx != len {
                            e_error = true;
                        }
                        return_type = Some(analyze(sa, expr));
                    }
                }
                if e_error {
                    sa.make_err(
                        &expr.pos,
                        format!("Only the last element in a block can be an expression"),
                    );
                }
                idx += 1;
            }
            if let Some(r) = return_type {
                if r != DEFAULT_NUM_TYPE_SIGNATURE && r != NIL_TYPE_SIGNATURE {
                    sa.make_err(
                        &ast.pos,
                        format!("Modules may only return I32 or Nil; found {:?}", r),
                    );
                }
                r
            } else {
                NIL_TYPE_SIGNATURE.clone()
            }
        }
        ast::Ast::Identifier(ref s) => {
            if let None = sa.check_if_var_in_scopes(&s) {
                sa.make_err(&ast.pos, format!("Variable {} not found in scope", s));
            }
            match sa.check_if_var_in_scopes(&s) {
                Some(v) => v.1.clone(),
                None => NIL_TYPE_SIGNATURE.clone(),
            }
        }
        ast::Ast::Number(_) => DEFAULT_NUM_TYPE_SIGNATURE.clone(),
        ast::Ast::String(_) => ast::TypeSignature::Custom(String::from("String")),
        ast::Ast::Bool(_) => BOOL_TYPE_SIGNATURE.clone(),
        ast::Ast::Statement(ref mut ast) => {
            analyze(sa, &mut **ast);
            NIL_TYPE_SIGNATURE.clone()
        }
        ast::Ast::Binary(ref op, ref mut l, ref mut r) => match op {
            ast::BinaryOperation::Less
            | ast::BinaryOperation::LessEqual
            | ast::BinaryOperation::Greater
            | ast::BinaryOperation::GreaterEqual
            | ast::BinaryOperation::Equal
            | ast::BinaryOperation::NotEqual => {
                let ltype = analyze(sa, &mut **l);
                let rtype = analyze(sa, &mut **r);
                if ltype != rtype {
                    sa.make_err(
                        &ast.pos,
                        format!(
                            "Binary operands are not the same type; {:?} != {:?}",
                            ltype, rtype
                        ),
                    );
                }
                BOOL_TYPE_SIGNATURE.clone()
            }
            ast::BinaryOperation::And => {
                let ltype = analyze(sa, &mut **l);
                if ltype != BOOL_TYPE_SIGNATURE {
                    sa.make_err(
                        &l.pos,
                        format!(
                            "Left binary and operand not of type Bool; found {:?}",
                            ltype
                        ),
                    );
                }
                let rtype = analyze(sa, &mut **r);
                if rtype != BOOL_TYPE_SIGNATURE {
                    sa.make_err(
                        &r.pos,
                        format!(
                            "Right binary and operand not of type Bool; found {:?}",
                            rtype
                        ),
                    );
                }
                BOOL_TYPE_SIGNATURE.clone()
            }
            ast::BinaryOperation::Or => {
                let ltype = analyze(sa, &mut **l);
                if ltype != BOOL_TYPE_SIGNATURE {
                    sa.make_err(
                        &l.pos,
                        format!("Left binary or operand not of type Bool; found {:?}", ltype),
                    );
                }
                let rtype = analyze(sa, &mut **r);
                if rtype != BOOL_TYPE_SIGNATURE {
                    sa.make_err(
                        &r.pos,
                        format!(
                            "Right binary or operand not of type Bool; found {:?}",
                            rtype
                        ),
                    );
                }
                BOOL_TYPE_SIGNATURE.clone()
            }
            ast::BinaryOperation::Assign => {
                let return_type = analyze(sa, &mut **l);
                if return_type != analyze(sa, &mut **r) {
                    sa.make_err(&r.pos, format!("Binary operands are not the same type"));
                }
                if let ast::Ast::Identifier(s) = &l.node {
                    if let Some(v) = sa.check_if_var_in_scopes(s) {
                        if !v.0 {
                            sa.make_err(&l.pos, format!("Variable {} not mutable", s));
                        }
                    } else {
                        sa.make_err(&l.pos, format!("Variable {} not found in scope", s));
                    }
                    return_type
                } else {
                    sa.make_err(&l.pos, format!("Binary assign not assigning variable"));
                    NIL_TYPE_SIGNATURE.clone()
                }
            }
            _ => {
                let ltype = analyze(sa, &mut **l);
                let rtype = analyze(sa, &mut **r);
                if ltype.is_nil() || ltype.is_bool() {
                    sa.make_err(&ast.pos, format!("Bool and Nil are not valid binary math operand types; Got type {:?} on the left", ltype));
                }
                if rtype.is_nil() || rtype.is_bool() {
                    sa.make_err(&ast.pos, format!("Bool and Nil are not valid binary math operand types; Got type {:?} on the right", rtype));
                }
                if ltype != rtype {
                    sa.make_err(
                        &ast.pos,
                        format!(
                            "Binary operands are not the same type; {:?} != {:?}",
                            ltype, rtype
                        ),
                    );
                }
                ltype
            }
        },
        ast::Ast::Unary(ref op, ref mut expr) => {
            let expr_type = analyze(sa, &mut **expr);
            match op {
                ast::UnaryOperation::Not => {
                    if expr_type != BOOL_TYPE_SIGNATURE {
                        sa.make_err(
                            &expr.pos,
                            format!("Unary not expression must evaluate to Bool"),
                        );
                    }
                    BOOL_TYPE_SIGNATURE.clone()
                }
                ast::UnaryOperation::Negate => {
                    if !expr_type.is_number() {
                        sa.make_err(
                            &expr.pos,
                            format!("Negate only supports primitive number types"),
                        );
                    }
                    expr_type
                }
            }
        }
        ast::Ast::Return(_) => {
            sa.make_err(
                &ast.pos,
                format!("Returns may only be present within function blocks"),
            );
            NIL_TYPE_SIGNATURE.clone()
        }
        ast::Ast::Block(ref mut exprs) => {
            sa.new_scope();
            let mut idx = 1;
            let len = exprs.len();
            let mut return_type: Option<ast::TypeSignature> = None;
            for expr in (&mut **exprs).iter_mut() {
                let mut e_error = false;
                match expr.node {
                    ast::Ast::Statement(ref mut e) => {
                        if let ast::Ast::Return(ref mut rexpr) = &mut e.node {
                            let ret = analyze(sa, rexpr);
                            if let Some(t) = &return_type {
                                if ret != *t {
                                    sa.make_err(
                                        &rexpr.pos,
                                        format!("Not the same return type as previous returns"),
                                    );
                                }
                            } else {
                                return_type = Some(ret);
                            }
                            if !sa.in_function_block {
                                sa.make_err(
                                    &rexpr.pos,
                                    format!("Returns not allowed outside of function blocks"),
                                );
                            }
                        } else {
                            analyze(sa, expr);
                        }
                    }
                    ref mut e => {
                        if idx != len {
                            e_error = true;
                        }
                        if let ast::Ast::Return(ref mut rexpr) = e {
                            let ret = analyze(sa, rexpr);
                            if let Some(t) = &return_type {
                                if ret != *t {
                                    sa.make_err(
                                        &rexpr.pos,
                                        format!("Not the same return type as previous returns"),
                                    );
                                }
                            } else {
                                return_type = Some(ret);
                            }
                            if !sa.in_function_block {
                                sa.make_err(
                                    &rexpr.pos,
                                    format!("Returns not allowed outside of function blocks"),
                                );
                            }
                        } else {
                            return_type = Some(analyze(sa, expr));
                        }
                    }
                }
                if e_error {
                    sa.make_err(
                        &expr.pos,
                        format!("Only the last element in a block can be an expression"),
                    );
                }
                idx += 1;
            }
            sa.pop_scope(&ast.pos);
            if let Some(r) = return_type {
                r
            } else {
                NIL_TYPE_SIGNATURE.clone()
            }
        }
        ast::Ast::IfElse(ref mut ifcond, ref mut ifexpr, ref mut elseifs, ref mut elseexpr) => {
            sa.new_scope();
            let ifcond_type = analyze(sa, &mut **ifcond);
            if ifcond_type != BOOL_TYPE_SIGNATURE {
                sa.make_err(
                    &ifcond.pos,
                    format!("If condition must evaluate to Bool; got {:?}", ifcond_type),
                );
            }
            let expr_type = analyze(sa, &mut **ifexpr);
            if expr_type != NIL_TYPE_SIGNATURE {
                if let None = elseexpr {
                    sa.make_err(
                        &ifcond.pos,
                        format!("If condition must have an else branch if a value is returned"),
                    );
                }
            }
            sa.pop_scope(&ifexpr.pos);
            for (eifc, eife) in elseifs {
                sa.new_scope();
                let eifc_type = analyze(sa, &mut **eifc);
                if eifc_type != BOOL_TYPE_SIGNATURE {
                    sa.make_err(
                        &eifc.pos,
                        format!(
                            "Else if condition must evaluate to Bool; got {:?}",
                            eifc_type
                        ),
                    );
                }
                let branch_type = analyze(sa, &mut **eife);
                if branch_type != expr_type {
                    sa.make_err(&eife.pos, format!("If branch doesn't have the same return type; expected {:?} but got {:?}", expr_type, branch_type));
                }
                sa.pop_scope(&eife.pos);
            }
            if let Some(eexpr) = elseexpr {
                let branch_type = analyze(sa, &mut **eexpr);
                if branch_type != expr_type {
                    sa.make_err(&eexpr.pos, format!("If branch doesn't have the same return type; expected {:?} but got {:?}", expr_type, branch_type));
                }
            }
            expr_type
        }
        ast::Ast::While(ref mut cond, ref mut expr) => {
            sa.new_scope();
            let cond_type = analyze(sa, &mut **cond);
            if cond_type != BOOL_TYPE_SIGNATURE {
                sa.make_err(
                    &cond.pos,
                    format!("While condition must evaluate to Bool; got {:?}", cond_type),
                );
            }
            let return_type = analyze(sa, &mut **expr);
            sa.pop_scope(&ast.pos);
            return_type
        }
        ast::Ast::Let(ref name, ref mut sig, ref mut expr) => {
            if let None = sa.check_if_var_in_scopes(&name) {
                let mut return_type = NIL_TYPE_SIGNATURE.clone();
                if let Some(type_sig) = &sig.type_sig {
                    if let Some(e) = expr {
                        let e_type = analyze(sa, &mut *e);
                        if *type_sig != e_type {
                            sa.make_err(&ast.pos, format!("Variable type and assign type do not match; expected {:?} but got {:?}", *type_sig, e_type));
                        }
                    }
                    sa.last_scope()
                        .variables
                        .insert(name.clone(), (sig.mutable, type_sig.clone()));
                    return_type = type_sig.clone();
                // println!("Variable {} of type {:?}, stated", sig.name, type_sig);
                } else {
                    if let Some(e) = expr {
                        let expr_type = analyze(sa, &mut *e);
                        // println!("Variable {} of type {:?}, infer", sig.name, expr_type);
                        sig.type_sig = Some(expr_type.clone());
                        return_type = expr_type.clone();
                        sa.last_scope()
                            .variables
                            .insert(name.clone(), (sig.mutable, expr_type));
                    } else {
                        sa.make_err(
                            &ast.pos,
                            format!("Cannot infer type without an assign expression"),
                        );
                        sa.last_scope()
                            .variables
                            .insert(name.clone(), (sig.mutable, NIL_TYPE_SIGNATURE.clone()));
                    }
                }
                return_type
            } else {
                sa.make_err(&ast.pos, format!("Variable {} already defined", name));
                NIL_TYPE_SIGNATURE.clone()
            }
        }
        ast::Ast::Import(ref name, ref mut expr) => {
            sa.file = Some(name.clone());
            analyze(sa, &mut **expr)
        }
        ast::Ast::FnDef(ref mut sig, ref param_names, ref mut expr) => {
            sa.new_scope();
            for (var, name) in sig.params.iter().zip(param_names.iter()) {
                if let Some(type_sig) = &var.type_sig {
                    if let None = sa.check_if_type_is_defined(type_sig) {
                        sa.make_err(&ast.pos, format!("Type {:?} is not defined", type_sig));
                    }
                    sa.last_scope()
                        .variables
                        .insert(name.clone(), (var.mutable, type_sig.clone()));
                } else {
                    sa.make_err(
                        &ast.pos,
                        format!(
                            "Function parameter types cannot be infered; Parameter: {}",
                            name
                        ),
                    );
                    sa.last_scope()
                        .variables
                        .insert(name.clone(), (var.mutable, NIL_TYPE_SIGNATURE.clone()));
                }
            }
            if let Some(type_sig) = &sig.return_type {
                if let None = sa.check_if_type_is_defined(type_sig) {
                    sa.make_err(&ast.pos, format!("Type {:?} is not defined", type_sig));
                }
                if let ast::Ast::Block(_) = &expr.node {
                    sa.in_function_block = true;
                }
                let expr_type = analyze(sa, &mut **expr);
                sa.in_function_block = false;
                if **type_sig != expr_type {
                    sa.make_err(
                        &expr.pos,
                        format!(
                            "Return types not the same; expected {:?} but got {:?}",
                            **type_sig, expr_type
                        ),
                    );
                }
            } else {
                sig.return_type = Some(Box::new(analyze(sa, &mut **expr)));
            }
            sa.pop_scope(&ast.pos);
            ast::TypeSignature::Function(sig.clone())
        }
        ast::Ast::FnCall(ref mut callee, ref mut args) => {
            let mut return_type: ast::TypeSignature = NIL_TYPE_SIGNATURE.clone();
            if let ast::TypeSignature::Function(sig) = analyze(sa, &mut **callee) {
                if sig.params.len() == args.len() {
                    for (param, arg) in sig.params.iter().zip(args.iter_mut()) {
                        if let Some(type_sig) = &param.type_sig {
                            let arg_return_type = analyze(sa, arg);
                            if *type_sig != arg_return_type {
                                sa.make_err(
                                    &arg.pos,
                                    format!(
                                        "Expected type {:?} but got type {:?}",
                                        type_sig, arg_return_type
                                    ),
                                );
                            }
                        } else {
                            unreachable!();
                        }
                    }
                    return_type = *sig.return_type.unwrap();
                } else {
                    sa.make_err(
                        &callee.pos,
                        format!(
                            "Function expected {} arguments but got {} arguments",
                            sig.params.len(),
                            args.len()
                        ),
                    );
                    return_type = NIL_TYPE_SIGNATURE.clone();
                }
            } else {
                sa.make_err(&callee.pos, format!("Not a callable expression"));
            }
            return_type
        }
        ast::Ast::As(ref mut expr, ref type_) => {
            let expr_type = analyze(sa, &mut **expr);
            if let None = sa.check_if_type_is_defined(&type_) {
                sa.make_err(&ast.pos, format!("Type {:?} is not defined", type_));
            }
            match (&expr_type, &type_) {
                (ast::TypeSignature::Primitive(_), ast::TypeSignature::Primitive(_)) => {
                    type_.clone()
                }
                (_, ast::TypeSignature::Primitive(_)) => {
                    sa.make_err(
                        &expr.pos,
                        format!(
                            "Can only cast between primtive types; Left of as is of type {:?}",
                            expr_type
                        ),
                    );
                    expr_type
                }
                (ast::TypeSignature::Primitive(_), _) => {
                    sa.make_err(
                        &ast.pos,
                        format!(
                            "Can only cast between primtive types; Right of as is of type {:?}",
                            type_
                        ),
                    );
                    expr_type
                }
                _ => {
                    sa.make_err(&ast.pos, format!("Can only cast between primtive types; {:?} and {:?} are not primitives", expr_type, type_));
                    expr_type
                }
            }
        }
    };
    ast.type_sig = Some(node_type.clone());
    node_type
}