Files
ziran/cmd/format_source.zi
T
waozi 4838ce9b2f Improve runtimes, add an experimental direct backend, and prune Ziran tests
Prepare short portable helpers and scalar record reads, improve JSON SIMD scanning, expose one-line execution, and publish qualified Alpha benchmark artifacts. Add the experimental verified lowered execution, serialization and Linux x86-64 backend with source maps.

Physically remove 263 redundant test entry points and unused helpers, migrate retained helpers to Ziran, and execute the full retained suite without sharding. Allow ignored backup state for the managed physical Alpha workflow.

Local validation: all 268 retained tests passed across the full run and corrected targeted reruns; bootstrap matches. Current assembly verifier, x86-64, executor and serializer tests passed. Website examples and all 4661 local links passed. Six Alpha JSON/text workloads retain verified artifacts, raw samples and explicit unsupported Wasm diagnostics. The mobile sub-500 ns target remains pending.
2026-11-11 18:46:34 -03:00

197 lines
7.3 KiB
Ziran

using Vectors :: #import "std/vec"
#import "std/option"
// Formatting policy lives in Ziran. This module has no filesystem or process
// dependency, so the same scanner can be used by other compiler tools.
#scope_file
FormatState :: struct {
indent: s64
parentheses: s64
comment_depth: s64
quote: u8
escaped: bool
delimiter: string
law_continuation: bool
}
Horizontal :: (byte: u8) -> bool {
return byte == #char " " || byte == #char "\t" || byte == #char "\r"
}
Identifier :: (byte: u8) -> bool {
return (byte >= #char "a" && byte <= #char "z") ||
(byte >= #char "A" && byte <= #char "Z") ||
(byte >= #char "0" && byte <= #char "9") || byte == #char "_"
}
Operator :: (byte: u8) -> bool {
return byte == #char "!" || byte == #char "<" || byte == #char ">" ||
byte == #char "=" || byte == #char ":" || byte == #char "+" ||
byte == #char "-" || byte == #char "*" || byte == #char "/" ||
byte == #char "%" || byte == #char "&" || byte == #char "|" ||
byte == #char "^"
}
// A raw-string opener ends its physical line. The returned view borrows the
// source, which stays alive throughout FormatSource.
RawMarker :: (line: string, at: s64) -> string {
if at + 7 >= line.count || line[at:at + 7] != "#string" ||
(at > 0 && Identifier(line[at - 1])) || !Horizontal(line[at + 7]) {
return ""
}
cursor: s64 = at + 7
while cursor < line.count && Horizontal(line[cursor]) { cursor += 1 }
start: s64 = cursor
if start >= line.count || !Identifier(line[start]) ||
(line[start] >= #char "0" && line[start] <= #char "9") { return "" }
while cursor < line.count && Identifier(line[cursor]) { cursor += 1 }
end: s64 = cursor
while cursor < line.count && Horizontal(line[cursor]) { cursor += 1 }
if cursor != line.count { return "" }
return line[start:end]
}
TrimOutput :: (output: *Vec(u8), start: s64) {
count := (<<output).count
while count > start && Horizontal((<<output)[count - 1]) {
count -= 1
}
unused VecResize(<<output, count)
}
// Structure is counted only in code. Strings and nested comments retain
// their bytes and cannot change the indentation of subsequent code.
ScanLine :: (state: *FormatState, line: string, output: *Vec(u8), spacing: bool) {
start: s64 = (<<output).count
index: s64 = 0
while index < line.count {
byte: u8 = line[index]
next_byte: u8 = 0
if index + 1 < line.count { next_byte = line[index + 1] }
if state.comment_depth > 0 {
if byte == #char "/" && next_byte == #char "*" {
state.comment_depth += 1
BuilderAppend(<<output, "/*")
index += 2
} else if byte == #char "*" && next_byte == #char "/" {
state.comment_depth -= 1
BuilderAppend(<<output, "*/")
index += 2
} else {
VecPush(<<output, byte)
index += 1
}
continue
}
if state.quote != 0 {
VecPush(<<output, byte)
if state.escaped { state.escaped = false }
else if byte == #char "\\" { state.escaped = true }
else if byte == state.quote { state.quote = 0 }
index += 1
continue
}
if byte == #char "/" && next_byte == #char "/" {
BuilderAppend(<<output, line[index:])
return
}
if byte == #char "/" && next_byte == #char "*" {
state.comment_depth = 1
BuilderAppend(<<output, "/*")
index += 2
continue
}
if byte == #char "\"" || byte == #char "'" {
state.quote = byte
VecPush(<<output, byte)
index += 1
continue
}
if byte == #char "#" {
if index == 0 && state.indent == 0 && line.count > 4 &&
line[0:4] == "#law" && Horizontal(line[4]) {
state.law_continuation = true
}
marker := RawMarker(line, index)
if marker.count > 0 {
state.delimiter = marker
BuilderAppend(<<output, line[index:])
if spacing { TrimOutput(output, start) }
return
}
}
if byte == #char "{" { state.indent += 1 }
if byte == #char "}" && state.indent > 0 { state.indent -= 1 }
if byte == #char "(" { state.parentheses += 1 }
if byte == #char ")" && state.parentheses > 0 { state.parentheses -= 1 }
token: string = ""
if byte == #char ";" && state.parentheses == 0 {
state.law_continuation = false
}
if spacing {
if byte == #char ":" && next_byte == #char ":" { token = " :: " }
else if byte == #char "=" && next_byte == #char ">" { token = " => " }
else if byte == #char "=" && next_byte != #char "=" &&
(index == 0 || !Operator(line[index - 1])) { token = " = " }
}
if token.count > 0 {
TrimOutput(output, start)
BuilderAppend(<<output, token)
index += 1
if token != " = " { index += 1 }
while index < line.count && Horizontal(line[index]) { index += 1 }
} else {
VecPush(<<output, byte)
index += 1
}
}
if spacing && state.quote == 0 && state.comment_depth == 0 {
TrimOutput(output, start)
}
// A backslash followed by a physical newline has consumed that newline,
// so it does not escape the first byte on the next line.
if state.quote != 0 { state.escaped = false }
}
#scope_export
FormatSource :: (source: string) -> Vec(u8) {
result: Vec(u8)
state: FormatState
cursor: s64 = 0
while cursor < source.count {
end: s64 = cursor
while end < source.count && source[end] != #char "\n" { end += 1 }
line: string = source[cursor:end]
if state.delimiter.count > 0 {
length: s64 = cast(s64)state.delimiter.count
if line.count >= length && line[0:length] == state.delimiter &&
(line.count == length || !Identifier(line[length])) {
BuilderAppend(result, line[0:length])
state.delimiter = ""
ScanLine(*state, line[length:], *result, false)
} else { BuilderAppend(result, line) }
} else if state.quote != 0 || state.comment_depth > 0 {
// Leading whitespace is content when a literal or comment spans
// lines; never trim it or prepend indentation.
ScanLine(*state, line, *result, false)
} else {
first: s64 = 0
while first < line.count && Horizontal(line[first]) { first += 1 }
if first < line.count {
indentation: s64 = state.indent
if line[first] == #char "}" && indentation > 0 { indentation -= 1 }
if state.parentheses > 0 || state.law_continuation { indentation += 1 }
level: s64 = 0
while level < indentation {
BuilderAppend(result, " ")
level += 1
}
ScanLine(*state, line[first:], *result, true)
}
}
VecPush(result, #char "\n")
cursor = end + 1
}
return result
}