Skip to content

CalcFile Language Specification v0.1 ​

Status: Experimental / pre-1.0 draft profile

This document is the normative definition of the experimental CalcFile v0.1 language profile. The TypeScript reference engine is an implementation of this specification, not the specification itself.

Conformance is determined against the v0.1 machine-readable corpus. Where this document and a fixture disagree, treat that as a specification defect and report it; implementations must not silently choose their own behavior.

Make calculation feel like writing a note, while keeping the note a real file the user owns.


1. Scope ​

CalcFile is a line-oriented, prose-friendly calculation document format.

A CalcFile document is UTF-8 plain text. Editors may render live results next to source lines, but generated results are not part of the file.

Example source:

text
@locale de-DE

# Japan

Flights to Tokyo = 740 EUR
Hotel = 120 EUR * 6
Food about 45 EUR * 10

total

An editor may display:

text
Flights to Tokyo = 740 EUR                  740,00 €
Hotel = 120 EUR * 6                         720,00 €
Food about 45 EUR * 10                      450,00 €

total                                      1.910,00 €

The source remains unchanged.


2. Design principles ​

  1. Prose first. Human context may coexist with expressions.
  2. Live editing. Temporary incomplete syntax is normal.
  3. Forgiving, not magical. Unknown prose may be ignored where intent is clear; ambiguity must not silently produce arbitrary values.
  4. Top-to-bottom evaluation.
  5. Friendly variables. Multi-word and case-insensitive.
  6. Typed values. Numbers, currencies, and physical units retain semantics.
  7. Locale is semantic where necessary.
  8. Derived results are not persisted.
  9. Not a general-purpose programming language.
  10. Spec + conformance define compatibility.

Part I — Document structure ​

3. Line model ​

Each physical line is exactly one of:

  • blank
  • directive
  • header
  • forced comment
  • prose-only text
  • valid calculation
  • incomplete calculation
  • ambiguous calculation
  • erroneous calculation

Evaluation proceeds top-to-bottom.

4. Blank lines ​

Whitespace-only lines are blank.

They:

  • produce no value,
  • keep named variables,
  • reset the current subtotal block,
  • break implicit line continuation,
  • do not reset total.

5. Directives ​

A directive begins with @.

v0.1 supports:

text
@locale de-DE

Grammar:

text
@locale <BCP-47 locale tag>

A later locale directive applies from that line onward.

Unknown directives produce unsupported-directive but do not invalidate unrelated lines.

6. Headers ​

A header begins with one or more # followed by whitespace:

text
# Travel
## Accommodation

Headers:

  • produce no numeric value,
  • reset the section accumulator used by total,
  • reset subtotal,
  • break continuation,
  • may appear in editor outline/navigation.

All heading levels have the same aggregate-reset semantics in v0.1.

7. Forced comments ​

A line whose first non-whitespace characters are // is ignored as a calculation.

Trailing comments are deferred.


Part II — Value model ​

8. Required value categories ​

v0.1 canonical result values include:

text
number
unit
currency

Canonical values are independent from localized presentation.

Examples:

json
{ "type": "number", "value": "3" }
{ "type": "unit", "value": "5.08", "unit": "cm" }
{ "type": "currency", "value": "1450", "currency": "EUR" }

Decimal values are serialized canonically as base-10 strings.

9. Precision ​

0.1 + 0.2 must semantically equal 0.3, never expose 0.30000000000000004.

The reference engine should use decimal-safe Math.js configuration or equivalent.


Part III — Locale and numbers ​

10. Locale ​

If the file has no @locale, the host supplies an input locale.

Portable documents SHOULD pin locale when locale-sensitive notation is used.

11. en-US ​

Accepted:

text
1.5
1,234.56
1 234.56
.5

Meaning:

text
1.5
1234.56
1234.56
0.5

12. de-DE ​

Accepted:

text
1,5
1.234,56
1 234,56
,5

Meaning:

text
1.5
1234.56
1234.56
0.5

13. Grouping validity ​

Malformed grouping must not be silently repaired.

Examples such as 1,23,4 under en-US or 1.23.4 under de-DE should become ambiguous or error, not 1234.

14. Magnitude suffixes ​

Required:

text
k / K = 1,000
M = 1,000,000
b / B = 1,000,000,000

Lowercase m is NOT million because it conflicts with metre.


Part IV — Arithmetic ​

15. Operators ​

Required:

text
+
-
*
/
^
()

Precedence:

  1. parentheses
  2. exponentiation
  3. multiplication/division
  4. addition/subtraction

Unary plus/minus are supported.


Part V — Prose-first semantics ​

16. Description boundary ​

A colon explicitly separates description from calculation:

text
Hotel: 120 EUR * 6

Result:

text
720 EUR

17. Free prose ​

These are valid target forms:

text
500 for tickets + 70 for taxi
Estimated hotel maybe 100 + 20
Train tickets around 45 * 3

Expected:

text
570
120
135

Unknown words may be ignored only when the remaining semantic token sequence forms one coherent expression.

18. Prose-only text ​

text
Maybe compare this with Airbnb later

is text, not an error.

19. Ambiguity ​

If more than one materially different valid calculation remains possible, the line is ambiguous.

Prefer ambiguity over silently choosing an arbitrary interpretation.

20. Semantic tokens must survive prose filtering ​

Never discard tokens merely because they are words.

Known semantic tokens include:

  • variables,
  • unit names,
  • currency codes,
  • conversion keywords,
  • percentage keyword of,
  • aggregate keywords.

Example:

text
100 USD in EUR

must preserve in.

Example:

text
Hotel in Berlin 120 EUR

must not interpret in Berlin as conversion because Berlin is not a valid target unit/currency.


Part VI — Variables ​

21. Assignment ​

text
Monthly rent = 1400 EUR

The left side defines the variable; the right side is evaluated.

Assignment lines also produce their assigned value as visible line results.

22. Friendly names ​

Variable names:

  • may contain spaces,
  • must contain at least one letter,
  • may include digits,
  • preserve spelling for display,
  • are case-insensitive for lookup,
  • cannot be only a reserved language keyword.

Examples:

text
Monthly rent = 1400 EUR
Internet bill = 50 EUR
iPhone 15 = 998 EUR

23. Case-insensitive lookup ​

text
iPhone 15 = 998 EUR
iphone 15 - 200 EUR

=> 798 EUR

24. Longest match ​

Given:

text
New York hotel = 1000 EUR
York hotel = 500 EUR
New York hotel - York hotel

the longest known phrase wins, producing 500 EUR.

25. Scope ​

Variables become available after successful assignment and remain available until end-of-document.

Failed/incomplete assignments do not mutate scope.


Part VII — Previous result and continuation ​

26. ans ​

ans is the previous successful calculation value.

Blank/header/comment/text/incomplete/ambiguous/error lines do not replace it.

27. Continuation ​

A line starting semantically with:

text
+
-
*
/

continues from the prior successful result if there is no intervening blank/header/comment.

text
100
+ 20
* 2
- 5

=> line values:

text
100
120
240
235

A blank line breaks continuation.


Part VIII — Percentages ​

28. Percentage literal ​

text
25%

has canonical numeric meaning 0.25.

29. of ​

text
25% of 100

=> 25

30. Calculator percentage addition/subtraction ​

text
100 + 25%

=> 125

text
100 - 25%

=> 75

31. Percentage multiplication ​

text
100 * 25%

=> 25

32. Modulo ​

Whitespace distinguishes modulo:

text
12 % 10

=> 2

while:

text
12%

is a percentage literal.


Part IX — Aggregates ​

33. Participation ​

Successful calculation lines participate in aggregates, including assignments and continuation lines.

Aggregate result lines themselves do not recursively participate.

34. subtotal ​

Sums compatible values since the latest:

  • blank line,
  • header,
  • document start.

35. total ​

Sums compatible values since the latest header or document start.

Blank lines do not reset it.

36. grand total ​

Sums compatible values from document start through the line before the aggregate.

37. Averages ​

v0.1 includes:

text
avg
subavg

using total and subtotal windows respectively.

38. Type compatibility ​

text
100 EUR
50 EUR
total

is valid.

text
100 EUR
5 km
total

is error: incompatible-aggregate-types.

Compatible physical units may be normalized before aggregation.


Part X — Physical units ​

39. Unit quantities ​

Use typed unit semantics compatible with Math.js where practical.

Examples:

text
2 inch
5 cm
2.5 hour
200 km / 2.5 hour

40. Conversion ​

Support:

text
to
in

when the left side is typed and the right side is a known compatible target.

text
2 inch to cm

=> 5.08 cm

41. Derived units ​

text
200 km / 2.5 hour

=> canonical equivalent of 80 km/hour.

42. Count labels are deferred ​

This remains undefined in v0.1:

