-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperand.js
108 lines (81 loc) · 3.06 KB
/
operand.js
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/*****************************************************************************
OPERAND
*****************************************************************************/
function getOriginalSourceLineTrimmed( start_pos ) {
return source_line[current_source_line_index].line.substring(start_pos).trim();
}
function getOperand() {
// --- operand object ---------------------------------------------
let operand = [ "data_type", "value", "addressing_mode" ];
// set operand's "data_type"
if ( currentTokenValue() == "word" ) {
operand.data_type = "word";
nextToken();
} else if ( currentTokenValue() == "byte" ) {
operand.data_type = "byte";
nextToken();
} else {
operand.data_type = "dbyte";
}
// --- set operand's "value" and "addressing_mode" ----------------
if ( currentTokenValueWithCase() == "[" ) {
operand.value = "";
nextToken();
while ( currentTokenValueWithCase() != "]" ) {
operand.value += currentTokenValueWithCase();
nextToken();
ThrowSyntaxErrorIfEOLInsteadOf( 'Closing "]"' );
}
let lcase_value = operand.value.toLowerCase()
if ( lcase_value[0] == "#" ) {
operand.addressing_mode = "imm";
operand.value = "#(" + operand.value.substring(1) + ")";
}
else if ( lcase_value.includes( "),y" ) )
operand.addressing_mode = "indy";
else if ( lcase_value.includes( ",y" ) )
operand.addressing_mode = "absy";
else if ( lcase_value.includes( ",x)" ) )
operand.addressing_mode = "xind";
else if ( lcase_value.includes( ",x" ) )
operand.addressing_mode = "absx";
else
operand.addressing_mode = "abs";
// TODO: [(c),x], [(c,y)] must trow an error! -> illegal addressing mode!
} else if ( currentTokenValueWithCase() == "#" ) {
nextToken();
operand.value = "#"+currentTokenValueWithCase();
operand.addressing_mode = "imm";
} else {
operand.value = currentTokenValueWithCase();
operand.addressing_mode = "abs";
}
// --- return operand object --------------------------------------
return operand;
}
function getLoOperand( operand_value, operand_addressing_mode ) {
if ( operand_addressing_mode == "imm" ) {
if ( operand_value == "#0" )
return operand_value;
else
return "#<(" + operand_value.substring(1) + ")";
}
else
return operand_value;
}
function getHiOperand( operand_value, operand_addressing_mode ) {
if ( operand_addressing_mode == "imm" ) {
if ( operand_value == "#0" )
return operand_value;
else
return "#>(" + operand_value.substring(1) + ")";
}
else if ( operand_addressing_mode == "abs" )
return operand_value + "+1";
else if ( operand_addressing_mode == "absx" || operand_addressing_mode == "absy" )
return operand_value.replace( ",", "+1," );
else
return operand_value;
// All other addressing modes make no sense here.
// Return the original "operand_value" unmodified.
}