MXVM 1.8.1
Virtual Machine, Compiler, and Pascal Frontend
Loading...
Searching...
No Matches
valid.hpp
Go to the documentation of this file.
1
6#ifndef _VALID_H_X
7#define _VALID_H_X
8
9#include "mxvm/instruct.hpp"
10#include "scanner/scanner.hpp"
11#include <string>
12#include <unordered_map>
13#include <unordered_set>
14#include <vector>
15
16namespace mxvm {
17
28
30 struct ParsedOp {
32 std::string text;
34 };
35
37 struct UseVar {
38 std::string name;
40 };
41
42 struct UseLabel {
43 std::string name;
45 };
46
48 enum class VArity {
52 };
53
55 struct OpSpec {
56 std::string name;
57 std::vector<OpKind> fixed;
59 int minArgs = -1;
60 int maxArgs = -1;
61 };
62
69 class Validator {
70 private:
71 std::string filename;
73 std::string source;
74 size_t index = 0;
75 const scan::TToken *token = nullptr;
76 std::string current_function;
77 std::unordered_map<std::string, std::unordered_set<std::string>> function_vars;
78
79 public:
84 Validator(const std::string &source);
86 struct VarNamePair {
87 std::string first;
89 };
90 std::vector<VarNamePair> var_names;
102 const std::string &op,
103 const std::vector<ParsedOp> &ops,
104 const std::unordered_map<std::string, Variable> &vars,
105 const std::unordered_map<std::string, std::string> &labels,
106 const std::unordered_set<std::string> &objects,
107 std::vector<UseVar> &usedVarsRef,
108 std::vector<UseLabel> &usedLabelsRef);
109
115 bool validate(const std::string &name);
117 bool match(const std::string &m);
119 void require(const std::string &r);
121 bool match(const types::TokenType &t);
123 void require(const types::TokenType &t);
125 bool next();
127 bool peekIs(const std::string &s);
129 bool peekIs(const types::TokenType &t);
131 std::string tokenTypeToString(types::TokenType t);
133 void collect_labels(std::unordered_map<std::string, std::string> &labels);
135 void collect_objects(std::unordered_set<std::string> &objects, size_t start_index, size_t end_index);
139 std::vector<ParsedOp> parseOperandList();
140 };
141} // namespace mxvm
142
143#endif
const scan::TToken * token
Definition valid.hpp:75
scan::Scanner scanner
Definition valid.hpp:72
std::vector< ParsedOp > parseOperandList()
Parse a comma-separated list of operands.
Definition valid.cpp:196
ParsedOp parseOperand()
Parse a single instruction operand from the token stream.
Definition valid.cpp:142
std::string source
Definition valid.hpp:73
bool match(const std::string &m)
Check if the current token value matches and advance.
Definition valid.cpp:718
std::string filename
Definition valid.hpp:71
size_t index
Definition valid.hpp:74
void require(const std::string &r)
Require the current token value to match, or report an error.
Definition valid.cpp:726
bool validate(const std::string &name)
Run full validation on the source program.
Definition valid.cpp:368
bool peekIs(const std::string &s)
Check if the current token value matches without consuming.
Definition valid.cpp:771
Validator(const std::string &source)
Construct a validator from MXVM source code.
Definition valid.cpp:796
bool next()
Advance to the next token; returns false at end.
Definition valid.cpp:757
std::vector< VarNamePair > var_names
Definition valid.hpp:90
std::string tokenTypeToString(types::TokenType t)
Convert a TokenType to its display string.
Definition valid.cpp:779
std::unordered_map< std::string, std::unordered_set< std::string > > function_vars
Definition valid.hpp:77
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.
Definition valid.cpp:53
std::string current_function
Definition valid.hpp:76
void collect_labels(std::unordered_map< std::string, std::string > &labels)
Pre-collect all label declarations and map them to function names.
Definition valid.cpp:219
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.
Definition valid.cpp:230
Lexical scanner that tokenizes source text.
Definition scanner.hpp:28
Instruction set enum, operand/instruction structs, variable types, and Variable/Variable_Value defini...
Definition ast.hpp:14
VArity
Variadic arity policy for instruction operand specs.
Definition valid.hpp:48
@ None
exact fixed operand count required
Definition valid.hpp:49
@ ArgsTail
fixed operands followed by argument-typed trailing operands
Definition valid.hpp:51
@ AnyTail
fixed operands followed by any number of trailing operands
Definition valid.hpp:50
OpKind
Classification of operand kinds for validation.
Definition valid.hpp:19
token::Token< char > TToken
Default token type.
Definition scanner.hpp:19
TokenType
Classification of scanned tokens.
Definition types.hpp:16
Lexical scanner that tokenizes source text into typed tokens.
Specification of valid operand patterns for an instruction.
Definition valid.hpp:55
std::string name
instruction mnemonic
Definition valid.hpp:56
int minArgs
Definition valid.hpp:59
VArity varPolicy
Definition valid.hpp:58
std::vector< OpKind > fixed
required fixed operand kinds
Definition valid.hpp:57
int maxArgs
Definition valid.hpp:60
A parsed operand with its kind, text, and source location.
Definition valid.hpp:30
OpKind kind
Definition valid.hpp:31
std::string text
Definition valid.hpp:32
const scan::TToken * at
token at the operand's source position
Definition valid.hpp:33
Records a label usage site for validation.
Definition valid.hpp:42
const scan::TToken * at
Definition valid.hpp:44
std::string name
Definition valid.hpp:43
Records a variable usage site for validation.
Definition valid.hpp:37
const scan::TToken * at
Definition valid.hpp:39
std::string name
Definition valid.hpp:38
A variable name paired with its declaration token.
Definition valid.hpp:86
const scan::TToken * second
token at the declaration
Definition valid.hpp:88
std::string first
variable name
Definition valid.hpp:87