gsc-tool/src/iw5/xsk/compiler.cpp

3575 lines
105 KiB
C++
Raw Normal View History

2020-05-21 07:32:38 -04:00
// Copyright 2021 xensik. All rights reserved.
//
// Use of this source code is governed by a GNU GPLv3 license
// that can be found in the LICENSE file.
#include "stdafx.hpp"
#include "iw5.hpp"
#include "parser.hpp"
#include "lexer.hpp"
namespace xsk::gsc::iw5
{
2021-12-25 14:10:30 -05:00
auto compiler::output() -> std::vector<function::ptr>
2020-05-21 07:32:38 -04:00
{
return std::move(assembly_);
}
void compiler::compile(const std::string& file, std::vector<std::uint8_t>& data)
{
filename_ = file;
2021-12-26 07:38:02 -05:00
sources_.clear();
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
auto prog = parse_buffer(filename_, data);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
compile_program(prog);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::read_callback(std::function<std::vector<std::uint8_t>(const std::string&)> func)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
read_callback_ = func;
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
auto compiler::parse_buffer(const std::string& file, std::vector<std::uint8_t>& data) -> ast::program::ptr
2020-05-21 07:32:38 -04:00
{
yyscan_t scanner;
2021-12-25 14:10:30 -05:00
context ctx;
ast::program::ptr result(nullptr);
ctx.header_top = 0;
ctx.mode = mode_;
ctx.read_callback = read_callback_;
ctx.sources = &sources_;
ctx.loc.initialize(&file);
2020-05-21 07:32:38 -04:00
// Add the two NULL terminators, required by flex.
data.push_back(0);
data.push_back(0);
if (iw5_lex_init(&scanner))
2021-12-25 14:10:30 -05:00
{
throw comp_error(ctx.loc, "An unknown error ocurred while starting lexer context.");
}
ctx.scanner = scanner;
2020-05-21 07:32:38 -04:00
YY_BUFFER_STATE yybuffer = iw5__scan_buffer(reinterpret_cast<char*>(data.data()), data.size(), scanner);
2021-12-25 14:10:30 -05:00
parser parser(scanner, &ctx, result);
2022-01-19 14:01:12 -05:00
2021-12-25 14:10:30 -05:00
if (parser.parse() || result == nullptr)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
throw comp_error(ctx.loc, "An unknown error ocurred while parsing gsc file.");
2020-05-21 07:32:38 -04:00
}
iw5__delete_buffer(yybuffer, scanner);
iw5_lex_destroy(scanner);
return result;
}
2021-12-25 14:10:30 -05:00
auto compiler::parse_file(const std::string& file) -> ast::program::ptr
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto buffer = read_callback_(file);
2020-05-21 07:32:38 -04:00
auto result = parse_buffer(file, buffer);
return result;
}
2021-12-25 14:10:30 -05:00
void compiler::compile_program(const ast::program::ptr& program)
2020-05-21 07:32:38 -04:00
{
assembly_.clear();
includes_.clear();
animtrees_.clear();
constants_.clear();
local_functions_.clear();
index_ = 1;
2021-12-31 09:22:21 -05:00
developer_thread_ = false;
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (const auto& entry : program->declarations)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (entry == ast::kind::decl_thread)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
local_functions_.push_back(entry.as_thread->name->value);
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
for (const auto& include : program->includes)
2020-05-21 07:32:38 -04:00
{
emit_include(include);
}
2021-12-25 14:10:30 -05:00
for (const auto& declaration : program->declarations)
2020-05-21 07:32:38 -04:00
{
2022-01-19 14:01:12 -05:00
emit_declaration(declaration);
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_include(const ast::include::ptr& include)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
const auto& path = include->path->value;
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (const auto& inc : includes_)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (inc.name == path)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
throw comp_error(include->loc(), "error duplicated include file '" + path + "'.");
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
if (map_known_includes(path)) return;
2020-05-21 07:32:38 -04:00
try
{
auto program = parse_file(path);
std::vector<std::string> funcs;
2021-12-25 14:10:30 -05:00
for (const auto& decl : program->declarations)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (decl == ast::kind::decl_thread)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
funcs.push_back(decl.as_thread->name->value);
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
if (funcs.size() == 0)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
throw comp_error(include->loc(), "error empty include file '" + path + "'.");
2020-05-21 07:32:38 -04:00
}
includes_.push_back(include_t(path, funcs));
}
2021-12-25 14:10:30 -05:00
catch (const std::exception& e)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
throw comp_error(include->loc(), "error parsing include file '" + path + "': " + e.what());
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_declaration(const ast::decl& decl)
2020-05-21 07:32:38 -04:00
{
2021-12-30 14:36:13 -05:00
switch (decl.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-31 09:22:21 -05:00
case ast::kind::decl_dev_begin:
developer_thread_ = true;
break;
case ast::kind::decl_dev_end:
developer_thread_ = false;
break;
2021-12-25 14:10:30 -05:00
case ast::kind::decl_usingtree:
emit_decl_usingtree(decl.as_usingtree);
break;
case ast::kind::decl_constant:
emit_decl_constant(decl.as_constant);
break;
case ast::kind::decl_thread:
emit_decl_thread(decl.as_thread);
break;
default:
2021-12-30 14:36:13 -05:00
throw comp_error(decl.loc(), "unknown declaration");
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_decl_usingtree(const ast::decl_usingtree::ptr& animtree)
2020-05-21 07:32:38 -04:00
{
2021-12-31 09:22:21 -05:00
if(developer_thread_)
throw comp_error(animtree->loc(), "cannot put #using_animtree inside /# ... #/ comment");
2021-12-25 14:10:30 -05:00
animtrees_.push_back({ animtree->name->value, false });
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_decl_constant(const ast::decl_constant::ptr& constant)
2020-05-21 07:32:38 -04:00
{
constants_.insert({ constant->name->value, std::move(constant->value) });
}
2021-12-25 14:10:30 -05:00
void compiler::emit_decl_thread(const ast::decl_thread::ptr& thread)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
function_ = std::make_unique<function>();
2020-05-21 07:32:38 -04:00
function_->index = index_;
function_->name = thread->name->value;
2021-12-25 14:10:30 -05:00
auto blk = std::make_unique<block>();
2020-05-21 07:32:38 -04:00
stack_idx_ = 0;
label_idx_ = 0;
can_break_ = false;
can_continue_ = false;
local_stack_.clear();
2021-12-25 14:10:30 -05:00
break_blks_.clear();
continue_blks_.clear();
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
process_thread(thread, blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
emit_expr_parameters(thread->params, blk);
emit_stmt_list(thread->stmt, blk, true);
emit_opcode(opcode::OP_End);
2020-05-21 07:32:38 -04:00
function_->size = index_ - function_->index;
assembly_.push_back(std::move(function_));
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt(const ast::stmt& stmt, const block::ptr& blk, bool last)
2020-05-21 07:32:38 -04:00
{
2021-12-30 14:36:13 -05:00
switch (stmt.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::stmt_list:
emit_stmt_list(stmt.as_list, blk, last);
break;
case ast::kind::stmt_expr:
emit_stmt_expr(stmt.as_expr, blk);
break;
case ast::kind::stmt_call:
emit_stmt_call(stmt.as_call, blk);
break;
case ast::kind::stmt_assign:
emit_stmt_assign(stmt.as_assign, blk);
break;
case ast::kind::stmt_endon:
emit_stmt_endon(stmt.as_endon, blk);
break;
case ast::kind::stmt_notify:
emit_stmt_notify(stmt.as_notify, blk);
break;
case ast::kind::stmt_wait:
emit_stmt_wait(stmt.as_wait, blk);
break;
case ast::kind::stmt_waittill:
emit_stmt_waittill(stmt.as_waittill, blk);
break;
case ast::kind::stmt_waittillmatch:
emit_stmt_waittillmatch(stmt.as_waittillmatch, blk);
break;
case ast::kind::stmt_waittillframeend:
emit_stmt_waittillframeend(stmt.as_waittillframeend, blk);
break;
case ast::kind::stmt_if:
emit_stmt_if(stmt.as_if, blk, last);
break;
case ast::kind::stmt_ifelse:
emit_stmt_ifelse(stmt.as_ifelse, blk, last);
break;
case ast::kind::stmt_while:
emit_stmt_while(stmt.as_while, blk);
break;
case ast::kind::stmt_dowhile:
emit_stmt_dowhile(stmt.as_dowhile, blk);
break;
case ast::kind::stmt_for:
emit_stmt_for(stmt.as_for, blk);
break;
case ast::kind::stmt_foreach:
emit_stmt_foreach(stmt.as_foreach, blk);
break;
case ast::kind::stmt_switch:
emit_stmt_switch(stmt.as_switch, blk);
break;
case ast::kind::stmt_case:
emit_stmt_case(stmt.as_case, blk);
break;
case ast::kind::stmt_default:
emit_stmt_default(stmt.as_default, blk);
break;
case ast::kind::stmt_break:
emit_stmt_break(stmt.as_break, blk);
break;
case ast::kind::stmt_continue:
emit_stmt_continue(stmt.as_continue, blk);
break;
case ast::kind::stmt_return:
emit_stmt_return(stmt.as_return, blk);
break;
case ast::kind::stmt_breakpoint:
emit_stmt_breakpoint(stmt.as_breakpoint, blk);
break;
case ast::kind::stmt_prof_begin:
emit_stmt_prof_begin(stmt.as_prof_begin, blk);
break;
case ast::kind::stmt_prof_end:
emit_stmt_prof_end(stmt.as_prof_end, blk);
break;
default:
2021-12-30 14:36:13 -05:00
throw comp_error(stmt.loc(), "unknown statement");
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_list(const ast::stmt_list::ptr& stmt, const block::ptr& blk, bool last)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
for (const auto& entry : stmt->list)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
bool last_ = (&entry == &stmt->list.back() && last) ? true : false;
emit_stmt(entry, blk, last_);
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_expr(const ast::stmt_expr::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (stmt->expr.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::expr_increment:
emit_expr_increment(stmt->expr.as_increment, blk, true);
break;
case ast::kind::expr_decrement:
emit_expr_decrement(stmt->expr.as_decrement, blk, true);
break;
case ast::kind::expr_assign_equal:
case ast::kind::expr_assign_add:
case ast::kind::expr_assign_sub:
case ast::kind::expr_assign_mul:
case ast::kind::expr_assign_div:
case ast::kind::expr_assign_mod:
case ast::kind::expr_assign_shift_left:
case ast::kind::expr_assign_shift_right:
case ast::kind::expr_assign_bitwise_or:
case ast::kind::expr_assign_bitwise_and:
case ast::kind::expr_assign_bitwise_exor:
emit_expr_assign(stmt->expr.as_assign, blk);
break;
case ast::kind::null:
break;
default:
throw comp_error(stmt->loc(), "unknown expr statement expression");
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_call(const ast::stmt_call::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (stmt->expr == ast::kind::expr_call)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (mode_ != gsc::build::dev && stmt->expr.as_call->call == ast::kind::expr_function)
2021-10-21 14:59:34 -04:00
{
2021-12-25 14:10:30 -05:00
const auto& name = stmt->expr.as_call->call.as_function->name->value;
if (name == "assert" || name == "assertex" || name == "assertmsg") return;
2021-10-21 14:59:34 -04:00
}
2021-12-25 14:10:30 -05:00
emit_expr_call(stmt->expr.as_call, blk);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
else if (stmt->expr == ast::kind::expr_method)
emit_expr_method(stmt->expr.as_method, blk);
else
throw comp_error(stmt->loc(), "unknown call statement expression");
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_DecTop);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_assign(const ast::stmt_assign::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (stmt->expr.kind())
{
case ast::kind::expr_increment:
emit_expr_increment(stmt->expr.as_increment, blk, true);
break;
case ast::kind::expr_decrement:
emit_expr_decrement(stmt->expr.as_decrement, blk, true);
break;
case ast::kind::expr_assign_equal:
case ast::kind::expr_assign_add:
case ast::kind::expr_assign_sub:
case ast::kind::expr_assign_mul:
case ast::kind::expr_assign_div:
case ast::kind::expr_assign_mod:
case ast::kind::expr_assign_shift_left:
case ast::kind::expr_assign_shift_right:
case ast::kind::expr_assign_bitwise_or:
case ast::kind::expr_assign_bitwise_and:
case ast::kind::expr_assign_bitwise_exor:
emit_expr_assign(stmt->expr.as_assign, blk);
break;
default:
throw comp_error(stmt->loc(), "unknown assign statement expression");
}
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_endon(const ast::stmt_endon::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(stmt->event, blk);
emit_expr(stmt->obj, blk);
emit_opcode(opcode::OP_endon);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_notify(const ast::stmt_notify::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_voidCodepos);
2022-01-19 14:01:12 -05:00
2020-05-21 07:32:38 -04:00
std::reverse(stmt->args->list.begin(), stmt->args->list.end());
2022-01-19 14:01:12 -05:00
2021-12-25 14:10:30 -05:00
for (const auto& arg : stmt->args->list)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(arg, blk);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
emit_expr(stmt->event, blk);
emit_expr(stmt->obj, blk);
emit_opcode(opcode::OP_notify);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_wait(const ast::stmt_wait::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(stmt->time, blk);
emit_opcode(opcode::OP_wait);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_waittill(const ast::stmt_waittill::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(stmt->event, blk);
emit_expr(stmt->obj, blk);
emit_opcode(opcode::OP_waittill);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (const auto& entry : stmt->args->list)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
create_variable(entry.as_identifier, blk);
emit_opcode(opcode::OP_SafeSetWaittillVariableFieldCached, variable_access_index(entry.as_identifier, blk));
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_clearparams);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_waittillmatch(const ast::stmt_waittillmatch::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr_arguments(stmt->args, blk);
emit_expr(stmt->event, blk);
emit_expr(stmt->obj, blk);
emit_opcode(opcode::OP_waittillmatch, utils::string::va("%d", stmt->args->list.size()));
emit_opcode(opcode::OP_waittillmatch2);
emit_opcode(opcode::OP_clearparams);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_waittillframeend(const ast::stmt_waittillframeend::ptr&, const block::ptr&)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_waittillFrameEnd);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_if(const ast::stmt_if::ptr& stmt, const block::ptr& blk, bool last)
2020-05-21 07:32:38 -04:00
{
auto end_loc = create_label();
2021-12-25 14:10:30 -05:00
if (stmt->test == ast::kind::expr_not)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(stmt->test.as_not->rvalue, blk);
emit_opcode(opcode::OP_JumpOnTrue, end_loc);
2020-05-21 07:32:38 -04:00
}
else
{
2021-12-25 14:10:30 -05:00
emit_expr(stmt->test, blk);
emit_opcode(opcode::OP_JumpOnFalse, end_loc);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
blk->transfer(stmt->blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
emit_stmt(stmt->stmt, stmt->blk, last);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
last ? emit_opcode(opcode::OP_End) : emit_remove_local_vars(stmt->blk);
2020-05-21 07:32:38 -04:00
insert_label(end_loc);
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_ifelse(const ast::stmt_ifelse::ptr& stmt, const block::ptr& blk, bool last)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
std::vector<block*> childs;
2020-05-21 07:32:38 -04:00
auto else_loc = create_label();
auto end_loc = create_label();
2021-12-25 14:10:30 -05:00
if (stmt->test == ast::kind::expr_not)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(stmt->test.as_not->rvalue, blk);
emit_opcode(opcode::OP_JumpOnTrue, else_loc);
2020-05-21 07:32:38 -04:00
}
else
{
2021-12-25 14:10:30 -05:00
emit_expr(stmt->test, blk);
emit_opcode(opcode::OP_JumpOnFalse, else_loc);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
blk->transfer(stmt->blk_if);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
emit_stmt(stmt->stmt_if, stmt->blk_if, last);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
emit_remove_local_vars(stmt->blk_if);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (stmt->blk_if->abort == abort_t::abort_none)
childs.push_back(stmt->blk_if.get());
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
last ? emit_opcode(opcode::OP_End) : emit_opcode(opcode::OP_jump, end_loc);
2020-05-21 07:32:38 -04:00
insert_label(else_loc);
2021-12-25 14:10:30 -05:00
blk->transfer(stmt->blk_else);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
emit_stmt(stmt->stmt_else, stmt->blk_else, last);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
last ? emit_opcode(opcode::OP_End) : emit_remove_local_vars(stmt->blk_else);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (stmt->blk_else->abort == abort_t::abort_none)
childs.push_back(stmt->blk_else.get());
2020-05-21 07:32:38 -04:00
insert_label(end_loc);
2021-12-25 14:10:30 -05:00
blk->init_from_child(childs);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_while(const ast::stmt_while::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto old_breaks = break_blks_;
auto old_continues = continue_blks_;
2020-05-21 07:32:38 -04:00
auto old_break = can_break_;
auto old_continue = can_continue_;
2021-12-25 14:10:30 -05:00
break_blks_.clear();
continue_blks_.clear();
2020-05-21 07:32:38 -04:00
can_break_ = true;
can_continue_ = true;
auto break_loc = create_label();
auto continue_loc = create_label();
2021-12-25 14:10:30 -05:00
blk->transfer(stmt->blk);
stmt->blk->loc_break = break_loc;
stmt->blk->loc_continue = continue_loc;
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
emit_create_local_vars(stmt->blk);
2022-01-19 14:01:12 -05:00
2021-12-25 14:10:30 -05:00
blk->local_vars_create_count = stmt->blk->local_vars_create_count;
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (auto i = 0u; i < blk->local_vars_create_count; i++)
2021-05-09 10:00:50 -04:00
{
2021-12-25 14:10:30 -05:00
if (!blk->local_vars.at(i).init)
blk->local_vars.at(i).init = true;
2021-05-09 10:00:50 -04:00
}
2020-05-21 07:32:38 -04:00
auto begin_loc = insert_label();
2021-12-25 14:10:30 -05:00
bool const_cond = is_constant_condition(stmt->test);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (!const_cond)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(stmt->test, blk);
emit_opcode(opcode::OP_JumpOnFalse, break_loc);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
emit_stmt(stmt->stmt, stmt->blk, false);
insert_label(continue_loc);
emit_opcode(opcode::OP_jumpback, begin_loc);
insert_label(break_loc);
if (const_cond)
blk->init_from_child(break_blks_);
can_break_ = old_break;
can_continue_ = old_continue;
break_blks_ = old_breaks;
continue_blks_ = old_continues;
}
void compiler::emit_stmt_dowhile(const ast::stmt_dowhile::ptr& stmt, const block::ptr& blk)
{
auto old_breaks = break_blks_;
auto old_continues = continue_blks_;
auto old_break = can_break_;
auto old_continue = can_continue_;
break_blks_.clear();
continue_blks_.clear();
can_break_ = true;
can_continue_ = true;
auto break_loc = create_label();
auto continue_loc = create_label();
blk->transfer(stmt->blk);
stmt->blk->loc_break = break_loc;
stmt->blk->loc_continue = continue_loc;
emit_create_local_vars(stmt->blk);
2022-01-19 14:01:12 -05:00
2021-12-25 14:10:30 -05:00
blk->local_vars_create_count = stmt->blk->local_vars_create_count;
for (auto i = 0u; i < blk->local_vars_create_count; i++)
{
if (!blk->local_vars.at(i).init)
blk->local_vars.at(i).init = true;
}
auto begin_loc = insert_label();
emit_stmt(stmt->stmt, stmt->blk, false);
bool const_cond = is_constant_condition(stmt->test);
if (!const_cond)
{
emit_expr(stmt->test, blk);
emit_opcode(opcode::OP_JumpOnFalse, break_loc);
}
2020-05-21 07:32:38 -04:00
insert_label(continue_loc);
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_jumpback, begin_loc);
2020-05-21 07:32:38 -04:00
insert_label(break_loc);
2021-12-25 14:10:30 -05:00
if (const_cond)
blk->init_from_child(break_blks_);
2020-05-21 07:32:38 -04:00
can_break_ = old_break;
can_continue_ = old_continue;
2021-12-25 14:10:30 -05:00
break_blks_ = old_breaks;
continue_blks_ = old_continues;
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_for(const ast::stmt_for::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto old_breaks = break_blks_;
auto old_continues = continue_blks_;
2020-05-21 07:32:38 -04:00
auto old_break = can_break_;
auto old_continue = can_continue_;
2021-12-25 14:10:30 -05:00
break_blks_.clear();
continue_blks_.clear();
2020-05-21 07:32:38 -04:00
can_break_ = false;
can_continue_ = false;
auto break_loc = create_label();
auto continue_loc = create_label();
2021-12-25 14:10:30 -05:00
emit_stmt(stmt->init, blk, false);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
blk->transfer(stmt->blk);
stmt->blk->loc_break = break_loc;
stmt->blk->loc_continue = continue_loc;
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
emit_create_local_vars(stmt->blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
blk->local_vars_create_count = stmt->blk->local_vars_create_count;
for (auto i = 0u; i < blk->local_vars_create_count; i++)
2021-05-09 10:00:50 -04:00
{
2021-12-25 14:10:30 -05:00
if (!blk->local_vars.at(i).init)
blk->local_vars.at(i).init = true;
2021-05-09 10:00:50 -04:00
}
2021-12-25 14:10:30 -05:00
blk->transfer(stmt->blk_iter);
2020-05-21 07:32:38 -04:00
auto begin_loc = insert_label();
2021-12-25 14:10:30 -05:00
bool const_cond = is_constant_condition(stmt->test);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (!const_cond)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(stmt->test, blk);
emit_opcode(opcode::OP_JumpOnFalse, break_loc);
2020-05-21 07:32:38 -04:00
}
can_break_ = true;
can_continue_ = true;
2021-12-25 14:10:30 -05:00
emit_stmt(stmt->stmt, stmt->blk, false);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (stmt->blk->abort == abort_t::abort_none)
continue_blks_.push_back(stmt->blk.get());
2020-05-21 07:32:38 -04:00
can_break_ = false;
can_continue_ = false;
insert_label(continue_loc);
2021-12-25 14:10:30 -05:00
stmt->blk_iter->init_from_child(continue_blks_);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
emit_stmt(stmt->iter, stmt->blk_iter, false);
emit_opcode(opcode::OP_jumpback, begin_loc);
2020-05-21 07:32:38 -04:00
insert_label(break_loc);
2021-12-25 14:10:30 -05:00
if (const_cond)
blk->init_from_child(break_blks_);
2020-05-21 07:32:38 -04:00
can_break_ = old_break;
can_continue_ = old_continue;
2021-12-25 14:10:30 -05:00
break_blks_ = old_breaks;
continue_blks_ = old_continues;
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_foreach(const ast::stmt_foreach::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto old_breaks = break_blks_;
auto old_continues = continue_blks_;
2020-05-21 07:32:38 -04:00
auto old_break = can_break_;
auto old_continue = can_continue_;
2021-12-25 14:10:30 -05:00
break_blks_.clear();
continue_blks_.clear();
2020-05-21 07:32:38 -04:00
can_break_ = false;
can_continue_ = false;
auto break_loc = create_label();
auto continue_loc = create_label();
2021-12-25 14:10:30 -05:00
emit_expr(stmt->array_expr, blk);
emit_expr_variable_ref(stmt->array, blk, true);
emit_expr_variable(stmt->array, blk);
emit_opcode(opcode::OP_CallBuiltin1, "getfirstarraykey");
emit_expr_variable_ref(stmt->key_expr, blk, true);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
blk->transfer(stmt->ctx);
2020-05-21 07:32:38 -04:00
stmt->ctx->loc_break = break_loc;
stmt->ctx->loc_continue = continue_loc;
emit_create_local_vars(stmt->ctx);
2021-12-25 14:10:30 -05:00
blk->local_vars_create_count = stmt->ctx->local_vars_create_count;
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (auto i = 0u; i < blk->local_vars_create_count; i++)
2021-05-09 10:00:50 -04:00
{
2021-12-25 14:10:30 -05:00
if (!blk->local_vars.at(i).init)
blk->local_vars.at(i).init = true;
2021-05-09 10:00:50 -04:00
}
2021-12-25 14:10:30 -05:00
blk->transfer(stmt->ctx_post);
2020-05-21 07:32:38 -04:00
auto begin_loc = insert_label();
2021-12-25 14:10:30 -05:00
emit_expr_variable(stmt->key_expr, blk);
emit_opcode(opcode::OP_CallBuiltin1, "isdefined");
2022-01-19 14:01:12 -05:00
emit_opcode(opcode::OP_JumpOnFalse, break_loc);
2020-05-21 07:32:38 -04:00
can_break_ = true;
can_continue_ = true;
2021-12-25 14:10:30 -05:00
emit_expr_variable(stmt->key_expr, stmt->ctx);
emit_opcode(opcode::OP_EvalLocalArrayCached, variable_access_index(stmt->array.as_identifier, stmt->ctx));
emit_expr_variable_ref(stmt->value_expr, stmt->ctx, true);
emit_stmt(stmt->stmt, stmt->ctx, false);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (stmt->ctx->abort == abort_t::abort_none)
continue_blks_.push_back(stmt->ctx.get());
2020-05-21 07:32:38 -04:00
can_break_ = false;
can_continue_ = false;
insert_label(continue_loc);
2021-12-25 14:10:30 -05:00
stmt->ctx_post->init_from_child(continue_blks_);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
emit_expr_variable(stmt->key_expr, stmt->ctx_post);
2022-01-19 14:01:12 -05:00
emit_expr_variable(stmt->array, stmt->ctx_post);
emit_opcode(opcode::OP_CallBuiltin2, "getnextarraykey");
emit_expr_variable_ref(stmt->key_expr, stmt->ctx_post, true);
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_jumpback, begin_loc);
2020-05-21 07:32:38 -04:00
insert_label(break_loc);
2021-12-25 14:10:30 -05:00
emit_expr_clear_local(stmt->array.as_identifier, blk);
if (!stmt->use_key) emit_expr_clear_local(stmt->key_expr.as_identifier, blk);
2020-05-21 07:32:38 -04:00
can_break_ = old_break;
can_continue_ = old_continue;
2021-12-25 14:10:30 -05:00
break_blks_ = old_breaks;
continue_blks_ = old_continues;
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_switch(const ast::stmt_switch::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto old_breaks = break_blks_;
2020-05-21 07:32:38 -04:00
auto old_break = can_break_;
2021-12-25 14:10:30 -05:00
break_blks_.clear();
2020-05-21 07:32:38 -04:00
can_break_ = false;
auto jmptable_loc = create_label();
auto break_loc = create_label();
2021-12-25 14:10:30 -05:00
emit_expr(stmt->test, blk);
emit_opcode(opcode::OP_switch, jmptable_loc);
2020-05-21 07:32:38 -04:00
can_break_ = true;
std::vector<std::string> data;
2021-12-25 14:10:30 -05:00
data.push_back(utils::string::va("%d", stmt->stmt->list.size()));
2020-05-21 07:32:38 -04:00
bool has_default = false;
2021-12-25 14:10:30 -05:00
block* default_ctx = nullptr;
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (auto i = 0; i < stmt->stmt->list.size(); i++)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto& entry = stmt->stmt->list[i];
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (entry == ast::kind::stmt_case)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (has_default)
2021-05-15 06:04:48 -04:00
{
2021-12-25 14:10:30 -05:00
comp_error(stmt->loc(), "default must be last case");
2021-05-15 06:04:48 -04:00
}
2020-05-21 07:32:38 -04:00
auto& case_ = entry.as_case;
2021-12-25 14:10:30 -05:00
if (case_->label == ast::kind::expr_integer)
2020-05-21 07:32:38 -04:00
{
auto loc = insert_label();
data.push_back("case");
2021-12-25 14:10:30 -05:00
data.push_back(case_->label.as_integer->value);
2020-05-21 07:32:38 -04:00
data.push_back(loc);
}
2021-12-25 14:10:30 -05:00
else if (case_->label == ast::kind::expr_string)
2020-05-21 07:32:38 -04:00
{
auto loc = insert_label();
data.push_back("case");
2021-12-25 14:10:30 -05:00
data.push_back(case_->label.as_string->value);
2020-05-21 07:32:38 -04:00
data.push_back(loc);
}
else
{
2021-12-25 14:10:30 -05:00
throw comp_error(stmt->loc(), "case type must be int or string");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
blk->transfer(case_->blk);
case_->blk->loc_break = break_loc;
emit_stmt_list(case_->stmt, case_->blk, false);
if (case_->stmt->list.size() > 0)
emit_remove_local_vars(case_->blk);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
else if (entry == ast::kind::stmt_default)
2020-05-21 07:32:38 -04:00
{
auto loc = insert_label();
data.push_back("default");
data.push_back(loc);
has_default = true;
2021-12-25 14:10:30 -05:00
default_ctx = entry.as_default->blk.get();
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
blk->transfer(entry.as_default->blk);
entry.as_default->blk->loc_break = break_loc;
emit_stmt_list(entry.as_default->stmt, entry.as_default->blk, false);
if (entry.as_default->stmt->list.size() > 0)
emit_remove_local_vars(entry.as_default->blk);
2020-05-21 07:32:38 -04:00
}
else
{
2021-12-30 14:36:13 -05:00
throw comp_error(entry.loc(), "missing case statement");
2020-05-21 07:32:38 -04:00
}
2022-01-19 14:01:12 -05:00
}
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (has_default)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (default_ctx->abort == abort_t::abort_none)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
break_blks_.push_back(default_ctx);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
blk->init_from_child(break_blks_);
2020-05-21 07:32:38 -04:00
}
insert_label(jmptable_loc);
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_endswitch, data);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
auto offset = 7 * stmt->stmt->list.size();
2020-05-21 07:32:38 -04:00
function_->instructions.back()->size += offset;
index_ += offset;
insert_label(break_loc);
can_break_ = old_break;
2021-12-25 14:10:30 -05:00
break_blks_ = old_breaks;
}
void compiler::emit_stmt_case(const ast::stmt_case::ptr& stmt, const block::ptr& blk)
{
throw comp_error(stmt->loc(), "illegal case statement");
}
void compiler::emit_stmt_default(const ast::stmt_default::ptr& stmt, const block::ptr& blk)
{
throw comp_error(stmt->loc(), "illegal default statement");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_break(const ast::stmt_break::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (!can_break_ || blk->abort != abort_t::abort_none || blk->loc_break == "")
throw comp_error(stmt->loc(), "illegal break statement");
break_blks_.push_back(blk.get());
emit_remove_local_vars(blk);
blk->abort = abort_t::abort_break;
emit_opcode(opcode::OP_jump, blk->loc_break);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_continue(const ast::stmt_continue::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (!can_continue_ || blk->abort != abort_t::abort_none || blk->loc_continue == "")
throw comp_error(stmt->loc(), "illegal continue statement");
2022-01-19 14:01:12 -05:00
2021-12-25 14:10:30 -05:00
continue_blks_.push_back(blk.get());
emit_remove_local_vars(blk);
blk->abort = abort_t::abort_continue;
emit_opcode(opcode::OP_jump, blk->loc_continue);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_stmt_return(const ast::stmt_return::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (blk->abort == abort_t::abort_none)
blk->abort = abort_t::abort_return;
if (stmt->expr != ast::kind::null)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(stmt->expr, blk);
emit_opcode(opcode::OP_Return);
2020-05-21 07:32:38 -04:00
}
else
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_End);
}
void compiler::emit_stmt_breakpoint(const ast::stmt_breakpoint::ptr& stmt, const block::ptr& blk)
{
// TODO:
}
void compiler::emit_stmt_prof_begin(const ast::stmt_prof_begin::ptr& stmt, const block::ptr& blk)
{
// TODO:
}
void compiler::emit_stmt_prof_end(const ast::stmt_prof_end::ptr& stmt, const block::ptr& blk)
{
// TODO:
}
void compiler::emit_expr(const ast::expr& expr, const block::ptr& blk)
{
switch (expr.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::expr_paren:
emit_expr(expr.as_paren->child, blk);
break;
case ast::kind::expr_ternary:
emit_expr_ternary(expr.as_ternary, blk);
break;
case ast::kind::expr_and:
emit_expr_and(expr.as_and, blk);
break;
case ast::kind::expr_or:
emit_expr_or(expr.as_or, blk);
break;
case ast::kind::expr_equality:
case ast::kind::expr_inequality:
case ast::kind::expr_less:
case ast::kind::expr_greater:
case ast::kind::expr_less_equal:
case ast::kind::expr_greater_equal:
case ast::kind::expr_bitwise_or:
case ast::kind::expr_bitwise_and:
case ast::kind::expr_bitwise_exor:
case ast::kind::expr_shift_left:
case ast::kind::expr_shift_right:
case ast::kind::expr_add:
case ast::kind::expr_sub:
case ast::kind::expr_mul:
case ast::kind::expr_div:
case ast::kind::expr_mod:
emit_expr_binary(expr.as_binary, blk);
break;
case ast::kind::expr_complement:
emit_expr_complement(expr.as_complement, blk);
break;
case ast::kind::expr_not:
emit_expr_not(expr.as_not, blk);
break;
case ast::kind::expr_call:
emit_expr_call(expr.as_call, blk);
break;
case ast::kind::expr_method:
emit_expr_method(expr.as_method, blk);
break;
case ast::kind::expr_reference:
emit_expr_reference(expr.as_reference, blk);
break;
case ast::kind::expr_add_array:
emit_expr_add_array(expr.as_add_array, blk);
break;
case ast::kind::expr_array:
emit_expr_array(expr.as_array, blk);
break;
case ast::kind::expr_field:
emit_expr_field(expr.as_field, blk);
break;
case ast::kind::expr_size:
emit_expr_size(expr.as_size, blk);
break;
case ast::kind::expr_thisthread:
emit_opcode(opcode::OP_GetThisthread);
break;
case ast::kind::expr_empty_array:
emit_opcode(opcode::OP_EmptyArray);
break;
case ast::kind::expr_undefined:
emit_opcode(opcode::OP_GetUndefined);
break;
case ast::kind::expr_game:
emit_opcode(opcode::OP_GetGame);
break;
case ast::kind::expr_self:
emit_opcode(opcode::OP_GetSelf);
break;
case ast::kind::expr_anim:
emit_opcode(opcode::OP_GetAnim);
break;
case ast::kind::expr_level:
emit_opcode(opcode::OP_GetLevel);
break;
case ast::kind::expr_animation:
emit_expr_animation(expr.as_animation);
break;
case ast::kind::expr_animtree:
emit_expr_animtree(expr.as_animtree);
break;
case ast::kind::expr_identifier:
emit_expr_local(expr.as_identifier, blk);
break;
case ast::kind::expr_istring:
emit_expr_istring(expr.as_istring);
break;
case ast::kind::expr_string:
emit_expr_string(expr.as_string);
break;
case ast::kind::expr_color:
emit_expr_color(expr.as_color);
break;
case ast::kind::expr_vector:
emit_expr_vector(expr.as_vector, blk);
break;
case ast::kind::expr_float:
emit_expr_float(expr.as_float);
break;
case ast::kind::expr_integer:
emit_expr_integer(expr.as_integer);
break;
case ast::kind::expr_false:
emit_expr_false(expr.as_false);
break;
case ast::kind::expr_true:
emit_expr_true(expr.as_true);
break;
case ast::kind::null:
break;
default:
2021-12-30 14:36:13 -05:00
throw comp_error(expr.loc(), "unknown expression");
2020-05-21 07:32:38 -04:00
}
}
2022-01-19 14:01:12 -05:00
void compiler::emit_expr_assign(const ast::expr_assign::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (expr->kind() == ast::kind::expr_assign_equal)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (expr->rvalue == ast::kind::expr_undefined)
{
emit_expr_clear(expr->lvalue, blk);
return;
}
emit_expr(expr->rvalue, blk);
emit_expr_variable_ref(expr->lvalue, blk, true);
return;
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
emit_expr(expr->lvalue, blk);
emit_expr(expr->rvalue, blk);
switch (expr->kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::expr_assign_add:
emit_opcode(opcode::OP_plus);
break;
case ast::kind::expr_assign_sub:
emit_opcode(opcode::OP_minus);
break;
case ast::kind::expr_assign_mul:
emit_opcode(opcode::OP_multiply);
break;
case ast::kind::expr_assign_div:
emit_opcode(opcode::OP_divide);
break;
case ast::kind::expr_assign_mod:
emit_opcode(opcode::OP_mod);
break;
case ast::kind::expr_assign_shift_left:
emit_opcode(opcode::OP_shift_left);
break;
case ast::kind::expr_assign_shift_right:
emit_opcode(opcode::OP_shift_right);
break;
case ast::kind::expr_assign_bitwise_or:
emit_opcode(opcode::OP_bit_or);
break;
case ast::kind::expr_assign_bitwise_and:
emit_opcode(opcode::OP_bit_and);
break;
case ast::kind::expr_assign_bitwise_exor:
emit_opcode(opcode::OP_bit_ex_or);
break;
default:
throw comp_error(expr->loc(), "unknown assign operation");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
2022-01-19 14:01:12 -05:00
emit_expr_variable_ref(expr->lvalue, blk, true);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_clear(const ast::expr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (expr.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::expr_array:
emit_expr(expr.as_array->key, blk);
expr.as_array->obj == ast::kind::expr_game ? emit_opcode(opcode::OP_GetGameRef) : emit_expr_variable_ref(expr.as_array->obj, blk, false);
emit_opcode(opcode::OP_ClearArray);
break;
case ast::kind::expr_field:
emit_expr_object(expr.as_field->obj, blk);
emit_opcode(opcode::OP_ClearFieldVariable, expr.as_field->field->value);
break;
case ast::kind::expr_identifier:
emit_opcode(opcode::OP_GetUndefined);
emit_expr_local_ref(expr.as_identifier, blk, true);
break;
default:
2021-12-30 14:36:13 -05:00
throw comp_error(expr.loc(), "unknown clear variable lvalue");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
}
void compiler::emit_expr_clear_local(const ast::expr_identifier::ptr& expr, const block::ptr& blk)
{
auto index = variable_stack_index(expr, blk);
if (index == 0)
emit_opcode(opcode::OP_ClearLocalVariableFieldCached0);
else
emit_opcode(opcode::OP_ClearLocalVariableFieldCached, variable_access_index(expr, blk));
}
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_increment(const ast::expr_increment::ptr& expr, const block::ptr& blk, bool fromstmt)
{
if (fromstmt)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr_variable_ref(expr->lvalue, blk, false);
emit_opcode(opcode::OP_inc);
emit_opcode(opcode::OP_SetVariableField);
2020-05-21 07:32:38 -04:00
}
else
{
2021-12-25 14:10:30 -05:00
// TODO:
}
}
void compiler::emit_expr_decrement(const ast::expr_decrement::ptr& expr, const block::ptr& blk, bool fromstmt)
{
if (fromstmt)
{
emit_expr_variable_ref(expr->lvalue, blk, false);
emit_opcode(opcode::OP_dec);
emit_opcode(opcode::OP_SetVariableField);
2020-05-21 07:32:38 -04:00
}
else
{
2021-12-25 14:10:30 -05:00
// TODO:
}
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_ternary(const ast::expr_ternary::ptr& expr, const block::ptr& blk)
2021-07-23 15:59:49 -04:00
{
auto else_loc = create_label();
auto end_loc = create_label();
2021-12-25 14:10:30 -05:00
if (expr->test == ast::kind::expr_not)
2021-07-23 15:59:49 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(expr->test.as_not->rvalue, blk);
emit_opcode(opcode::OP_JumpOnTrue, else_loc);
2021-07-23 15:59:49 -04:00
}
else
{
2021-12-25 14:10:30 -05:00
emit_expr(expr->test, blk);
emit_opcode(opcode::OP_JumpOnFalse, else_loc);
2021-07-23 15:59:49 -04:00
}
2021-12-25 14:10:30 -05:00
emit_expr(expr->true_expr, blk);
emit_opcode(opcode::OP_jump, end_loc);
2021-07-23 15:59:49 -04:00
insert_label(else_loc);
2021-12-25 14:10:30 -05:00
emit_expr(expr->false_expr, blk);
2021-07-23 15:59:49 -04:00
insert_label(end_loc);
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_binary(const ast::expr_binary::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(expr->lvalue, blk);
emit_expr(expr->rvalue, blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
switch (expr->kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::expr_equality:
emit_opcode(opcode::OP_equality);
break;
case ast::kind::expr_inequality:
emit_opcode(opcode::OP_inequality);
break;
case ast::kind::expr_less:
emit_opcode(opcode::OP_less);
break;
case ast::kind::expr_greater:
emit_opcode(opcode::OP_greater);
break;
case ast::kind::expr_less_equal:
emit_opcode(opcode::OP_less_equal);
break;
case ast::kind::expr_greater_equal:
emit_opcode(opcode::OP_greater_equal);
break;
case ast::kind::expr_bitwise_or:
emit_opcode(opcode::OP_bit_or);
break;
case ast::kind::expr_bitwise_and:
emit_opcode(opcode::OP_bit_and);
break;
case ast::kind::expr_bitwise_exor:
emit_opcode(opcode::OP_bit_ex_or);
break;
case ast::kind::expr_shift_left:
emit_opcode(opcode::OP_shift_left);
break;
case ast::kind::expr_shift_right:
emit_opcode(opcode::OP_shift_right);
break;
case ast::kind::expr_add:
emit_opcode(opcode::OP_plus);
break;
case ast::kind::expr_sub:
emit_opcode(opcode::OP_minus);
break;
case ast::kind::expr_mul:
emit_opcode(opcode::OP_multiply);
break;
case ast::kind::expr_div:
emit_opcode(opcode::OP_divide);
break;
case ast::kind::expr_mod:
emit_opcode(opcode::OP_mod);
break;
default:
throw comp_error(expr->loc(), "unknown binary expression");
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_and(const ast::expr_and::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
auto label = create_label();
2021-12-25 14:10:30 -05:00
emit_expr(expr->lvalue, blk);
emit_opcode(opcode::OP_JumpOnFalseExpr, label);
emit_expr(expr->rvalue, blk);
emit_opcode(opcode::OP_CastBool);
2020-05-21 07:32:38 -04:00
insert_label(label);
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_or(const ast::expr_or::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
auto label = create_label();
2021-12-25 14:10:30 -05:00
emit_expr(expr->lvalue, blk);
emit_opcode(opcode::OP_JumpOnTrueExpr, label);
emit_expr(expr->rvalue, blk);
emit_opcode(opcode::OP_CastBool);
2020-05-21 07:32:38 -04:00
insert_label(label);
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_complement(const ast::expr_complement::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(expr->rvalue, blk);
emit_opcode(opcode::OP_BoolComplement);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_not(const ast::expr_not::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(expr->rvalue, blk);
emit_opcode(opcode::OP_BoolNot);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_call(const ast::expr_call::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (expr->call == ast::kind::expr_pointer)
emit_expr_call_pointer(expr->call.as_pointer, blk);
else if (expr->call == ast::kind::expr_function)
emit_expr_call_function(expr->call.as_function, blk);
2020-05-21 07:32:38 -04:00
else
2021-12-25 14:10:30 -05:00
throw comp_error(expr->loc(), "unknown function call expression");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_call_pointer(const ast::expr_pointer::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (expr->mode == ast::call::mode::normal)
emit_opcode(opcode::OP_PreScriptCall);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
emit_expr_arguments(expr->args, blk);
emit_expr(expr->func, blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
auto argcount = utils::string::va("%d", expr->args->list.size());
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
switch (expr->mode)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::call::mode::normal:
emit_opcode(opcode::OP_ScriptFunctionCallPointer);
break;
case ast::call::mode::thread:
emit_opcode(opcode::OP_ScriptThreadCallPointer, argcount);
break;
case ast::call::mode::childthread:
emit_opcode(opcode::OP_ScriptChildThreadCallPointer, argcount);
break;
case ast::call::mode::builtin:
emit_opcode(opcode::OP_CallBuiltinPointer, argcount);
break;
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_call_function(const ast::expr_function::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto type = resolve_function_type(expr);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (type != ast::call::type::builtin && expr->mode == ast::call::mode::normal && expr->args->list.size() > 0)
emit_opcode(opcode::OP_PreScriptCall);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
emit_expr_arguments(expr->args, blk);
auto argcount = utils::string::va("%d", expr->args->list.size());
if (type == ast::call::type::local)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (expr->mode)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::call::mode::normal:
if (expr->args->list.size() > 0)
emit_opcode(opcode::OP_ScriptLocalFunctionCall, expr->name->value);
else
emit_opcode(opcode::OP_ScriptLocalFunctionCall2, expr->name->value);
break;
case ast::call::mode::thread:
emit_opcode(opcode::OP_ScriptLocalThreadCall, { expr->name->value, argcount });
break;
case ast::call::mode::childthread:
emit_opcode(opcode::OP_ScriptLocalChildThreadCall, { expr->name->value, argcount });
break;
case ast::call::mode::builtin:
// no local builtins
break;
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
else if (type == ast::call::type::far)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (expr->mode)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::call::mode::normal:
if (expr->args->list.size() > 0)
emit_opcode(opcode::OP_ScriptFarFunctionCall, { expr->path->value, expr->name->value });
else
emit_opcode(opcode::OP_ScriptFarFunctionCall2, { expr->path->value, expr->name->value });
break;
case ast::call::mode::thread:
2022-01-19 14:01:12 -05:00
emit_opcode(opcode::OP_ScriptFarThreadCall, { expr->path->value, expr->name->value, argcount });
2021-12-25 14:10:30 -05:00
break;
case ast::call::mode::childthread:
2022-01-19 14:01:12 -05:00
emit_opcode(opcode::OP_ScriptFarChildThreadCall, { expr->path->value, expr->name->value, argcount });
2021-12-25 14:10:30 -05:00
break;
case ast::call::mode::builtin:
// no far builtins
break;
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
else if (type == ast::call::type::builtin)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (expr->mode != ast::call::mode::normal)
throw comp_error(expr->loc(), "builtin calls can't be threaded");
switch (expr->args->list.size())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case 0:
emit_opcode(opcode::OP_CallBuiltin0, expr->name->value);
break;
2022-01-19 14:01:12 -05:00
case 1:
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_CallBuiltin1, expr->name->value);
break;
case 2:
emit_opcode(opcode::OP_CallBuiltin2, expr->name->value);
break;
case 3:
emit_opcode(opcode::OP_CallBuiltin3, expr->name->value);
break;
case 4:
emit_opcode(opcode::OP_CallBuiltin4, expr->name->value);
break;
case 5:
emit_opcode(opcode::OP_CallBuiltin5, expr->name->value);
break;
default:
2022-01-19 14:01:12 -05:00
emit_opcode(opcode::OP_CallBuiltin, { expr->name->value, argcount });
2021-12-25 14:10:30 -05:00
break;
2020-05-21 07:32:38 -04:00
}
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_method(const ast::expr_method::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (expr->call == ast::kind::expr_pointer)
emit_expr_method_pointer(expr->call.as_pointer, expr->obj, blk);
else if (expr->call == ast::kind::expr_function)
emit_expr_method_function(expr->call.as_function, expr->obj, blk);
else
throw comp_error(expr->loc(), "unknown method call expression");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_method_pointer(const ast::expr_pointer::ptr& expr, const ast::expr& obj, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (expr->mode == ast::call::mode::normal)
emit_opcode(opcode::OP_PreScriptCall);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
emit_expr_arguments(expr->args, blk);
emit_expr(obj, blk);
emit_expr(expr->func, blk);
auto argcount = utils::string::va("%d", expr->args->list.size());
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
switch (expr->mode)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::call::mode::normal:
emit_opcode(opcode::OP_ScriptMethodCallPointer);
break;
case ast::call::mode::thread:
emit_opcode(opcode::OP_ScriptMethodThreadCallPointer, argcount);
break;
case ast::call::mode::childthread:
emit_opcode(opcode::OP_ScriptMethodChildThreadCallPointer, argcount);
break;
case ast::call::mode::builtin:
emit_opcode(opcode::OP_CallBuiltinMethodPointer, argcount);
break;
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_method_function(const ast::expr_function::ptr& expr, const ast::expr& obj, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto type = resolve_function_type(expr);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (type != ast::call::type::builtin && expr->mode == ast::call::mode::normal)
emit_opcode(opcode::OP_PreScriptCall);
emit_expr_arguments(expr->args, blk);
emit_expr(obj, blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
auto argcount = utils::string::va("%d", expr->args->list.size());
if (type == ast::call::type::local)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (expr->mode)
{
case ast::call::mode::normal:
emit_opcode(opcode::OP_ScriptLocalMethodCall, expr->name->value);
break;
case ast::call::mode::thread:
emit_opcode(opcode::OP_ScriptLocalMethodThreadCall, { expr->name->value, argcount });
break;
case ast::call::mode::childthread:
emit_opcode(opcode::OP_ScriptLocalMethodChildThreadCall, { expr->name->value, argcount });
break;
case ast::call::mode::builtin:
// no local builtins
break;
}
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
else if (type == ast::call::type::far)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (expr->mode)
{
case ast::call::mode::normal:
emit_opcode(opcode::OP_ScriptFarMethodCall, { expr->path->value, expr->name->value });
break;
case ast::call::mode::thread:
2022-01-19 14:01:12 -05:00
emit_opcode(opcode::OP_ScriptFarMethodThreadCall, { expr->path->value, expr->name->value, argcount });
2021-12-25 14:10:30 -05:00
break;
case ast::call::mode::childthread:
2022-01-19 14:01:12 -05:00
emit_opcode(opcode::OP_ScriptFarMethodChildThreadCall, { expr->path->value, expr->name->value, argcount });
2021-12-25 14:10:30 -05:00
break;
case ast::call::mode::builtin:
// no far builtins
break;
}
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
else if (type == ast::call::type::builtin)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (expr->mode != ast::call::mode::normal)
throw comp_error(expr->loc(), "builtin calls can't be threaded");
switch (expr->args->list.size())
{
case 0:
emit_opcode(opcode::OP_CallBuiltinMethod0, expr->name->value);
break;
2022-01-19 14:01:12 -05:00
case 1:
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_CallBuiltinMethod1, expr->name->value);
break;
case 2:
emit_opcode(opcode::OP_CallBuiltinMethod2, expr->name->value);
break;
case 3:
emit_opcode(opcode::OP_CallBuiltinMethod3, expr->name->value);
break;
case 4:
emit_opcode(opcode::OP_CallBuiltinMethod4, expr->name->value);
break;
case 5:
emit_opcode(opcode::OP_CallBuiltinMethod5, expr->name->value);
break;
default:
2022-01-19 14:01:12 -05:00
emit_opcode(opcode::OP_CallBuiltinMethod, { expr->name->value, argcount });
2021-12-25 14:10:30 -05:00
break;
}
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
}
void compiler::emit_expr_add_array(const ast::expr_add_array::ptr& expr, const block::ptr& blk)
{
emit_opcode(opcode::OP_EmptyArray);
for (const auto& arg : expr->args->list)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(arg, blk);
emit_opcode(opcode::OP_AddArray);
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_parameters(const ast::expr_parameters::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
for (const auto& entry : expr->list)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
initialize_variable(entry, blk);
2022-01-19 14:01:12 -05:00
emit_opcode(opcode::OP_SafeCreateVariableFieldCached, variable_create_index(entry, blk));
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_checkclearparams);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_arguments(const ast::expr_arguments::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
std::reverse(expr->list.begin(), expr->list.end());
for (auto& entry : expr->list)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(entry, blk);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
}
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_reference(const ast::expr_reference::ptr& expr, const block::ptr& blk)
{
bool method = false;
auto type = resolve_reference_type(expr, method);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
switch (type)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::call::type::local:
emit_opcode(opcode::OP_GetLocalFunction, expr->name->value);
break;
case ast::call::type::far:
emit_opcode(opcode::OP_GetFarFunction, { expr->path->value, expr->name->value });
break;
case ast::call::type::builtin:
if (method)
emit_opcode(opcode::OP_GetBuiltinMethod, expr->name->value);
else
emit_opcode(opcode::OP_GetBuiltinFunction, expr->name->value);
break;
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_size(const ast::expr_size::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-30 14:36:13 -05:00
emit_expr(expr->obj, blk);
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_size);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_variable_ref(const ast::expr& expr, const block::ptr& blk, bool set)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (expr.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::expr_array:
emit_expr_array_ref(expr.as_array, blk, set);
break;
case ast::kind::expr_field:
emit_expr_field_ref(expr.as_field, blk, set);
break;
case ast::kind::expr_identifier:
emit_expr_local_ref(expr.as_identifier, blk, set);
break;
default:
2021-12-30 14:36:13 -05:00
throw comp_error(expr.loc(), "invalid lvalue");
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_array_ref(const ast::expr_array::ptr& expr, const block::ptr& blk, bool set)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(expr->key, blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
switch (expr->obj.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::expr_game:
emit_opcode(opcode::OP_GetGameRef);
emit_opcode(opcode::OP_EvalArrayRef);
if (set) emit_opcode(opcode::OP_SetVariableField);
break;
case ast::kind::expr_array:
case ast::kind::expr_field:
emit_expr_variable_ref(expr->obj, blk, false);
2022-01-19 14:01:12 -05:00
emit_opcode(opcode::OP_EvalArrayRef);
2021-12-25 14:10:30 -05:00
if (set) emit_opcode(opcode::OP_SetVariableField);
break;
case ast::kind::expr_identifier:
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (!variable_initialized(expr->obj.as_identifier, blk))
{
initialize_variable(expr->obj.as_identifier, blk);
emit_opcode(opcode::OP_EvalNewLocalArrayRefCached0, variable_create_index(expr->obj.as_identifier, blk));
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (!set) throw comp_error(expr->loc(), "INTERNAL: VAR CREATED BUT NOT SET!");
}
else if (variable_stack_index(expr->obj.as_identifier, blk) == 0)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_EvalLocalArrayRefCached0);
}
else
{
emit_opcode(opcode::OP_EvalLocalArrayRefCached, variable_access_index(expr->obj.as_identifier, blk));
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
if (set) emit_opcode(opcode::OP_SetVariableField);
}
break;
case ast::kind::expr_call:
case ast::kind::expr_method:
default:
throw comp_error(expr->loc(), "invalid array lvalue");
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_field_ref(const ast::expr_field::ptr& expr, const block::ptr& blk, bool set)
2020-05-21 07:32:38 -04:00
{
const auto& field = expr->field->value;
2021-12-25 14:10:30 -05:00
switch (expr->obj.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::expr_level:
set ? emit_opcode(opcode::OP_SetLevelFieldVariableField, field) : emit_opcode(opcode::OP_EvalLevelFieldVariableRef, field);
break;
case ast::kind::expr_anim:
set ? emit_opcode(opcode::OP_SetAnimFieldVariableField, field) : emit_opcode(opcode::OP_EvalAnimFieldVariableRef, field);
break;
case ast::kind::expr_self:
set ? emit_opcode(opcode::OP_SetSelfFieldVariableField, field) : emit_opcode(opcode::OP_EvalSelfFieldVariableRef, field);
break;
case ast::kind::expr_array:
emit_expr_array(expr->obj.as_array, blk);
emit_opcode(opcode::OP_CastFieldObject);
emit_opcode(opcode::OP_EvalFieldVariableRef, field);
if (set) emit_opcode(opcode::OP_SetVariableField);
break;
case ast::kind::expr_field:
emit_expr_field(expr->obj.as_field, blk);
emit_opcode(opcode::OP_CastFieldObject);
emit_opcode(opcode::OP_EvalFieldVariableRef, field);
if (set) emit_opcode(opcode::OP_SetVariableField);
break;
case ast::kind::expr_identifier:
emit_opcode(opcode::OP_EvalLocalVariableObjectCached, variable_access_index(expr->obj.as_identifier, blk));
emit_opcode(opcode::OP_EvalFieldVariableRef, field);
if (set) emit_opcode(opcode::OP_SetVariableField);
break;
case ast::kind::expr_call:
emit_expr_call(expr->obj.as_call, blk);
emit_opcode(opcode::OP_CastFieldObject);
emit_opcode(opcode::OP_EvalFieldVariableRef, field);
if (set) emit_opcode(opcode::OP_SetVariableField);
break;
case ast::kind::expr_method:
emit_expr_method(expr->obj.as_method, blk);
emit_opcode(opcode::OP_CastFieldObject);
emit_opcode(opcode::OP_EvalFieldVariableRef, field);
if (set) emit_opcode(opcode::OP_SetVariableField);
break;
default:
throw comp_error(expr->obj.loc(), "not an object");
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_local_ref(const ast::expr_identifier::ptr& expr, const block::ptr& blk, bool set)
2020-05-21 07:32:38 -04:00
{
const auto itr = constants_.find(expr->value);
if (itr != constants_.end())
{
2021-12-25 14:10:30 -05:00
throw comp_error(expr->loc(), "variable name already defined as constant '" + expr->value + "'");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
if (set)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (!variable_initialized(expr, blk))
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
initialize_variable(expr, blk);
emit_opcode(opcode::OP_SetNewLocalVariableFieldCached0, variable_create_index(expr, blk));
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
else if (variable_stack_index(expr, blk) == 0)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_SetLocalVariableFieldCached0);
2020-05-21 07:32:38 -04:00
}
else
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_SetLocalVariableFieldCached, variable_access_index(expr, blk));
2020-05-21 07:32:38 -04:00
}
}
else
{
2021-12-25 14:10:30 -05:00
auto index = variable_stack_index(expr, blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (index == 0)
emit_opcode(opcode::OP_EvalLocalVariableRefCached0);
2020-05-21 07:32:38 -04:00
else
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_EvalLocalVariableRefCached, variable_access_index(expr, blk));
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_variable(const ast::expr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (expr.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::expr_array:
emit_expr_array(expr.as_array, blk);
break;
case ast::kind::expr_field:
emit_expr_field(expr.as_field, blk);
break;
case ast::kind::expr_identifier:
emit_expr_local(expr.as_identifier, blk);
break;
default:
2021-12-30 14:36:13 -05:00
throw comp_error(expr.loc(), "invalid variable type.");
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_array(const ast::expr_array::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_expr(expr->key, blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (expr->obj == ast::kind::expr_identifier)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_EvalLocalArrayCached, variable_access_index(expr->obj.as_identifier, blk));
2020-05-21 07:32:38 -04:00
}
else
{
2021-12-25 14:10:30 -05:00
emit_expr(expr->obj, blk);
emit_opcode(opcode::OP_EvalArray);
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_field(const ast::expr_field::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
const auto& field = expr->field->value;
2021-12-25 14:10:30 -05:00
switch (expr->obj.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::expr_level:
emit_opcode(opcode::OP_EvalLevelFieldVariable, field);
break;
case ast::kind::expr_anim:
emit_opcode(opcode::OP_EvalAnimFieldVariable, field);
break;
case ast::kind::expr_self:
emit_opcode(opcode::OP_EvalSelfFieldVariable, field);
break;
case ast::kind::expr_array:
emit_expr_array(expr->obj.as_array, blk);
emit_opcode(opcode::OP_CastFieldObject);
2022-01-19 14:01:12 -05:00
emit_opcode(opcode::OP_EvalFieldVariable, field);
2021-12-25 14:10:30 -05:00
break;
case ast::kind::expr_field:
emit_expr_field(expr->obj.as_field, blk);
emit_opcode(opcode::OP_CastFieldObject);
emit_opcode(opcode::OP_EvalFieldVariable, field);
break;
case ast::kind::expr_call:
emit_expr_call(expr->obj.as_call, blk);
emit_opcode(opcode::OP_CastFieldObject);
emit_opcode(opcode::OP_EvalFieldVariable, field);
break;
case ast::kind::expr_method:
emit_expr_method(expr->obj.as_method, blk);
emit_opcode(opcode::OP_CastFieldObject);
emit_opcode(opcode::OP_EvalFieldVariable, field);
break;
case ast::kind::expr_identifier:
emit_opcode(opcode::OP_EvalLocalVariableObjectCached, variable_access_index(expr->obj.as_identifier, blk));
emit_opcode(opcode::OP_EvalFieldVariable, field);
break;
default:
throw comp_error(expr->loc(), "unknown field variable object type");
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_local(const ast::expr_identifier::ptr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
// is constant ( should only allow: string, loc string, number, vector)
const auto itr = constants_.find(expr->value);
if (itr != constants_.end())
{
const auto& value = itr->second;
2021-12-25 14:10:30 -05:00
emit_expr(value, blk);
2020-05-21 07:32:38 -04:00
return;
}
// is local var
2021-12-25 14:10:30 -05:00
auto index = variable_stack_index(expr, blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
switch (index)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case 0:
emit_opcode(opcode::OP_EvalLocalVariableCached0);
break;
case 1:
emit_opcode(opcode::OP_EvalLocalVariableCached1);
break;
case 2:
emit_opcode(opcode::OP_EvalLocalVariableCached2);
break;
case 3:
emit_opcode(opcode::OP_EvalLocalVariableCached3);
break;
case 4:
emit_opcode(opcode::OP_EvalLocalVariableCached4);
break;
case 5:
emit_opcode(opcode::OP_EvalLocalVariableCached5);
break;
default:
emit_opcode(opcode::OP_EvalLocalVariableCached, variable_access_index(expr, blk));
break;
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_object(const ast::expr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (expr.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::expr_level:
emit_opcode(opcode::OP_GetLevelObject);
2020-05-21 07:32:38 -04:00
break;
2021-12-25 14:10:30 -05:00
case ast::kind::expr_anim:
emit_opcode(opcode::OP_GetAnimObject);
2020-05-21 07:32:38 -04:00
break;
2021-12-25 14:10:30 -05:00
case ast::kind::expr_self:
emit_opcode(opcode::OP_GetSelfObject);
2020-05-21 07:32:38 -04:00
break;
2021-12-25 14:10:30 -05:00
case ast::kind::expr_array:
emit_expr_array(expr.as_array, blk);
emit_opcode(opcode::OP_CastFieldObject);
2020-05-21 07:32:38 -04:00
break;
2021-12-25 14:10:30 -05:00
case ast::kind::expr_field:
emit_expr_field(expr.as_field, blk);
emit_opcode(opcode::OP_CastFieldObject);
2020-05-21 07:32:38 -04:00
break;
2021-12-25 14:10:30 -05:00
case ast::kind::expr_call:
emit_expr_call(expr.as_call, blk);
emit_opcode(opcode::OP_CastFieldObject);
2021-05-09 10:00:50 -04:00
break;
2021-12-25 14:10:30 -05:00
case ast::kind::expr_method:
emit_expr_method(expr.as_method, blk);
emit_opcode(opcode::OP_CastFieldObject);
2021-05-09 10:00:50 -04:00
break;
2021-12-25 14:10:30 -05:00
case ast::kind::expr_identifier:
emit_opcode(opcode::OP_EvalLocalVariableObjectCached, variable_access_index(expr.as_identifier, blk));
2020-05-21 07:32:38 -04:00
break;
2021-12-25 14:10:30 -05:00
default:
2021-12-30 14:36:13 -05:00
throw comp_error(expr.loc(), "not an object");
2021-12-25 14:10:30 -05:00
}
}
void compiler::emit_expr_vector(const ast::expr_vector::ptr& expr, const block::ptr& blk)
{
std::vector<std::string> data;
bool isexpr = false;
if (expr->x == ast::kind::expr_integer)
data.push_back(expr->x.as_integer->value);
else if (expr->x == ast::kind::expr_float)
data.push_back(expr->x.as_float->value);
else isexpr = true;
if (expr->y == ast::kind::expr_integer)
data.push_back(expr->y.as_integer->value);
else if (expr->y == ast::kind::expr_float)
data.push_back(expr->y.as_float->value);
else isexpr = true;
if (expr->z == ast::kind::expr_integer)
data.push_back(expr->z.as_integer->value);
else if (expr->z == ast::kind::expr_float)
data.push_back(expr->z.as_float->value);
else isexpr = true;
if (!isexpr)
{
emit_opcode(opcode::OP_GetVector, data);
}
else
{
emit_expr(expr->z, blk);
emit_expr(expr->y, blk);
emit_expr(expr->x, blk);
emit_opcode(opcode::OP_vector);
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_animation(const ast::expr_animation::ptr& expr)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (animtrees_.size() == 0)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
throw comp_error(expr->loc(), "trying to use animation without specified using animtree");
2020-05-21 07:32:38 -04:00
}
auto& tree = animtrees_.back();
2021-12-25 14:10:30 -05:00
if (tree.loaded)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetAnimation, { "''", expr->value });
2020-05-21 07:32:38 -04:00
}
else
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetAnimation, { tree.name, expr->value });
2020-05-21 07:32:38 -04:00
tree.loaded = true;
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_animtree(const ast::expr_animtree::ptr& expr)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (animtrees_.size() == 0)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
throw comp_error( expr->loc(), "trying to use animtree without specified using animtree");
2020-05-21 07:32:38 -04:00
}
auto& tree = animtrees_.back();
2021-12-25 14:10:30 -05:00
if (tree.loaded)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetAnimTree, "''");
2020-05-21 07:32:38 -04:00
}
else
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetAnimTree, tree.name);
2020-05-21 07:32:38 -04:00
tree.loaded = true;
}
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_istring(const ast::expr_istring::ptr& expr)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetIString, expr->value);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_string(const ast::expr_string::ptr& expr)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetString, expr->value);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_color(const ast::expr_color::ptr& expr)
{
std::vector<std::string> data;
std::string x, y, z;
2021-12-25 14:10:30 -05:00
if (expr->value.size() == 3)
{
2021-12-25 14:10:30 -05:00
x = "0x" + expr->value.substr(0, 1) + expr->value.substr(0, 1);
y = "0x" + expr->value.substr(1, 1) + expr->value.substr(1, 1);
z = "0x" + expr->value.substr(2, 1) + expr->value.substr(2, 1);
}
else
{
2021-12-25 14:10:30 -05:00
x = "0x" + expr->value.substr(0, 2);
y = "0x" + expr->value.substr(2, 2);
z = "0x" + expr->value.substr(4, 2);
}
2021-12-25 14:10:30 -05:00
data.push_back(utils::string::hex_to_dec(x.data()));
data.push_back(utils::string::hex_to_dec(y.data()));
data.push_back(utils::string::hex_to_dec(z.data()));
emit_opcode(opcode::OP_GetVector, data);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_float(const ast::expr_float::ptr& expr)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetFloat, expr->value);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_integer(const ast::expr_integer::ptr& expr)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto value = std::atoi(expr->value.data());
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (value == 0)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetZero);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
else if (value > 0 && value < 256)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetByte, expr->value);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
else if (value < 0 && value > -256)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetNegByte, expr->value.substr(1));
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
else if (value > 0 && value < 65536)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetUnsignedShort, expr->value);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
else if (value < 0 && value > -65536)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetNegUnsignedShort, expr->value.substr(1));
2020-05-21 07:32:38 -04:00
}
else
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetInteger, expr->value);
}
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_expr_false(const ast::expr_false::ptr&)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_GetZero);
}
void compiler::emit_expr_true(const ast::expr_true::ptr&)
{
emit_opcode(opcode::OP_GetByte, "1");
}
void compiler::emit_create_local_vars(const block::ptr& blk)
{
if ( blk->local_vars_create_count != blk->local_vars_public_count )
{
for (auto i = blk->local_vars_create_count; i < blk->local_vars_public_count; i++)
{
auto data = utils::string::va("%d", blk->local_vars.at(i).create);
emit_opcode(opcode::OP_CreateLocalVariable, data);
blk->local_vars.at(i).init = true;
}
blk->local_vars_create_count = blk->local_vars_public_count;
}
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_remove_local_vars(const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (blk->abort == abort_t::abort_none)
{
auto count = blk->local_vars_create_count - blk->local_vars_public_count;
if (count > 0)
{
auto data = utils::string::va("%d", count);
emit_opcode(opcode::OP_RemoveLocalVariables, data);
}
}
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::emit_opcode(opcode op)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
function_->instructions.push_back(std::make_unique<instruction>());
2022-01-19 14:01:12 -05:00
2020-05-21 07:32:38 -04:00
auto& inst = function_->instructions.back();
inst->opcode = static_cast<std::uint8_t>(op);
inst->size = opcode_size(std::uint8_t(op));
inst->index = index_;
index_ += inst->size;
}
2021-12-25 14:10:30 -05:00
void compiler::emit_opcode(opcode op, const std::string& data)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
function_->instructions.push_back(std::make_unique<instruction>());
2022-01-19 14:01:12 -05:00
2020-05-21 07:32:38 -04:00
auto& inst = function_->instructions.back();
inst->opcode = static_cast<std::uint8_t>(op);
inst->size = opcode_size(std::uint8_t(op));
inst->index = index_;
inst->data.push_back(data);
index_ += inst->size;
}
2021-12-25 14:10:30 -05:00
void compiler::emit_opcode(opcode op, const std::vector<std::string>& data)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
function_->instructions.push_back(std::make_unique<instruction>());
2022-01-19 14:01:12 -05:00
2020-05-21 07:32:38 -04:00
auto& inst = function_->instructions.back();
inst->opcode = static_cast<std::uint8_t>(op);
inst->size = opcode_size(std::uint8_t(op));
inst->index = index_;
inst->data = data;
index_ += inst->size;
}
2021-12-25 14:10:30 -05:00
void compiler::process_thread(const ast::decl_thread::ptr& decl, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
process_expr_parameters(decl->params, blk);
process_stmt_list(decl->stmt, blk);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::process_stmt(const ast::stmt& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (stmt.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::stmt_list:
process_stmt_list(stmt.as_list, blk);
break;
case ast::kind::stmt_expr:
process_stmt_expr(stmt.as_expr, blk);
break;
case ast::kind::stmt_assign:
process_stmt_assign(stmt.as_assign, blk);
break;
case ast::kind::stmt_waittill:
process_stmt_waittill(stmt.as_waittill, blk);
break;
case ast::kind::stmt_if:
process_stmt_if(stmt.as_if, blk);
break;
case ast::kind::stmt_ifelse:
process_stmt_ifelse(stmt.as_ifelse, blk);
break;
case ast::kind::stmt_while:
process_stmt_while(stmt.as_while, blk);
break;
case ast::kind::stmt_dowhile:
process_stmt_dowhile(stmt.as_dowhile, blk);
break;
case ast::kind::stmt_for:
process_stmt_for(stmt.as_for, blk);
break;
case ast::kind::stmt_foreach:
process_stmt_foreach(stmt.as_foreach, blk);
break;
case ast::kind::stmt_switch:
process_stmt_switch(stmt.as_switch, blk);
break;
case ast::kind::stmt_break:
process_stmt_break(stmt.as_break, blk);
break;
case ast::kind::stmt_continue:
process_stmt_continue(stmt.as_continue, blk);
break;
case ast::kind::stmt_return:
process_stmt_return(stmt.as_return, blk);
break;
case ast::kind::stmt_call:
case ast::kind::stmt_endon:
case ast::kind::stmt_notify:
case ast::kind::stmt_wait:
case ast::kind::stmt_waittillmatch:
case ast::kind::stmt_waittillframeend:
case ast::kind::stmt_case:
case ast::kind::stmt_default:
case ast::kind::stmt_breakpoint:
case ast::kind::stmt_prof_begin:
case ast::kind::stmt_prof_end:
break;
default:
2021-12-30 14:36:13 -05:00
throw comp_error(stmt.loc(), "unknown statement");
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::process_stmt_list(const ast::stmt_list::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
for (const auto& entry : stmt->list)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
process_stmt(entry, blk);
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::process_stmt_expr(const ast::stmt_expr::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (stmt->expr.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::expr_increment:
process_expr(stmt->expr.as_increment->lvalue, blk);
break;
case ast::kind::expr_decrement:
process_expr(stmt->expr.as_decrement->lvalue, blk);
break;
case ast::kind::expr_assign_equal:
case ast::kind::expr_assign_add:
case ast::kind::expr_assign_sub:
case ast::kind::expr_assign_mul:
case ast::kind::expr_assign_div:
case ast::kind::expr_assign_mod:
case ast::kind::expr_assign_shift_left:
case ast::kind::expr_assign_shift_right:
case ast::kind::expr_assign_bitwise_or:
case ast::kind::expr_assign_bitwise_and:
case ast::kind::expr_assign_bitwise_exor:
process_expr(stmt->expr.as_assign->lvalue, blk);
break;
case ast::kind::null:
break;
default:
throw comp_error(stmt->loc(), "unknown expr statement expression");
2020-05-21 07:32:38 -04:00
}
}
2022-01-19 14:01:12 -05:00
2021-12-25 14:10:30 -05:00
void compiler::process_stmt_assign(const ast::stmt_assign::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (stmt->expr.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::expr_increment:
process_expr(stmt->expr.as_increment->lvalue, blk);
break;
case ast::kind::expr_decrement:
process_expr(stmt->expr.as_decrement->lvalue, blk);
break;
case ast::kind::expr_assign_equal:
case ast::kind::expr_assign_add:
case ast::kind::expr_assign_sub:
case ast::kind::expr_assign_mul:
case ast::kind::expr_assign_div:
case ast::kind::expr_assign_mod:
case ast::kind::expr_assign_shift_left:
case ast::kind::expr_assign_shift_right:
case ast::kind::expr_assign_bitwise_or:
case ast::kind::expr_assign_bitwise_and:
case ast::kind::expr_assign_bitwise_exor:
process_expr(stmt->expr.as_assign->lvalue, blk);
break;
default:
throw comp_error(stmt->loc(), "unknown assign statement expression");
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::process_stmt_waittill(const ast::stmt_waittill::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
for (const auto& entry : stmt->args->list)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (entry != ast::kind::expr_identifier)
{
2021-12-30 14:36:13 -05:00
throw comp_error(entry.loc(), "illegal waittill param, must be a local variable");
2021-12-25 14:10:30 -05:00
}
2022-01-19 14:01:12 -05:00
2021-12-25 14:10:30 -05:00
register_variable(entry.as_identifier->value, blk);
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::process_stmt_if(const ast::stmt_if::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
stmt->blk = std::make_unique<block>();
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
blk->copy(stmt->blk);
process_stmt(stmt->stmt, stmt->blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
std::vector<block*> childs({ stmt->blk.get() });
blk->merge(childs);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::process_stmt_ifelse(const ast::stmt_ifelse::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
std::vector<block*> childs;
2020-05-21 07:32:38 -04:00
auto abort = abort_t::abort_return;
2021-12-25 14:10:30 -05:00
stmt->blk_if = std::make_unique<block>();
stmt->blk_else = std::make_unique<block>();
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
blk->copy(stmt->blk_if);
process_stmt(stmt->stmt_if, stmt->blk_if);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (stmt->blk_if->abort <= abort_t::abort_return)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
abort = stmt->blk_if->abort;
if (abort == abort_t::abort_none)
childs.push_back(stmt->blk_if.get());
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
blk->copy(stmt->blk_else);
process_stmt(stmt->stmt_else, stmt->blk_else);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (stmt->blk_else->abort <= abort)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
abort = stmt->blk_else->abort;
if (abort == abort_t::abort_none)
childs.push_back(stmt->blk_else.get());
2020-05-21 07:32:38 -04:00
}
2022-01-19 14:01:12 -05:00
2021-12-25 14:10:30 -05:00
if (blk->abort == abort_t::abort_none)
blk->abort = abort;
blk->append(childs);
blk->merge(childs);
}
void compiler::process_stmt_while(const ast::stmt_while::ptr& stmt, const block::ptr& blk)
{
bool const_cond = is_constant_condition(stmt->test);
auto old_breaks = break_blks_;
auto old_continues = continue_blks_;
break_blks_.clear();
continue_blks_.clear();
stmt->blk = std::make_unique<block>();
blk->copy(stmt->blk);
process_stmt(stmt->stmt, stmt->blk);
continue_blks_.push_back(stmt->blk.get());
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (auto i = 0; i < continue_blks_.size(); i++)
blk->append({continue_blks_.at(i)});
if (const_cond) blk->append(break_blks_);
blk->merge({stmt->blk.get()});
break_blks_ = old_breaks;
continue_blks_ = old_continues;
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::process_stmt_dowhile(const ast::stmt_dowhile::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
bool const_cond = is_constant_condition(stmt->test);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
auto old_breaks = break_blks_;
auto old_continues = continue_blks_;
break_blks_.clear();
continue_blks_.clear();
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
stmt->blk = std::make_unique<block>();
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
blk->copy(stmt->blk);
process_stmt(stmt->stmt, stmt->blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
continue_blks_.push_back(stmt->blk.get());
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (auto i = 0; i < continue_blks_.size(); i++)
blk->append({continue_blks_.at(i)});
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (const_cond) blk->append(break_blks_);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
blk->merge({stmt->blk.get()});
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
break_blks_ = old_breaks;
continue_blks_ = old_continues;
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::process_stmt_for(const ast::stmt_for::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
bool const_cond = is_constant_condition(stmt->test);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
stmt->blk = std::make_unique<block>();
stmt->blk_iter = std::make_unique<block>();
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
process_stmt(stmt->init, blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
auto old_breaks = break_blks_;
auto old_continues = continue_blks_;
break_blks_.clear();
continue_blks_.clear();
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
blk->copy(stmt->blk);
blk->copy(stmt->blk_iter);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
process_stmt(stmt->stmt, stmt->blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
continue_blks_.push_back(stmt->blk.get());
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (auto i = 0; i < continue_blks_.size(); i++)
blk->append({continue_blks_.at(i)});
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
process_stmt(stmt->iter, stmt->blk_iter);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
blk->append({ stmt->blk_iter.get() });
blk->merge({ stmt->blk_iter.get() });
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (const_cond) blk->append(break_blks_);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
blk->merge({stmt->blk.get()});
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
break_blks_ = old_breaks;
continue_blks_ = old_continues;
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::process_stmt_foreach(const ast::stmt_foreach::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
auto array_name = utils::string::va("temp_%d", ++label_idx_);
auto key_name = utils::string::va("temp_%d", ++label_idx_);
2021-12-25 14:10:30 -05:00
stmt->array = ast::expr(std::make_unique<ast::expr_identifier>(stmt->loc(), array_name));
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (!stmt->use_key)
stmt->key_expr = ast::expr(std::make_unique<ast::expr_identifier>(stmt->loc(), key_name));
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
key_name = stmt->key_expr.as_identifier->value;
2020-05-21 07:32:38 -04:00
// calculate variables
2021-12-25 14:10:30 -05:00
stmt->ctx = std::make_unique<block>();
stmt->ctx_post = std::make_unique<block>();
2020-05-21 07:32:38 -04:00
// calculate pre_expr variables
2021-12-25 14:10:30 -05:00
process_expr(stmt->array, blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
auto old_breaks = break_blks_;
auto old_continues = continue_blks_;
break_blks_.clear();
continue_blks_.clear();
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
blk->copy(stmt->ctx);
blk->copy(stmt->ctx_post);
2020-05-21 07:32:38 -04:00
// calculate stmt variables & add missing array access as first stmt
2021-12-25 14:10:30 -05:00
process_expr(stmt->value_expr, stmt->ctx);
process_stmt(stmt->stmt, stmt->ctx);
2022-01-19 14:01:12 -05:00
2021-12-25 14:10:30 -05:00
continue_blks_.push_back(stmt->ctx.get());
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (auto i = 0; i < continue_blks_.size(); i++)
blk->append({continue_blks_.at(i)});
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
process_expr(stmt->key_expr, stmt->ctx_post);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
blk->append({ stmt->ctx_post.get() });
blk->merge({ stmt->ctx_post.get() });
blk->merge({stmt->ctx.get()});
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
break_blks_ = old_breaks;
continue_blks_ = old_continues;
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::process_stmt_switch(const ast::stmt_switch::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto stmt_list = std::make_unique<ast::stmt_list>(stmt->stmt->loc());
auto current_case = ast::stmt(nullptr);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
auto num = stmt->stmt->list.size();
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (auto i = 0; i < num; i++)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto& entry = stmt->stmt->list[0];
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (entry == ast::kind::stmt_case || entry == ast::kind::stmt_default)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (current_case.as_node != nullptr)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
stmt_list->list.push_back(std::move(current_case));
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
current_case = std::move(stmt->stmt->list[0]);
stmt->stmt->list.erase(stmt->stmt->list.begin());
2020-05-21 07:32:38 -04:00
}
else
{
2021-12-25 14:10:30 -05:00
if (current_case.as_node != nullptr)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (current_case == ast::kind::stmt_case)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
current_case.as_case->stmt->list.push_back(std::move(entry));
stmt->stmt->list.erase(stmt->stmt->list.begin());
2020-05-21 07:32:38 -04:00
}
else
{
2021-12-25 14:10:30 -05:00
current_case.as_default->stmt->list.push_back(std::move(entry));
stmt->stmt->list.erase(stmt->stmt->list.begin());
2020-05-21 07:32:38 -04:00
}
}
else
{
2021-12-30 14:36:13 -05:00
throw comp_error(entry.loc(), "missing case statement");
2020-05-21 07:32:38 -04:00
}
}
}
2021-12-25 14:10:30 -05:00
if (current_case.as_node != nullptr)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
stmt_list->list.push_back(std::move(current_case));
2020-05-21 07:32:38 -04:00
}
// calculate variables
2021-12-25 14:10:30 -05:00
stmt->ctx = std::make_unique<block>();
std::vector<block*> childs;
2020-05-21 07:32:38 -04:00
auto abort = abort_t::abort_return;
bool has_default = false;
2021-12-25 14:10:30 -05:00
block* default_ctx = nullptr;
auto old_breaks = break_blks_;
break_blks_.clear();
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (auto i = 0; i < stmt_list->list.size(); i++)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto& entry = stmt_list->list[i];
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (entry == ast::kind::stmt_case)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
entry.as_case->blk = std::make_unique<block>();
blk->copy(entry.as_case->blk);
process_stmt_list(entry.as_case->stmt, entry.as_case->blk);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (entry.as_case->blk->abort != abort_t::abort_none)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (entry.as_case->blk->abort == abort_t::abort_break )
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
entry.as_case->blk->abort = abort_t::abort_none;
2020-05-21 07:32:38 -04:00
abort = abort_t::abort_none;
2021-12-25 14:10:30 -05:00
childs.push_back(entry.as_case->blk.get());
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
else if (entry.as_case->blk->abort <= abort )
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
abort = entry.as_case->blk->abort;
2020-05-21 07:32:38 -04:00
}
}
}
2021-12-25 14:10:30 -05:00
else if (entry == ast::kind::stmt_default)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
entry.as_default->blk = std::make_unique<block>();
blk->copy(entry.as_default->blk);
process_stmt_list(entry.as_default->stmt, entry.as_default->blk);
2020-05-21 07:32:38 -04:00
has_default = true;
2021-12-25 14:10:30 -05:00
default_ctx = entry.as_default->blk.get();
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (entry.as_default->blk->abort != abort_t::abort_none)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (entry.as_default->blk->abort == abort_t::abort_break )
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
entry.as_default->blk->abort = abort_t::abort_none;
2020-05-21 07:32:38 -04:00
abort = abort_t::abort_none;
2021-12-25 14:10:30 -05:00
childs.push_back(entry.as_default->blk.get());
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
else if (entry.as_default->blk->abort <= abort )
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
abort = entry.as_default->blk->abort;
2020-05-21 07:32:38 -04:00
}
}
}
}
stmt->stmt =std::move(stmt_list);
2021-12-25 14:10:30 -05:00
if (has_default)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (default_ctx->abort == abort_t::abort_none)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
break_blks_.push_back(default_ctx);
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
if (blk->abort == abort_t::abort_none)
blk->abort = abort;
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
blk->append(break_blks_);
blk->merge(childs);
}
break_blks_ = old_breaks;
}
void compiler::process_stmt_break(const ast::stmt_break::ptr& stmt, const block::ptr& blk)
{
if (blk->abort == abort_t::abort_none)
{
break_blks_.push_back(blk.get());
blk->abort = abort_t::abort_break;
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
}
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
void compiler::process_stmt_continue(const ast::stmt_continue::ptr& stmt, const block::ptr& blk)
{
if (blk->abort == abort_t::abort_none)
{
continue_blks_.push_back(blk.get());
blk->abort = abort_t::abort_continue;
}
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::process_stmt_return(const ast::stmt_return::ptr& stmt, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (blk->abort == abort_t::abort_none)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
blk->abort = abort_t::abort_return;
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::process_expr(const ast::expr& expr, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (expr == ast::kind::expr_identifier)
{
register_variable(expr.as_identifier->value, blk);
}
else if (expr == ast::kind::expr_array)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
process_expr(expr.as_array->obj, blk);
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::process_expr_parameters(const ast::expr_parameters::ptr& decl, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
for (const auto& entry : decl->list)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
register_variable(entry->value, blk);
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
void compiler::register_variable(const std::string& name, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto it = std::find_if (blk->local_vars.begin(), blk->local_vars.end(),
2020-05-21 07:32:38 -04:00
[&](const gsc::local_var& v) { return v.name == name; });
2021-12-25 14:10:30 -05:00
if (it == blk->local_vars.end())
2020-05-21 07:32:38 -04:00
{
auto found = false;
2021-12-25 14:10:30 -05:00
for (std::size_t i = 0; i < local_stack_.size(); i++)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (local_stack_[i] == name)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
blk->local_vars.push_back({ name, static_cast<uint8_t>(i), false });
2020-05-21 07:32:38 -04:00
found = true;
break;
}
}
2021-12-25 14:10:30 -05:00
if (!found)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
blk->local_vars.push_back({ name, stack_idx_, false });
2020-05-21 07:32:38 -04:00
local_stack_.push_back(name);
stack_idx_++;
}
}
}
2021-12-25 14:10:30 -05:00
void compiler::initialize_variable(const ast::expr_identifier::ptr& name, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
for (std::uint32_t i = 0; i < blk->local_vars.size(); i++)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (blk->local_vars[i].name == name->value)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (!blk->local_vars[i].init)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
for (std::uint32_t j = 0; j < i; j++)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (!blk->local_vars[j].init)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_CreateLocalVariable, utils::string::va("%d", blk->local_vars[j].create));
blk->local_vars[j].init = true;
2020-05-21 07:32:38 -04:00
//ctx->local_vars_create_count++;
}
}
2021-12-25 14:10:30 -05:00
blk->local_vars[i].init = true;
blk->local_vars_create_count = i + 1;
2020-05-21 07:32:38 -04:00
return;
}
}
}
2021-12-25 14:10:30 -05:00
throw comp_error(name->loc(), "local variable '" + name->value + "' not found.");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
void compiler::create_variable(const ast::expr_identifier::ptr& name, const block::ptr& blk)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
for (std::size_t i = 0; i < blk->local_vars.size(); i++)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
auto& var = blk->local_vars.at(i);
if (var.name == name->value)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (!var.init)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
emit_opcode(opcode::OP_CreateLocalVariable, utils::string::va("%d", var.create));
2020-05-21 07:32:38 -04:00
var.init = true;
2021-12-25 14:10:30 -05:00
blk->local_vars_create_count++;
2020-05-21 07:32:38 -04:00
}
return;
}
}
2021-12-25 14:10:30 -05:00
throw comp_error(name->loc(), "local variable '" + name->value + "' not found.");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
auto compiler::variable_stack_index(const ast::expr_identifier::ptr& name, const block::ptr& blk) -> std::uint8_t
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
for (std::size_t i = 0; i < blk->local_vars.size(); i++)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (blk->local_vars[i].name == name->value)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (blk->local_vars.at(i).init)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
return blk->local_vars_create_count - 1 - i;
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
throw comp_error(name->loc(), "local variable '" + name->value + "' not initialized.");
2022-01-19 14:01:12 -05:00
}
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
throw comp_error(name->loc(), "local variable '" + name->value + "' not found.");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
auto compiler::variable_create_index(const ast::expr_identifier::ptr& name, const block::ptr& blk) -> std::string
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
for (std::size_t i = 0; i < blk->local_vars.size(); i++)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (blk->local_vars[i].name == name->value)
return utils::string::va("%d", blk->local_vars[i].create);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
throw comp_error(name->loc(), "local variable '" + name->value + "' not found.");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
auto compiler::variable_access_index(const ast::expr_identifier::ptr& name, const block::ptr& blk) -> std::string
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
for (std::size_t i = 0; i < blk->local_vars.size(); i++)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (blk->local_vars[i].name == name->value)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (blk->local_vars.at(i).init)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
return utils::string::va("%d", blk->local_vars_create_count - 1 - i);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
throw comp_error(name->loc(), "local variable '" + name->value + "' not initialized.");
2022-01-19 14:01:12 -05:00
}
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
throw comp_error(name->loc(), "local variable '" + name->value + "' not found.");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
auto compiler::variable_initialized(const ast::expr_identifier::ptr& name, const block::ptr& blk) -> bool
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
for (std::size_t i = 0; i < blk->local_vars.size(); i++)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (blk->local_vars[i].name == name->value)
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
return blk->local_vars.at(i).init;
2020-05-21 07:32:38 -04:00
}
}
2021-12-25 14:10:30 -05:00
throw comp_error(name->loc(), "local variable '" + name->value + "' not found.");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
auto compiler::resolve_function_type(const ast::expr_function::ptr& expr) -> ast::call::type
2021-05-10 09:11:46 -04:00
{
2021-12-25 14:10:30 -05:00
if (expr->path->value != "")
return ast::call::type::far;
auto& name = expr->name->value;
if (resolver::find_function(name) || resolver::find_method(name))
return ast::call::type::builtin;
for (const auto& entry : local_functions_)
{
if (entry == name)
return ast::call::type::local;
}
for (const auto& inc : includes_)
2021-05-10 09:11:46 -04:00
{
2021-12-25 14:10:30 -05:00
for (const auto& fun : inc.funcs)
2021-05-10 09:11:46 -04:00
{
2021-12-25 14:10:30 -05:00
if (name == fun)
2021-05-10 09:11:46 -04:00
{
2021-12-25 14:10:30 -05:00
expr->path->value = inc.name;
return ast::call::type::far;
2021-05-10 09:11:46 -04:00
}
}
}
2021-12-25 14:10:30 -05:00
throw comp_error(expr->loc(), "couldn't determine function type");
2021-05-10 09:11:46 -04:00
}
2021-12-25 14:10:30 -05:00
auto compiler::resolve_reference_type(const ast::expr_reference::ptr& expr, bool& method) -> ast::call::type
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
if (expr->path->value != "")
return ast::call::type::far;
auto& name = expr->name->value;
if (resolver::find_function(name))
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
method = false;
return ast::call::type::builtin;
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
if (resolver::find_method(name))
{
method = true;
return ast::call::type::builtin;
}
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (const auto& entry : local_functions_)
{
if (entry == name)
return ast::call::type::local;
}
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
for (const auto& inc : includes_)
{
for (const auto& fun : inc.funcs)
{
if (name == fun)
{
expr->path->value = inc.name;
return ast::call::type::far;
}
}
}
2020-05-21 07:32:38 -04:00
2021-12-25 14:10:30 -05:00
throw comp_error(expr->loc(), "couldn't determine function reference type");
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
auto compiler::is_constant_condition(const ast::expr& expr) -> bool
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
switch (expr.kind())
2020-05-21 07:32:38 -04:00
{
2021-12-25 14:10:30 -05:00
case ast::kind::null:
case ast::kind::expr_true:
2020-05-21 07:32:38 -04:00
return true;
2021-12-25 14:10:30 -05:00
case ast::kind::expr_false:
2021-12-30 14:36:13 -05:00
throw comp_error(expr.loc(), "condition can't be always false!");
2021-12-25 14:10:30 -05:00
case ast::kind::expr_integer:
2020-05-21 07:32:38 -04:00
{
auto num = std::stoi(expr.as_integer->value);
2021-12-25 14:10:30 -05:00
if (num != 0)
2020-05-21 07:32:38 -04:00
return true;
else
2021-12-30 14:36:13 -05:00
throw comp_error(expr.loc(), "condition can't be always false!");
2020-05-21 07:32:38 -04:00
}
default:
break;
}
return false;
}
auto compiler::create_label() -> std::string
{
label_idx_++;
auto name = utils::string::va("loc_%d", label_idx_);
return name;
}
auto compiler::insert_label() -> std::string
{
const auto itr = function_->labels.find(index_);
if (itr != function_->labels.end())
{
return itr->second;
}
else
{
label_idx_++;
auto name = utils::string::va("loc_%d", label_idx_);
function_->labels.insert({index_, name});
return name;
}
}
void compiler::insert_label(const std::string& name)
{
const auto itr = function_->labels.find(index_);
if (itr != function_->labels.end())
{
2021-12-25 14:10:30 -05:00
for (auto& inst : function_->instructions)
2020-05-21 07:32:38 -04:00
{
switch (opcode(inst->opcode))
{
2021-12-25 14:10:30 -05:00
case opcode::OP_JumpOnFalse:
case opcode::OP_JumpOnTrue:
case opcode::OP_JumpOnFalseExpr:
case opcode::OP_JumpOnTrueExpr:
case opcode::OP_jump:
case opcode::OP_jumpback:
case opcode::OP_switch:
if (inst->data[0] == name)
inst->data[0] = itr->second;
break;
case opcode::OP_endswitch:
default:
break;
2020-05-21 07:32:38 -04:00
}
}
}
else
{
function_->labels.insert({index_, name});
}
}
2022-01-19 14:01:12 -05:00
gsc::include_t compiler::include_maps_mp_utility_ =
2020-05-21 07:32:38 -04:00
{
2022-01-19 14:01:12 -05:00
"maps/mp/_utility",
2020-05-21 07:32:38 -04:00
{
"exploder_sound",
"_beginlocationselection",
"stoplocationselection",
"endselectiononemp",
"endselectiononaction",
"endselectiononendgame",
"isattachment",
"getattachmenttype",
"delaythread",
"delaythread_proc",
"getplant",
"orienttonormal",
"deleteplacedentity",
"playsoundonplayers",
"sortlowermessages",
"addlowermessage",
"removelowermessage",
"getlowermessage",
"setlowermessage",
"updatelowermessage",
"clearondeath",
"clearafterfade",
"clearlowermessage",
"clearlowermessages",
"printonteam",
"printboldonteam",
"printboldonteamarg",
"printonteamarg",
"printonplayers",
"printandsoundoneveryone",
"printandsoundonteam",
"printandsoundonplayer",
"_playlocalsound",
"dvarintvalue",
"dvarfloatvalue",
"play_sound_on_tag",
"getotherteam",
"wait_endon",
"initpersstat",
"getpersstat",
"incpersstat",
"setpersstat",
"initplayerstat",
"incplayerstat",
"setplayerstat",
"getplayerstat",
"getplayerstattime",
"setplayerstatifgreater",
"setplayerstatiflower",
"updatepersratio",
"updatepersratiobuffered",
"waittillslowprocessallowed",
"waitfortimeornotify",
"isexcluded",
"leaderdialog",
"leaderdialogbothteams",
"leaderdialogonplayers",
"leaderdialogonplayer",
"playleaderdialogonplayer",
"updatemainmenu",
"updateobjectivetext",
"setobjectivetext",
"setobjectivescoretext",
"setobjectivehinttext",
"getobjectivetext",
"getobjectivescoretext",
"getobjectivehinttext",
"gettimepassed",
"getsecondspassed",
"getminutespassed",
"clearkillcamstate",
"isinkillcam",
"isvalidclass",
"getvalueinrange",
"waitfortimeornotifies",
"closemenus",
"logxpgains",
"registerroundswitchdvar",
"registerroundlimitdvar",
"registerwinlimitdvar",
"registerscorelimitdvar",
"registertimelimitdvar",
"registerhalftimedvar",
"registernumlivesdvar",
"setovertimelimitdvar",
"get_damageable_player",
"get_damageable_sentry",
"get_damageable_grenade",
"get_damageable_mine",
"get_damageable_player_pos",
"getstancecenter",
"get_damageable_grenade_pos",
"getdvarvec",
"strip_suffix",
"_takeweaponsexcept",
"savedata",
"restoredata",
"_setactionslot",
"isfloat",
"registerwatchdvarint",
"registerwatchdvarfloat",
"registerwatchdvar",
"setoverridewatchdvar",
"getwatcheddvar",
"updatewatcheddvars",
"isroundbased",
"islastround",
"wasonlyround",
"waslastround",
"hitroundlimit",
"hitscorelimit",
"hitwinlimit",
"getscorelimit",
"getroundswon",
"isobjectivebased",
"gettimelimit",
"gethalftime",
"inovertime",
"gamehasstarted",
"getaverageorigin",
"getlivingplayers",
"setusingremote",
"getremotename",
"freezecontrolswrapper",
"clearusingremote",
"isusingremote",
"queuecreate",
"queueadd",
"queueremovefirst",
"_giveweapon",
"_hasperk",
"giveperk",
"_setperk",
"_setextraperks",
"_unsetperk",
"_unsetextraperks",
"_clearperks",
"quicksort",
"quicksortmid",
"swap",
"_suicide",
"isreallyalive",
"playdeathsound",
"rankingenabled",
"privatematch",
"matchmakinggame",
"setaltsceneobj",
"endsceneondeath",
"getgametypenumlives",
"givecombathigh",
"arrayinsertion",
"getproperty",
"getintproperty",
"getfloatproperty",
"statusmenu",
"ischangingweapon",
"killshouldaddtokillstreak",
"streakshouldchain",
"isjuggernaut",
"iskillstreakweapon",
"isenvironmentweapon",
"getweaponclass",
"isdeathstreakweapon",
"getbaseweaponname",
"fixakimbostring",
"playsoundinspace",
"limitdecimalplaces",
"rounddecimalplaces",
"playerforclientid",
"isrested",
"stringtofloat",
"setselfusable",
"maketeamusable",
"_updateteamusable",
"makeenemyusable",
"_updateenemyusable",
"getnextlifeid",
"initgameflags",
"gameflaginit",
"gameflag",
"gameflagset",
"gameflagclear",
"gameflagwait",
"isprimarydamage",
"isbulletdamage",
"initlevelflags",
"levelflaginit",
"levelflag",
"levelflagset",
"levelflagclear",
"levelflagwait",
"levelflagwaitopen",
"getweaponattachments",
"isemped",
"isairdenied",
"isnuked",
"getplayerforguid",
"teamplayercardsplash",
"iscacprimaryweapon",
"iscacsecondaryweapon",
"getlastlivingplayer",
"getpotentiallivingplayers",
"waittillrecoveredhealth",
"attachmentmap",
"validateattachment",
"_objective_delete",
"touchingbadtrigger",
"setthirdpersondof",
"killtrigger",
"findisfacing",
"combinearrays",
"setrecoilscale",
"cleanarray",
"notusableforjoiningplayers",
"isstrstart",
"validateusestreak",
"currentactivevehiclecount",
"maxvehiclesallowed",
"incrementfauxvehiclecount",
"decrementfauxvehiclecount",
"lightweightscalar",
"allowteamchoice",
"allowclasschoice",
"isbuffunlockedforweapon",
"isbuffequippedonweapon",
"setcommonrulesfrommatchrulesdata",
"reinitializematchrulesonmigration",
"getmatchrulesspecialclass",
"recipeclassapplyjuggernaut",
}
};
gsc::include_t compiler::include_common_scripts_createfx_ =
{
"common_scripts/_createfx",
{
"createeffect",
"getloopeffectdelaydefault",
"getoneshoteffectdelaydefault",
"getexploderdelaydefault",
"getintervalsounddelaymindefault",
"getintervalsounddelaymaxdefault",
"add_effect",
"createloopsound",
"createintervalsound",
"createnewexploder",
"createexploderex",
"set_origin_and_angles",
"set_forward_and_up_vectors",
"createfx_common",
"createfxlogic",
"copy_angles_of_selected_ents",
"reset_axis_of_selected_ents",
"last_selected_entity_has_changed",
"createfx_showorigin",
"drop_selection_to_ground",
"set_off_exploders",
"draw_distance",
"createfx_autosave",
"rotate_over_time",
"delete_pressed",
"remove_selected_option",
"remove_option",
"delete_selection",
"move_selection_to_cursor",
"insert_effect",
"show_help",
"select_last_entity",
"select_all_exploders_of_currently_selected",
"copy_ents",
"post_entity_creation_function",
"paste_ents",
"add_and_select_entity",
"get_center_of_array",
"ent_draw_axis",
"rotation_is_occuring",
"print_fx_options",
"entity_highlight_disable",
"entity_highlight_enable",
"toggle_createfx_drawing",
"manipulate_createfx_ents",
"clear_settable_fx",
"reset_fx_hud_colors",
"button_is_held",
"button_is_clicked",
"toggle_entity_selection",
"select_entity",
"ent_is_highlighted",
"deselect_entity",
"index_is_selected",
"ent_is_selected",
"clear_entity_selection",
"draw_axis",
"clear_fx_hudelements",
"set_fx_hudelement",
"createfx_centerprint",
"createfx_centerprint_thread",
"buttondown",
"buttonpressed_internal",
"get_selected_move_vector",
"process_button_held_and_clicked",
"locked",
"kb_locked",
"add_button",
"add_kb_button",
"set_anglemod_move_vector",
"cfxprintlnstart",
"cfxprintln",
"cfxprintlnend",
"update_selected_entities",
"hack_start",
"get_player",
"createfx_orgranize_array",
"stop_fx_looper",
"stop_loopsound",
"func_get_level_fx",
"restart_fx_looper",
"process_fx_rotater",
"generate_fx_log",
}
};
2022-01-19 14:01:12 -05:00
gsc::include_t compiler::include_common_scripts_utility_ =
2020-05-21 07:32:38 -04:00
{
2022-01-19 14:01:12 -05:00
"common_scripts/utility",
2020-05-21 07:32:38 -04:00
{
"scriptprintln",
"debugprintln",
"draw_debug_line",
"waittillend",
"noself_func",
"self_func",
"randomvector",
"randomvectorrange",
"angle_dif",
"sign",
"track",
"get_enemy_team",
"clear_exception",
"set_exception",
"set_all_exceptions",
"cointoss",
"choose_from_weighted_array",
"get_cumulative_weights",
"waittill_string",
"waittill_multiple",
"waittill_multiple_ents",
"waittill_any_return",
"waittill_any_timeout",
"_timeout",
"waittill_any",
"waittill_any_ents",
"isflashed",
"flag_exist",
"flag",
"init_flags",
"flag_init",
"empty_init_func",
"issuffix",
"flag_set",
"assign_unique_id",
"flag_wait",
"flag_clear",
"flag_waitopen",
"waittill_either",
"array_thread",
"array_call",
"array_thread4",
"array_thread5",
"trigger_on",
"trigger_on_proc",
"trigger_off",
"trigger_off_proc",
"set_trigger_flag_permissions",
"update_trigger_based_on_flags",
"create_flags_and_return_tokens",
"init_trigger_flags",
"getstruct",
"getstructarray",
"struct_class_init",
"fileprint_start",
"fileprint_map_start",
"fileprint_map_header",
"fileprint_map_keypairprint",
"fileprint_map_entity_start",
"fileprint_map_entity_end",
"fileprint_radiant_vec",
"array_remove",
"array_remove_array",
"array_removeundefined",
"array_levelthread",
"array_levelcall",
"add_to_array",
"flag_assert",
"flag_wait_either",
"flag_wait_either_return",
"flag_wait_any",
"flag_wait_any_return",
"flag_wait_all",
"flag_wait_or_timeout",
"flag_waitopen_or_timeout",
"wait_for_flag_or_time_elapses",
"delaycall",
"delaycall_proc",
"noself_delaycall",
"noself_delaycall_proc",
"issp",
"issp_towerdefense",
"string_starts_with",
"plot_points",
"draw_line_for_time",
"array_combine",
"flat_angle",
"flat_origin",
"draw_arrow_time",
"get_linked_ents",
"get_linked_vehicle_nodes",
"get_linked_ent",
"get_linked_vehicle_node",
"get_links",
"run_thread_on_targetname",
"run_thread_on_noteworthy",
"draw_arrow",
"getfx",
"fxexists",
"print_csv_asset",
"fileprint_csv_start",
"_loadfx",
"getlastweapon",
"playerunlimitedammothread",
"isusabilityenabled",
"_disableusability",
"_enableusability",
"resetusability",
"_disableweapon",
"_enableweapon",
"isweaponenabled",
"_disableweaponswitch",
"_enableweaponswitch",
"isweaponswitchenabled",
"_disableoffhandweapons",
"_enableoffhandweapons",
"isoffhandweaponenabled",
"random",
"spawn_tag_origin",
"waittill_notify_or_timeout",
"fileprint_launcher_start_file",
"fileprint_launcher",
"fileprint_launcher_end_file",
"launcher_write_clipboard",
"isdestructible",
"pauseeffect",
"activate_individual_exploder",
"waitframe",
"brush_delete",
"brush_throw",
"get_target_ent",
"brush_show",
"exploder_earthquake",
"do_earthquake",
"exploder_rumble",
"exploder_delay",
"exploder_damage",
"effect_loopsound",
"play_loopsound_in_space",
"sound_effect",
"effect_soundalias",
"play_sound_in_space",
"cannon_effect",
"exploder_playsound",
"fire_effect",
"loop_fx_sound",
"loop_fx_sound_interval",
"loop_sound_delete",
"exploder_before_load",
"exploder_after_load",
"activate_exploder",
"createloopeffect",
"createoneshoteffect",
"createexploder",
"alphabetize",
"is_later_in_alphabet",
"alphabet_compare",
"play_loop_sound_on_entity",
"stop_loop_sound_on_entity",
"delete_on_death",
"error",
"exploder",
"create_dvar",
"void",
"tag_project",
"ter_op",
"create_lock",
"lock",
"is_locked",
"unlock_wait",
"unlock",
"unlock_thread",
"get_template_level",
}
};
2022-01-19 14:01:12 -05:00
gsc::include_t compiler::include_maps_mp_gametypes_hud_util_ =
2021-05-10 09:11:46 -04:00
{
2022-01-19 14:01:12 -05:00
"maps/mp/gametypes/_hud_util",
2021-05-10 09:11:46 -04:00
{
"setparent",
"getparent",
"addchild",
"removechild",
"setpoint",
"setpointbar",
"updatebar",
"updatebarscale",
"createfontstring",
"createserverfontstring",
"createservertimer",
"createtimer",
"createicon",
"createservericon",
"createserverbar",
"createbar",
"getcurrentfraction",
"createprimaryprogressbar",
"createprimaryprogressbartext",
"createteamprogressbar",
"createteamprogressbartext",
"setflashfrac",
"hideelem",
"showelem",
"flashthread",
"destroyelem",
"seticonshader",
"geticonshader",
"seticonsize",
"setwidth",
"setheight",
"setsize",
"updatechildren",
"transitionreset",
"transitionzoomin",
"transitionpulsefxin",
"transitionslidein",
"transitionslideout",
"transitionzoomout",
"transitionfadein",
"transitionfadeout",
"getweeklyref",
"getdailyref",
"ch_getprogress",
"ch_getstate",
"ch_setprogress",
"ch_setstate",
"ch_gettarget",
}
};
2020-05-21 07:32:38 -04:00
auto compiler::map_known_includes(const std::string& include) -> bool
{
2021-12-25 14:10:30 -05:00
if (include == "maps/mp/_utility")
2020-05-21 07:32:38 -04:00
{
includes_.push_back(include_maps_mp_utility_);
return true;
}
2021-12-25 14:10:30 -05:00
else if (include == "common_scripts/utility")
2020-05-21 07:32:38 -04:00
{
includes_.push_back(include_common_scripts_utility_);
return true;
}
2021-12-25 14:10:30 -05:00
else if (include == "common_scripts/_createfx")
2020-05-21 07:32:38 -04:00
{
includes_.push_back(include_common_scripts_createfx_);
return true;
}
2021-12-25 14:10:30 -05:00
else if (include == "maps/mp/gametypes/_hud_util")
2021-05-10 09:11:46 -04:00
{
includes_.push_back(include_maps_mp_gametypes_hud_util_);
return true;
}
2020-05-21 07:32:38 -04:00
return false;
}
void compiler::print_debug_info()
{
printf("----------------------------------\n");
printf("files included: %zu\n", includes_.size());
printf("animtrees used: %zu\n", animtrees_.size());
printf("functions compiled: %zu\n",assembly_.size());
for (auto& func : assembly_)
{
2021-12-25 14:10:30 -05:00
print_function(func);
2020-05-21 07:32:38 -04:00
for (auto& inst : func->instructions)
{
const auto itr = func->labels.find(inst->index);
if (itr != func->labels.end())
{
2021-12-25 14:10:30 -05:00
print_label(itr->second);
2020-05-21 07:32:38 -04:00
}
2021-12-25 14:10:30 -05:00
print_instruction(inst);
2020-05-21 07:32:38 -04:00
}
}
printf("----------------------------------\n");
}
void compiler::print_opcodes(std::uint32_t index, std::uint32_t size)
{
printf(" ");
}
2021-12-25 14:10:30 -05:00
void compiler::print_function(const function::ptr& func)
2020-05-21 07:32:38 -04:00
{
printf("\n");
printf("%s\n", func->name.data());
}
2021-12-25 14:10:30 -05:00
void compiler::print_instruction(const instruction::ptr& inst)
2020-05-21 07:32:38 -04:00
{
switch (opcode(inst->opcode))
{
case opcode::OP_endswitch:
2021-12-25 14:10:30 -05:00
print_opcodes(inst->index, 3);
2020-05-21 07:32:38 -04:00
printf("%s", resolver::opcode_name(inst->opcode).data());
printf(" %s\n", inst->data[0].data());
{
std::uint32_t totalcase = std::stoul(inst->data[0]);
auto index = 0;
for (auto casenum = 0u; casenum < totalcase; casenum++)
{
2021-12-25 14:10:30 -05:00
print_opcodes(inst->index, 7);
2020-05-21 07:32:38 -04:00
if (inst->data[1 + index] == "case")
{
printf("%s %s %s", inst->data[1 + index].data(), inst->data[1 + index + 1].data(), inst->data[1 + index + 2].data());
index += 3;
}
else if (inst->data[1 + index] == "default")
{
printf("%s %s", inst->data[1 + index].data(), inst->data[1 + index + 1].data());
index += 2;
}
if (casenum != totalcase - 1)
{
printf("\n");
}
}
}
break;
default:
2021-12-25 14:10:30 -05:00
print_opcodes(inst->index, inst->size);
2020-05-21 07:32:38 -04:00
printf("%s", resolver::opcode_name(inst->opcode).data());
for (auto& d : inst->data)
{
printf(" %s", d.data());
}
break;
}
printf("\n");
}
void compiler::print_label(const std::string& label)
{
printf(" %s\n", label.data());
}
} // namespace xsk::gsc::iw5