mirror of
https://github.com/ton-blockchain/ton
synced 2025-02-12 19:22:37 +00:00
Now, the whole .tolk file can be loaded as AST tree and then converted to Expr/Op. This gives a great ability to implement AST transformations. In the future, more and more code analysis will be moved out of legacy to AST-level.
103 lines
2.6 KiB
C++
103 lines
2.6 KiB
C++
/*
|
|
This file is part of TON Blockchain Library.
|
|
|
|
TON Blockchain Library is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Lesser General Public License as published by
|
|
the Free Software Foundation, either version 2 of the License, or
|
|
(at your option) any later version.
|
|
|
|
TON Blockchain Library is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Lesser General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Lesser General Public License
|
|
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
#pragma once
|
|
|
|
#include "src-file.h"
|
|
#include <functional>
|
|
#include <memory>
|
|
|
|
namespace tolk {
|
|
|
|
typedef int var_idx_t;
|
|
typedef int sym_idx_t;
|
|
|
|
enum class SymValKind { _Param, _Var, _Func, _Typename, _GlobVar, _Const };
|
|
|
|
struct SymValBase {
|
|
SymValKind kind;
|
|
int idx;
|
|
SymValBase(SymValKind kind, int idx) : kind(kind), idx(idx) {
|
|
}
|
|
virtual ~SymValBase() = default;
|
|
};
|
|
|
|
|
|
struct Symbol {
|
|
std::string str;
|
|
sym_idx_t idx;
|
|
|
|
Symbol(std::string str, sym_idx_t idx) : str(std::move(str)), idx(idx) {}
|
|
|
|
static std::string unknown_symbol_name(sym_idx_t i);
|
|
};
|
|
|
|
class SymTable {
|
|
public:
|
|
static constexpr int SIZE_PRIME = 100003;
|
|
|
|
private:
|
|
sym_idx_t def_sym{0};
|
|
std::unique_ptr<Symbol> sym[SIZE_PRIME + 1];
|
|
sym_idx_t gen_lookup(std::string_view str, int mode = 0, sym_idx_t idx = 0);
|
|
|
|
public:
|
|
|
|
static constexpr sym_idx_t not_found = 0;
|
|
sym_idx_t lookup(std::string_view str, int mode = 0) {
|
|
return gen_lookup(str, mode);
|
|
}
|
|
sym_idx_t lookup_add(std::string_view str) {
|
|
return gen_lookup(str, 1);
|
|
}
|
|
Symbol* operator[](sym_idx_t i) const {
|
|
return sym[i].get();
|
|
}
|
|
std::string get_name(sym_idx_t i) const {
|
|
return sym[i] ? sym[i]->str : Symbol::unknown_symbol_name(i);
|
|
}
|
|
};
|
|
|
|
struct SymTableOverflow {
|
|
int sym_def;
|
|
explicit SymTableOverflow(int x) : sym_def(x) {
|
|
}
|
|
};
|
|
|
|
|
|
struct SymDef {
|
|
int level;
|
|
sym_idx_t sym_idx;
|
|
SymValBase* value;
|
|
SrcLocation loc;
|
|
#ifdef TOLK_DEBUG
|
|
std::string sym_name;
|
|
#endif
|
|
SymDef(int lvl, sym_idx_t idx, SrcLocation _loc, SymValBase* val = nullptr)
|
|
: level(lvl), sym_idx(idx), value(val), loc(_loc) {
|
|
}
|
|
std::string name() const;
|
|
};
|
|
|
|
|
|
void open_scope(SrcLocation loc);
|
|
void close_scope();
|
|
SymDef* lookup_symbol(sym_idx_t idx);
|
|
|
|
SymDef* define_global_symbol(sym_idx_t name_idx, bool force_new = false, SrcLocation loc = {});
|
|
SymDef* define_symbol(sym_idx_t name_idx, bool force_new, SrcLocation loc);
|
|
|
|
} // namespace tolk
|