From d14b816fa0d76effe28d5b78804410496b5cff72 Mon Sep 17 00:00:00 2001 From: ValKmjolnir Date: Sat, 7 Oct 2023 00:55:20 +0800 Subject: [PATCH] :memo: add notes --- makefile | 2 +- src/nasal.h | 4 +++- src/nasal_codegen.cpp | 15 +++++++++------ src/nasal_codegen.h | 4 ++-- src/nasal_lexer.cpp | 38 ++++++++++++++++++++++++++------------ 5 files changed, 41 insertions(+), 22 deletions(-) diff --git a/makefile b/makefile index 5ec7f3e..059502c 100644 --- a/makefile +++ b/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 diff --git a/src/nasal.h b/src/nasal.h index 7d23ff5..6d4e15e 100644 --- a/src/nasal.h +++ b/src/nasal.h @@ -11,6 +11,7 @@ #include #include +// 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*); diff --git a/src/nasal_codegen.cpp b/src/nasal_codegen.cpp index f93a218..5be79a4 100644 --- a/src/nasal_codegen.cpp +++ b/src/nasal_codegen.cpp @@ -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; iget_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"); diff --git a/src/nasal_codegen.h b/src/nasal_codegen.h index 05790ed..4aaf212 100644 --- a/src/nasal_codegen.h +++ b/src/nasal_codegen.h @@ -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&); // used for generate pop in return expression - std::vector in_loop_level; + std::vector in_foreach_loop_level; // constant numbers and strings std::unordered_map const_number_map; diff --git a/src/nasal_lexer.cpp b/src/nasal_lexer.cpp index 7228c6f..cd8cce3 100644 --- a/src/nasal_lexer.cpp +++ b/src/nasal_lexer.cpp @@ -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"); + "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()) { 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, ""}); } else { + // if token sequence is empty, generate a default location toks.push_back({{line, column, line, column, filename}, tok::eof, ""}); } res = "";