text
Hotel = 120 EUR * 6 nights

Do not silently erase physical units or prose labels until a dedicated RFC defines count-label semantics.

For v0.1 use:

text
Hotel = 120 EUR * 6

Part XI — Currencies ​

43. Literals ​

Required:

text
100 EUR
100 USD
20 GBP
€100
100€
$100
£20

Canonical identity uses ISO 4217 codes.

44. Same-currency arithmetic ​

text
100 EUR + 50 EUR

=> 150 EUR

text
120 EUR * 6

=> 720 EUR

45. Conversion ​

text
100 USD in EUR
100 USD to EUR

requires externally supplied rate data.

The core engine MUST NOT fetch rates itself.

Without required rates:

text
error: missing-external-data

Freshness/source metadata is environment/editor information, not part of canonical numeric value.


Part XII — Functions ​

46. Curated function set ​

Required initial functions:

text
sqrt
abs
round
floor
ceil
min
max

Recommended but optional within v0.1 implementation milestone:

text
sin
cos
tan
log

Do not expose every Math.js parser function automatically.

47. Locale-sensitive argument separator ​

Under dot-decimal locales:

text
round(10.234, 2)

Under comma-decimal locales:

text
round(10,234; 2)

Part XIII — Status and diagnostics ​

48. Primary statuses ​

Every line has one:

text
empty
directive
header
comment
text
valid
incomplete
ambiguous
error

49. Incomplete ​

Examples:

text
100 +
Hotel =
(100 + 20
100 USD in

These are incomplete, not hard syntax errors.

50. Error ​

Examples:

text
10 EUR + 5 km
100 USD in cm

Suggested diagnostic codes:

text
incompatible-types
invalid-conversion
unknown-variable
missing-external-data
incompatible-aggregate-types
unsupported-function
unsupported-directive

51. Failed-line state ​

Incomplete/ambiguous/error lines:

  • do not update ans,
  • do not define variables,
  • do not participate in aggregates.

Later unrelated lines may still evaluate.


Part XIV — Canonical conformance model ​

52. Semantic comparison ​

Conformance compares canonical semantics, not CodeMirror rendering.

Example:

json
{
  "line": 4,
  "status": "valid",
  "value": {
    "type": "currency",
    "value": "740",
    "currency": "EUR"
  }
}

Localized display such as 740,00 € is editor behavior and normally not conformance-critical.


Part XV — Deferred syntax ​

53. Explicitly deferred ​

Not part of v0.1:

  • dates/timezones
  • plotting
  • matrices
  • user-defined functions
  • cross-file references
  • Markdown embedding
  • nested aggregate sections
  • localized keywords such as von
  • count-label grammar
  • custom units
  • vendor extensions
  • conditionals/control flow
  • AI features

Implementations should not casually invent syntax for these under the CalcFile name.


Part XVI — Feature completion rule ​

54. Semantic feature definition ​

A CalcFile semantic feature is complete only when all exist:

  1. specification text,
  2. machine-readable conformance tests,
  3. reference implementation passing those tests.

An undocumented parser behavior is not CalcFile syntax.


Appendix A — Target documents ​

German budget ​

text
@locale de-DE

# Fixed

Rent = 1.400 EUR
Internet = 49,99 EUR
Phone = 18 EUR

total

# Variable

Food = 350 EUR
Transport = 75 EUR

total

grand total

Prose ​

text
500 for flights + 90 for bags
Hotel: 120 EUR * 6
Food budget around 45 EUR * 10

total

Continuation ​

text
1000
- 19%
+ 50
/ 2

Units ​

text
Distance = 200 km
Time = 2.5 hour
Distance / Time

Appendix B — Non-normative implementation architecture ​

Recommended reference pipeline:

text
source line
   ↓
line classifier
   ↓
locale-aware tokenizer
   ↓
friendly-variable phrase resolver
   ↓
prose-aware semantic-token extraction
   ↓
normalized expression / AST
   ↓
typed Math.js-backed evaluator
   ↓
canonical line result

Recommended implementation order:

  1. statuses + line classifier
  2. locale-aware tokenizer
  3. assignment + friendly variables
  4. prose extraction
  5. ans
  6. continuation
  7. percentages
  8. aggregates
  9. units
  10. currencies
  11. curated functions
  12. diagnostic/editor polish

Do not implement prose as a regex loop that deletes arbitrary words until Math.js accepts the remainder.

Experimental and pre-1.0 open format for calculation documents.