MXVM 1.8.1
Virtual Machine, Compiler, and Pascal Frontend
Loading...
Searching...
No Matches
mxvm::Validator Class Reference

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< ParsedOpparseOperandList ()
 Parse a comma-separated list of operands.

Public Attributes

std::vector< VarNamePairvar_names

Private Attributes

std::string filename
scan::Scanner scanner
std::string source
size_t index = 0
const scan::TTokentoken = nullptr
std::string current_function
std::unordered_map< std::string, std::unordered_set< std::string > > function_vars

Detailed Description

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.

Definition at line 69 of file valid.hpp.

Constructor & Destructor Documentation

◆ Validator()

mxvm::Validator::Validator ( const std::string & source)

Construct a validator from MXVM source code.

Parameters
sourceThe MXVM source text to validate

Definition at line 796 of file valid.cpp.

References scanner, and source.

Member Function Documentation

◆ collect_labels()

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().

◆ collect_objects()

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().

◆ match() [1/2]

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().

◆ match() [2/2]

bool mxvm::Validator::match ( const types::TokenType & t)

Check if the current token type matches and advance.

Definition at line 737 of file valid.cpp.

References index, scanner, and token.

◆ next()

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().

◆ parseOperand()

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().

◆ 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().

◆ peekIs() [1/2]

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().

◆ peekIs() [2/2]

bool mxvm::Validator::peekIs ( const types::TokenType & t)

Check if the current token type matches without consuming.

Definition at line 775 of file valid.cpp.

References index, and scanner.

◆ require() [1/2]

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().

◆ require() [2/2]

void mxvm::Validator::require ( const types::TokenType & t)

Require the current token type to match, or report an error.

Definition at line 745 of file valid.cpp.

References filename, index, scanner, token, and tokenTypeToString().

◆ tokenTypeToString()

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().

◆ validate()

bool mxvm::Validator::validate ( const std::string & name)

Run full validation on the source program.

Parameters
nameProgram name (for error messages)
Returns
true if the program passes all checks

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().

◆ validateAgainstSpec()

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.

Parameters
opInstruction mnemonic
opsParsed operand list
varsDeclared variables map
labelsDeclared labels map
objectsDeclared object names
usedVarsRefOutput: variable usage records
usedLabelsRefOutput: 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().

Member Data Documentation

◆ current_function

std::string mxvm::Validator::current_function
private

Definition at line 76 of file valid.hpp.

◆ filename

std::string mxvm::Validator::filename
private

◆ function_vars

std::unordered_map<std::string, std::unordered_set<std::string> > mxvm::Validator::function_vars
private

Definition at line 77 of file valid.hpp.

◆ index

size_t mxvm::Validator::index = 0
private

Definition at line 74 of file valid.hpp.

Referenced by match(), next(), peekIs(), peekIs(), require(), and validate().

◆ scanner

scan::Scanner mxvm::Validator::scanner
private

◆ source

std::string mxvm::Validator::source
private

Definition at line 73 of file valid.hpp.

Referenced by Validator(), and validate().

◆ token

const scan::TToken* mxvm::Validator::token = nullptr
private

Definition at line 75 of file valid.hpp.

Referenced by match(), match(), next(), parseOperand(), parseOperandList(), require(), require(), and validate().

◆ var_names

std::vector<VarNamePair> mxvm::Validator::var_names

Definition at line 90 of file valid.hpp.

Referenced by mxvm::Program::validateNames().


The documentation for this class was generated from the following files: