224 lines
5.8 KiB
C++
224 lines
5.8 KiB
C++
/**************************************************************************/
|
|
/* aethex_compiler.h */
|
|
/**************************************************************************/
|
|
/* This file is part of: */
|
|
/* AETHEX ENGINE */
|
|
/* https://aethex.foundation */
|
|
/**************************************************************************/
|
|
/* Copyright (c) 2026-present AeThex Labs. */
|
|
/**************************************************************************/
|
|
|
|
#ifndef AETHEX_COMPILER_H
|
|
#define AETHEX_COMPILER_H
|
|
|
|
#include "aethex_parser.h"
|
|
#include "core/io/resource.h"
|
|
#include "core/object/ref_counted.h"
|
|
|
|
// ==========================================
|
|
// AeThex Cross-Platform Compiler
|
|
// ==========================================
|
|
// Compiles AeThex AST to:
|
|
// - AeThex Bytecode (for engine VM)
|
|
// - Roblox Luau
|
|
// - UEFN Verse
|
|
// - Unity C#
|
|
// - JavaScript (Web)
|
|
// ==========================================
|
|
|
|
class AeThexCompiler : public RefCounted {
|
|
GDCLASS(AeThexCompiler, RefCounted);
|
|
|
|
public:
|
|
// Compilation targets
|
|
enum Target {
|
|
TARGET_BYTECODE, // Engine VM
|
|
TARGET_LUAU, // Roblox
|
|
TARGET_VERSE, // UEFN
|
|
TARGET_CSHARP, // Unity
|
|
TARGET_JAVASCRIPT, // Web
|
|
TARGET_MAX
|
|
};
|
|
|
|
// Bytecode opcodes
|
|
enum Opcode : uint8_t {
|
|
// Stack
|
|
OP_PUSH_NULL,
|
|
OP_PUSH_BOOL,
|
|
OP_PUSH_INT,
|
|
OP_PUSH_FLOAT,
|
|
OP_PUSH_STRING,
|
|
OP_POP,
|
|
OP_DUP,
|
|
|
|
// Variables
|
|
OP_LOAD_LOCAL,
|
|
OP_STORE_LOCAL,
|
|
OP_LOAD_GLOBAL,
|
|
OP_STORE_GLOBAL,
|
|
OP_LOAD_MEMBER,
|
|
OP_STORE_MEMBER,
|
|
|
|
// Arithmetic
|
|
OP_ADD,
|
|
OP_SUB,
|
|
OP_MUL,
|
|
OP_DIV,
|
|
OP_MOD,
|
|
OP_NEG,
|
|
|
|
// Comparison
|
|
OP_EQ,
|
|
OP_NE,
|
|
OP_LT,
|
|
OP_LE,
|
|
OP_GT,
|
|
OP_GE,
|
|
|
|
// Logic
|
|
OP_NOT,
|
|
OP_AND,
|
|
OP_OR,
|
|
|
|
// Control flow
|
|
OP_JUMP,
|
|
OP_JUMP_IF,
|
|
OP_JUMP_IF_NOT,
|
|
OP_LOOP,
|
|
|
|
// Functions
|
|
OP_CALL,
|
|
OP_CALL_METHOD,
|
|
OP_RETURN,
|
|
|
|
// Objects
|
|
OP_NEW_ARRAY,
|
|
OP_NEW_DICT,
|
|
OP_INDEX_GET,
|
|
OP_INDEX_SET,
|
|
|
|
// AeThex specific
|
|
OP_SYNC, // Sync across platforms
|
|
OP_BEACON_EMIT, // Emit beacon (signal)
|
|
OP_NOTIFY, // Console output
|
|
OP_AWAIT, // Async await
|
|
|
|
OP_MAX
|
|
};
|
|
|
|
// Compiled bytecode chunk
|
|
struct Chunk {
|
|
Vector<uint8_t> code;
|
|
Vector<Variant> constants;
|
|
Vector<String> strings;
|
|
HashMap<String, int> local_indices;
|
|
int line_info = 0;
|
|
|
|
void write(uint8_t byte) { code.push_back(byte); }
|
|
void write_int(int32_t value);
|
|
void write_float(float value);
|
|
int add_constant(const Variant &value);
|
|
int add_string(const String &str);
|
|
};
|
|
|
|
// Compilation result
|
|
struct CompiledScript {
|
|
String source_path;
|
|
Target target = TARGET_BYTECODE;
|
|
|
|
// For bytecode
|
|
HashMap<String, Chunk> functions;
|
|
Chunk main_chunk;
|
|
Vector<String> beacons; // Signal declarations
|
|
|
|
// For cross-platform
|
|
String output_code;
|
|
|
|
// Metadata
|
|
String reality_name;
|
|
Vector<String> supported_platforms;
|
|
};
|
|
|
|
private:
|
|
CompiledScript result;
|
|
Target current_target = TARGET_BYTECODE;
|
|
Chunk *current_chunk = nullptr;
|
|
int scope_depth = 0;
|
|
bool had_error = false;
|
|
|
|
struct Local {
|
|
String name;
|
|
int depth = 0;
|
|
bool is_const = false;
|
|
};
|
|
Vector<Local> locals;
|
|
|
|
// Bytecode compilation
|
|
void compile_node(const AeThexParser::ASTNode *node);
|
|
void compile_reality(const AeThexParser::ASTNode *node);
|
|
void compile_journey(const AeThexParser::ASTNode *node);
|
|
void compile_beacon(const AeThexParser::ASTNode *node);
|
|
void compile_statement(const AeThexParser::ASTNode *node);
|
|
void compile_expression(const AeThexParser::ASTNode *node);
|
|
void compile_binary(const AeThexParser::ASTNode *node);
|
|
void compile_unary(const AeThexParser::ASTNode *node);
|
|
void compile_call(const AeThexParser::ASTNode *node);
|
|
void compile_literal(const AeThexParser::ASTNode *node);
|
|
void compile_identifier(const AeThexParser::ASTNode *node);
|
|
void compile_if(const AeThexParser::ASTNode *node);
|
|
void compile_sync(const AeThexParser::ASTNode *node);
|
|
|
|
void emit_byte(uint8_t byte);
|
|
void emit_bytes(uint8_t b1, uint8_t b2);
|
|
void emit_return();
|
|
int emit_jump(Opcode op);
|
|
void patch_jump(int offset);
|
|
void emit_loop(int loop_start);
|
|
|
|
void begin_scope();
|
|
void end_scope();
|
|
void declare_local(const String &name, bool is_const);
|
|
int resolve_local(const String &name);
|
|
|
|
// Cross-platform code generation
|
|
String generate_luau(const AeThexParser::ASTNode *root);
|
|
String generate_verse(const AeThexParser::ASTNode *root);
|
|
String generate_csharp(const AeThexParser::ASTNode *root);
|
|
String generate_javascript(const AeThexParser::ASTNode *root);
|
|
|
|
String luau_node(const AeThexParser::ASTNode *node, int indent = 0);
|
|
String verse_node(const AeThexParser::ASTNode *node, int indent = 0);
|
|
String csharp_node(const AeThexParser::ASTNode *node, int indent = 0);
|
|
String js_node(const AeThexParser::ASTNode *node, int indent = 0);
|
|
|
|
String indent_str(int level) const;
|
|
|
|
protected:
|
|
static void _bind_methods();
|
|
|
|
public:
|
|
AeThexCompiler();
|
|
~AeThexCompiler();
|
|
|
|
// Compile AST to target
|
|
Error compile(const AeThexParser::ASTNode *root, Target p_target = TARGET_BYTECODE);
|
|
|
|
// Get results
|
|
const CompiledScript &get_result() const { return result; }
|
|
String get_output_code() const { return result.output_code; }
|
|
bool has_error() const { return had_error; }
|
|
|
|
// Cross-platform export
|
|
String export_to_luau(const AeThexParser::ASTNode *root);
|
|
String export_to_verse(const AeThexParser::ASTNode *root);
|
|
String export_to_csharp(const AeThexParser::ASTNode *root);
|
|
String export_to_javascript(const AeThexParser::ASTNode *root);
|
|
|
|
// Static helpers
|
|
static String target_name(Target t);
|
|
static String opcode_name(Opcode op);
|
|
};
|
|
|
|
VARIANT_ENUM_CAST(AeThexCompiler::Target);
|
|
|
|
#endif // AETHEX_COMPILER_H
|