aboutsummaryrefslogtreecommitdiff
path: root/source
diff options
context:
space:
mode:
authorEugen Wissner <belka@caraus.de>2026-11-14 23:39:52 +0100
committerEugen Wissner <belka@caraus.de>2026-11-14 23:39:52 +0100
commit9b4b13e0d9030bb21c86eea6c4ba00bf4d91f0a0 (patch)
treedd1c8fa2336b098d6380e579125d01ac8711a3c6 /source
parent7ab6e1021611cc6913ee4ab5faf1b31f928a65e9 (diff)
downloadelna-9b4b13e0d9030bb21c86eea6c4ba00bf4d91f0a0.tar.gz
Add arity check for generic parameters
Diffstat (limited to 'source')
-rw-r--r--source/elna/ast.elna3
-rw-r--r--source/elna/lexer.elna7
-rw-r--r--source/elna/parser.elna45
-rw-r--r--source/elna/source.elna3
-rw-r--r--source/main.elna25
-rw-r--r--source/std.elna62
6 files changed, 115 insertions, 30 deletions
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.