mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
[Tolk] Embedded stdlib.tolk, CompilerState, strict includes
Several related changes: - stdlib.tolk is embedded into a distribution (deb package or tolk-js), the user won't have to download it and store as a project file; it's an important step to maintain correct language versioning - stdlib.tolk is auto-included, that's why all its functions are available out of the box - strict includes: you can't use symbol `f` from another file unless you've #include'd this file - drop all C++ global variables holding compilation state, merge them into a single struct CompilerState located at compiler-state.h; for instance, stdlib filename is also there
This commit is contained in:
parent
f0e6470d0b
commit
6c30e5a7eb
21 changed files with 604 additions and 506 deletions
|
@ -15,20 +15,15 @@
|
|||
along with TON Blockchain Library. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#pragma once
|
||||
|
||||
#include "src-file.h"
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
namespace tolk {
|
||||
|
||||
/*
|
||||
*
|
||||
* SYMBOL VALUES (DECLARED)
|
||||
*
|
||||
*/
|
||||
|
||||
typedef int var_idx_t;
|
||||
typedef int sym_idx_t;
|
||||
|
||||
enum class SymValKind { _Param, _Var, _Func, _Typename, _GlobVar, _Const };
|
||||
|
||||
|
@ -40,11 +35,6 @@ struct SymValBase {
|
|||
virtual ~SymValBase() = default;
|
||||
};
|
||||
|
||||
/*
|
||||
*
|
||||
* SYMBOL TABLE
|
||||
*
|
||||
*/
|
||||
|
||||
enum class SymbolSubclass {
|
||||
undef = 0,
|
||||
|
@ -52,8 +42,6 @@ enum class SymbolSubclass {
|
|||
tilde_identifier = 2 // begins with ~ (a non-const method)
|
||||
};
|
||||
|
||||
typedef int sym_idx_t;
|
||||
|
||||
struct Symbol {
|
||||
std::string str;
|
||||
sym_idx_t idx;
|
||||
|
@ -73,9 +61,6 @@ private:
|
|||
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);
|
||||
|
||||
static constexpr int max_kw_idx = 10000;
|
||||
sym_idx_t keywords[max_kw_idx];
|
||||
|
||||
public:
|
||||
|
||||
static constexpr sym_idx_t not_found = 0;
|
||||
|
@ -88,22 +73,12 @@ public:
|
|||
Symbol* operator[](sym_idx_t i) const {
|
||||
return sym[i].get();
|
||||
}
|
||||
bool is_keyword(sym_idx_t i) const {
|
||||
return sym[i] && sym[i]->idx < 0;
|
||||
}
|
||||
std::string get_name(sym_idx_t i) const {
|
||||
return sym[i] ? sym[i]->str : Symbol::unknown_symbol_name(i);
|
||||
}
|
||||
SymbolSubclass get_subclass(sym_idx_t i) const {
|
||||
return sym[i] ? sym[i]->subclass : SymbolSubclass::undef;
|
||||
}
|
||||
Symbol* get_keyword(int i) const {
|
||||
return ((unsigned)i < (unsigned)max_kw_idx) ? sym[keywords[i]].get() : nullptr;
|
||||
}
|
||||
|
||||
SymTable() {
|
||||
std::memset(keywords, 0, sizeof(keywords));
|
||||
}
|
||||
};
|
||||
|
||||
struct SymTableOverflow {
|
||||
|
@ -112,15 +87,6 @@ struct SymTableOverflow {
|
|||
}
|
||||
};
|
||||
|
||||
struct SymTableKwRedef {
|
||||
std::string kw;
|
||||
SymTableKwRedef(std::string _kw) : kw(_kw) {
|
||||
}
|
||||
};
|
||||
|
||||
extern SymTable symbols;
|
||||
|
||||
extern int scope_level;
|
||||
|
||||
struct SymDef {
|
||||
int level;
|
||||
|
@ -133,18 +99,9 @@ struct SymDef {
|
|||
SymDef(int lvl, sym_idx_t idx, SrcLocation _loc, SymValBase* val = nullptr)
|
||||
: level(lvl), sym_idx(idx), value(val), loc(_loc) {
|
||||
}
|
||||
bool has_name() const {
|
||||
return sym_idx;
|
||||
}
|
||||
std::string name() const {
|
||||
return symbols.get_name(sym_idx);
|
||||
}
|
||||
std::string name() const;
|
||||
};
|
||||
|
||||
extern SymDef* sym_def[symbols.SIZE_PRIME + 1];
|
||||
extern SymDef* global_sym_def[symbols.SIZE_PRIME + 1];
|
||||
extern std::vector<std::pair<int, SymDef>> symbol_stack;
|
||||
extern std::vector<SrcLocation> scope_opened_at;
|
||||
|
||||
void open_scope(SrcLocation loc);
|
||||
void close_scope(SrcLocation loc);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue