Files
BlitzNext/compiler/lib/environ.cpp
T

92 lines
1.8 KiB
C++
Raw Normal View History

2019-01-18 15:55:51 +01:00
#include "environ.hpp"
2019-01-19 18:28:07 +01:00
#include "decl.hpp"
#include "label.hpp"
#include "type.hpp"
2014-01-31 08:23:00 +13:00
2019-01-19 18:28:07 +01:00
Environ::Environ(const std::string& f, Type* r, int l, Environ* gs) : funcLabel(f), returnType(r), level(l), globals(gs)
2019-01-18 17:04:57 +01:00
{
decls = new DeclSeq();
typeDecls = new DeclSeq();
funcDecls = new DeclSeq();
if (globals)
globals->children.push_back(this);
2014-01-31 08:23:00 +13:00
}
2019-01-18 17:04:57 +01:00
Environ::~Environ()
{
if (globals)
globals->children.remove(this);
while (children.size())
delete children.back();
for (; labels.size(); labels.pop_back())
delete labels.back();
2014-01-31 08:23:00 +13:00
//delete all types
delete decls;
delete funcDecls;
delete typeDecls;
2019-01-18 17:04:57 +01:00
for (int k = 0; k < types.size(); ++k)
delete types[k];
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
Decl* Environ::findDecl(const std::string& s)
2019-01-18 17:04:57 +01:00
{
for (Environ* e = this; e; e = e->globals) {
if (Decl* d = e->decls->findDecl(s)) {
if (d->kind & (DECL_LOCAL | DECL_PARAM)) {
if (e == this)
return d;
} else
return d;
2014-01-31 08:23:00 +13:00
}
}
return 0;
}
2019-01-19 18:28:07 +01:00
Decl* Environ::findFunc(const std::string& s)
2019-01-18 17:04:57 +01:00
{
for (Environ* e = this; e; e = e->globals) {
if (Decl* d = e->funcDecls->findDecl(s))
return d;
2014-01-31 08:23:00 +13:00
}
return 0;
}
2019-01-19 18:28:07 +01:00
Type* Environ::findType(const std::string& s)
2019-01-18 17:04:57 +01:00
{
if (s == "%")
return Type::int_type;
if (s == "#")
return Type::float_type;
if (s == "$")
return Type::string_type;
for (Environ* e = this; e; e = e->globals) {
if (Decl* d = e->typeDecls->findDecl(s))
return d->type->structType();
2014-01-31 08:23:00 +13:00
}
return 0;
}
2019-01-19 18:28:07 +01:00
Label* Environ::findLabel(const std::string& s)
2019-01-18 17:04:57 +01:00
{
for (int k = 0; k < labels.size(); ++k)
if (labels[k]->name == s)
return labels[k];
2014-01-31 08:23:00 +13:00
return 0;
}
2019-01-19 18:28:07 +01:00
Label* Environ::insertLabel(const std::string& s, int def, int src, int sz)
2019-01-18 17:04:57 +01:00
{
Label* l = new Label(s, def, src, sz);
labels.push_back(l);
return l;
2014-01-31 08:23:00 +13:00
}
2019-01-19 18:28:07 +01:00
std::string Environ::setBreak(const std::string& s)
2019-01-18 17:04:57 +01:00
{
2019-01-19 18:28:07 +01:00
std::string t = breakLabel;
2019-01-18 17:04:57 +01:00
breakLabel = s;
return t;
2014-01-31 08:23:00 +13:00
}