Use pointers
This commit is contained in:
@@ -1,2 +1,3 @@
|
|||||||
TypoML
|
TypoML
|
||||||
src.bin
|
src.bin
|
||||||
|
typoml
|
||||||
|
|||||||
+2
-2
@@ -10,7 +10,7 @@ read_stdin :: proc() -> []string {
|
|||||||
return strings.split_lines(content)
|
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)
|
indent := strings.repeat(" ", depth, context.temp_allocator)
|
||||||
|
|
||||||
type_str := ""
|
type_str := ""
|
||||||
@@ -56,5 +56,5 @@ print_token :: proc(token: Token, depth: int = 0) {
|
|||||||
main :: proc() {
|
main :: proc() {
|
||||||
lines := read_stdin()
|
lines := read_stdin()
|
||||||
parsed := parse(lines)
|
parsed := parse(lines)
|
||||||
print_token(parsed)
|
print_token(&parsed)
|
||||||
}
|
}
|
||||||
|
|||||||
+15
-35
@@ -21,16 +21,15 @@ TokenValue :: union {
|
|||||||
Token :: struct {
|
Token :: struct {
|
||||||
type: TokenType,
|
type: TokenType,
|
||||||
value: TokenValue,
|
value: TokenValue,
|
||||||
children: [dynamic]Token,
|
children: [dynamic]^Token,
|
||||||
parent: ^Token,
|
parent: ^Token,
|
||||||
}
|
}
|
||||||
|
|
||||||
replace_text :: proc(text: string, start: int, len_: int, repl: string) -> string {
|
push_token :: proc(parent: ^Token, type: TokenType, value: TokenValue = nil) -> ^Token {
|
||||||
builder := strings.builder_make(context.temp_allocator)
|
token := new(Token)
|
||||||
strings.write_string(&builder, text[:start])
|
token^ = Token{type, value, nil, parent}
|
||||||
strings.write_string(&builder, repl)
|
append(&parent.children, token)
|
||||||
strings.write_string(&builder, text[start + len_:])
|
return token
|
||||||
return strings.to_string(builder)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
parse_header :: proc(line: string) -> (int, string) {
|
parse_header :: proc(line: string) -> (int, string) {
|
||||||
@@ -57,13 +56,7 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token {
|
|||||||
|
|
||||||
if len(line) == 0 {
|
if len(line) == 0 {
|
||||||
if line_index != len(lines) - 1 {
|
if line_index != len(lines) - 1 {
|
||||||
current_token = &root
|
current_token = push_token(&root, TokenType.Paragraph, nil)
|
||||||
|
|
||||||
append(
|
|
||||||
¤t_token.children,
|
|
||||||
Token{TokenType.Paragraph, nil, nil, current_token},
|
|
||||||
)
|
|
||||||
current_token = ¤t_token.children[len(current_token.children) - 1]
|
|
||||||
continue
|
continue
|
||||||
} else {
|
} else {
|
||||||
continue
|
continue
|
||||||
@@ -72,37 +65,24 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token {
|
|||||||
|
|
||||||
header_level, text := parse_header(line)
|
header_level, text := parse_header(line)
|
||||||
if header_level > 0 {
|
if header_level > 0 {
|
||||||
if current_token.parent != nil {
|
current_token = push_token(&root, TokenType.Header, header_level)
|
||||||
current_token = current_token.parent
|
push_token(current_token, TokenType.Text, text)
|
||||||
}
|
current_token = &root
|
||||||
|
|
||||||
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
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
if text[0:2] == "> " {
|
if strings.has_prefix(text, "> ") {
|
||||||
if current_token.type != TokenType.Blockquote {
|
if current_token.type != TokenType.Blockquote {
|
||||||
append(
|
current_token = push_token(&root, TokenType.Blockquote, nil)
|
||||||
¤t_token.children,
|
|
||||||
Token{TokenType.Blockquote, nil, nil, current_token},
|
|
||||||
)
|
|
||||||
current_token = ¤t_token.children[len(current_token.children) - 1]
|
|
||||||
}
|
}
|
||||||
append(¤t_token.children, Token{TokenType.Text, text[2:], nil, current_token})
|
push_token(current_token, TokenType.Text, text[2:])
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// LAST-CASE SCENARIO: NORMAL TEXT
|
// LAST-CASE SCENARIO: NORMAL TEXT
|
||||||
|
|
||||||
if current_token.parent == nil {
|
if current_token.parent == nil {
|
||||||
append(¤t_token.children, Token{TokenType.Paragraph, nil, nil, current_token})
|
current_token = push_token(&root, TokenType.Paragraph, nil)
|
||||||
current_token = ¤t_token.children[len(current_token.children) - 1]
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#partial switch current_token.parent.type {
|
#partial switch current_token.parent.type {
|
||||||
@@ -110,7 +90,7 @@ parse :: proc(lines: []string, allocator := context.allocator) -> Token {
|
|||||||
current_token = current_token.parent
|
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
|
return root
|
||||||
|
|||||||
Reference in New Issue
Block a user