This commit is contained in:
blueloveTH 2024-06-09 16:13:43 +08:00
parent 8a049b06bd
commit bec168ab53
5 changed files with 27 additions and 14 deletions

View File

@ -14,7 +14,7 @@ if len(sys.argv) == 2:
else:
config = 'Release'
assert config in ['Debug', 'Release', 'RelWithDebInfo', 'MinSizeRel']
assert config in ['Debug', 'Release', 'RelWithDebInfo']
os.chdir("build")

View File

@ -129,7 +129,10 @@ struct Compiler {
[[nodiscard]] Error* SyntaxError(const char* msg = "invalid syntax", ...) noexcept;
[[nodiscard]] Error* IndentationError(const char* msg) noexcept{ return lexer._error(false, "IndentationError", msg, {}); }
[[nodiscard]] Error* NeedMoreLines() noexcept{ return lexer._error(false, "NeedMoreLines", "", {}, (i64)ctx()->is_compiling_class); }
[[nodiscard]] Error* NeedMoreLines() noexcept{
assert(false);
return lexer._error(false, "NeedMoreLines", "", {}, (i64)ctx()->is_compiling_class);
}
public:
Compiler(VM* vm, std::string_view source, const Str& filename, CompileMode mode, bool unknown_global_scope = false) noexcept;

View File

@ -129,14 +129,16 @@ struct Lexer {
[[nodiscard]] Error* _error(bool lexer_err, const char* type, const char* msg, va_list args, i64 userdata=0) noexcept;
[[nodiscard]] Error* SyntaxError(const char* fmt, ...) noexcept;
[[nodiscard]] Error* IndentationError(const char* msg) noexcept { return _error(true, "IndentationError", msg, {}); }
[[nodiscard]] Error* NeedMoreLines() noexcept { return _error(true, "NeedMoreLines", "", {}, 0); }
[[nodiscard]] Error* NeedMoreLines() noexcept {
assert(false);
return _error(true, "NeedMoreLines", "", {}, 0);
}
Lexer(VM* vm, std::shared_ptr<SourceData> src) noexcept;
[[nodiscard]] Error* run() noexcept;
void from_precompiled();
[[nodiscard]] Error* precompile(Str* out);
[[nodiscard]] Error* from_precompiled() noexcept;
[[nodiscard]] Error* precompile(Str* out) noexcept;
};
enum class IntParsingResult {

View File

@ -1279,12 +1279,16 @@ Error* Compiler::compile(CodeObject_* out) noexcept{
Error* err;
check(lexer.run());
// for(int i=0; i<lexer.nexts.size(); i++){
// printf("%s: %s\n", TK_STR(tk(i).type), tk(i).str().escape().c_str());
// if(lexer.src->filename[0] != '<'){
// printf("%s\n", lexer.src->filename.c_str());
// for(int i=0; i<lexer.nexts.size(); i++){
// printf("%s: %s\n", TK_STR(tk(i).type), tk(i).str().escape().c_str());
// }
// }
CodeObject_ code = push_global_context();
assert(curr().type == TK("@sof"));
advance(); // skip @sof, so prev() is always valid
match_newlines(); // skip possible leading '\n'

View File

@ -537,8 +537,7 @@ Error* Lexer::run() noexcept{
assert(!this->used);
this->used = true;
if(src->is_precompiled) {
from_precompiled();
return NULL;
return from_precompiled();
}
// push initial tokens
this->nexts.push_back(Token{TK("@sof"), token_start, 0, current_line, brackets_level, {}});
@ -552,13 +551,17 @@ Error* Lexer::run() noexcept{
return NULL;
}
void Lexer::from_precompiled() {
Error* Lexer::from_precompiled() noexcept{
TokenDeserializer deserializer(src->source.c_str());
deserializer.curr += 5; // skip "pkpy:"
std::string_view version = deserializer.read_string('\n');
assert(version == PK_VERSION);
assert(deserializer.read_uint('\n') == (i64)src->mode);
if(version != PK_VERSION){
return SyntaxError("precompiled version mismatch");
}
if(deserializer.read_uint('\n') != (i64)src->mode){
return SyntaxError("precompiled mode mismatch");
}
int count = deserializer.read_count();
vector<Str>& precompiled_tokens = src->_precompiled_tokens;
@ -600,9 +603,10 @@ void Lexer::from_precompiled() {
}
nexts.push_back(t);
}
return NULL;
}
Error* Lexer::precompile(Str* out) {
Error* Lexer::precompile(Str* out) noexcept{
assert(!src->is_precompiled);
Error* err = run();
if(err) return err;