diff options
| author | Eugen Wissner <belka@caraus.de> | 2026-11-08 19:50:24 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2026-11-08 19:50:24 +0100 |
| commit | afa516a3bae3a2dd4bb4a92cb12f3f6fcdcc8e5c (patch) | |
| tree | 8cf468bee3e762a270a256dda2ddc53d410fe861 | |
| parent | 39db5d5b89dfd2086d73d171487ab9f185a0adf0 (diff) | |
| download | elna-afa516a3bae3a2dd4bb4a92cb12f3f6fcdcc8e5c.tar.gz | |
Fix comparison with an exported constant
| -rw-r--r-- | gcc/gcc/elna-builtins.cc | 12 | ||||
| -rw-r--r-- | rakelib/compiler.rake | 7 | ||||
| -rw-r--r-- | rakelib/support/platform.rb | 16 | ||||
| -rw-r--r-- | source/elna/lexer.elna | 88 | ||||
| -rw-r--r-- | source/main.elna | 2 |
5 files changed, 80 insertions, 45 deletions
diff --git a/gcc/gcc/elna-builtins.cc b/gcc/gcc/elna-builtins.cc index 69b8229..a364ce6 100644 --- a/gcc/gcc/elna-builtins.cc +++ b/gcc/gcc/elna-builtins.cc @@ -431,6 +431,14 @@ namespace elna::gcc } } + static void declare_constant(const std::string& name, + const boot::constant_info& info, const std::shared_ptr<symbol_table>& symbols) + { + tree type = get_inner_alias(info.symbol, symbols); + tree constant_tree = constant_to_tree(info.value, symbols, type); + symbols->enter(name, constant_tree); + } + void rewrite_symbol_table(const std::shared_ptr<boot::symbol_table>& info_table, const std::shared_ptr<symbol_table>& symbols, const bool imported) { @@ -458,6 +466,10 @@ namespace elna::gcc { declare_procedure(symbol_name, *procedure_info, symbols); } + else if (const auto constant_info = symbol_info->is_constant()) + { + declare_constant(symbol_name, *constant_info, symbols); + } } } } diff --git a/rakelib/compiler.rake b/rakelib/compiler.rake index 349f74a..0ee34e2 100644 --- a/rakelib/compiler.rake +++ b/rakelib/compiler.rake @@ -13,8 +13,7 @@ Rake.application.add_loader('d', Rake::MakefileLoader.new) FileList['build/gcc/obj/**/*.d'].each { |file| import file } rule(%r{gcc/obj/.+\.o$} => lambda { |file| - source = Pathname.new('source') + - Pathname.new(file).relative_path_from('build/gcc/obj').sub_ext('.elna') + source = Pathname.new(file).relative_path_from('build/gcc/obj').sub_ext('.elna') [source, 'build/host/install/bin/gelna'] }) do |t| @@ -23,10 +22,10 @@ rule(%r{gcc/obj/.+\.o$} => lambda { |file| sh t.prerequisites.last, '-c', '-MD', '-MP', '-O0', '-g', *Platform.includes, '-o', t.name, t.source.to_s end -sources = FileList['source/**/*.elna'].select { |file| file == file.downcase } +sources = Platform.sources file 'build/gcc/bin/elna' => sources.map { |file| Pathname.new('build/gcc/obj') + - Pathname.new(file).relative_path_from('source').sub_ext('.o') + Pathname.new(file).sub_ext('.o') } do |t| Pathname.new(t.name).dirname.mkpath diff --git a/rakelib/support/platform.rb b/rakelib/support/platform.rb index 0785b52..a622c7f 100644 --- a/rakelib/support/platform.rb +++ b/rakelib/support/platform.rb @@ -3,6 +3,8 @@ # obtain one at https://mozilla.org/MPL/2.0/. # frozen_string_literal: true +require 'rake' + # Platform and architecture helpers for build includes and targets. module Platform def self.host_os @@ -20,13 +22,23 @@ module Platform end end - def self.includes(platform = host_os, arch = host_arch) + def self.directories(platform = host_os, arch = host_arch) [ "arch/#{platform}/#{arch}", "arch/platform/#{platform}", 'arch/platform/posix', "arch/#{arch}", 'source' - ].select { |dir| Dir.exist?(dir) }.flat_map { |dir| ['-I', dir] } + ].select { |dir| Dir.exist?(dir) } + end + + def self.includes(platform = host_os, arch = host_arch) + directories(platform, arch).flat_map { |dir| ['-I', dir] } + end + + def self.sources(platform = host_os, arch = host_arch) + FileList[*directories(platform, arch).map { |dir| "#{dir}/**/*.elna" }].select do |file| + file == file.downcase + end end end diff --git a/source/elna/lexer.elna b/source/elna/lexer.elna index 0c32973..d190526 100644 --- a/source/elna/lexer.elna +++ b/source/elna/lexer.elna @@ -5,7 +5,9 @@ (* Lexical analysis. *) module -import std, sys.posix.stdlib, sys.posix.string, sys.posix.stdio, elna.source +import + std, sys.posix.stdlib, sys.posix.string, sys.posix.stdio, sys.posix.errno, + elna.source type (* @@ -210,16 +212,7 @@ type trait, generic_open, attribute_open, - word, - word8, - word16, - word32, - word64, integer, - integer8, - integer16, - integer32, - integer64, double, single, character, @@ -273,11 +266,10 @@ type ElnaLexerCommentToken = record(ElnaLexerToken) content: StdString end - ElnaLexerWordToken = record(ElnaLexerToken) - value: Word64 - end ElnaLexerIntegerToken = record(ElnaLexerToken) - value: Int64 + magnitude: Word64; + size: Word8; -- 0 for target Word/Int, or 8, 16, 32, 64 for explicit width. + is_signed: Bool end ElnaLexerFloatToken = record(ElnaLexerToken) value: Double @@ -1061,10 +1053,12 @@ begin -- Unicode consumes more than 4 characters and is not supported here. if consumed > 0 & consumed <= 4 & current_position^ = '\'' then var - byte_token := malloc#[ElnaLexerWordToken](#size(ElnaLexerWordToken)) + byte_token := malloc#[ElnaLexerIntegerToken](#size(ElnaLexerIntegerToken)) begin - byte_token^.kind := ElnaLexerTokenKind.word8; - byte_token^.value := cast(buffer[1]: Word64); + byte_token^.kind := ElnaLexerTokenKind.integer; + byte_token^.magnitude := cast(buffer[1]: Word64); + byte_token^.is_signed := false; + byte_token^.size := 8u8; result := std_optional_make#[ElnaLexerToken](byte_token) end @@ -1103,10 +1097,31 @@ return result proc elna_lexer_parse_integer(start, cursor: ^const Word8) -> StdOptional#[ElnaLexerToken] var number_string := start[1 to cursor - start] + parsed_number: Word64 + number_stop: ^Word8 + result: ^ElnaLexerIntegerToken begin + errno()^ := 0i32; + if number_string.length > 2u & number_string[1] = '0' & (number_string[2] = 'b' or number_string[2] = 'B') then + parsed_number := strtoull(start + 2, @number_stop, 2) + elsif number_string.length > 2u & number_string[1] = '0' & (number_string[2] = 'x' or number_string[2] = 'X') then + parsed_number := strtoull(start + 2, @number_stop, 16) + else + parsed_number := strtoull(start, @number_stop, 10) + end; + if errno()^ <> ERANGE then + if cursor = number_stop or (cursor - number_stop = 1 & number_stop^ = 'i') then + end; + result := malloc#[ElnaLexerIntegerToken](#size(ElnaLexerIntegerToken)); + result^.kind := ElnaLexerTokenKind.integer; + result^.magnitude := cast(parsed_number: Word64); + result^.is_signed := true; + result^.size := 0u8 + + -- printf("%p %p %p\n\0".ptr, start, cursor, number_stop) end -return { StdOptional#[ElnaLexerToken] } +return std_optional_make#[ElnaLexerToken](result) -- TODO: Numbers are not tokenized. proc elna_lexer_parse_float(start, cursor: ^const Word8) -> StdOptional#[ElnaLexerToken] @@ -1376,6 +1391,22 @@ proc elna_lexer_dump_string(token: ^ElnaLexerStringToken) putchar(cast('"': Int)) return +proc elna_lexer_dump_integer(token: ^ElnaLexerIntegerToken) + if token^.is_signed then + if token^.size = 0u8 then + printf("«%lld»\0".ptr, token^.magnitude) + else + printf("«%lldi%d»\0".ptr, token^.magnitude, token^.size) + end + else + if token^.size = 0u8 then + printf("«0x%Xu»\0".ptr, token^.magnitude) + else + printf("«0x%Xu%d»\0".ptr, token^.magnitude, token^.size) + end + end +return + -- TODO: Replace printf and other writing functions with a writer callback. -- TODO: Cursor shouldn't be a parameter, only token. proc elna_lexer_token_dump*(token: ^ElnaLexerToken; cursor: ^ElnaLexerCursor) @@ -1435,26 +1466,7 @@ proc elna_lexer_token_dump*(token: ^ElnaLexerToken; cursor: ^ElnaLexerCursor) | ElnaLexerTokenKind.generic_open: printf("«#[»\0".ptr) | ElnaLexerTokenKind.attribute_open: printf("«#{»\0".ptr) | - ElnaLexerTokenKind.word: - printf("«0x%Xu»\0".ptr, cast(token: ^ElnaLexerWordToken)^.value) | - ElnaLexerTokenKind.word8: - printf("«0x%02Xu8»\0".ptr, cast(token: ^ElnaLexerWordToken)^.value) | - ElnaLexerTokenKind.word16: - printf("«0x%04Xu16»\0".ptr, cast(token: ^ElnaLexerWordToken)^.value) | - ElnaLexerTokenKind.word32: - printf("«0x%08Xu32»\0".ptr, cast(token: ^ElnaLexerWordToken)^.value) | - ElnaLexerTokenKind.word64: - printf("«0x%016lXu64»\0".ptr, cast(token: ^ElnaLexerWordToken)^.value) | - ElnaLexerTokenKind.integer: - printf("«%lld»\0".ptr, cast(token: ^ElnaLexerIntegerToken)^.value) | - ElnaLexerTokenKind.integer8: - printf("«%lldi8»\0".ptr, cast(token: ^ElnaLexerIntegerToken)^.value) | - ElnaLexerTokenKind.integer16: - printf("«%lldi16»\0".ptr, cast(token: ^ElnaLexerIntegerToken)^.value) | - ElnaLexerTokenKind.integer32: - printf("«%lldi32»\0".ptr, cast(token: ^ElnaLexerIntegerToken)^.value) | - ElnaLexerTokenKind.integer64: - printf("«%lldi64»\0".ptr, cast(token: ^ElnaLexerIntegerToken)^.value) | + ElnaLexerTokenKind.integer: elna_lexer_dump_integer(cast(token: ^ElnaLexerIntegerToken)) | ElnaLexerTokenKind.double: printf("«%g»\0".ptr, cast(token: ^ElnaLexerFloatToken)^.value) | ElnaLexerTokenKind.single: diff --git a/source/main.elna b/source/main.elna index b2145c8..fb1ae86 100644 --- a/source/main.elna +++ b/source/main.elna @@ -6,7 +6,7 @@ import sys.posix.stdio, elna.lexer var global_cursor: ElnaLexerCursor - sample_source_text := "Here.a few (* documentation block *) or #size if '\\n' `У` \"некоторая строка\" -- End line comment\0" + sample_source_text := "Here.a few (* documentation block *) or #size if '\\n' `У` \"некоторая строка\" 23 -- End line comment\0" token: ^ElnaLexerToken begin |
