From bec168ab534c6cb0b57cc5b66a8ecb1c0fa71f60 Mon Sep 17 00:00:00 2001 From: blueloveTH Date: Sun, 9 Jun 2024 16:13:43 +0800 Subject: [PATCH] some fix --- cmake_build.py | 2 +- include/pocketpy/compiler/compiler.hpp | 5 ++++- include/pocketpy/compiler/lexer.hpp | 10 ++++++---- src/compiler/compiler.cpp | 8 ++++++-- src/compiler/lexer.cpp | 16 ++++++++++------ 5 files changed, 27 insertions(+), 14 deletions(-) diff --git a/cmake_build.py b/cmake_build.py index 6e33f034..30fb58a1 100644 --- a/cmake_build.py +++ b/cmake_build.py @@ -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") diff --git a/include/pocketpy/compiler/compiler.hpp b/include/pocketpy/compiler/compiler.hpp index f5c7b1a3..3a8abb8e 100644 --- a/include/pocketpy/compiler/compiler.hpp +++ b/include/pocketpy/compiler/compiler.hpp @@ -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; diff --git a/include/pocketpy/compiler/lexer.hpp b/include/pocketpy/compiler/lexer.hpp index af62afc3..348bf7fe 100644 --- a/include/pocketpy/compiler/lexer.hpp +++ b/include/pocketpy/compiler/lexer.hpp @@ -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 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 { diff --git a/src/compiler/compiler.cpp b/src/compiler/compiler.cpp index 391855fc..3624b453 100644 --- a/src/compiler/compiler.cpp +++ b/src/compiler/compiler.cpp @@ -1279,12 +1279,16 @@ Error* Compiler::compile(CodeObject_* out) noexcept{ Error* err; check(lexer.run()); - // for(int i=0; ifilename[0] != '<'){ + // printf("%s\n", lexer.src->filename.c_str()); + // for(int i=0; iused); 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& 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;