Skip to content

feat: read 5'10" as feet and inches - #24

Merged
aurelleb merged 1 commit into
mainfrom
feat/12-prime-notation
Nov 16, 2026
Merged

aurelleb merged 1 commit into
mainfrom
feat/12-prime-notation

Conversation

@aurelleb

Copy link
Copy Markdown
Contributor

Part of #12

What was wrong

  • 5'10" evaluated to 5. The quote opened an unterminated string literal, and the rest of the input was dropped.
  • 5' and 5" evaluated to 5parsec. The empty string literal was looked up as a unit name. UnitDatabase::matchExact("") matched every unit with an empty symbol, and parsec comes first.

What changed

  • Lexer (lexer.cpp, +5 lines): a ' or " directly after a digit becomes a one-character unit token (a prime mark). It no longer opens a string literal there. Quotes anywhere else still delimit strings.
  • Parser (parsePrimes, called after a number in parseTerm): N' is N ft and N" is N in. N'M" becomes N' + M". The closing inch mark can be left off (5'10). Inside the compound, the inches must be glued to the foot mark. The whole compound is consumed, so 5'2" is never read as ft². This uses the existing ' → foot and " → inch aliases.
  • Unit lookup (unit.cpp): matchExact returns nothing for an empty name, so an empty name never resolves to a unit.

No interpreter or unit-table changes.

Not done: the minutes and seconds reading

Giving minute the ' alias and second the " alias does not work with the current ambiguity mechanism. With both marks ambiguous, resolvePair returns nullopt, reconcileUnits leaves both sides alone, and add() sums the raw numbers. So 5'10" came out as 15in, and the length reading broke too. I tried this and reverted it. 5'10" to s is now an error ("Incompatible units"), not a wrong number.

To support time, the interpreter would need two things:

  1. resolvePair picks a default pairing when both sides are ambiguous: the first reading of the left side that shares a dimension with the right side, which gives feet and inches.
  2. The conversion target's dimension is passed down into the evaluation of the left side. Feet to inches is 12:1 but minutes to seconds is 60:1, so the sum can't stay unresolved and be settled later the way 1m + 1m to s is.

Tests

New tests/corpus/prime.corpus:

  • 5', 5", 5'10", 5'10, 6'0", and 5'2 / 5'3" (not read as squared units)
  • conversions to cm and in, and arithmetic on the compound
  • 5'10" to s → !error Incompatible units: foot (length) to second (duration)
  • 5 '' → 5 (it used to be parsec)
  • string checks: "5'10" → 5'10, '5"', 5 'abc', 'hello world', upper('a1'), "12:30", 'unfinished

The new cases fail on main and pass with this change. The existing corpus "typing an expression never crashes" test also runs every prefix of the new lines.

  • make && ./build/numen-tests: all tests passed (15337 assertions in 93 test cases)
  • make debug && ./build-debug/numen-tests (ASan+UBSan): all tests passed (15337 assertions in 93 test cases)
  • clang-format -i run on the changed files. make tidy was not run because run-clang-tidy isn't installed on this machine.
  • Tested only on macOS (AppleClang). Linux GCC/Clang, MSVC and Nix rely on CI.

Out of scope: coordinates and angles, and spaced forms like 5' 10", which still evaluates to 5ft and ignores the rest.

🤖 Generated with Claude Code

A quote glued to a number is now a prime mark instead of a string opener,
so 5' is 5 ft, 5" is 5 in and 5'10" (or 5'10) is 5 ft + 10 in. Quotes
anywhere else still delimit string literals.

An empty name no longer resolves to parsec: matchExact("") matched every
unit without a symbol, parsec first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@aurelleb
aurelleb marked this pull request as ready for review November 16, 2026 17:04
@aurelleb
aurelleb merged commit 42dcb73 into main Nov 16, 2026
8 checks passed
@aurelleb
aurelleb deleted the feat/12-prime-notation branch November 16, 2026 17:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant