Move command line handling into a module

This commit is contained in:
2025-08-15 18:37:40 +03:00
parent cec020ea92
commit f880e3d2d7
6 changed files with 25 additions and 81 deletions

View File

@@ -85,12 +85,15 @@ static elna::boot::dependency elna_parse_file(dependency_state& state, const cha
for (const auto& sub_tree : outcome.tree->imports) for (const auto& sub_tree : outcome.tree->imports)
{ {
std::filesystem::path sub_path = "source" / elna::boot::build_path(sub_tree->segments); std::filesystem::path sub_path = "source" / elna::boot::build_path(sub_tree->segments);
std::unordered_map<std::filesystem::path, elna::boot::symbol_bag>::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); elna::boot::error_list semantic_errors = analyze_semantics(filename, outcome.tree, outcome_bag);

View File

@@ -50,16 +50,5 @@ namespace elna::boot
: globals(elna::boot::builtin_symbol_table()), custom(custom) : 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;
}; };
} }

View File

@@ -3,7 +3,7 @@
obtain one at https://mozilla.org/MPL/2.0/. *) obtain one at https://mozilla.org/MPL/2.0/. *)
module; module;
import cstdio, cstring, common; import cstdio, cstring, cctype, cstdlib, common;
const const
CHUNK_SIZE := 85536; CHUNK_SIZE := 85536;
@@ -301,21 +301,20 @@ proc compare_keyword(keyword: String, token_start: BufferPosition, token_end: ^C
var var
result: Bool; result: Bool;
index: Word; index: Word;
keyword_length: Word;
continue: Bool; continue: Bool;
begin begin
index := 0u; index := 0u;
result := true; 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 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; token_start.iterator := token_start.iterator + 1;
index := index + 1u; index := index + 1u;
continue := (index < keyword_length) & (token_start.iterator <> token_end) continue := (index < keyword.length) & (token_start.iterator <> token_end)
end; end;
result := result & (index = Length(keyword)); result := result & index = keyword.length;
return result & (token_start.iterator = token_end) return result & (token_start.iterator = token_end)
end; end;

View File

@@ -3,9 +3,12 @@
obtain one at https://mozilla.org/MPL/2.0/. *) obtain one at https://mozilla.org/MPL/2.0/. *)
module; module;
proc isdigit(c: Int ) -> Int; extern; proc isdigit*(c: Int ) -> Int; extern;
proc isalnum(c: Int) -> Int; extern; proc isalnum*(c: Int) -> Int; extern;
proc isalpha(c: Int) -> Int; extern; proc isalpha*(c: Int) -> Int; extern;
proc isspace(c: Int) -> Int; extern; proc isspace*(c: Int) -> Int; extern;
proc tolower*(c: Int) -> Int; extern;
proc toupper*(c: Int) -> Int; extern;
end. end.

View File

@@ -1,6 +1,10 @@
(* This Source Code Form is subject to the terms of the Mozilla Public License, (* 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 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/. *) obtain one at https://mozilla.org/MPL/2.0/. *)
(*
Command line handling.
*)
module; module;
import cstdlib, cstring, common; import cstdlib, cstring, common;
@@ -52,7 +56,7 @@ begin
end; end;
parsed := true parsed := true
end; end;
if (parameter[1] <> '-') & ~parsed then if (parameter^ <> '-') & ~parsed then
parsed := true; parsed := true;
if result^.input <> nil then if result^.input <> nil then

View File

@@ -3,7 +3,7 @@
obtain one at https://mozilla.org/MPL/2.0/. *) obtain one at https://mozilla.org/MPL/2.0/. *)
program; program;
import cstdlib, cstdio, cctype, common, Lexer; import cstdio, cctype, common, command_line_interface, Lexer;
const const
SEEK_SET* := 0; SEEK_SET* := 0;
@@ -113,11 +113,6 @@ type
end; end;
location: Location location: Location
end; end;
CommandLine = record
input: ^Char;
lex: Bool;
parse: Bool
end;
Tokenizer* = record Tokenizer* = record
length: Word; length: Word;
data: ^Token data: ^Token
@@ -702,55 +697,6 @@ begin
return lexer return lexer
end; 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. Parser.
*) *)