Use pointers
This commit is contained in:
@@ -1,2 +1,3 @@
|
||||
TypoML
|
||||
src.bin
|
||||
typoml
|
||||
|
||||
+2
-2
@@ -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)
|
||||
}
|
||||
|
||||
+15
-35
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user