-
Notifications
You must be signed in to change notification settings - Fork 0
/
compiler.js
145 lines (94 loc) · 4.03 KB
/
compiler.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
/*****************************************************************************
COMPILER
*****************************************************************************/
//============================================================================
// Token stream
//============================================================================
function nextToken() {
current_token_index++;
}
function currentTokenValue() {
return token[ current_token_index ].value.toLowerCase();
}
function currentTokenValueWithCase() {
return token[ current_token_index ].value;
}
function currentTokenType() {
return token[ current_token_index ].type;
}
//============================================================================
// Compile, Block
//============================================================================
function _createSourceLinesFromSourceText() {
// Split every source line into global var "source_line"
var line = textarea_source.value.split('\n');
// Trim input, ignore every emtpy line and comment with "//"
source_line = [];
for (var j = 0; j < line.length; j++) {
let tmp_line = line[j].trimLeft();
if ( tmp_line !== "" && tmp_line.substring(0,2) !== "//" ) {
source_line.push({
line: line[j],
line_number: j+1
});
}
} /* next */
}
function _initCompiler() {
initCode();
textarea_output.value = "";
code = [];
label_nr = -1;
auto_code_label_no = -1;
place_runtime_automatically = true;
no_error = true;
current_STA_operand="";
current_LDA_operand="";
start_address = "$0801";
}
function compile() {
/*
is called by EITHER
(1) function "postSourceToTextareaSource()" right after loading (see loader.js)
OR
(2) Button "<button type="button" onclick="compile()" >Compile</button>"
*/
// Split every source line into global var "source_line"
// Ignore every emtpy line & comment starting with "//"
// and init compiler then compile
_createSourceLinesFromSourceText();
_initCompiler();
// --- Block ------------------------------------------------------------------------------------------------------
for ( current_source_line_index = 0; current_source_line_index < source_line.length; current_source_line_index++ ) {
tokenizeCurrentSourceLine(); // returns with global "current_token_index = 0"
//emitCurrentSourceLineAsCommentToCode(); // (for debugging)
//debug_emitTokensToCode(); // (for debugging)
//-----------------------------------------------------------------------
// Keyword handlers
//-----------------------------------------------------------------------
let lcase_value = currentTokenValue();
// *** Block end keywords ( break out of block ) ******
if ( lcase_value == "endif"
|| lcase_value == "else"
|| lcase_value == "elseif"
|| lcase_value == "until"
|| lcase_value == "while"
|| lcase_value == "forever" )
break;
// *** Math *******************************************
else if ( lcase_value == "let" ) handleLet();
// *** Print to Screen ********************************
else if ( lcase_value == "print" ) handlePrint();
// *** Conditionals ***********************************
// *** !source "Runtime.asm" **************************
else if ( lcase_value == "runtime" ) handleRuntime();
// *** if nothing matches, output original line *******
else emitCurrentOriginalSourceLineToCode();
} // next ---------------------------------------------------------------------------------------------------------
// copy ASM code to clipboard
writeCodeToTextAreaOutput();
textarea_output.select();
document.execCommand("copy");
// print a 2nd time to deselect selection
writeCodeToTextAreaOutput();
}