Skip to content

Syntax, Operators & Precedence

Kalc allows you to write calculations using both classical mathematical symbols and conversational English phrases. Its parser is backed by a top-down Pratt Parser (operator precedence from level 10 to 100), ensuring intuitive factor binding and natural expression evaluation.


Operators & Natural Language Synonyms

Mathematical symbols and their English counterparts can be used interchangeably:

OperationSymbolicConversational SynonymsExample
Addition+plus, and10 + 20 or 10 plus 20
Subtraction-minus, subtract, without50 - 15 or 50 minus 15
Multiplication*, ×, ·times, multiplied by, mul12 * 8 or 12 times 8
Division/, ÷divided by, divide, over100 / 4 or 100 divided by 4
Exponentiation^, **pow, to the power of2^8 or 2 to the power of 8
Conversion->in, to, as, into50 cm in inches or 100 EUR to USD
Percentages%percent15% of $80

Pratt Operator Precedence Table

When multiple operations appear on a single line, Kalc resolves them using top-down operator precedence:

Precedence LevelOperators / TokensAssociativityDescription
10 (Lowest)->, in, to, as, intoLeftUnit, currency, and radix conversions
20as a % of, as a % on, as a % off, to ... as %LeftRelative and proportional percentages
30+, -, plus, minus, subtractLeftBinary addition and subtraction
40Adjacent multi-part units (1 m 20 cm, $5 50 c)LeftDimensional combination addition
50*, /, times, mul, divide, overLeftMultiplication and division
60of, on, off (in percentage context)LeftPercentage scaling and offsets
70Implicit multiplication (2(3+4), 5x, 10USD)LeftAdjacent factors and scalar prefixes
80^, pow, to the power ofRightExponentiation
90Prefix +, -, sqrt, sin, cos, notRight (Prefix)Unary signs, bitwise NOT, and function calls
100 (Highest)Parentheses (...), Literals, Variables, Tab@varAtomGrouping, primary identifiers, cross-tab refs

Precedence Example

text
4 + 5 * 2                                       // 14 (Multiplication binds tighter than addition)
(4 + 5) * 2                                     // 18 (Parentheses take highest precedence)
100 + 10% in EUR                                // 101.45 EUR (Percentage offset binds before conversion)

Implicit Multiplication

Kalc understands common mathematical shorthand without requiring explicit * or times operators:

  1. Number Adjacent to Parentheses:
    text
    2(3 + 4)                                     // 14
  2. Adjacent Grouped Parentheses:
    text
    (4)(5)                                       // 20
  3. Scalar Adjacent to Variable or Unit:
    text
    x = 25
    4x                                           // 100
    10USD + 20USD                                // $30.00
    200m in feet                                 // 656.17 ft

Unicode Normalization & Casing Rules

1. Unicode Normalization Form C (NFC)

All source text is normalized to Unicode NFC before lexing. Mathematical operators like × (U+00D7), ÷ (U+00F7), non-breaking spaces, and typographic minus signs (, , ) are recognized cleanly as their standard ASCII counterparts.

2. Case Sensitivity

  • Case-Insensitive: Keywords, operator synonyms, built-in functions, and date keywords:
    • sqrt, SQRT, Sqrt
    • in, IN
    • plus, PLUS
    • today, TODAY
  • Case-Sensitive: Identifiers and unit symbols where case conveys dimensional meaning:
    • m (meter) vs. M (mega prefix)
    • k (kilo prefix) vs. K (Kelvin temperature)
    • b (bits) vs. B (bytes)
    • User variable identifiers (taxRate vs. TaxRate)

Formula Formatting (⌥ ⇧ ⌘ F)

Kalc includes a built-in AST FormulaFormatter:

  • Normalizes binary operator spacing (2+3*4 $\rightarrow$ 2 + 3 * 4).
  • Standardizes keyword casing (SQRT $\rightarrow$ sqrt, IN $\rightarrow$ in).
  • Balances nested parentheses.
  • Cleans trailing whitespace while preserving comments and headings.

Native macOS Computational Notepad • Pure Swift with Strict Concurrency