Skip to content

Commit 80001d1

Browse files
committed
[Tolk] Implement AST: intermediate representation of tolk files
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.
1 parent 6c30e5a commit 80001d1

23 files changed

+3798
-2233
lines changed

tolk/CMakeLists.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ set(TOLK_SOURCE
55
lexer.cpp
66
symtable.cpp
77
compiler-state.cpp
8+
ast.cpp
9+
ast-from-tokens.cpp
10+
ast-to-legacy.cpp
811
unify-types.cpp
9-
parse-tolk.cpp
1012
abscode.cpp
1113
gen-abscode.cpp
1214
analyzer.cpp

tolk/abscode.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ namespace tolk {
2525
*
2626
*/
2727

28-
TmpVar::TmpVar(var_idx_t _idx, int _cls, TypeExpr* _type, SymDef* sym, SrcLocation loc)
29-
: v_type(_type), idx(_idx), cls(_cls), coord(0), where(loc) {
28+
TmpVar::TmpVar(var_idx_t _idx, bool _is_tmp_unnamed, TypeExpr* _type, SymDef* sym, SrcLocation loc)
29+
: v_type(_type), idx(_idx), is_tmp_unnamed(_is_tmp_unnamed), coord(0), where(loc) {
3030
if (sym) {
3131
name = sym->sym_idx;
3232
sym->value->idx = _idx;
@@ -59,9 +59,9 @@ void TmpVar::dump(std::ostream& os) const {
5959
}
6060

6161
void TmpVar::show(std::ostream& os, int omit_idx) const {
62-
if (cls & _Named) {
62+
if (!is_tmp_unnamed) {
6363
os << G.symbols.get_name(name);
64-
if (omit_idx && (omit_idx >= 2 || (cls & _UniqueName))) {
64+
if (omit_idx >= 2) {
6565
return;
6666
}
6767
}
@@ -474,8 +474,8 @@ void CodeBlob::print(std::ostream& os, int flags) const {
474474
os << "-------- END ---------\n\n";
475475
}
476476

477-
var_idx_t CodeBlob::create_var(int cls, TypeExpr* var_type, SymDef* sym, SrcLocation location) {
478-
vars.emplace_back(var_cnt, cls, var_type, sym, location);
477+
var_idx_t CodeBlob::create_var(bool is_tmp_unnamed, TypeExpr* var_type, SymDef* sym, SrcLocation location) {
478+
vars.emplace_back(var_cnt, is_tmp_unnamed, var_type, sym, location);
479479
if (sym) {
480480
sym->value->idx = var_cnt;
481481
}
@@ -492,7 +492,7 @@ bool CodeBlob::import_params(FormalArgList arg_list) {
492492
SymDef* arg_sym;
493493
SrcLocation arg_loc;
494494
std::tie(arg_type, arg_sym, arg_loc) = par;
495-
list.push_back(create_var(arg_sym ? (TmpVar::_In | TmpVar::_Named) : TmpVar::_In, arg_type, arg_sym, arg_loc));
495+
list.push_back(create_var(arg_sym == nullptr, arg_type, arg_sym, arg_loc));
496496
}
497497
emplace_back(loc, Op::_Import, list);
498498
in_var_cnt = var_cnt;

tolk/analyzer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ int CodeBlob::split_vars(bool strict) {
4646
if (k != 1) {
4747
var.coord = ~((n << 8) + k);
4848
for (int i = 0; i < k; i++) {
49-
auto v = create_var(vars[j].cls, comp_types[i], 0, vars[j].where);
49+
auto v = create_var(vars[j].is_tmp_unnamed, comp_types[i], 0, vars[j].where);
5050
tolk_assert(v == n + i);
5151
tolk_assert(vars[v].idx == v);
5252
vars[v].name = vars[j].name;

0 commit comments

Comments
 (0)