semantic-analyzer 0.4.6

Semantic analyzer library for compilers written in Rust for semantic analysis of programming languages AST
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
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
use crate::utils::{CustomExpression, CustomExpressionInstruction, SemanticTest};
use semantic_analyzer::ast;
use semantic_analyzer::ast::Ident;
use semantic_analyzer::types::block_state::BlockState;
use semantic_analyzer::types::condition::LoopBodyStatement;
use semantic_analyzer::types::error::StateErrorKind;
use semantic_analyzer::types::expression::{ExpressionResult, ExpressionResultValue};
use semantic_analyzer::types::semantic::SemanticStackContext;
use semantic_analyzer::types::types::{PrimitiveTypes, Type};
use semantic_analyzer::types::{Function, PrimitiveValue, Value};
use std::cell::RefCell;
use std::rc::Rc;

mod utils;

#[test]
fn loop_transform() {
    let let_binding = ast::LetBinding {
        name: ast::ValueName::new(Ident::new("x")),
        mutable: false,
        value_type: None,
        value: Box::new(ast::Expression {
            expression_value: ast::ExpressionValue::<
                CustomExpressionInstruction,
                CustomExpression<CustomExpressionInstruction>,
            >::PrimitiveValue(ast::PrimitiveValue::Bool(true)),
            operation: None,
        }),
    };
    let binding = ast::Binding {
        name: ast::ValueName::new(Ident::new("x")),
        value: Box::new(ast::Expression {
            expression_value: ast::ExpressionValue::PrimitiveValue(ast::PrimitiveValue::Bool(true)),
            operation: None,
        }),
    };
    let fn_call = ast::FunctionCall {
        name: ast::FunctionName::new(Ident::new("fn1")),
        parameters: vec![],
    };
    let if_statement = ast::IfStatement {
        condition: ast::IfCondition::Single(ast::Expression {
            expression_value: ast::ExpressionValue::PrimitiveValue(ast::PrimitiveValue::F32(1.2)),
            operation: None,
        }),
        body: ast::IfBodyStatements::If(vec![]),
        else_statement: None,
        else_if_statement: None,
    };
    let loop_statement = ast::LoopBodyStatement::Break;
    let return_statement = ast::Expression {
        expression_value: ast::ExpressionValue::PrimitiveValue(ast::PrimitiveValue::F32(1.2)),
        operation: None,
    };
    let loop_stmts = vec![
        ast::LoopBodyStatement::LetBinding(let_binding.clone()),
        ast::LoopBodyStatement::Binding(binding.clone()),
        ast::LoopBodyStatement::FunctionCall(fn_call.clone()),
        ast::LoopBodyStatement::If(if_statement.clone()),
        ast::LoopBodyStatement::Loop(vec![loop_statement.clone()]),
        ast::LoopBodyStatement::Return(return_statement.clone()),
        ast::LoopBodyStatement::Break,
        ast::LoopBodyStatement::Continue,
    ];
    // For grcov
    let _ = format!("{loop_stmts:#?}");
    for loop_stmt in loop_stmts {
        let loop_stmt_into: LoopBodyStatement = loop_stmt.into();
        // For grcov
        let _ = format!("{loop_stmt_into:#?}");
        match loop_stmt_into {
            LoopBodyStatement::LetBinding(val) => assert_eq!(val, let_binding.clone().into()),
            LoopBodyStatement::Binding(val) => assert_eq!(val, binding.clone().into()),
            LoopBodyStatement::FunctionCall(val) => assert_eq!(val, fn_call.clone().into()),
            LoopBodyStatement::If(val) => assert_eq!(val, if_statement.clone().into()),
            LoopBodyStatement::Loop(val) => assert_eq!(val, vec![loop_statement.clone().into()]),
            LoopBodyStatement::Return(val) => assert_eq!(val, return_statement.clone().into()),
            LoopBodyStatement::Break => assert_eq!(
                LoopBodyStatement::Break,
                ast::LoopBodyStatement::<
                    CustomExpressionInstruction,
                    CustomExpression<CustomExpressionInstruction>,
                >::Break
                    .into()
            ),
            LoopBodyStatement::Continue => assert_eq!(
                LoopBodyStatement::Continue,
                ast::LoopBodyStatement::<
                    CustomExpressionInstruction,
                    CustomExpression<CustomExpressionInstruction>,
                >::Continue
                    .into()
            ),
        }
    }
}

#[test]
fn loop_statements() {
    let block_state = Rc::new(RefCell::new(BlockState::new(None)));
    let mut t = SemanticTest::new();

    let fn2 = ast::FunctionStatement::new(
        ast::FunctionName::new(Ident::new("fn2")),
        vec![],
        ast::Type::Primitive(ast::PrimitiveTypes::U16),
        vec![ast::BodyStatement::Expression(ast::Expression {
            expression_value: ast::ExpressionValue::PrimitiveValue(ast::PrimitiveValue::U16(23)),
            operation: None,
        })],
    );
    t.state.function_declaration(&fn2);

    let loop_body_let_binding = ast::LoopBodyStatement::LetBinding(ast::LetBinding {
        name: ast::ValueName::new(Ident::new("x")),
        mutable: true,
        value_type: None,
        value: Box::new(ast::Expression {
            expression_value: ast::ExpressionValue::PrimitiveValue(ast::PrimitiveValue::Bool(
                false,
            )),
            operation: None,
        }),
    });
    let loop_body_binding = ast::LoopBodyStatement::Binding(ast::Binding {
        name: ast::ValueName::new(Ident::new("x")),
        value: Box::new(ast::Expression {
            expression_value: ast::ExpressionValue::PrimitiveValue(ast::PrimitiveValue::Bool(true)),
            operation: None,
        }),
    });
    let loop_body_fn_call = ast::LoopBodyStatement::FunctionCall(ast::FunctionCall {
        name: ast::FunctionName::new(Ident::new("fn2")),
        parameters: vec![],
    });
    let loop_body_if = ast::LoopBodyStatement::If(ast::IfStatement {
        condition: ast::IfCondition::Single(ast::Expression {
            expression_value: ast::ExpressionValue::PrimitiveValue(ast::PrimitiveValue::Bool(true)),
            operation: None,
        }),
        body: ast::IfBodyStatements::Loop(vec![ast::IfLoopBodyStatement::FunctionCall(
            ast::FunctionCall {
                name: ast::FunctionName::new(Ident::new("fn2")),
                parameters: vec![],
            },
        )]),
        else_statement: None,
        else_if_statement: None,
    });
    let loop_body_loop = ast::LoopBodyStatement::Loop(vec![ast::LoopBodyStatement::FunctionCall(
        ast::FunctionCall {
            name: ast::FunctionName::new(Ident::new("fn2")),
            parameters: vec![],
        },
    )]);

    let loop_stmt = [
        loop_body_let_binding,
        loop_body_binding,
        loop_body_fn_call,
        loop_body_if,
        loop_body_loop,
    ];
    t.state.loop_statement(&loop_stmt, &block_state);

    assert!(t.is_empty_error());
    let main_ctx = block_state.borrow().get_context().get();
    assert_eq!(main_ctx.len(), 17);
    assert!(block_state.borrow().parent.is_none());
    assert!(block_state.borrow().parent.is_none());
    assert_eq!(block_state.borrow().children.len(), 1);

    let ctx = block_state.borrow().children[0].clone();
    assert!(ctx.borrow().parent.is_some());
    assert_eq!(ctx.borrow().children.len(), 2);

    let ch_ctx1 = ctx.borrow().children[0].clone();
    assert!(ch_ctx1.borrow().parent.is_some());
    assert!(ch_ctx1.borrow().children.is_empty());

    let ctx1 = ch_ctx1.borrow().get_context().clone().get();
    assert_eq!(ctx1.len(), 5);
    assert_eq!(
        ctx1[0],
        SemanticStackContext::IfConditionExpression {
            expr_result: ExpressionResult {
                expr_type: Type::Primitive(PrimitiveTypes::Bool),
                expr_value: ExpressionResultValue::PrimitiveValue(PrimitiveValue::Bool(true)),
            },
            label_if_begin: String::from("if_begin").into(),
            label_if_end: String::from("if_end").into(),
        }
    );
    assert_eq!(
        ctx1[1],
        SemanticStackContext::SetLabel {
            label: String::from("if_begin").into()
        }
    );
    assert_eq!(
        ctx1[2],
        SemanticStackContext::Call {
            call: Function {
                inner_name: String::from("fn2").into(),
                inner_type: Type::Primitive(PrimitiveTypes::U16),
                parameters: vec![],
            },
            params: vec![],
            register_number: 2,
        }
    );
    assert_eq!(
        ctx1[3],
        SemanticStackContext::JumpTo {
            label: String::from("if_end").into()
        }
    );
    assert_eq!(
        ctx1[4],
        SemanticStackContext::SetLabel {
            label: String::from("if_end").into()
        }
    );

    let ch_ctx2 = ctx.borrow().children[1].clone();
    assert!(ch_ctx2.borrow().parent.is_some());
    assert!(ch_ctx2.borrow().children.is_empty());

    let ctx2 = ch_ctx2.borrow().get_context().clone().get();
    assert_eq!(ctx2.len(), 5);
    assert_eq!(
        ctx2[0],
        SemanticStackContext::JumpTo {
            label: String::from("loop_begin.0").into()
        }
    );
    assert_eq!(
        ctx2[1],
        SemanticStackContext::SetLabel {
            label: String::from("loop_begin.0").into()
        }
    );
    assert_eq!(
        ctx2[2],
        SemanticStackContext::Call {
            call: Function {
                inner_name: String::from("fn2").into(),
                inner_type: Type::Primitive(PrimitiveTypes::U16),
                parameters: vec![],
            },
            params: vec![],
            register_number: 3,
        }
    );
    assert_eq!(
        ctx2[3],
        SemanticStackContext::JumpTo {
            label: String::from("loop_begin.0").into()
        }
    );
    assert_eq!(
        ctx2[4],
        SemanticStackContext::SetLabel {
            label: String::from("loop_end.0").into()
        }
    );

    let stm_ctx = ctx.borrow().get_context().clone().get();
    assert_eq!(stm_ctx.len(), 17);
    assert_eq!(stm_ctx, main_ctx);
    assert_eq!(
        stm_ctx[0],
        SemanticStackContext::JumpTo {
            label: String::from("loop_begin").into()
        }
    );
    assert_eq!(
        stm_ctx[1],
        SemanticStackContext::SetLabel {
            label: String::from("loop_begin").into()
        }
    );
    assert_eq!(
        stm_ctx[2],
        SemanticStackContext::LetBinding {
            let_decl: Value {
                inner_name: "x.0".into(),
                inner_type: Type::Primitive(PrimitiveTypes::Bool),
                mutable: true,
                alloca: false,
                malloc: false,
            },
            expr_result: ExpressionResult {
                expr_type: Type::Primitive(PrimitiveTypes::Bool),
                expr_value: ExpressionResultValue::PrimitiveValue(PrimitiveValue::Bool(false)),
            },
        }
    );
    assert_eq!(
        stm_ctx[3],
        SemanticStackContext::Binding {
            val: Value {
                inner_name: "x.0".into(),
                inner_type: Type::Primitive(PrimitiveTypes::Bool),
                mutable: true,
                alloca: false,
                malloc: false,
            },
            expr_result: ExpressionResult {
                expr_type: Type::Primitive(PrimitiveTypes::Bool),
                expr_value: ExpressionResultValue::PrimitiveValue(PrimitiveValue::Bool(true)),
            },
        }
    );
    assert_eq!(
        stm_ctx[4],
        SemanticStackContext::Call {
            call: Function {
                inner_name: String::from("fn2").into(),
                inner_type: Type::Primitive(PrimitiveTypes::U16),
                parameters: vec![],
            },
            params: vec![],
            register_number: 1,
        }
    );
    assert_eq!(stm_ctx[5], ctx1[0]);
    assert_eq!(stm_ctx[6], ctx1[1]);
    assert_eq!(stm_ctx[7], ctx1[2]);
    assert_eq!(stm_ctx[8], ctx1[3]);
    assert_eq!(stm_ctx[9], ctx1[4]);
    assert_eq!(stm_ctx[10], ctx2[0]);
    assert_eq!(stm_ctx[11], ctx2[1]);
    assert_eq!(stm_ctx[12], ctx2[2]);
    assert_eq!(stm_ctx[13], ctx2[3]);
    assert_eq!(stm_ctx[14], ctx2[4]);
    assert_eq!(
        stm_ctx[15],
        SemanticStackContext::JumpTo {
            label: String::from("loop_begin").into()
        }
    );
    assert_eq!(
        stm_ctx[16],
        SemanticStackContext::SetLabel {
            label: String::from("loop_end").into()
        }
    );
}

#[test]
fn loop_statements_instructions_after_return() {
    let block_state = Rc::new(RefCell::new(BlockState::new(None)));
    let mut t = SemanticTest::new();

    let loop_body_let_binding = ast::LoopBodyStatement::LetBinding(ast::LetBinding {
        name: ast::ValueName::new(Ident::new("x")),
        mutable: true,
        value_type: None,
        value: Box::new(ast::Expression {
            expression_value: ast::ExpressionValue::PrimitiveValue(ast::PrimitiveValue::Bool(
                false,
            )),
            operation: None,
        }),
    });
    let loop_body_return = ast::LoopBodyStatement::Return(ast::Expression {
        expression_value: ast::ExpressionValue::PrimitiveValue(ast::PrimitiveValue::Bool(true)),
        operation: None,
    });

    let loop_stmt = [loop_body_return, loop_body_let_binding];
    t.state.loop_statement(&loop_stmt, &block_state);
    assert!(t.check_errors_len(1), "Errors: {:?}", t.state.errors.len());
    assert!(
        t.check_error(StateErrorKind::ForbiddenCodeAfterReturnDeprecated),
        "Errors: {:?}",
        t.state.errors[0]
    );
}

#[test]
fn loop_statements_instructions_after_break() {
    let block_state = Rc::new(RefCell::new(BlockState::new(None)));
    let mut t = SemanticTest::new();

    let loop_body_let_binding = ast::LoopBodyStatement::LetBinding(ast::LetBinding {
        name: ast::ValueName::new(Ident::new("x")),
        mutable: true,
        value_type: None,
        value: Box::new(ast::Expression {
            expression_value: ast::ExpressionValue::PrimitiveValue(ast::PrimitiveValue::Bool(
                false,
            )),
            operation: None,
        }),
    });
    let loop_body_break = ast::LoopBodyStatement::Break;

    let loop_stmt = [loop_body_break, loop_body_let_binding];
    t.state.loop_statement(&loop_stmt, &block_state);
    assert!(t.check_errors_len(1), "Errors: {:?}", t.state.errors.len());
    assert!(
        t.check_error(StateErrorKind::ForbiddenCodeAfterBreakDeprecated),
        "Errors: {:?}",
        t.state.errors[0]
    );
}

#[test]
fn loop_statements_instructions_after_continue() {
    let block_state = Rc::new(RefCell::new(BlockState::new(None)));
    let mut t = SemanticTest::new();

    let loop_body_let_binding = ast::LoopBodyStatement::LetBinding(ast::LetBinding {
        name: ast::ValueName::new(Ident::new("x")),
        mutable: true,
        value_type: None,
        value: Box::new(ast::Expression {
            expression_value: ast::ExpressionValue::PrimitiveValue(ast::PrimitiveValue::Bool(
                false,
            )),
            operation: None,
        }),
    });
    let loop_body_continue = ast::LoopBodyStatement::Continue;

    let loop_stmt = [loop_body_continue, loop_body_let_binding];
    t.state.loop_statement(&loop_stmt, &block_state);
    assert!(t.check_errors_len(1), "Errors: {:?}", t.state.errors.len());
    assert!(
        t.check_error(StateErrorKind::ForbiddenCodeAfterContinueDeprecated),
        "Errors: {:?}",
        t.state.errors[0]
    );
}

#[test]
fn loop_statements_with_return_invocation() {
    let block_state = Rc::new(RefCell::new(BlockState::new(None)));
    let mut t = SemanticTest::new();

    let loop_body_let_binding = ast::LoopBodyStatement::LetBinding(ast::LetBinding {
        name: ast::ValueName::new(Ident::new("x")),
        mutable: true,
        value_type: None,
        value: Box::new(ast::Expression {
            expression_value: ast::ExpressionValue::PrimitiveValue(ast::PrimitiveValue::Bool(
                false,
            )),
            operation: None,
        }),
    });
    let loop_body_return = ast::LoopBodyStatement::Return(ast::Expression {
        expression_value: ast::ExpressionValue::PrimitiveValue(ast::PrimitiveValue::Bool(true)),
        operation: None,
    });

    let loop_stmt = [loop_body_let_binding, loop_body_return];
    t.state.loop_statement(&loop_stmt, &block_state);

    let main_ctx = block_state.borrow().get_context().get();
    assert_eq!(main_ctx.len(), 4);
    assert!(block_state.borrow().parent.is_none());
    assert!(block_state.borrow().parent.is_none());
    assert_eq!(block_state.borrow().children.len(), 1);

    let ctx = block_state.borrow().children[0].clone();
    assert!(ctx.borrow().parent.is_some());
    assert!(ctx.borrow().children.is_empty());

    let stm_ctx = ctx.borrow().get_context().clone().get();
    assert_eq!(stm_ctx.len(), 4);
    assert_eq!(stm_ctx, main_ctx);
    assert_eq!(
        stm_ctx[0],
        SemanticStackContext::JumpTo {
            label: String::from("loop_begin").into()
        }
    );
    assert_eq!(
        stm_ctx[1],
        SemanticStackContext::SetLabel {
            label: String::from("loop_begin").into()
        }
    );
    assert_eq!(
        stm_ctx[2],
        SemanticStackContext::LetBinding {
            let_decl: Value {
                inner_name: "x.0".into(),
                inner_type: Type::Primitive(PrimitiveTypes::Bool),
                mutable: true,
                alloca: false,
                malloc: false,
            },
            expr_result: ExpressionResult {
                expr_type: Type::Primitive(PrimitiveTypes::Bool),
                expr_value: ExpressionResultValue::PrimitiveValue(PrimitiveValue::Bool(false)),
            },
        }
    );
    assert_eq!(
        stm_ctx[3],
        SemanticStackContext::JumpFunctionReturn {
            expr_result: ExpressionResult {
                expr_type: Type::Primitive(PrimitiveTypes::Bool),
                expr_value: ExpressionResultValue::PrimitiveValue(PrimitiveValue::Bool(true)),
            }
        }
    );

    assert!(t.is_empty_error());
}