Commit 2e5b193b0e
Verified · cmc
Layout: unified · split
Sources/OrgCore/Compute/Calc.swift +58 −11
| @@ -16,8 +16,10 @@ public enum Calc { | |||
| 16 | /// An error Calc reports, which a table shows as `#ERROR`. | 16 | /// An error Calc reports, which a table shows as `#ERROR`. |
| 17 | public struct Failure: Error, Equatable, CustomStringConvertible { | 17 | public struct Failure: Error, Equatable, CustomStringConvertible { |
| 18 | public let reason: String | 18 | public let reason: String |
| 19 | /// Where in the expression Calc reports the error. | ||
| 20 | public let position: Int | ||
| 19 | public var description: String { reason } | 21 | public var description: String { reason } |
| 20 | init(_ reason: String) { self.reason = reason } | 22 | init(_ reason: String, position: Int = 0) { (self.reason, self.position) = (reason, position) } |
| 21 | } | 23 | } |
| 22 | 24 | ||
| 23 | public indirect enum Value: Equatable, Sendable { | 25 | public indirect enum Value: Equatable, Sendable { |
| @@ -67,10 +69,11 @@ public enum Calc { | |||
| 67 | if numeric { | 69 | if numeric { |
| 68 | switch value { | 70 | switch value { |
| 69 | case .int, .float, .frac: break | 71 | case .int, .float, .frac: break |
| 70 | // Calc explains a division by zero or a power it left alone. | 72 | default: |
| 71 | case .symbolic(let t) where t.contains(where: { ["/", "^"].contains($0) }): | 73 | // Calc explains the first argument a quotient, power or function |
| 72 | throw Unsupported("symbolic arithmetic") | 74 | // rejected along the way, which isn't tracked here. |
| 73 | default: throw Failure("Number expected") | 75 | if tree.mayExplain { throw Unsupported("symbolic arithmetic") } |
| 76 | throw Failure("Number expected") | ||
| 74 | } | 77 | } |
| 75 | } | 78 | } |
| 76 | return try format(value, modes.format) | 79 | return try format(value, modes.format) |
| @@ -929,6 +932,17 @@ public enum Calc { | |||
| 929 | case binary(Character, Node, Node) | 932 | case binary(Character, Node, Node) |
| 930 | case call(String, [Node]) | 933 | case call(String, [Node]) |
| 931 | case variable(String) | 934 | case variable(String) |
| 935 | |||
| 936 | /// Whether the formula has a quotient, a power or a function call. | ||
| 937 | var mayExplain: Bool { | ||
| 938 | switch self { | ||
| 939 | case .number, .variable: false | ||
| 940 | case .call: true | ||
| 941 | case .vector(let items): items.contains { $0.mayExplain } | ||
| 942 | case .unary(_, let a): a.mayExplain | ||
| 943 | case .binary(let op, let a, let b): "/%\\^".contains(op) || a.mayExplain || b.mayExplain | ||
| 944 | } | ||
| 945 | } | ||
| 932 | } | 946 | } |
| 933 | 947 | ||
| 934 | struct Parser { | 948 | struct Parser { |
| @@ -936,6 +950,8 @@ public enum Calc { | |||
| 936 | var i = 0 | 950 | var i = 0 |
| 937 | /// Inside `[...]`, where Calc reads `x -1` as two elements. | 951 | /// Inside `[...]`, where Calc reads `x -1` as two elements. |
| 938 | var inVector = false | 952 | var inVector = false |
| 953 | /// Inside `[...]` with no comma, where Calc reads `1 2` as two elements. | ||
| 954 | var spaceSeparated = false | ||
| 939 | 955 | ||
| 940 | init(_ s: String) { chars = Array(s) } | 956 | init(_ s: String) { chars = Array(s) } |
| 941 | 957 | ||
| @@ -1026,6 +1042,9 @@ public enum Calc { | |||
| 1026 | } else if !(c.isASCII && c.isNumber) && !c.isLetter && c != "(" && c != "[" | 1042 | } else if !(c.isASCII && c.isNumber) && !c.isLetter && c != "(" && c != "[" |
| 1027 | && !(c == "." && i + 1 < chars.count && chars[i + 1].isASCII && chars[i + 1].isNumber) { | 1043 | && !(c == "." && i + 1 < chars.count && chars[i + 1].isASCII && chars[i + 1].isNumber) { |
| 1028 | break | 1044 | break |
| 1045 | } else if spaceSeparated, chars[i - 1] == " " || chars[i - 1] == "\t" { | ||
| 1046 | // Calc reads `[1 2]` as two elements and `[1 2 * 3]` as one; not reproduced. | ||
| 1047 | throw Unsupported("formula syntax") | ||
| 1029 | } | 1048 | } |
| 1030 | left = .binary("*", left, try unary()) | 1049 | left = .binary("*", left, try unary()) |
| 1031 | } | 1050 | } |
| @@ -1086,19 +1105,19 @@ public enum Calc { | |||
| 1086 | guard let c = peek() else { throw Unsupported("formula syntax") } | 1105 | guard let c = peek() else { throw Unsupported("formula syntax") } |
| 1087 | if c == "(" { | 1106 | if c == "(" { |
| 1088 | i += 1 | 1107 | i += 1 |
| 1089 | let outer = inVector | 1108 | let outer = (inVector, spaceSeparated) |
| 1090 | inVector = false | 1109 | (inVector, spaceSeparated) = (false, false) |
| 1091 | let inner = try or() | 1110 | let inner = try or() |
| 1092 | inVector = outer | 1111 | (inVector, spaceSeparated) = outer |
| 1093 | guard peek() == ")" else { throw Unsupported("formula syntax") } | 1112 | guard peek() == ")" else { throw Unsupported("formula syntax") } |
| 1094 | i += 1 | 1113 | i += 1 |
| 1095 | return inner | 1114 | return inner |
| 1096 | } | 1115 | } |
| 1097 | if c == "[" { | 1116 | if c == "[" { |
| 1098 | i += 1 | 1117 | i += 1 |
| 1099 | let outer = inVector | 1118 | let outer = (inVector, spaceSeparated) |
| 1100 | inVector = true | 1119 | (inVector, spaceSeparated) = (true, !hasComma()) |
| 1101 | defer { inVector = outer } | 1120 | defer { (inVector, spaceSeparated) = outer } |
| 1102 | var items: [Node] = [] | 1121 | var items: [Node] = [] |
| 1103 | if peek() != "]" { | 1122 | if peek() != "]" { |
| 1104 | items.append(try or()) | 1123 | items.append(try or()) |
| @@ -1124,6 +1143,8 @@ public enum Calc { | |||
| 1124 | name.append(chars[i]) | 1143 | name.append(chars[i]) |
| 1125 | i += 1 | 1144 | i += 1 |
| 1126 | } | 1145 | } |
| 1146 | // `[f (x)]` is two elements. | ||
| 1147 | if spaceSeparated, i < chars.count, chars[i] != "(", peek() == "(" { throw Unsupported("formula syntax") } | ||
| 1127 | guard peek() == "(" else { | 1148 | guard peek() == "(" else { |
| 1128 | if name == "nan" { return .number(.nan) } | 1149 | if name == "nan" { return .number(.nan) } |
| 1129 | // Calc's names are letters and digits; `_` is a subscript, and `'` and `#` | 1150 | // Calc's names are letters and digits; `_` is a subscript, and `'` and `#` |
| @@ -1133,6 +1154,9 @@ public enum Calc { | |||
| 1133 | return .variable(name) | 1154 | return .variable(name) |
| 1134 | } | 1155 | } |
| 1135 | i += 1 | 1156 | i += 1 |
| 1157 | let outer = (inVector, spaceSeparated) | ||
| 1158 | (inVector, spaceSeparated) = (false, false) | ||
| 1159 | defer { (inVector, spaceSeparated) = outer } | ||
| 1136 | var args: [Node] = [] | 1160 | var args: [Node] = [] |
| 1137 | if peek() != ")" { | 1161 | if peek() != ")" { |
| 1138 | args.append(try or()) | 1162 | args.append(try or()) |
| @@ -1148,6 +1172,18 @@ public enum Calc { | |||
| 1148 | throw Unsupported("formula syntax") | 1172 | throw Unsupported("formula syntax") |
| 1149 | } | 1173 | } |
| 1150 | 1174 | ||
| 1175 | /// `math-check-for-commas`: whether a comma follows before the bracket that closes. | ||
| 1176 | func hasComma() -> Bool { | ||
| 1177 | var depth = 0 | ||
| 1178 | for c in chars[i...] { | ||
| 1179 | if "[{(".contains(c) { depth += 1 } | ||
| 1180 | if "]})".contains(c) { depth -= 1 } | ||
| 1181 | if depth < 0 { return false } | ||
| 1182 | if c == ",", depth == 0 { return true } | ||
| 1183 | } | ||
| 1184 | return false | ||
| 1185 | } | ||
| 1186 | |||
| 1151 | /// Calc's number syntax: `7`, `007`, `1.5`, `.5`, `5.`, `1e3`, `1.5e-3`, fractions `1:3` | 1187 | /// Calc's number syntax: `7`, `007`, `1.5`, `.5`, `5.`, `1e3`, `1.5e-3`, fractions `1:3` |
| 1152 | /// and `1:2:3`, and integers in a radix, `16#FF`. | 1188 | /// and `1:2:3`, and integers in a radix, `16#FF`. |
| 1153 | mutating func number() throws -> Value { | 1189 | mutating func number() throws -> Value { |
| @@ -1193,6 +1229,17 @@ public enum Calc { | |||
| 1193 | i += 1 | 1229 | i += 1 |
| 1194 | } | 1230 | } |
| 1195 | fraction = f | 1231 | fraction = f |
| 1232 | // Calc reads digits and points as one number: `21.11.2026` is a bad format. | ||
| 1233 | if i < chars.count, chars[i] == ".", i + 1 >= chars.count || chars[i + 1] != "." { | ||
| 1234 | while i < chars.count, (chars[i].isASCII && chars[i].isNumber) | ||
| 1235 | || (chars[i] == "." && (i + 1 >= chars.count || chars[i + 1] != ".")) { i += 1 } | ||
| 1236 | // The position is in the string Calc reads, after it rewrites `..` and | ||
| 1237 | // characters such as `²`; an exponent belongs to the number. | ||
| 1238 | let before = String(chars[..<i]) | ||
| 1239 | guard before.allSatisfy(\.isASCII), !before.contains(".."), | ||
| 1240 | i >= chars.count || !"eE".contains(chars[i]) else { throw Unsupported("formula syntax") } | ||
| 1241 | throw Failure("Bad format", position: i) | ||
| 1242 | } | ||
| 1196 | } | 1243 | } |
| 1197 | var exponent: Int? | 1244 | var exponent: Int? |
| 1198 | if i < chars.count, chars[i] == "e" || chars[i] == "E" { | 1245 | if i < chars.count, chars[i] == "e" || chars[i] == "E" { |
Sources/OrgCore/Compute/CalcSymbolic.swift −8
| @@ -654,11 +654,3 @@ extension Calc { | |||
| 654 | } | 654 | } |
| 655 | } | 655 | } |
| 656 | } | 656 | } |
| 657 | |||
| 658 | extension Calc.Term { | ||
| 659 | /// Whether an operator or function in the formula satisfies `predicate`. | ||
| 660 | func contains(where predicate: (String) -> Bool) -> Bool { | ||
| 661 | guard case .op(let f, let args) = self else { return false } | ||
| 662 | return predicate(f) || args.contains { $0.contains(where: predicate) } | ||
| 663 | } | ||
| 664 | } | ||
Sources/OrgCore/Compute/TableFormulas.swift +3 −3
| @@ -1,8 +1,8 @@ | |||
| 1 | import Foundation | 1 | import Foundation |
| 2 | 2 | ||
| 3 | // Table formulas (org-table.el, Org 9.8.7): `org-table-recalculate` and | 3 | // Table formulas (org-table.el, Org 9.8.7): `org-table-recalculate` and |
| 4 | // `org-table-eval-formula` with Calc and Lisp evaluated natively. Symbolic results, Calc | 4 | // `org-table-eval-formula` with Calc and Lisp evaluated natively. Calc features beyond |
| 5 | // features beyond `Calc` and Lisp beyond `Elisp` throw `Calc.Unsupported`; the caller then | 5 | // `Calc` and Lisp beyond `Elisp` throw `Calc.Unsupported`; the caller then |
| 6 | // recalculates the table in Emacs. `remote()` finds tables by name in the file, and by ID | 6 | // recalculates the table in Emacs. `remote()` finds tables by name in the file, and by ID |
| 7 | // here or, through `RemoteTables`, in other files. | 7 | // here or, through `RemoteTables`, in other files. |
| 8 | 8 | ||
| @@ -626,7 +626,7 @@ extension EmacsBuffer { | |||
| 626 | result = try Calc.evaluate(form, modes: modes, numeric: !mode.keepEmpty && mode.numbers) | 626 | result = try Calc.evaluate(form, modes: modes, numeric: !mode.keepEmpty && mode.numbers) |
| 627 | } catch let failure as Calc.Failure { | 627 | } catch let failure as Calc.Failure { |
| 628 | // A duration formula takes Calc's error for a string, as org does. | 628 | // A duration formula takes Calc's error for a string, as org does. |
| 629 | if duration != nil { throw UserError(message: "Wrong type argument: stringp, (0 \"\(failure.reason)\")") } | 629 | if duration != nil { throw UserError(message: "Wrong type argument: stringp, (\(failure.position) \"\(failure.reason)\")") } |
| 630 | (result, fmt) = ("#ERROR", nil) | 630 | (result, fmt) = ("#ERROR", nil) |
| 631 | } | 631 | } |
| 632 | } | 632 | } |
Tests/OrgCoreTests/CalcTests.swift +18 −1
| @@ -359,6 +359,8 @@ struct CalcTests { | |||
| 359 | "uinf+1", "inf/0", "uinf/0", "inf^-1", "x/inf", "2^inf", "0.5^inf", "(-2)^inf", "inf^0", "nan*x", "x+nan", "x/nan", | 359 | "uinf+1", "inf/0", "uinf/0", "inf^-1", "x/inf", "2^inf", "0.5^inf", "(-2)^inf", "inf^0", "nan*x", "x+nan", "x/nan", |
| 360 | "vsum([x,1,2])", "vmean([x,1])", "vmean([x,2,3])", "vsum([x,y,1])", "vprod([x,2,3])", "vmean([a,b])", "vcount([x,1])", | 360 | "vsum([x,1,2])", "vmean([x,1])", "vmean([x,2,3])", "vsum([x,y,1])", "vprod([x,2,3])", "vmean([a,b])", "vcount([x,1])", |
| 361 | "vsum([1/0, 2])", "vsum([5 kg,3])", "vsum([2 (3),1])", | 361 | "vsum([1/0, 2])", "vsum([5 kg,3])", "vsum([2 (3),1])", |
| 362 | // No comma: Calc separates elements by spaces, except inside parentheses and arguments. | ||
| 363 | "vsum([1 - 2])", "vsum([x - 1])", "vsum([2x])", "vsum([(1 2)])", "vsum([max(1 2,3)])", "vcount([x])", | ||
| 362 | ] | 364 | ] |
| 363 | 365 | ||
| 364 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | 366 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) |
| @@ -386,7 +388,9 @@ struct CalcTests { | |||
| 386 | } | 388 | } |
| 387 | 389 | ||
| 388 | @Test func unsupported() throws { | 390 | @Test func unsupported() throws { |
| 389 | for e in ["sqrt(x)", "vmax([x,1])", "vsum(x,1)", "x<1", "if(x,1,2)", "x%2", "x_1", "x'", "Holidays+1", "café+1", | 391 | for e in ["vsum([5 kg])", "vsum([1 2])", "vcount([N A])", "vsum([2 3 4])", "vsum([1 1/2])", "vmin([3 4])", |
| 392 | "vcount([f (x)])", "vcount([1 [2]])", "vcount([(1) 2])", "vcount([1 sin (2)])", "1.2.3e5", | ||
| 393 | "sqrt(x)", "vmax([x,1])", "vsum(x,1)", "x<1", "if(x,1,2)", "x%2", "x_1", "x'", "Holidays+1", "café+1", | ||
| 390 | "x+<2026-10-05 Mon>", "[x,1]", "vcount([x -1])", "(1/0)+1", "vmean([])", "(-8)^(1/3)", "vvar([5])", "1<2<3", | 394 | "x+<2026-10-05 Mon>", "[x,1]", "vcount([x -1])", "(1/0)+1", "vmean([])", "(-8)^(1/3)", "vvar([5])", "1<2<3", |
| 391 | "vmax([])", "vmin([])", "vrms([1,2])", "vmeane([1,2])", "vcov([1,2],[3])", "vcov([1],[2])", | 395 | "vmax([])", "vmin([])", "vrms([1,2])", "vmeane([1,2])", "vcov([1,2],[3])", "vcov([1],[2])", |
| 392 | "fdiv(7.5,2)", "fdiv(7,0)", "perm(5,6)", "log(8,1)", "log(0,2)", "log(-8,2)", "arctanh(1)", "arccosh(0.5)", | 396 | "fdiv(7.5,2)", "fdiv(7,0)", "perm(5,6)", "log(8,1)", "log(0,2)", "log(-8,2)", "arctanh(1)", "arccosh(0.5)", |
| @@ -422,6 +426,19 @@ struct CalcTests { | |||
| 422 | // `calc-eval` with `num`: a formula is an error, unless Calc explains it. | 426 | // `calc-eval` with `num`: a formula is an error, unless Calc explains it. |
| 423 | #expect(throws: Calc.Failure("Number expected")) { try Calc.evaluate("x+1", numeric: true) } | 427 | #expect(throws: Calc.Failure("Number expected")) { try Calc.evaluate("x+1", numeric: true) } |
| 424 | #expect(throws: Calc.Unsupported.self) { try Calc.evaluate("1/0", numeric: true) } | 428 | #expect(throws: Calc.Unsupported.self) { try Calc.evaluate("1/0", numeric: true) } |
| 429 | // Calc explains what a quotient, power or function rejected, even when it cancels. | ||
| 430 | for e in ["x^2/x", "x*x/x", "inf+(1/0)", "x/2", "abs(x)", "x%2"] { | ||
| 431 | #expect(throws: Calc.Unsupported.self, "\(e)") { try Calc.evaluate(e, numeric: true) } | ||
| 432 | } | ||
| 433 | for e in ["-x", "x*2", "2 x", "x+2-2", "nan"] { | ||
| 434 | #expect(throws: Calc.Failure("Number expected"), "\(e)") { try Calc.evaluate(e, numeric: true) } | ||
| 435 | } | ||
| 436 | #expect(try Calc.evaluate("x/x", numeric: true) == "1") | ||
| 437 | // Digits and points read as one number, at the position Calc gives. | ||
| 438 | for (e, position) in [("21.11.2026+1", 10), ("(21.11.2026)+1", 11), ("1.5.+1", 4), (".5.5", 4), ("3.14.15", 7), | ||
| 439 | ("192.168.1.1", 11), ("1.2.3x", 5), ("1.5.", 4)] { | ||
| 440 | #expect(throws: Calc.Failure("Bad format", position: position), "\(e)") { try Calc.evaluate(e) } | ||
| 441 | } | ||
| 425 | var symbolic = Calc.Modes() | 442 | var symbolic = Calc.Modes() |
| 426 | symbolic.symbolic = true | 443 | symbolic.symbolic = true |
| 427 | for e in ["sqrt(2)", "2^0.5", "ln(2)", "x^0.5"] { | 444 | for e in ["sqrt(2)", "2^0.5", "ln(2)", "x^0.5"] { |
Tests/OrgCoreTests/TableFormulaTests.swift +25 −5
| @@ -96,11 +96,30 @@ struct TableFormulaTests { | |||
| 96 | "| r | area | half |\n|---+------+------|\n| 3 | | |\n| x | | |\n#+TBLFM: $2=$1^2*pi::$3=$2/2;S\n", | 96 | "| r | area | half |\n|---+------+------|\n| 3 | | |\n| x | | |\n#+TBLFM: $2=$1^2*pi::$3=$2/2;S\n", |
| 97 | "| a | b | c | d |\n|---+---+---+---|\n| 1 | | n/a | |\n| 2 | 3 | | |\n#+TBLFM: $4=vsum($1..$3)::@2$2=$1 + inf;S\n", | 97 | "| a | b | c | d |\n|---+---+---+---|\n| 1 | | n/a | |\n| 2 | 3 | | |\n#+TBLFM: $4=vsum($1..$3)::@2$2=$1 + inf;S\n", |
| 98 | "| a | b | c |\n|---+---+---|\n| x | 1 | |\n| | 2 | |\n#+TBLFM: $3=$1*$2;E\n", | 98 | "| a | b | c |\n|---+---+---|\n| x | 1 | |\n| | 2 | |\n#+TBLFM: $3=$1*$2;E\n", |
| 99 | // Calc rejects a number with two points. | ||
| 100 | "| a | b |\n|---+---|\n| 21.11.2026 | |\n| 192.168.1.1 | |\n| 1.5 | |\n#+TBLFM: $2=$1+1\n", | ||
| 101 | "| a | b |\n|---+---|\n| 21.11.2026 | |\n#+TBLFM: $2=$1+1;T\n", | ||
| 102 | "| a | b |\n|---+---|\n| 1:00 | |\n#+TBLFM: $2=$1+x;T\n", | ||
| 99 | ] | 103 | ] |
| 100 | 104 | ||
| 105 | /// A range of one field with a space is a vector without commas, which Calc splits at the | ||
| 106 | /// space; a power or quotient left symbolic under a duration flag has Calc's explanation. | ||
| 107 | @Test func refusesWhatCalcReadsOrExplainsDifferently() { | ||
| 108 | for text in [ | ||
| 109 | "| a | b | c |\n|---+---+---|\n| 5 kg | | |\n#+TBLFM: $3=vsum($1..$2)\n", | ||
| 110 | "| a | b | c |\n|---+---+---|\n| 1 2 | | |\n#+TBLFM: $3=vsum($1..$2)\n", | ||
| 111 | "| a | b |\n|---+---|\n| N A | |\n| | |\n#+TBLFM: @2$2=vmean(@2$1..@3$1)\n", | ||
| 112 | "| a | b |\n|---+---|\n| 1:00 | |\n#+TBLFM: $2=$1+x^2/x;T\n", | ||
| 113 | ] { | ||
| 114 | let caret = (text as NSString).range(of: "#+TBLFM").location + 3 | ||
| 115 | let failure = Self.recalculate(TableRecalculate(all: true), text, caret: caret).failure ?? "" | ||
| 116 | #expect(failure.hasPrefix("external:"), "\(failure) in\n\(text)") | ||
| 117 | } | ||
| 118 | } | ||
| 119 | |||
| 101 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | 120 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) |
| 102 | func textAndBlankFieldsMatchEmacsNatively() throws { | 121 | func textAndBlankFieldsMatchEmacsNatively() throws { |
| 103 | try compare(Self.symbolic, fallbacksAllowed: false) | 122 | try compare(Self.symbolic, fallbacksAllowed: false, headerFallbacksAllowed: false) |
| 104 | } | 123 | } |
| 105 | 124 | ||
| 106 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) | 125 | @Test(.enabled(if: ProcessInfo.processInfo.environment["ORGSTAR_SKIP_ORACLE"] == nil)) |
| @@ -164,7 +183,9 @@ struct TableFormulaTests { | |||
| 164 | try compare(Self.lisp, fallbacksAllowed: false) | 183 | try compare(Self.lisp, fallbacksAllowed: false) |
| 165 | } | 184 | } |
| 166 | 185 | ||
| 167 | func compare(_ tables: [String], fallbacksAllowed: Bool) throws { | 186 | /// With `headerFallbacksAllowed`, a formula Calc would leave symbolic over a header's |
| 187 | /// text may go to Emacs even when other fallbacks aren't allowed. | ||
| 188 | func compare(_ tables: [String], fallbacksAllowed: Bool, headerFallbacksAllowed: Bool = true) throws { | ||
| 168 | var cases: [EmacsOracle.Case] = [] | 189 | var cases: [EmacsOracle.Case] = [] |
| 169 | var ours: [(text: String, caret: Int, failure: String?)] = [] | 190 | var ours: [(text: String, caret: Int, failure: String?)] = [] |
| 170 | for text in tables { | 191 | for text in tables { |
| @@ -184,9 +205,8 @@ struct TableFormulaTests { | |||
| 184 | for (i, (mine, theirs)) in zip(ours, emacs).enumerated() { | 205 | for (i, (mine, theirs)) in zip(ours, emacs).enumerated() { |
| 185 | if let failure = mine.failure, failure.hasPrefix("external:") { | 206 | if let failure = mine.failure, failure.hasPrefix("external:") { |
| 186 | fallbacks += 1 | 207 | fallbacks += 1 |
| 187 | // Formulas over a header's text that Calc would leave symbolic in ways not | 208 | let header = failure.contains("symbolic") || failure.contains("of a formula") |
| 188 | // reproduced go to Emacs. | 209 | #expect(fallbacksAllowed || (headerFallbacksAllowed && header), "\(failure) in\n\(cases[i].text)") |
| 189 | #expect(fallbacksAllowed || failure.contains("symbolic") || failure.contains("of a formula"), "\(failure) in\n\(cases[i].text)") | ||
| 190 | continue | 210 | continue |
| 191 | } | 211 | } |
| 192 | let expected = (theirs.text, EmacsOracle.offset(theirs.point, in: theirs.text), theirs.error.isEmpty ? nil : theirs.error) | 212 | let expected = (theirs.text, EmacsOracle.offset(theirs.point, in: theirs.text), theirs.error.isEmpty ? nil : theirs.error) |
docs/manual/guide/10-tables.org +4
| @@ -234,6 +234,10 @@ and are left out of ranges. | |||
| 234 | A field's text goes into the formula as it is, so a field holding text such as =N/A= or | 234 | A field's text goes into the formula as it is, so a field holding text such as =N/A= or |
| 235 | =TBD= becomes Calc variables and the result is a formula: =$1+5= with =N/A= in =$1= | 235 | =TBD= becomes Calc variables and the result is a formula: =$1+5= with =N/A= in =$1= |
| 236 | gives =N / A + 5=. The =N= flag reads every field as a number instead, with text as 0. | 236 | gives =N / A + 5=. The =N= flag reads every field as a number instead, with text as 0. |
| 237 | A field such as =21.11.2026= or =192.168.1.1=, with more than one decimal point, is | ||
| 238 | =#ERROR=, as Calc can't read it as a number. A range whose only non-empty field holds | ||
| 239 | text with a space, such as =5 kg=, needs Emacs: Calc reads a vector without commas as | ||
| 240 | elements separated by spaces. | ||
| 237 | 241 | ||
| 238 | ** Names, parameters and constants | 242 | ** Names, parameters and constants |
| 239 | 243 | ||