-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlet_icode_stack.js
150 lines (112 loc) · 3.95 KB
/
let_icode_stack.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*****************************************************************************
LET STACK
*****************************************************************************/
var math_stack = [];
// opcode
// data_type
// value
// addressing_mode
//============================================================================
// Debug
//============================================================================
function debugOuputStackCode(item) {
let data_type = "";
let value = "";
let addressing_mode = "";
if ( typeof item.data_type !== "undefined" )
data_type = item.data_type;
if ( typeof item.value !== "undefined" )
value = item.value;
if ( typeof item.addressing_mode !== "undefined" )
addressing_mode = " ("+item.addressing_mode+")";
DebugCode( item.opcode + " " + data_type + " " + value + addressing_mode );
}
//============================================================================
// Optimizer
//============================================================================
function optimizeStackCode() {
// stub
// TODO optimizeStackCode()
}
//============================================================================
// Stack machine math parser
//============================================================================
function parseMathExpressionToMathStack() {
//----------------------------------------------------------------------
// Stack code math parser
//----------------------------------------------------------------------
function factor() {
if ( currentTokenType() == "EOL" ) {
ThrowSyntaxError( '"Factor" in math expression expected' );
}
if ( currentTokenValueWithCase() == "(" ) {
nextToken();
simpleExpression();
if ( currentTokenValueWithCase() != ")" )
ThrowSyntaxError( 'Closing ")" expected' );
nextToken(); // skip ")"
} else {
let operand = getOperand();
math_stack.push({
opcode: "push",
data_type: operand.data_type,
value: operand.value,
addressing_mode: operand.addressing_mode
});
nextToken();
}
}
function mulExpression() {
factor();
while ( currentTokenValueWithCase() == "*" ||
currentTokenValueWithCase() == "/" ) {
let operator = currentTokenValueWithCase();
nextToken();
factor();
math_stack.push({
opcode: operator
});
} /* wend */
}
function addExpression() {
mulExpression();
while ( currentTokenValueWithCase() == "+" ||
currentTokenValueWithCase() == "-" ) {
let operator = currentTokenValueWithCase();
nextToken();
mulExpression();
math_stack.push({
opcode: operator
});
} /* wend */
}
function shiftAndLogicalExpression() {
addExpression();
while ( currentTokenValueWithCase() == "&" ||
currentTokenValueWithCase() == "|" ||
currentTokenValueWithCase() == "^" ||
currentTokenValueWithCase() == ">>" ||
currentTokenValueWithCase() == ">>" ) {
let operator = currentTokenValueWithCase();
nextToken();
addExpression();
math_stack.push({
opcode: operator
});
} /* wend */
}
function simpleExpression() {
shiftAndLogicalExpression();
}
//----------------------------------------------------------------------
// Invoke parsing steps
//----------------------------------------------------------------------
math_stack = [];
simpleExpression();
math_stack.push({
opcode: "pull",
data_type: target_operand.data_type,
value: target_operand.value,
addressing_mode: target_operand.addressing_mode
});
}