fibonacci works again
[ozulis.git] / src / ast / scope-builder-visitor.cc
blob40c57502b9763886716c9dfcc45c3cc5051c652d
1 #include <boost/foreach.hpp>
3 #include <core/assert.hh>
4 #include "scope-builder-visitor.hh"
5 #include "scope.hh"
7 namespace ast
9 ScopeBuilderVisitor::ScopeBuilderVisitor()
10 : parent_(0)
14 ScopeBuilderVisitor::~ScopeBuilderVisitor()
18 void
19 ScopeBuilderVisitor::visit(File & node)
21 node.scope = new Scope();
22 node.scope->setParent(parent_);
23 parent_ = node.scope;
24 super_t::visit(node);
25 parent_ = node.scope->parent();
28 void
29 ScopeBuilderVisitor::visit(Function & node)
31 assert(parent_);
32 assert(node.block);
33 assert(node.args);
35 FunctionType *ftype = new FunctionType;
36 ftype->returnType = node.returnType;
37 ftype->argsType = new std::vector<Type *> ();
38 BOOST_FOREACH (VarDecl * varDecl, *node.args)
39 ftype->argsType->push_back(varDecl->type);
41 Symbol * symbol = new Symbol;
42 symbol->name = node.name;
43 symbol->type = ftype;
44 symbol->address = new MemoryAddress;
45 parent_->addSymbol(symbol);
47 node.scope = new Scope;
48 node.scope->setParent(parent_);
50 BOOST_FOREACH (VarDecl * varDecl, *node.args)
52 symbol = new Symbol;
54 assert(varDecl);
55 symbol->name = varDecl->name;
56 symbol->type = varDecl->type;
57 symbol->address = new MemoryAddress;
58 node.scope->addSymbol(symbol);
61 parent_ = node.scope;
62 node.block->accept(*this);
63 parent_ = node.scope->parent();
66 void
67 ScopeBuilderVisitor::visit(Block & node)
69 assert(parent_);
70 node.scope = new Scope();
71 node.scope->setParent(parent_);
72 parent_ = node.scope;
74 /// @todo fill the scope with the declarated variables
75 assert(node.varDecls);
76 BOOST_FOREACH (VarDecl * varDecl, (*node.varDecls))
78 Symbol * symbol = new Symbol;
80 assert(varDecl);
81 symbol->name = varDecl->name;
82 symbol->type = varDecl->type;
83 symbol->address = new MemoryAddress;
84 node.scope->addSymbol(symbol);
87 super_t::visit(node);
88 parent_ = node.scope->parent();