module scheme import strings import lexer fn numeric_prefix(text string, default_base int) !(int, int, int) { mut base, mut exactness, mut start := default_base, 0, 0 mut seen_base, mut seen_exact := false, false for start < text.len - 1 && text[start] == `#` { prefix := text[start + 1] | 0x20 if prefix in [`b`, `o`, `d`, `x`] { if seen_base { return failure('duplicate radix prefix') } seen_base = true base = match prefix { `b` { 2 } `o` { 8 } `x` { 16 } else { 10 } } } else if prefix in [`e`, `i`] { if seen_exact { return failure('duplicate exactness prefix') } seen_exact = true exactness = if prefix == `e` { 1 } else { -1 } } else { return failure('unsupported numeric prefix') } start += 2 } return base, exactness, start } // Skip decimal exponent signs, but not hexadecimal e/d/f digits. fn rectangular_separator(body string, base int) int { for pos in 1 .. body.len { if body[pos] in [`+`, `-`] && !(base == 10 && (body[pos - 1] | 0x20) in [`e`, `s`, `f`, `d`, `l`]) { return pos } } return -1 } fn parse_complex_body(body string, base int, exactness int) !Value { if at := body.index('@') { r := parse_real_body(number_view(body, 0, at), base, exactness)! a := parse_real_body(number_view(body, at + 1, body.len), base, exactness)! if exactness == 1 { if exact_sign(r) == 0 { return integer(0) } if exact_sign(a) == 0 { return r } return failure('exact polar result requires unsupported irrational components') } return polar_number(r, a) } coefficient := number_view(body, 0, body.len - 1) separator := rectangular_separator(coefficient, base) mut re := if exactness == -1 { real(0) } else { integer(0) } im_text := if separator < 0 { coefficient } else { number_view(coefficient, separator, coefficient.len) } if separator >= 0 { re = parse_real_body(number_view(coefficient, 0, separator), base, exactness)! } im := if im_text == '+' || im_text == '-' { if exactness == -1 { real(if im_text == '-' { -1.0 } else { 1.0 }) } else { integer(if im_text == '-' { -1 } else { 1 }) } } else { parse_real_body(im_text, base, exactness)! } return owned_complex(re, im) } fn complex_nonfinite_spelling(text string) bool { if text.len < 7 || text[text.len - 1] | 0x20 != `i` { return false } for pos in 0 .. text.len - 6 { if text[pos] in [`+`, `-`] && nonfinite_spelling(number_view(text, pos, pos + 6)) { return true } } return false } fn validated_real_component(text string, base int, exactness int) ! { if nonfinite_spelling(text) { if base != 10 || exactness == 1 { return failure('invalid nonfinite complex component') } read_nonfinite(text, base)! return } // This cold string-only extension validates complete ordinary components // through the protected scanner too; partial numeric prefixes never pass. input := match base { 2 { '#b' + text } 8 { '#o' + text } 16 { '#x' + text } else { text } } mut scanner := lexer.new_scanner(input)! token := scanner.next()! if token.kind != .number || token.start != 0 || token.end != input.len || scanner.next()!.kind != .eof { return failure('invalid complex component') } if text.contains('@') || text.ends_with('i') || text.ends_with('I') { return failure('nested complex component') } } fn read_nonfinite_complex(text string, radix int) !Value { base, exactness, start := numeric_prefix(text, radix)! body := number_view(text, start, text.len) if body.len < 2 || body.contains('@') { return failure('invalid nonfinite complex notation') } coefficient := number_view(body, 0, body.len - 1) separator := rectangular_separator(coefficient, base) if separator < 0 { if coefficient.len == 0 || coefficient[0] !in [`+`, `-`] { return failure('imaginary coefficient requires a sign') } validated_real_component(coefficient, base, exactness)! } else { validated_real_component(number_view(coefficient, 0, separator), base, exactness)! im := number_view(coefficient, separator, coefficient.len) if im != '+' && im != '-' { validated_real_component(im, base, exactness)! } } return parse_complex_body(body, base, exactness) } fn complex_notation(value Value, radix int) !string { re, im := complex_parts(value) raw_re, raw_im := number_notation(re, radix)!, number_notation(im, radix)! prefix := if radix != 10 && value.tag == .inexact_complex { '#i' } else { '' } r := if raw_re.starts_with('#i') { number_view(raw_re, 2, raw_re.len) } else { raw_re } i := if raw_im.starts_with('#i') { number_view(raw_im, 2, raw_im.len) } else { raw_im } negative := i.len > 0 && i[0] == `-` digits := if i.len > 0 && i[0] in [`+`, `-`] { number_view(i, 1, i.len) } else { i } if r.len > max_int - digits.len - prefix.len - 2 { return failure('numeric output exceeds supported size') } mut out := strings.new_buffer(r.len + digits.len + prefix.len + 2) out.write_string(prefix) out.write_string(r) out.write_u8(if negative { `-` } else { `+` }) out.write_string(digits) out.write_u8(`i`) return out.take_string() }