16#include <unordered_map>
65 explicit Parser(
const std::string &source);
114 std::unique_ptr<ProgramNode>
parseAST();
127 void registerObjectExterns(std::unique_ptr<Program> &mainProgram,
const std::unique_ptr<Program> &objProgram);
140 std::unique_ptr<SectionNode>
parseSection(uint64_t &index);
143 std::unique_ptr<CommentNode>
parseComment(uint64_t &index);
144 std::unique_ptr<LabelNode>
parseLabel(uint64_t &index);
145 std::unique_ptr<ModuleNode>
parseModule(uint64_t &index);
146 std::unique_ptr<ObjectNode>
parseObject(uint64_t &index);
152 void processModuleFile(
const std::string &src, std::unique_ptr<Program> &program);
153 void processObjectFile(
const std::string &src, std::unique_ptr<Program> &program);
157 void collectObjectNames(std::vector<std::pair<std::string, std::string>> &names,
const std::unique_ptr<Program> &program);
158 void printObjectHTML(std::ostream &out,
const std::unique_ptr<Program> &objPtr);
201 if (
this != &other) {
233 bool match(
const std::string &s);
237 void require(
const std::string &s);
AST node representing a single MXVM instruction with its operands.
AST node for a label (or function entry point) in the code section.
AST node for a module import declaration.
bool parse()
Parse the module declarations.
Mode parser_mode
interpret or compile
scan::TToken * token
current token pointer
uint64_t index
current token index
void require(const std::string &s)
Require the current token value to match, or throw.
ModuleParser(const Mode &m, const std::string &mod_name, const std::string &source)
Construct a module parser.
ModuleParser(const ModuleParser &other)
Copy constructor.
bool match(const std::string &s)
Check if the current token value matches and advance.
bool next()
Advance to the next token.
std::string mod_name
module name
scan::Scanner scanner
underlying scanner
scan::TToken operator[](size_t pos)
Access a token by position.
uint64_t scan()
Tokenize the module source.
bool generateProgramCode(const Mode &m, const std::string &mod_id, const std::string &mod_name, std::unique_ptr< Program > &program)
Register the module's external functions in a Program.
std::vector< ExternalFunction > functions
parsed external function declarations
scan::TToken at(size_t pos)
Access a token by position (bounds-checked).
ModuleParser & operator=(const ModuleParser &other)
Copy assignment.
AST node for an object import declaration.
std::string object_path
search path for .mxvm object files
std::unique_ptr< ModuleNode > parseModule(uint64_t &index)
Parser & operator=(const Parser &other)
Copy assignment.
Parser(const std::string &source)
Construct a parser from MXVM source text.
std::unique_ptr< ObjectNode > parseObject(uint64_t &index)
bool generateDebugHTML(std::ostream &out, std::unique_ptr< Program > &program)
Generate debug HTML output for the program.
void generateObjectAssemblyFile(std::unique_ptr< Program > &objProgram)
Generate a native assembly file for an object.
std::unique_ptr< CommentNode > parseComment(uint64_t &index)
auto operator[](size_t pos)
Access a token by position.
void collectObjectNames(std::vector< std::pair< std::string, std::string > > &names, const std::unique_ptr< Program > &program)
std::unique_ptr< ProgramNode > parseProgramOrObject(uint64_t &index)
Parse a top-level program or object node.
void registerObjectExterns(std::unique_ptr< Program > &mainProgram, const std::unique_ptr< Program > &objProgram)
Register external functions from an object into the main program.
void processObjectFile(const std::string &src, std::unique_ptr< Program > &program)
void processModuleFile(const std::string &src, std::unique_ptr< Program > &program)
void resolveLabelReference(Operand &operand, const std::unordered_map< std::string, size_t > &labelMap)
void setDefaultVariableValue(Variable &var, VarType type)
std::unique_ptr< LabelNode > parseLabel(uint64_t &index)
void processDataSection(SectionNode *sectionNode, std::unique_ptr< Program > &program)
std::unique_ptr< SectionNode > parseSection(uint64_t &index)
Parser(const Parser &other)
Copy constructor.
uint64_t scan()
Tokenize the source and return the token count.
std::string include_path
system module include path
void parse()
Parse all tokens into the internal AST.
void processModuleSection(SectionNode *sectionNode, std::unique_ptr< Program > &program)
void printObjectHTML(std::ostream &out, const std::unique_ptr< Program > &objPtr)
void setVariableValue(Variable &var, VarType type, const std::string &value, size_t buf_size=0)
std::string module_path
search path for .mxvm module files
std::unique_ptr< ProgramNode > parseAST()
Build the full AST from tokens.
bool generateProgramCode(const Mode &m, std::unique_ptr< Program > &program)
Walk the AST and populate a Program with instructions and variables.
friend class ModuleParser
void processObjectSection(SectionNode *sectionNode, std::unique_ptr< Program > &program)
std::unique_ptr< VariableNode > parseDataVariable(uint64_t &index)
void processCodeSection(SectionNode *sectionNode, std::unique_ptr< Program > &program)
bool object_mode
true when parsing an object definition
std::unique_ptr< InstructionNode > parseCodeInstruction(uint64_t &index)
Root AST node representing an entire program or object.
Complete MXVM program: instruction interpreter and native x64 code generator.
Represents a program section (.data, .code, .module, .object).
Validates MXVM source for correct variable/label usage and instruction operands.
AST node for a variable declaration in a .data section.
Lexical scanner that tokenizes source text.
int64_t pos(const char *substr, const char *s)
Instruction set enum, operand/instruction structs, variable types, and Variable/Variable_Value defini...
bool instruct_mode
enable instruction trace mode
Platform
Target platform for native code generation.
bool debug_mode
enable verbose debug output during parsing
VarType
MXVM variable type discriminator.
bool html_mode
enable HTML debug output
Mode
Execution mode: interpretation or native compilation.
token::Token< char > TToken
Default token type.
TokenType
Classification of scanned tokens.
Lexical scanner that tokenizes source text into typed tokens.
Describes an external function imported from a module or object.
bool operator==(const ExternalFunction &f)
Equality comparison by name and module.
bool ret_owned
true if the return value is an owned pointer
VarType ret_type
return type of the external function
std::string name
function symbol name
std::string mod
module or object name
bool module
true if from a module, false if from an object
A single instruction operand (constant value or variable reference).
A named variable with type, value, and optional object association.
Validator for MXVM programs — checks variable/label usage and instruction operand specifications.