|
MXVM 1.8.1
Virtual Machine, Compiler, and Pascal Frontend
|
Validates MXVM source for correct variable/label usage and instruction operands. More...
#include <valid.hpp>
Classes | |
| struct | VarNamePair |
| A variable name paired with its declaration token. More... | |
Public Member Functions | |
| Validator (const std::string &source) | |
| Construct a validator from MXVM source code. | |
| void | validateAgainstSpec (const std::string &op, const std::vector< ParsedOp > &ops, const std::unordered_map< std::string, Variable > &vars, const std::unordered_map< std::string, std::string > &labels, const std::unordered_set< std::string > &objects, std::vector< UseVar > &usedVarsRef, std::vector< UseLabel > &usedLabelsRef) |
| Validate an instruction's operands against the spec table. | |
| bool | validate (const std::string &name) |
| Run full validation on the source program. | |
| bool | match (const std::string &m) |
| Check if the current token value matches and advance. | |
| void | require (const std::string &r) |
| Require the current token value to match, or report an error. | |
| bool | match (const types::TokenType &t) |
| Check if the current token type matches and advance. | |
| void | require (const types::TokenType &t) |
| Require the current token type to match, or report an error. | |
| bool | next () |
| Advance to the next token; returns false at end. | |
| bool | peekIs (const std::string &s) |
| Check if the current token value matches without consuming. | |
| bool | peekIs (const types::TokenType &t) |
| Check if the current token type matches without consuming. | |
| std::string | tokenTypeToString (types::TokenType t) |
| Convert a TokenType to its display string. | |
| void | collect_labels (std::unordered_map< std::string, std::string > &labels) |
| Pre-collect all label declarations and map them to function names. | |
| void | collect_objects (std::unordered_set< std::string > &objects, size_t start_index, size_t end_index) |
| Collect all object names declared between token indices. | |
| ParsedOp | parseOperand () |
| Parse a single instruction operand from the token stream. | |
| std::vector< ParsedOp > | parseOperandList () |
| Parse a comma-separated list of operands. | |
Public Attributes | |
| std::vector< VarNamePair > | var_names |
Private Attributes | |
| std::string | filename |
| scan::Scanner | scanner |
| std::string | source |
| size_t | index = 0 |
| const scan::TToken * | token = nullptr |
| std::string | current_function |
| std::unordered_map< std::string, std::unordered_set< std::string > > | function_vars |
Validates MXVM source for correct variable/label usage and instruction operands.
Scans the source token stream and checks that all instructions use valid operand patterns, all referenced variables are declared, and all jump targets exist.
| mxvm::Validator::Validator | ( | const std::string & | source | ) |
| void mxvm::Validator::collect_labels | ( | std::unordered_map< std::string, std::string > & | labels | ) |
Pre-collect all label declarations and map them to function names.
Definition at line 219 of file valid.cpp.
References scanner, and types::TT_ID.
Referenced by validate().
| void mxvm::Validator::collect_objects | ( | std::unordered_set< std::string > & | objects, |
| size_t | start_index, | ||
| size_t | end_index ) |
Collect all object names declared between token indices.
Definition at line 53 of file valid.cpp.
References filename, scanner, and types::TT_ID.
Referenced by validate().
| bool mxvm::Validator::match | ( | const std::string & | m | ) |
Check if the current token value matches and advance.
Definition at line 718 of file valid.cpp.
References token.
Referenced by parseOperand(), parseOperandList(), and validate().
| bool mxvm::Validator::match | ( | const types::TokenType & | t | ) |
| bool mxvm::Validator::next | ( | ) |
Advance to the next token; returns false at end.
Definition at line 757 of file valid.cpp.
References index, scanner, token, and types::TT_SYM.
Referenced by parseOperand(), parseOperandList(), and validate().
| ParsedOp mxvm::Validator::parseOperand | ( | ) |
Parse a single instruction operand from the token stream.
Definition at line 142 of file valid.cpp.
References mxvm::ParsedOp::at, filename, mxvm::Hex, mxvm::Id, mxvm::ParsedOp::kind, match(), mxvm::Member, next(), mxvm::Num, peekIs(), require(), mxvm::Str, mxvm::ParsedOp::text, token, types::TT_HEX, types::TT_ID, types::TT_NUM, and types::TT_STR.
Referenced by parseOperandList().
| std::vector< ParsedOp > mxvm::Validator::parseOperandList | ( | ) |
Parse a comma-separated list of operands.
Definition at line 196 of file valid.cpp.
References filename, match(), next(), parseOperand(), and token.
Referenced by validate().
| bool mxvm::Validator::peekIs | ( | const std::string & | s | ) |
Check if the current token value matches without consuming.
Definition at line 771 of file valid.cpp.
References index, and scanner.
Referenced by parseOperand(), and validate().
| bool mxvm::Validator::peekIs | ( | const types::TokenType & | t | ) |
| void mxvm::Validator::require | ( | const std::string & | r | ) |
Require the current token value to match, or report an error.
Definition at line 726 of file valid.cpp.
References filename, and token.
Referenced by parseOperand(), and validate().
| void mxvm::Validator::require | ( | const types::TokenType & | t | ) |
| std::string mxvm::Validator::tokenTypeToString | ( | types::TokenType | t | ) |
Convert a TokenType to its display string.
Definition at line 779 of file valid.cpp.
References types::TT_HEX, types::TT_ID, types::TT_NUM, types::TT_STR, and types::TT_SYM.
Referenced by require().
| bool mxvm::Validator::validate | ( | const std::string & | name | ) |
Run full validation on the source program.
| name | Program name (for error messages) |
Definition at line 368 of file valid.cpp.
References collect_labels(), collect_objects(), filename, mxvm::has_semicolon(), IncType, index, match(), next(), parseOperandList(), peekIs(), require(), scanner, source, token, types::TT_HEX, types::TT_ID, types::TT_NUM, types::TT_STR, types::TT_SYM, and validateAgainstSpec().
Referenced by mxvm::Parser::processObjectFile().
| void mxvm::Validator::validateAgainstSpec | ( | const std::string & | op, |
| const std::vector< ParsedOp > & | ops, | ||
| const std::unordered_map< std::string, Variable > & | vars, | ||
| const std::unordered_map< std::string, std::string > & | labels, | ||
| const std::unordered_set< std::string > & | objects, | ||
| std::vector< UseVar > & | usedVarsRef, | ||
| std::vector< UseLabel > & | usedLabelsRef ) |
Validate an instruction's operands against the spec table.
| op | Instruction mnemonic |
| ops | Parsed operand list |
| vars | Declared variables map |
| labels | Declared labels map |
| objects | Declared object names |
| usedVarsRef | Output: variable usage records |
| usedLabelsRef | Output: label usage records |
Definition at line 230 of file valid.cpp.
References mxvm::Any, mxvm::ArgsTail, filename, mxvm::OpSpec::fixed, mxvm::Id, mxvm::isIdLike(), mxvm::isImmediate(), mxvm::kOpSpecs, mxvm::Label, mxvm::OpSpec::maxArgs, mxvm::Member, mxvm::OpSpec::minArgs, mxvm::None, and mxvm::OpSpec::varPolicy.
Referenced by validate().
|
private |
Definition at line 71 of file valid.hpp.
Referenced by collect_objects(), parseOperand(), parseOperandList(), require(), require(), validate(), and validateAgainstSpec().
|
private |
|
private |
|
private |
Definition at line 72 of file valid.hpp.
Referenced by Validator(), collect_labels(), collect_objects(), match(), next(), peekIs(), peekIs(), require(), and validate().
|
private |
Definition at line 73 of file valid.hpp.
Referenced by Validator(), and validate().
|
private |
Definition at line 75 of file valid.hpp.
Referenced by match(), match(), next(), parseOperand(), parseOperandList(), require(), require(), and validate().
| std::vector<VarNamePair> mxvm::Validator::var_names |
Definition at line 90 of file valid.hpp.
Referenced by mxvm::Program::validateNames().