From f880e3d2d76a281d15800e36443fec0371c472b6 Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Fri, 15 Aug 2025 18:37:40 +0300 Subject: [PATCH] Move command line handling into a module --- gcc/elna1.cc | 9 ++- include/elna/boot/dependency.h | 11 ---- source/Lexer.elna | 13 ++--- source/cctype.elna | 11 ++-- ...rface.elna => command_line_interface.elna} | 6 +- source/main.elna | 56 +------------------ 6 files changed, 25 insertions(+), 81 deletions(-) rename source/{CommandLineInterface.elna => command_line_interface.elna} (96%) diff --git a/gcc/elna1.cc b/gcc/elna1.cc index f912241..017f30a 100644 --- a/gcc/elna1.cc +++ b/gcc/elna1.cc @@ -85,12 +85,15 @@ static elna::boot::dependency elna_parse_file(dependency_state& state, const cha for (const auto& sub_tree : outcome.tree->imports) { std::filesystem::path sub_path = "source" / elna::boot::build_path(sub_tree->segments); + std::unordered_map::const_iterator cached_import = + state.cache.find(sub_path); - if (state.cache.find(sub_path) == state.cache.end()) + if (cached_import == state.cache.end()) { - elna_parse_file(state, state.allocate_path(sub_path)); + elna_parse_file(state, sub_path.c_str()); + cached_import = state.cache.find(sub_path); } - outcome_bag.add_import(state.cache.find(sub_path)->second); + outcome_bag.add_import(cached_import->second); } elna::boot::error_list semantic_errors = analyze_semantics(filename, outcome.tree, outcome_bag); diff --git a/include/elna/boot/dependency.h b/include/elna/boot/dependency.h index b419ff2..962578f 100644 --- a/include/elna/boot/dependency.h +++ b/include/elna/boot/dependency.h @@ -50,16 +50,5 @@ namespace elna::boot : globals(elna::boot::builtin_symbol_table()), custom(custom) { } - - const char *allocate_path(const std::filesystem::path path) - { - std::size_t current_size = this->allocated_paths.size(); - this->allocated_paths += path.native() + '\0'; - - return this->allocated_paths.data() + current_size; - } - - private: - std::string allocated_paths; }; } diff --git a/source/Lexer.elna b/source/Lexer.elna index f684ab4..4db586d 100644 --- a/source/Lexer.elna +++ b/source/Lexer.elna @@ -3,7 +3,7 @@ obtain one at https://mozilla.org/MPL/2.0/. *) module; -import cstdio, cstring, common; +import cstdio, cstring, cctype, cstdlib, common; const CHUNK_SIZE := 85536; @@ -301,21 +301,20 @@ proc compare_keyword(keyword: String, token_start: BufferPosition, token_end: ^C var result: Bool; index: Word; - keyword_length: Word; continue: Bool; begin index := 0u; result := true; - keyword_length := Length(keyword); - continue := (index < keyword_length) & (token_start.iterator <> token_end); + continue := (index < keyword.length) & (token_start.iterator <> token_end); while continue & result do - result := (keyword[index] = token_start.iterator^) or (Lower(keyword[index]) = token_start.iterator^); + result := keyword[index] = token_start.iterator^ + or cast(tolower(cast(keyword[index]: Int)): Char) = token_start.iterator^; token_start.iterator := token_start.iterator + 1; index := index + 1u; - continue := (index < keyword_length) & (token_start.iterator <> token_end) + continue := (index < keyword.length) & (token_start.iterator <> token_end) end; - result := result & (index = Length(keyword)); + result := result & index = keyword.length; return result & (token_start.iterator = token_end) end; diff --git a/source/cctype.elna b/source/cctype.elna index a538c96..3906cd1 100644 --- a/source/cctype.elna +++ b/source/cctype.elna @@ -3,9 +3,12 @@ obtain one at https://mozilla.org/MPL/2.0/. *) module; -proc isdigit(c: Int ) -> Int; extern; -proc isalnum(c: Int) -> Int; extern; -proc isalpha(c: Int) -> Int; extern; -proc isspace(c: Int) -> Int; extern; +proc isdigit*(c: Int ) -> Int; extern; +proc isalnum*(c: Int) -> Int; extern; +proc isalpha*(c: Int) -> Int; extern; +proc isspace*(c: Int) -> Int; extern; + +proc tolower*(c: Int) -> Int; extern; +proc toupper*(c: Int) -> Int; extern; end. diff --git a/source/CommandLineInterface.elna b/source/command_line_interface.elna similarity index 96% rename from source/CommandLineInterface.elna rename to source/command_line_interface.elna index 22f1495..040fdeb 100644 --- a/source/CommandLineInterface.elna +++ b/source/command_line_interface.elna @@ -1,6 +1,10 @@ (* This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. *) + +(* + Command line handling. +*) module; import cstdlib, cstring, common; @@ -52,7 +56,7 @@ begin end; parsed := true end; - if (parameter[1] <> '-') & ~parsed then + if (parameter^ <> '-') & ~parsed then parsed := true; if result^.input <> nil then diff --git a/source/main.elna b/source/main.elna index 8bf54fb..65dcb7e 100644 --- a/source/main.elna +++ b/source/main.elna @@ -3,7 +3,7 @@ obtain one at https://mozilla.org/MPL/2.0/. *) program; -import cstdlib, cstdio, cctype, common, Lexer; +import cstdio, cctype, common, command_line_interface, Lexer; const SEEK_SET* := 0; @@ -113,11 +113,6 @@ type end; location: Location end; - CommandLine = record - input: ^Char; - lex: Bool; - parse: Bool - end; Tokenizer* = record length: Word; data: ^Token @@ -702,55 +697,6 @@ begin return lexer end; -(* - Command line handling. -*) - -proc parse_command_line*(argc: Int, argv: ^^Char) -> ^CommandLine; -var - parameter: ^^Char; - i: Int; - result: ^CommandLine; -begin - i := 1; - result := cast(malloc(#size(CommandLine)): ^CommandLine); - result^.lex := false; - result^.parse := false; - result^.input := nil; - - while i < argc & result <> nil do - parameter := argv + i; - - if strcmp(parameter^, "--lex\0".ptr) = 0 then - result^.lex := true - elsif strcmp(parameter^, "--parse\0".ptr) = 0 then - result^.parse := true - elsif parameter^^ <> '-' then - if result^.input <> nil then - write_s("Fatal error: Only one source file can be given.\n"); - result := nil - else - result^.input := parameter^ - end - else - write_s("Fatal error: Unknown command line options: "); - - write_z(parameter^); - write_s(".\n"); - - result := nil - end; - - i := i + 1 - end; - if result <> nil & result^.input = nil then - write_s("Fatal error: no input files.\n"); - result := nil - end; - - return result -end; - (* Parser. *)