From 8a2865828f62989896cb45ffc95e13e89cd0e27e Mon Sep 17 00:00:00 2001 From: "N0\\A" Date: Tue, 14 Jul 2026 12:04:59 +0200 Subject: [PATCH] Use pointers --- .gitignore | 1 + src/main.odin | 4 ++-- src/parser.odin | 50 +++++++++++++++---------------------------------- 3 files changed, 18 insertions(+), 37 deletions(-) diff --git a/.gitignore b/.gitignore index 22b8c39..259ef2b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ TypoML src.bin +typoml diff --git a/src/main.odin b/src/main.odin index 4e4d90a..2b639ba 100644 --- a/src/main.odin +++ b/src/main.odin @@ -10,7 +10,7 @@ read_stdin :: proc() -> []string { return strings.split_lines(content) } -print_token :: proc(token: Token, depth: int = 0) { +print_token :: proc(token: ^Token, depth: int = 0) { indent := strings.repeat(" ", depth, context.temp_allocator) type_str := "" @@ -56,5 +56,5 @@ print_token :: proc(token: Token, depth: int = 0) { main :: proc() { lines := read_stdin() parsed := parse(lines) - print_token(parsed) + print_token(&parsed) } diff --git a/src/parser.odin b/src/parser.odin index 8c4327a..4b09759 100644 --- a/src/parser.odin +++ b/src/parser.odin @@ -21,16 +21,15 @@ TokenValue :: union { Token :: struct { type: TokenType, value: TokenValue, - children: [dynamic]Token, + children: [dynamic]^Token, parent: ^Token, } -replace_text :: proc(text: string, start: int, len_: int, repl: string) -> string { - builder := strings.builder_make(context.temp_allocator) - strings.write_string(&builder, text[:start]) - strings.write_string(&builder, repl) - strings.write_string(&builder, text[start + len_:]) - return strings.to_string(builder) +push_token :: proc(parent: ^Token, type: TokenType, value: TokenValue = nil) -> ^Token { + token := new(Token) + token^ = Token{type, value, nil, parent} + append(&parent.children, token) + return token } parse_header :: proc(line: string) -> (int, string) { @@ -57,13 +56,7 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token { if len(line) == 0 { if line_index != len(lines) - 1 { - current_token = &root - - append( - ¤t_token.children, - Token{TokenType.Paragraph, nil, nil, current_token}, - ) - current_token = ¤t_token.children[len(current_token.children) - 1] + current_token = push_token(&root, TokenType.Paragraph, nil) continue } else { continue @@ -72,37 +65,24 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token { header_level, text := parse_header(line) if header_level > 0 { - if current_token.parent != nil { - current_token = current_token.parent - } - - append( - ¤t_token.children, - Token{TokenType.Header, header_level, nil, current_token}, - ) - current_token = ¤t_token.children[len(current_token.children) - 1] - append(¤t_token.children, Token{TokenType.Text, text, nil, current_token}) - current_token = current_token.parent + current_token = push_token(&root, TokenType.Header, header_level) + push_token(current_token, TokenType.Text, text) + current_token = &root continue } - if text[0:2] == "> " { + if strings.has_prefix(text, "> ") { if current_token.type != TokenType.Blockquote { - append( - ¤t_token.children, - Token{TokenType.Blockquote, nil, nil, current_token}, - ) - current_token = ¤t_token.children[len(current_token.children) - 1] + current_token = push_token(&root, TokenType.Blockquote, nil) } - append(¤t_token.children, Token{TokenType.Text, text[2:], nil, current_token}) + push_token(current_token, TokenType.Text, text[2:]) continue } // LAST-CASE SCENARIO: NORMAL TEXT if current_token.parent == nil { - append(¤t_token.children, Token{TokenType.Paragraph, nil, nil, current_token}) - current_token = ¤t_token.children[len(current_token.children) - 1] + current_token = push_token(&root, TokenType.Paragraph, nil) } #partial switch current_token.parent.type { @@ -110,7 +90,7 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token { current_token = current_token.parent } - append(¤t_token.children, Token{TokenType.Text, line, nil, current_token}) + push_token(current_token, TokenType.Text, line) } return root