pandemonium_engine/modules/cscript/cscript_compiler.h

172 lines
6.7 KiB
C++
Raw Normal View History

#ifndef CSCRIPT_COMPILER_H
#define CSCRIPT_COMPILER_H
2023-12-17 23:07:28 +01:00
/*************************************************************************/
2023-12-17 23:07:28 +01:00
/* cscript_compiler.h */
/*************************************************************************/
2023-12-17 23:07:28 +01:00
/* This file is part of: */
/* PANDEMONIUM ENGINE */
/* https://github.com/Relintai/pandemonium_engine */
/*************************************************************************/
2023-12-17 23:07:28 +01:00
/* Copyright (c) 2022-present Péter Magyar. */
/* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */
2023-12-17 23:07:28 +01:00
/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/*************************************************************************/
2023-01-15 19:42:08 +01:00
#include "core/containers/rb_set.h"
#include "cscript.h"
#include "cscript_parser.h"
class CScriptCompiler {
const CScriptParser *parser;
2023-01-15 19:42:08 +01:00
RBSet<CScript *> parsed_classes;
RBSet<CScript *> parsing_classes;
CScript *main_script;
struct CodeGen {
CScript *script;
const CScriptParser::ClassNode *class_node;
const CScriptParser::FunctionNode *function_node;
bool debug_stack;
2023-01-15 19:12:50 +01:00
List<RBMap<StringName, int>> stack_id_stack;
RBMap<StringName, int> stack_identifiers;
List<CScriptFunction::StackDebug> stack_debug;
2023-01-15 19:12:50 +01:00
List<RBMap<StringName, int>> block_identifier_stack;
RBMap<StringName, int> block_identifiers;
void add_stack_identifier(const StringName &p_id, int p_stackpos) {
stack_identifiers[p_id] = p_stackpos;
if (debug_stack) {
block_identifiers[p_id] = p_stackpos;
CScriptFunction::StackDebug sd;
sd.added = true;
sd.line = current_line;
sd.identifier = p_id;
sd.pos = p_stackpos;
stack_debug.push_back(sd);
}
}
void push_stack_identifiers() {
stack_id_stack.push_back(stack_identifiers);
if (debug_stack) {
block_identifier_stack.push_back(block_identifiers);
block_identifiers.clear();
}
}
void pop_stack_identifiers() {
stack_identifiers = stack_id_stack.back()->get();
stack_id_stack.pop_back();
if (debug_stack) {
2023-01-15 19:12:50 +01:00
for (RBMap<StringName, int>::Element *E = block_identifiers.front(); E; E = E->next()) {
CScriptFunction::StackDebug sd;
sd.added = false;
sd.identifier = E->key();
sd.line = current_line;
sd.pos = E->get();
stack_debug.push_back(sd);
}
block_identifiers = block_identifier_stack.back()->get();
block_identifier_stack.pop_back();
}
}
HashMap<Variant, int, VariantHasher, VariantComparator> constant_map;
2023-01-15 19:12:50 +01:00
RBMap<StringName, int> name_map;
#ifdef TOOLS_ENABLED
Vector<StringName> named_globals;
#endif
int get_name_map_pos(const StringName &p_identifier) {
int ret;
if (!name_map.has(p_identifier)) {
ret = name_map.size();
name_map[p_identifier] = ret;
} else {
ret = name_map[p_identifier];
}
return ret;
}
int get_constant_pos(const Variant &p_constant) {
if (constant_map.has(p_constant)) {
return constant_map[p_constant];
}
int pos = constant_map.size();
constant_map[p_constant] = pos;
return pos;
}
Vector<int> opcodes;
void alloc_stack(int p_level) {
if (p_level >= stack_max) {
stack_max = p_level + 1;
}
}
void alloc_call(int p_params) {
if (p_params >= call_max) {
call_max = p_params;
}
}
int current_line;
int stack_max;
int call_max;
};
bool _is_class_member_property(CodeGen &codegen, const StringName &p_name);
bool _is_class_member_property(CScript *owner, const StringName &p_name);
void _set_error(const String &p_error, const CScriptParser::Node *p_node);
bool _create_unary_operator(CodeGen &codegen, const CScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level);
bool _create_binary_operator(CodeGen &codegen, const CScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level, bool p_initializer = false, int p_index_addr = 0);
CScriptDataType _gdtype_from_datatype(const CScriptParser::DataType &p_datatype, CScript *p_owner = nullptr) const;
int _parse_assign_right_expression(CodeGen &codegen, const CScriptParser::OperatorNode *p_expression, int p_stack_level, int p_index_addr = 0);
int _parse_expression(CodeGen &codegen, const CScriptParser::Node *p_expression, int p_stack_level, bool p_root = false, bool p_initializer = false, int p_index_addr = 0);
Error _parse_block(CodeGen &codegen, const CScriptParser::BlockNode *p_block, int p_stack_level = 0, int p_break_addr = -1, int p_continue_addr = -1);
Error _parse_function(CScript *p_script, const CScriptParser::ClassNode *p_class, const CScriptParser::FunctionNode *p_func, bool p_for_ready = false);
Error _parse_class_level(CScript *p_script, const CScriptParser::ClassNode *p_class, bool p_keep_state);
Error _parse_class_blocks(CScript *p_script, const CScriptParser::ClassNode *p_class, bool p_keep_state);
void _make_scripts(CScript *p_script, const CScriptParser::ClassNode *p_class, bool p_keep_state);
int err_line;
int err_column;
StringName source;
String error;
public:
Error compile(const CScriptParser *p_parser, CScript *p_script, bool p_keep_state = false);
String get_error() const;
int get_error_line() const;
int get_error_column() const;
CScriptCompiler();
};
#endif // CSCRIPT_COMPILER_H