| 1 | module scheme |
| 2 | |
| 3 | import strings |
| 4 | import lexer |
| 5 | |
| 6 | fn numeric_prefix(text string, default_base int) !(int, int, int) { |
| 7 | mut base, mut exactness, mut start := default_base, 0, 0 |
| 8 | mut seen_base, mut seen_exact := false, false |
| 9 | for start < text.len - 1 && text[start] == `#` { |
| 10 | prefix := text[start + 1] | 0x20 |
| 11 | if prefix in [`b`, `o`, `d`, `x`] { |
| 12 | if seen_base { return failure('duplicate radix prefix') } |
| 13 | seen_base = true |
| 14 | base = match prefix { |
| 15 | `b` { 2 } |
| 16 | `o` { 8 } |
| 17 | `x` { 16 } |
| 18 | else { 10 } |
| 19 | } |
| 20 | } else if prefix in [`e`, `i`] { |
| 21 | if seen_exact { return failure('duplicate exactness prefix') } |
| 22 | seen_exact = true |
| 23 | exactness = if prefix == `e` { 1 } else { -1 } |
| 24 | } else { |
| 25 | return failure('unsupported numeric prefix') |
| 26 | } |
| 27 | start += 2 |
| 28 | } |
| 29 | return base, exactness, start |
| 30 | } |
| 31 | |
| 32 | // Skip decimal exponent signs, but not hexadecimal e/d/f digits. |
| 33 | fn rectangular_separator(body string, base int) int { |
| 34 | for pos in 1 .. body.len { |
| 35 | if body[pos] in [`+`, `-`] && !(base == 10 && (body[pos - 1] | 0x20) in [`e`, `s`, `f`, |
| 36 | `d`, `l`]) { |
| 37 | return pos |
| 38 | } |
| 39 | } |
| 40 | return -1 |
| 41 | } |
| 42 | |
| 43 | fn parse_complex_body(body string, base int, exactness int) !Value { |
| 44 | if at := body.index('@') { |
| 45 | r := parse_real_body(number_view(body, 0, at), base, exactness)! |
| 46 | a := parse_real_body(number_view(body, at + 1, body.len), base, exactness)! |
| 47 | if exactness == 1 { |
| 48 | if exact_sign(r) == 0 { return integer(0) } |
| 49 | if exact_sign(a) == 0 { return r } |
| 50 | return failure('exact polar result requires unsupported irrational components') |
| 51 | } |
| 52 | return polar_number(r, a) |
| 53 | } |
| 54 | coefficient := number_view(body, 0, body.len - 1) |
| 55 | separator := rectangular_separator(coefficient, base) |
| 56 | mut re := if exactness == -1 { real(0) } else { integer(0) } |
| 57 | im_text := if separator < 0 { |
| 58 | coefficient |
| 59 | } else { |
| 60 | number_view(coefficient, separator, coefficient.len) |
| 61 | } |
| 62 | if separator >= 0 { |
| 63 | re = parse_real_body(number_view(coefficient, 0, separator), base, exactness)! |
| 64 | } |
| 65 | im := if im_text == '+' || im_text == '-' { |
| 66 | if exactness == -1 { |
| 67 | real(if im_text == '-' { -1.0 } else { 1.0 }) |
| 68 | } else { |
| 69 | integer(if im_text == '-' { -1 } else { 1 }) |
| 70 | } |
| 71 | } else { |
| 72 | parse_real_body(im_text, base, exactness)! |
| 73 | } |
| 74 | return owned_complex(re, im) |
| 75 | } |
| 76 | |
| 77 | fn complex_nonfinite_spelling(text string) bool { |
| 78 | if text.len < 7 || text[text.len - 1] | 0x20 != `i` { return false } |
| 79 | for pos in 0 .. text.len - 6 { |
| 80 | if text[pos] in [`+`, `-`] && nonfinite_spelling(number_view(text, pos, pos + 6)) { |
| 81 | return true |
| 82 | } |
| 83 | } |
| 84 | return false |
| 85 | } |
| 86 | |
| 87 | fn validated_real_component(text string, base int, exactness int) ! { |
| 88 | if nonfinite_spelling(text) { |
| 89 | if base != 10 || exactness == 1 { return failure('invalid nonfinite complex component') } |
| 90 | read_nonfinite(text, base)! |
| 91 | return |
| 92 | } |
| 93 | // This cold string-only extension validates complete ordinary components |
| 94 | // through the protected scanner too; partial numeric prefixes never pass. |
| 95 | input := match base { |
| 96 | 2 { '#b' + text } |
| 97 | 8 { '#o' + text } |
| 98 | 16 { '#x' + text } |
| 99 | else { text } |
| 100 | } |
| 101 | mut scanner := lexer.new_scanner(input)! |
| 102 | token := scanner.next()! |
| 103 | if token.kind != .number || token.start != 0 || token.end != input.len || scanner.next()!.kind != .eof { |
| 104 | return failure('invalid complex component') |
| 105 | } |
| 106 | if text.contains('@') || text.ends_with('i') || text.ends_with('I') { |
| 107 | return failure('nested complex component') |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | fn read_nonfinite_complex(text string, radix int) !Value { |
| 112 | base, exactness, start := numeric_prefix(text, radix)! |
| 113 | body := number_view(text, start, text.len) |
| 114 | if body.len < 2 || body.contains('@') { return failure('invalid nonfinite complex notation') } |
| 115 | coefficient := number_view(body, 0, body.len - 1) |
| 116 | separator := rectangular_separator(coefficient, base) |
| 117 | if separator < 0 { |
| 118 | if coefficient.len == 0 || coefficient[0] !in [`+`, `-`] { |
| 119 | return failure('imaginary coefficient requires a sign') |
| 120 | } |
| 121 | validated_real_component(coefficient, base, exactness)! |
| 122 | } else { |
| 123 | validated_real_component(number_view(coefficient, 0, separator), base, exactness)! |
| 124 | im := number_view(coefficient, separator, coefficient.len) |
| 125 | if im != '+' && im != '-' { validated_real_component(im, base, exactness)! } |
| 126 | } |
| 127 | return parse_complex_body(body, base, exactness) |
| 128 | } |
| 129 | |
| 130 | fn complex_notation(value Value, radix int) !string { |
| 131 | re, im := complex_parts(value) |
| 132 | raw_re, raw_im := number_notation(re, radix)!, number_notation(im, radix)! |
| 133 | prefix := if radix != 10 && value.tag == .inexact_complex { '#i' } else { '' } |
| 134 | r := if raw_re.starts_with('#i') { number_view(raw_re, 2, raw_re.len) } else { raw_re } |
| 135 | i := if raw_im.starts_with('#i') { number_view(raw_im, 2, raw_im.len) } else { raw_im } |
| 136 | negative := i.len > 0 && i[0] == `-` |
| 137 | digits := if i.len > 0 && i[0] in [`+`, `-`] { number_view(i, 1, i.len) } else { i } |
| 138 | if r.len > max_int - digits.len - prefix.len - 2 { |
| 139 | return failure('numeric output exceeds supported size') |
| 140 | } |
| 141 | mut out := strings.new_buffer(r.len + digits.len + prefix.len + 2) |
| 142 | out.write_string(prefix) |
| 143 | out.write_string(r) |
| 144 | out.write_u8(if negative { `-` } else { `+` }) |
| 145 | out.write_string(digits) |
| 146 | out.write_u8(`i`) |
| 147 | return out.take_string() |
| 148 | } |
| 149 | |