📝 add notes
This commit is contained in:
parent
63b0112b9d
commit
d14b816fa0
2
makefile
2
makefile
|
@ -69,7 +69,7 @@ build/main.o: $(NASAL_HEADER) src/main.cpp | build
|
|||
build/nasal_misc.o: src/nasal.h src/nasal_misc.cpp | build
|
||||
$(CXX) -std=$(STD) -c -O3 src/nasal_misc.cpp -fno-exceptions -fPIC -o build/nasal_misc.o -I .
|
||||
|
||||
build/repl.o: src/nasal.h src/repl.h src/repl.cpp | build
|
||||
build/repl.o: $(NASAL_HEADER) src/repl.h src/repl.cpp | build
|
||||
$(CXX) -std=$(STD) -c -O3 src/repl.cpp -fno-exceptions -fPIC -o build/repl.o -I .
|
||||
|
||||
build/nasal_err.o: src/nasal.h src/repl.h src/nasal_err.h src/nasal_err.cpp | build
|
||||
|
|
|
@ -11,6 +11,7 @@
|
|||
#include <cmath>
|
||||
#include <vector>
|
||||
|
||||
// abbreviation of some useful basic type
|
||||
using i32 = std::int32_t;
|
||||
using i64 = std::int64_t;
|
||||
using u8 = std::uint8_t;
|
||||
|
@ -35,7 +36,8 @@ bool is_powerpc();
|
|||
bool is_superh();
|
||||
|
||||
|
||||
|
||||
// virtual machine stack depth
|
||||
// both global depth and value stack depth
|
||||
const u32 STACK_DEPTH = 4096;
|
||||
|
||||
f64 hex2f(const char*);
|
||||
|
|
|
@ -284,9 +284,11 @@ void codegen::func_gen(function* node) {
|
|||
}
|
||||
add_symbol(arg);
|
||||
|
||||
in_loop_level.push_back(0);
|
||||
// generate code block
|
||||
in_foreach_loop_level.push_back(0);
|
||||
block_gen(block);
|
||||
in_loop_level.pop_back();
|
||||
in_foreach_loop_level.pop_back();
|
||||
|
||||
code[lsize].num = local.back().size();
|
||||
if (local.back().size()>=STACK_DEPTH) {
|
||||
die("too many local variants: " +
|
||||
|
@ -771,6 +773,7 @@ void codegen::cond_gen(condition_expr* node) {
|
|||
void codegen::loop_gen(expr* node) {
|
||||
continue_ptr.push_front({});
|
||||
break_ptr.push_front({});
|
||||
|
||||
switch(node->get_type()) {
|
||||
case expr_type::ast_while: while_gen((while_expr*)node); break;
|
||||
case expr_type::ast_for: for_gen((for_expr*)node); break;
|
||||
|
@ -846,9 +849,9 @@ void codegen::forei_gen(forei_expr* node) {
|
|||
}
|
||||
|
||||
// generate code block
|
||||
++in_loop_level.back();
|
||||
++in_foreach_loop_level.back();
|
||||
block_gen(node->get_code_block());
|
||||
--in_loop_level.back();
|
||||
--in_foreach_loop_level.back();
|
||||
|
||||
// jump to loop begin
|
||||
emit(op_jmp, loop_begin, node->get_location());
|
||||
|
@ -1207,7 +1210,7 @@ void codegen::block_gen(code_block* node) {
|
|||
}
|
||||
|
||||
void codegen::ret_gen(return_expr* node) {
|
||||
for(u32 i = 0; i<in_loop_level.back(); ++i) {
|
||||
for(u32 i = 0; i<in_foreach_loop_level.back(); ++i) {
|
||||
emit(op_pop, 0, node->get_location());
|
||||
emit(op_pop, 0, node->get_location());
|
||||
}
|
||||
|
@ -1220,7 +1223,7 @@ const error& codegen::compile(parse& parse, linker& import, bool repl_flag) {
|
|||
init_file_map(import.get_file_list());
|
||||
need_repl_output = repl_flag;
|
||||
|
||||
in_loop_level.push_back(0);
|
||||
in_foreach_loop_level.push_back(0);
|
||||
|
||||
// add special symbol globals, which is a hash stores all global variables
|
||||
add_symbol("globals");
|
||||
|
|
|
@ -34,7 +34,7 @@ class codegen {
|
|||
private:
|
||||
error err;
|
||||
|
||||
//
|
||||
// repl output flag, will generate op_repl to output stack top value if true
|
||||
bool need_repl_output;
|
||||
|
||||
// file mapper for file -> index
|
||||
|
@ -42,7 +42,7 @@ private:
|
|||
void init_file_map(const std::vector<std::string>&);
|
||||
|
||||
// used for generate pop in return expression
|
||||
std::vector<u32> in_loop_level;
|
||||
std::vector<u32> in_foreach_loop_level;
|
||||
|
||||
// constant numbers and strings
|
||||
std::unordered_map<f64, u32> const_number_map;
|
||||
|
|
|
@ -51,7 +51,8 @@ bool lexer::is_calc_opr(char c) {
|
|||
}
|
||||
|
||||
void lexer::skip_note() {
|
||||
// avoid note, after this process ptr will point to a '\n', so next loop line counter+1
|
||||
// avoid note, after this process ptr will point to '\n'
|
||||
// so next loop line counter+1
|
||||
while(++ptr<res.size() && res[ptr]!='\n') {}
|
||||
}
|
||||
|
||||
|
@ -60,7 +61,8 @@ void lexer::err_char() {
|
|||
char c = res[ptr++];
|
||||
err.err("lexer",
|
||||
{line, column-1, line, column, filename},
|
||||
"invalid character 0x"+chrhex(c));
|
||||
"invalid character 0x"+chrhex(c)
|
||||
);
|
||||
++invalid_char;
|
||||
}
|
||||
|
||||
|
@ -125,11 +127,13 @@ std::string lexer::utf8_gen() {
|
|||
}
|
||||
err.err("lexer",
|
||||
{line, column-1, line, column, filename},
|
||||
"invalid utf-8 <"+utf_info+">");
|
||||
"invalid utf-8 <"+utf_info+">"
|
||||
);
|
||||
++invalid_char;
|
||||
}
|
||||
str += tmp;
|
||||
column += 2; // may have some problems because not all the unicode takes 2 space
|
||||
// may have some problems because not all the unicode takes 2 space
|
||||
column += 2;
|
||||
}
|
||||
return str;
|
||||
}
|
||||
|
@ -149,7 +153,8 @@ token lexer::id_gen() {
|
|||
tok type = get_type(str);
|
||||
return {
|
||||
{begin_line, begin_column, line, column, filename},
|
||||
(type!=tok::null)? type:tok::id, str};
|
||||
(type!=tok::null)? type:tok::id, str
|
||||
};
|
||||
}
|
||||
|
||||
token lexer::num_gen() {
|
||||
|
@ -167,7 +172,8 @@ token lexer::num_gen() {
|
|||
if (str.length()<3) {
|
||||
err.err("lexer",
|
||||
{begin_line, begin_column, line, column, filename},
|
||||
"invalid number `"+str+"`");
|
||||
"invalid number `"+str+"`"
|
||||
);
|
||||
}
|
||||
return {{begin_line, begin_column, line, column, filename}, tok::num, str};
|
||||
} else if (ptr+1<res.size() && res[ptr]=='0' && res[ptr+1]=='o') { // generate oct number
|
||||
|
@ -185,7 +191,8 @@ token lexer::num_gen() {
|
|||
if (str.length()==2 || erfmt) {
|
||||
err.err("lexer",
|
||||
{begin_line, begin_column, line, column, filename},
|
||||
"invalid number `"+str+"`");
|
||||
"invalid number `"+str+"`"
|
||||
);
|
||||
}
|
||||
return {{begin_line, begin_column, line, column, filename}, tok::num, str};
|
||||
}
|
||||
|
@ -205,7 +212,8 @@ token lexer::num_gen() {
|
|||
column += str.length();
|
||||
err.err("lexer",
|
||||
{begin_line, begin_column, line, column, filename},
|
||||
"invalid number `"+str+"`");
|
||||
"invalid number `"+str+"`"
|
||||
);
|
||||
return {{begin_line, begin_column, line, column, filename}, tok::num, "0"};
|
||||
}
|
||||
}
|
||||
|
@ -222,7 +230,8 @@ token lexer::num_gen() {
|
|||
column += str.length();
|
||||
err.err("lexer",
|
||||
{begin_line, begin_column, line, column, filename},
|
||||
"invalid number `"+str+"`");
|
||||
"invalid number `"+str+"`"
|
||||
);
|
||||
return {{begin_line, begin_column, line, column, filename}, tok::num, "0"};
|
||||
}
|
||||
}
|
||||
|
@ -273,7 +282,8 @@ token lexer::str_gen() {
|
|||
if (ptr++>=res.size()) {
|
||||
err.err("lexer",
|
||||
{begin_line, begin_column, line, column, filename},
|
||||
"get EOF when generating string");
|
||||
"get EOF when generating string"
|
||||
);
|
||||
return {{begin_line, begin_column, line, column, filename}, tok::str, str};
|
||||
}
|
||||
++column;
|
||||
|
@ -282,7 +292,8 @@ token lexer::str_gen() {
|
|||
if (begin=='`' && str.length()!=1+utf8_hdchk(str[0])) {
|
||||
err.err("lexer",
|
||||
{begin_line, begin_column, line, column, filename},
|
||||
"\'`\' is used for string including one character");
|
||||
"\'`\' is used for string including one character"
|
||||
);
|
||||
}
|
||||
return {{begin_line, begin_column, line, column, filename}, tok::str, str};
|
||||
}
|
||||
|
@ -296,7 +307,8 @@ token lexer::single_opr() {
|
|||
if (type==tok::null) {
|
||||
err.err("lexer",
|
||||
{begin_line, begin_column, line, column, filename},
|
||||
"invalid operator `"+str+"`");
|
||||
"invalid operator `"+str+"`"
|
||||
);
|
||||
}
|
||||
++ptr;
|
||||
return {{begin_line, begin_column, line, column, filename}, type, str};
|
||||
|
@ -368,8 +380,10 @@ const error& lexer::scan(const std::string& file) {
|
|||
}
|
||||
}
|
||||
if (toks.size()) {
|
||||
// eof token's location is the last token's location
|
||||
toks.push_back({toks.back().loc, tok::eof, "<eof>"});
|
||||
} else {
|
||||
// if token sequence is empty, generate a default location
|
||||
toks.push_back({{line, column, line, column, filename}, tok::eof, "<eof>"});
|
||||
}
|
||||
res = "";
|
||||
|
|
Loading…
Reference in New Issue