106 lines
3 KiB
C++
106 lines
3 KiB
C++
/**************************************************************************/
|
|
/* aethex_parser.h */
|
|
/**************************************************************************/
|
|
/* This file is part of: */
|
|
/* AETHEX ENGINE */
|
|
/* https://aethex.foundation */
|
|
/**************************************************************************/
|
|
/* Copyright (c) 2026-present AeThex Labs. */
|
|
/**************************************************************************/
|
|
|
|
#pragma once
|
|
|
|
#include "aethex_tokenizer.h"
|
|
#include "core/templates/hash_map.h"
|
|
|
|
class AeThexParser {
|
|
public:
|
|
// AST Node types
|
|
struct ASTNode {
|
|
enum Type {
|
|
NODE_REALITY, // Module declaration
|
|
NODE_JOURNEY, // Function/method
|
|
NODE_BEACON, // Signal/event
|
|
NODE_ARTIFACT, // Class
|
|
NODE_VARIABLE, // Variable declaration
|
|
NODE_ASSIGNMENT, // Assignment
|
|
NODE_BINARY_OP, // Binary operation
|
|
NODE_UNARY_OP, // Unary operation
|
|
NODE_CALL, // Function call
|
|
NODE_MEMBER, // Member access
|
|
NODE_IF, // When/otherwise
|
|
NODE_LOOP, // Traverse/while
|
|
NODE_SYNC, // Cross-platform sync
|
|
NODE_NOTIFY, // Print/output
|
|
NODE_REVEAL, // Return
|
|
NODE_LITERAL, // Literal value
|
|
NODE_IDENTIFIER, // Variable reference
|
|
NODE_ARRAY, // Array literal
|
|
NODE_DICT, // Dictionary literal
|
|
};
|
|
|
|
Type type;
|
|
String value;
|
|
Vector<ASTNode *> children;
|
|
HashMap<String, String> attributes;
|
|
int line = 0;
|
|
|
|
~ASTNode() {
|
|
for (ASTNode *child : children) {
|
|
memdelete(child);
|
|
}
|
|
}
|
|
};
|
|
|
|
struct ParseError {
|
|
String message;
|
|
int line;
|
|
int column;
|
|
};
|
|
|
|
private:
|
|
Vector<AeThexTokenizer::Token> tokens;
|
|
int current = 0;
|
|
Vector<ParseError> errors;
|
|
|
|
bool is_at_end() const;
|
|
AeThexTokenizer::Token peek() const;
|
|
AeThexTokenizer::Token previous() const;
|
|
AeThexTokenizer::Token advance();
|
|
bool check(AeThexTokenizer::TokenType type) const;
|
|
bool match(AeThexTokenizer::TokenType type);
|
|
void consume(AeThexTokenizer::TokenType type, const String &message);
|
|
void error(const String &message);
|
|
void synchronize();
|
|
|
|
// Parsing methods
|
|
ASTNode *parse_reality();
|
|
ASTNode *parse_journey();
|
|
ASTNode *parse_beacon();
|
|
ASTNode *parse_artifact();
|
|
ASTNode *parse_statement();
|
|
ASTNode *parse_expression();
|
|
ASTNode *parse_assignment();
|
|
ASTNode *parse_or();
|
|
ASTNode *parse_and();
|
|
ASTNode *parse_equality();
|
|
ASTNode *parse_comparison();
|
|
ASTNode *parse_term();
|
|
ASTNode *parse_factor();
|
|
ASTNode *parse_unary();
|
|
ASTNode *parse_call();
|
|
ASTNode *parse_primary();
|
|
|
|
public:
|
|
Error parse(const Vector<AeThexTokenizer::Token> &p_tokens);
|
|
ASTNode *get_root() const { return root; }
|
|
const Vector<ParseError> &get_errors() const { return errors; }
|
|
|
|
ASTNode *root = nullptr;
|
|
|
|
~AeThexParser() {
|
|
if (root) {
|
|
memdelete(root);
|
|
}
|
|
}
|
|
};
|