diff options
| -rw-r--r-- | boot/ast.cc | 4 | ||||
| -rw-r--r-- | boot/type_check.cc | 50 | ||||
| -rw-r--r-- | include/elna/boot/type_check.h | 2 | ||||
| -rw-r--r-- | source/elna/ast.elna | 3 | ||||
| -rw-r--r-- | source/elna/lexer.elna | 7 | ||||
| -rw-r--r-- | source/elna/parser.elna | 45 | ||||
| -rw-r--r-- | source/elna/source.elna | 3 | ||||
| -rw-r--r-- | source/main.elna | 25 | ||||
| -rw-r--r-- | source/std.elna | 62 | ||||
| -rw-r--r-- | testsuite/fail_compilation/generic_base_arity.elna | 11 |
10 files changed, 163 insertions, 49 deletions
diff --git a/boot/ast.cc b/boot/ast.cc index e712f16..b892d23 100644 --- a/boot/ast.cc +++ b/boot/ast.cc @@ -395,6 +395,10 @@ namespace elna::boot void walking_visitor::visit(record_type_expression *expression) { + for (type_expression *argument : expression->base_arguments) + { + argument->accept(this); + } for (const field_declaration& field : expression->fields) { field.second->accept(this); diff --git a/boot/type_check.cc b/boot/type_check.cc index a135bcb..bebee9a 100644 --- a/boot/type_check.cc +++ b/boot/type_check.cc @@ -996,6 +996,12 @@ namespace elna::boot add_error<type_requirement_error>(expression->position(), base_type, type_requirement_error::kind::record_base); } + else + { + const std::shared_ptr<generic_type> generic = unwrap_generic(base_symbol->is_type()->symbol); + check_generic_arity(expression->base.value().position(), expression->base.value().name(), + generic, expression->base_arguments.size()); + } } } walking_visitor::visit(expression); @@ -1065,6 +1071,25 @@ namespace elna::boot return false; } + void type_analysis_visitor::check_generic_arity(const source_position position, + const std::string& name, const std::shared_ptr<generic_type>& generic, + const std::size_t argument_count) + { + if (generic == nullptr) + { + if (argument_count > 0) + { + add_error<generic_error>(position, name, generic_error::not_generic{}); + } + } + else if (argument_count != generic->parameters.size()) + { + add_error<argument_count_error>(position, + argument_count_error::kind::generic, name, + generic->parameters.size(), argument_count); + } + } + void type_analysis_visitor::check_parameters_used(const source_position position, const std::string& name, const std::vector<type>& parameters, const type& heading) { @@ -1153,31 +1178,18 @@ namespace elna::boot if (const auto instance = denoted.get<instantiated_type>()) { - std::shared_ptr<generic_type> const generic = unwrap_generic(instance->generic); + const std::shared_ptr<generic_type> generic = unwrap_generic(instance->generic); - if (generic == nullptr) - { - add_error<generic_error>(expression->position(), expression->name, - generic_error::not_generic{}); - } - else if (instance->arguments.size() != generic->parameters.size()) - { - add_error<argument_count_error>(expression->position(), - argument_count_error::kind::generic, expression->name, - generic->parameters.size(), instance->arguments.size()); - } - else + check_generic_arity(expression->position(), expression->name, + generic, instance->arguments.size()); + if (generic != nullptr && instance->arguments.size() == generic->parameters.size()) { check_arguments(*expression, expression->argument_types, argument_rule::opaque); } } - else if (std::shared_ptr<generic_type> const generic = unwrap_generic(denoted)) + else if (const std::shared_ptr<generic_type> generic = unwrap_generic(denoted)) { - // A bare generic is not a type at all, which is simply zero - // arguments where some were expected. - add_error<argument_count_error>(expression->position(), - argument_count_error::kind::generic, expression->name, - generic->parameters.size(), 0); + check_generic_arity(expression->position(), expression->name, generic, 0); } else if (procedure != nullptr && !expression->arguments.empty()) { diff --git a/include/elna/boot/type_check.h b/include/elna/boot/type_check.h index 3774311..ae06749 100644 --- a/include/elna/boot/type_check.h +++ b/include/elna/boot/type_check.h @@ -277,6 +277,8 @@ namespace elna::boot static bool is_equality_compatible(const type& left, const type& right); void validate_condition(const expression& condition); + void check_generic_arity(const source_position position, const std::string& name, + const std::shared_ptr<generic_type>& generic, const std::size_t argument_count); void check_arguments(const named_expression& reference, const std::vector<type>& arguments, const argument_rule rule); void check_parameters_used(const source_position position, const std::string& name, diff --git a/source/elna/ast.elna b/source/elna/ast.elna index 2fca087..6aed563 100644 --- a/source/elna/ast.elna +++ b/source/elna/ast.elna @@ -12,8 +12,7 @@ type unit ) ElnaAstNode* = record - kind: ElnaAstKind; - position: ElnaSourceSpan + kind: ElnaAstKind end ElnaAstUnit* = record(ElnaAstNode) end diff --git a/source/elna/lexer.elna b/source/elna/lexer.elna index 05dd1b2..9c83f06 100644 --- a/source/elna/lexer.elna +++ b/source/elna/lexer.elna @@ -769,6 +769,13 @@ begin elna_lexer_set_transition(ElnaLexerState.string_escape, ElnaLexerClass.invalid, ElnaLexerState.reject) return +proc elna_lexer_cursor_make*(source_text: []const Word8) -> ElnaLexerCursor +return { ElnaLexerCursor; + start: source_text.ptr, + cursor: source_text.ptr, + position: 1u +} + (* Advances the cursor so that it spans the next lexeme and contains the final accepting state. diff --git a/source/elna/parser.elna b/source/elna/parser.elna index d1448fa..0d7d7bd 100644 --- a/source/elna/parser.elna +++ b/source/elna/parser.elna @@ -5,25 +5,38 @@ (* Syntactic analysis. *) module -import sys.posix.stdlib, sys.posix.stdio, std, elna.lexer, elna.ast +import sys.posix.stdlib, sys.posix.stdio, std, + elna.source, elna.lexer, elna.ast -proc elna_parser_program*(lexer_cursor: ^ElnaLexerCursor) -> StdOptional#[ElnaAstUnit] -var - token: ^ElnaLexerToken - unit: ^ElnaAstUnit -begin - token := elna_lexer_lex(lexer_cursor).value; - elna_lexer_token_dump(token, stderr_()); - - while token^.kind <> ElnaLexerTokenKind.eof do - elna_lexer_token_destroy(token); +type + ElnaParser* = record + lexer: ^ElnaLexerCursor; + current: StdOptional#[ElnaLexerToken]; + diagnostics: StdSList#[ElnaSourceDiagnostic] + end - token := elna_lexer_lex(lexer_cursor).value; - elna_lexer_token_dump(token, stderr_()) - end; - elna_lexer_token_destroy(token); +proc elna_parser_make*(lexer: ^ElnaLexerCursor) -> ElnaParser +return { ElnaParser; + lexer: lexer, + current: elna_lexer_lex(lexer) +} +proc elna_parser_program*(parser: ^ElnaParser) -> ^ElnaAstUnit +var unit := malloc#[ElnaAstUnit](#size(ElnaAstUnit)) -return std_optional_make(unit) +begin + unit^.kind := ElnaAstKind.unit; + + if parser^.current.has_value then + if parser^.current.value^.kind = ElnaLexerTokenKind.module_ then + elsif parser^.current.value^.kind = ElnaLexerTokenKind.program_ then + else + var + error := malloc#[ElnaSourceDiagnostic](#size(ElnaSourceDiagnostic)) + begin + std_slist_append(@parser^.diagnostics, error) + end + end +return unit end. diff --git a/source/elna/source.elna b/source/elna/source.elna index af17b45..399701e 100644 --- a/source/elna/source.elna +++ b/source/elna/source.elna @@ -10,5 +10,8 @@ type start_offset: Word; end_offset: Word end + ElnaSourceDiagnostic* = record + code: Word + end end. diff --git a/source/main.elna b/source/main.elna index ebdeee1..f6b213f 100644 --- a/source/main.elna +++ b/source/main.elna @@ -5,20 +5,21 @@ program() import sys.posix.stdio, std, elna.lexer, elna.ast, elna.parser var - global_cursor: ElnaLexerCursor - sample_source_text := "Here.a few (* documentation block *) or #size if '\\n' `У` \"некоторая строка\" 23 -- End line comment\0" - token: ^ElnaLexerToken - tree: StdOptional#[ElnaAstUnit] + sample_source_text := "program() Here.a few (* documentation block *) or #size if '\\n' `У` \"некоторая строка\" 23 -- End line comment\0" begin elna_lexer_transitions(); - global_cursor := { ElnaLexerCursor; - start: sample_source_text.ptr, - cursor: sample_source_text.ptr, - position: 1u - }; - tree := elna_parser_program(@global_cursor); - - puts("void main(){}\0".ptr) + block local_main + var + global_cursor := elna_lexer_cursor_make(sample_source_text) + parser := elna_parser_make(@global_cursor) + tree := elna_parser_program(@parser) + begin + if std_slist_empty(@parser.diagnostics) then + fputs("int main(){ return 0; }\n\0".ptr, stdout_()) + else + fputs("Parsing failed.\n\0".ptr, stderr_()) + end; + end end. diff --git a/source/std.elna b/source/std.elna index c028b93..98fb35d 100644 --- a/source/std.elna +++ b/source/std.elna @@ -3,12 +3,74 @@ obtain one at https://mozilla.org/MPL/2.0/. *) module +import sys.posix.stdlib + type StdString* = []const Word8 + (* + Stores a nullable pointer. + + An empty StdOptional can created by just using the default initializer. + std_optional_make should be used to create an StdOptional holding a pointer. + + @param T Pointer target type. + *) StdOptional#[T]* = record has_value: Bool; value: ^T end + (* + Initializes a sinlgy-linked list. + + @param T List element type. + *) + StdSListNode#[T]* = record + next: ^StdSListNode#[T]; + value: ^T + end + (* + Singly-linked list. + + @param T List element type. + *) + StdSList#[T]* = record + head: StdOptional#[StdSListNode#[T]]; + tail: ^StdSListNode#[T] + end + +(* + Inserts a new element into the list. + + @param list List. + @param element Element to insert. +*) +proc std_slist_append#[T]*(list: ^StdSList#[T]; element: ^T) +var + node := malloc#[StdSListNode#[T]](#size(StdSListNode#[T])) +begin + node^.value := element; + + if list^.head.has_value then + node^.next := list^.head.value; + + list^.tail^.next := node; + list^.tail := node + else + node^.next := node; + + list^.head := std_optional_make(node); + list^.tail := node + end +return + +(* + Tells whether the list is empty. + + @param list List to exemine. + @return Whether the provided list is empty. +*) +proc std_slist_empty#[T]*(list: ^StdSList#[T]) -> Bool +return ~list^.head.has_value (* Constructs an optional holding the pointer value. diff --git a/testsuite/fail_compilation/generic_base_arity.elna b/testsuite/fail_compilation/generic_base_arity.elna new file mode 100644 index 0000000..d523780 --- /dev/null +++ b/testsuite/fail_compilation/generic_base_arity.elna @@ -0,0 +1,11 @@ +module + +type + Base#[T] = record + item: ^T + end + + Derived = record(Base) (* @Error Too few type arguments for generic 'Base', expected 1, got 0 *) + end + +end. |
