Skip to content

Commit

Permalink
feat: add an option to disable tail call optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
mrunix00 committed Apr 24, 2024
1 parent c91f4c9 commit 3d48d2c
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
struct options {
bool displayTokens = false;
bool dumpBytecode = false;
bool tailCallOptimization = true;
};

void exec_program(const std::string &program, struct options opts) {
Expand All @@ -32,14 +33,14 @@ void exec_program(const std::string &program, struct options opts) {

ast->compile(compiled_bytecode);
delete ast;

}

for (size_t i = 1; i < compiled_bytecode.segments.size(); i++) {
const auto segment = compiled_bytecode.segments[i];
if (Bytecode::Optimizer::is_tail_recursive(*segment, i))
Bytecode::Optimizer::optimize_tail_calls(*segment);
}
if (opts.tailCallOptimization)
for (size_t i = 1; i < compiled_bytecode.segments.size(); i++) {
const auto segment = compiled_bytecode.segments[i];
if (Bytecode::Optimizer::is_tail_recursive(*segment, i))
Bytecode::Optimizer::optimize_tail_calls(*segment);
}

if (opts.dumpBytecode)
dump_bytecode(compiled_bytecode);
Expand All @@ -65,14 +66,17 @@ int main(int argc, char *argv[]) {
struct options opts;

int opt;
while ((opt = getopt(argc, argv, "tbdO")) != -1) {
while ((opt = getopt(argc, argv, "tdU")) != -1) {
switch (opt) {
case 't':
opts.displayTokens = true;
break;
case 'd':
opts.dumpBytecode = true;
break;
case 'U':
opts.tailCallOptimization = false;
break;
default:
break;
}
Expand Down

0 comments on commit 3d48d2c

Please sign in to comment.