Skip to content

Commit

Permalink
Merge branch 'symbol' into 'master'
Browse files Browse the repository at this point in the history
Make ast_to_asr to accept a TranslationUnit

See merge request lfortran/lfortran!400
  • Loading branch information
certik committed Jul 8, 2020
2 parents a4b4f55 + 41af3bd commit a079a97
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/bin/cpptranslate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ int emit_asr(const std::string &infile)
LFortran::ASR::asr_t* asr;
try {
// FIXME: For now we only transform the first node in the list:
asr = LFortran::ast_to_asr(al, *ast->m_items[0]);
asr = LFortran::ast_to_asr(al, *ast);
} catch (const LFortran::LFortranException &e) {
std::cerr << "LFortran exception: " << e.msg() << std::endl;
return 4;
Expand Down
6 changes: 3 additions & 3 deletions src/bin/lfortran.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ int prompt()
// AST -> ASR
LFortran::ASR::asr_t* asr;
try {
asr = LFortran::ast_to_asr(al, *ast->m_items[0]);
asr = LFortran::ast_to_asr(al, *ast);
} catch (const LFortran::LFortranException &e) {
std::cout << "LFortran exception: " << e.msg() << std::endl;
continue;
Expand Down Expand Up @@ -194,7 +194,7 @@ int emit_asr(const std::string &infile)
LFortran::ASR::asr_t* asr;
try {
// FIXME: For now we only transform the first node in the list:
asr = LFortran::ast_to_asr(al, *ast->m_items[0]);
asr = LFortran::ast_to_asr(al, *ast);
} catch (const LFortran::LFortranException &e) {
std::cerr << "LFortran exception: " << e.msg() << std::endl;
return 4;
Expand Down Expand Up @@ -229,7 +229,7 @@ int emit_llvm(const std::string &infile)
LFortran::ASR::asr_t* asr;
try {
// FIXME: For now we only transform the first node in the list:
asr = LFortran::ast_to_asr(al, *ast->m_items[0]);
asr = LFortran::ast_to_asr(al, *ast);
} catch (const LFortran::LFortranException &e) {
std::cerr << "LFortran exception: " << e.msg() << std::endl;
return 4;
Expand Down
19 changes: 16 additions & 3 deletions src/lfortran/semantics/ast_to_asr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ class SymbolTableVisitor : public AST::BaseVisitor<SymbolTableVisitor>
ASR::asr_t *asr;
Allocator &al;
SymbolTableVisitor(Allocator &al) : al{al} {}

void visit_TranslationUnit(const AST::TranslationUnit_t &x) {
for (size_t i=0; i<x.n_items; i++) {
visit_ast(*x.m_items[i]);
}
}

void visit_Function(const AST::Function_t &x) {
ASR::ttype_t *type = TYPE(ASR::make_Integer_t(al, x.base.base.loc,
8, nullptr, 0));
Expand All @@ -56,6 +63,12 @@ class BodyVisitor : public AST::BaseVisitor<BodyVisitor>
ASR::asr_t *asr, *tmp;
BodyVisitor(Allocator &al, ASR::asr_t *unit) : al{al}, asr{unit} {}

void visit_TranslationUnit(const AST::TranslationUnit_t &x) {
for (size_t i=0; i<x.n_items; i++) {
visit_ast(*x.m_items[i]);
}
}

void visit_Function(const AST::Function_t &x) {
Vec<ASR::stmt_t*> body;
body.reserve(al, 8);
Expand Down Expand Up @@ -93,14 +106,14 @@ class BodyVisitor : public AST::BaseVisitor<BodyVisitor>
}
};

ASR::asr_t *ast_to_asr(Allocator &al, AST::ast_t &ast)
ASR::asr_t *ast_to_asr(Allocator &al, AST::TranslationUnit_t &ast)
{
SymbolTableVisitor v(al);
v.visit_ast(ast);
v.visit_TranslationUnit(ast);
ASR::asr_t *unit = v.asr;

BodyVisitor b(al, unit);
b.visit_ast(ast);
b.visit_TranslationUnit(ast);
return unit;
}

Expand Down
2 changes: 1 addition & 1 deletion src/lfortran/semantics/ast_to_asr.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace LFortran {

ASR::asr_t *ast_to_asr(Allocator &al, AST::ast_t &ast);
ASR::asr_t *ast_to_asr(Allocator &al, AST::TranslationUnit_t &ast);

} // namespace LFortran

Expand Down
4 changes: 2 additions & 2 deletions src/lfortran/tests/test_llvm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ end function)";
CHECK(LFortran::pickle(*ast) == "(Function f [] () () () [] [(Declaration [(f \"integer\" [] [] ())])] [(= f 5)] [])");

// AST -> ASR
LFortran::ASR::asr_t* asr = LFortran::ast_to_asr(al, *ast);
LFortran::ASR::asr_t* asr = LFortran::ast_to_asr(al, *tu);
CHECK(LFortran::pickle(*asr) == "(Function f [] [(= (Variable f () 1 (Integer 8 [])) (Num Unimplementedobject (Integer 8 [])))] () (Variable f () 1 (Integer 8 [])) () Unimplementedobject)");

// ASR -> LLVM
Expand Down Expand Up @@ -382,7 +382,7 @@ end function)";
CHECK(LFortran::pickle(*ast) == "(Function f [] () () () [] [(Declaration [(f \"integer\" [] [] ())])] [(= f 4)] [])");

// AST -> ASR
LFortran::ASR::asr_t* asr = LFortran::ast_to_asr(al, *ast);
LFortran::ASR::asr_t* asr = LFortran::ast_to_asr(al, *tu);
CHECK(LFortran::pickle(*asr) == "(Function f [] [(= (Variable f () 1 (Integer 8 [])) (Num Unimplementedobject (Integer 8 [])))] () (Variable f () 1 (Integer 8 [])) () Unimplementedobject)");

// ASR -> LLVM
Expand Down

0 comments on commit a079a97

Please sign in to comment.