-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSemanticAnalyzer.cpp
More file actions
69 lines (56 loc) · 2.04 KB
/
SemanticAnalyzer.cpp
File metadata and controls
69 lines (56 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include "SemanticAnalyzer.h"
#include "Utils.h"
#include <iostream>
namespace AST
{
void SemanticAnalyzer::Visit(CommandNode* node)
{
}
void SemanticAnalyzer::Visit(OneOperandCommandNode* node)
{
const InstructionGroup g = GetInstructionGroup(node->Opcode);
switch (g)
{
case InstructionGroup::SingleOperand:
CheckOneOperandCommand(node);
return;
case InstructionGroup::Branch:
CheckBranchCommand(node);
return;
default:
Errors.push_back(Error{ node, "wrong operands number." });
}
}
void SemanticAnalyzer::Visit(DoubleOperandCommandNode* node)
{
const InstructionGroup g = GetInstructionGroup(node->Opcode);
switch (g)
{
case InstructionGroup::DoubleOperand:
return;
case InstructionGroup::OneAndHalf:
CheckOneAndHalfCommand(node);
return;
default:
Errors.push_back(Error{node, "wrong operands number."});
}
}
void SemanticAnalyzer::CheckOneAndHalfCommand(const DoubleOperandCommandNode* node)
{
const OperandNode* second = node->Second;
if (second->OpType != OperandType::Register)
Errors.push_back(Error{ node, "wrong operand (second operand is expected to be a register)." });
}
void SemanticAnalyzer::CheckBranchCommand(const OneOperandCommandNode* node)
{
const OperandNode* first = node->First;
if (first->OpType != OperandType::Number && first->OpType != OperandType::LabelName)
Errors.push_back(Error{ node, "wrong operand(label or int is expected)." });
}
void SemanticAnalyzer::CheckOneOperandCommand(const OneOperandCommandNode* node)
{
const OperandNode* first = node->First;
if ((node->Opcode == OPCODE_RTS) && (first->OpType != OperandType::Register || first->AddrType != AddressingType::Register))
Errors.push_back(Error{ node, "wrong operand(RTS expects only a register.)" });
}
}