forked from lcompilers/lpython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_ast.cpp
More file actions
59 lines (42 loc) · 1.43 KB
/
test_ast.cpp
File metadata and controls
59 lines (42 loc) · 1.43 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
#include <tests/doctest.h>
#include <iostream>
#include <libasr/alloc.h>
#include <lpython/ast.h>
#include <libasr/asr.h>
#include <lpython/parser/parser.h>
#include <lpython/semantics/ast_to_asr.h>
#include <libasr/asr_verify.h>
namespace LFortran {
TEST_CASE("Test types") {
Allocator al(1024*1024);
Location loc;
AST::ast_t &a = *AST::make_Num_t(al, loc, 5, nullptr);
CHECK(AST::is_a<AST::expr_t>(a));
CHECK(! AST::is_a<AST::stmt_t>(a));
AST::Num_t &x = *AST::down_cast2<AST::Num_t>(&a);
CHECK(x.m_n == 5);
}
TEST_CASE("ASR Verify") {
Allocator al(4*1024);
std::string src = R"""(
program expr2
implicit none
integer :: x
x = (2+3)*5
print *, x
end program
)""";
LFortran::diag::Diagnostics diagnostics;
AST::TranslationUnit_t* ast = TRY(LFortran::parse(al, src, diagnostics));
ASR::TranslationUnit_t* asr = TRY(LFortran::ast_to_asr(al, *ast,
diagnostics));
CHECK(asr_verify(*asr)); // Passes
// Extract the variable "x" from the "x = (2+3)*5" line:
ASR::Program_t *prog = ASR::down_cast<ASR::Program_t>(asr->m_global_scope->scope["expr2"]);
ASR::Assignment_t *a = ASR::down_cast<ASR::Assignment_t>(prog->m_body[0]);
ASR::Var_t *v = ASR::down_cast<ASR::Var_t>(a->m_target);
v->m_v = &(prog->base); // Assign the wrong symbol to Var_t::m_v
// This will be caught by the verifier
CHECK_THROWS_AS(asr_verify(*asr), LFortranException);
}
} // namespace LFortran