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:
@locale de-DE
# Japan
Flights to Tokyo = 740 EUR
Hotel = 120 EUR * 6
Food about 45 EUR * 10
totalAn editor may display:
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
- Prose first. Human context may coexist with expressions.
- Live editing. Temporary incomplete syntax is normal.
- Forgiving, not magical. Unknown prose may be ignored where intent is clear; ambiguity must not silently produce arbitrary values.
- Top-to-bottom evaluation.
- Friendly variables. Multi-word and case-insensitive.
- Typed values. Numbers, currencies, and physical units retain semantics.
- Locale is semantic where necessary.
- Derived results are not persisted.
- Not a general-purpose programming language.
- 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
subtotalblock, - break implicit line continuation,
- do not reset
total.
5. Directives
A directive begins with @.
v0.1 supports:
@locale de-DEGrammar:
@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:
# Travel
## AccommodationHeaders:
- 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:
number
unit
currencyCanonical values are independent from localized presentation.
Examples:
{ "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:
1.5
1,234.56
1 234.56
.5Meaning:
1.5
1234.56
1234.56
0.512. de-DE
Accepted:
1,5
1.234,56
1 234,56
,5Meaning:
1.5
1234.56
1234.56
0.513. 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:
k / K = 1,000
M = 1,000,000
b / B = 1,000,000,000Lowercase m is NOT million because it conflicts with metre.
Part IV — Arithmetic
15. Operators
Required:
+
-
*
/
^
()Precedence:
- parentheses
- exponentiation
- multiplication/division
- addition/subtraction
Unary plus/minus are supported.
Part V — Prose-first semantics
16. Description boundary
A colon explicitly separates description from calculation:
Hotel: 120 EUR * 6Result:
720 EUR17. Free prose
These are valid target forms:
500 for tickets + 70 for taxi
Estimated hotel maybe 100 + 20
Train tickets around 45 * 3Expected:
570
120
135Unknown words may be ignored only when the remaining semantic token sequence forms one coherent expression.
18. Prose-only text
Maybe compare this with Airbnb lateris 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:
100 USD in EURmust preserve in.
Example:
Hotel in Berlin 120 EURmust not interpret in Berlin as conversion because Berlin is not a valid target unit/currency.
Part VI — Variables
21. Assignment
Monthly rent = 1400 EURThe 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:
Monthly rent = 1400 EUR
Internet bill = 50 EUR
iPhone 15 = 998 EUR23. Case-insensitive lookup
iPhone 15 = 998 EUR
iphone 15 - 200 EUR=> 798 EUR
24. Longest match
Given:
New York hotel = 1000 EUR
York hotel = 500 EUR
New York hotel - York hotelthe 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:
+
-
*
/continues from the prior successful result if there is no intervening blank/header/comment.
100
+ 20
* 2
- 5=> line values:
100
120
240
235A blank line breaks continuation.
Part VIII — Percentages
28. Percentage literal
25%has canonical numeric meaning 0.25.
29. of
25% of 100=> 25
30. Calculator percentage addition/subtraction
100 + 25%=> 125
100 - 25%=> 75
31. Percentage multiplication
100 * 25%=> 25
32. Modulo
Whitespace distinguishes modulo:
12 % 10=> 2
while:
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:
avg
subavgusing total and subtotal windows respectively.
38. Type compatibility
100 EUR
50 EUR
totalis valid.
100 EUR
5 km
totalis 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:
2 inch
5 cm
2.5 hour
200 km / 2.5 hour40. Conversion
Support:
to
inwhen the left side is typed and the right side is a known compatible target.
2 inch to cm=> 5.08 cm
41. Derived units
200 km / 2.5 hour=> canonical equivalent of 80 km/hour.
42. Count labels are deferred
This remains undefined in v0.1:
Hotel = 120 EUR * 6 nightsDo not silently erase physical units or prose labels until a dedicated RFC defines count-label semantics.
For v0.1 use:
Hotel = 120 EUR * 6Part XI — Currencies
43. Literals
Required:
100 EUR
100 USD
20 GBP
€100
100€
$100
£20Canonical identity uses ISO 4217 codes.
44. Same-currency arithmetic
100 EUR + 50 EUR=> 150 EUR
120 EUR * 6=> 720 EUR
45. Conversion
100 USD in EUR
100 USD to EURrequires externally supplied rate data.
The core engine MUST NOT fetch rates itself.
Without required rates:
error: missing-external-dataFreshness/source metadata is environment/editor information, not part of canonical numeric value.
Part XII — Functions
46. Curated function set
Required initial functions:
sqrt
abs
round
floor
ceil
min
maxRecommended but optional within v0.1 implementation milestone:
sin
cos
tan
logDo not expose every Math.js parser function automatically.
47. Locale-sensitive argument separator
Under dot-decimal locales:
round(10.234, 2)Under comma-decimal locales:
round(10,234; 2)Part XIII — Status and diagnostics
48. Primary statuses
Every line has one:
empty
directive
header
comment
text
valid
incomplete
ambiguous
error49. Incomplete
Examples:
100 +
Hotel =
(100 + 20
100 USD inThese are incomplete, not hard syntax errors.
50. Error
Examples:
10 EUR + 5 km
100 USD in cmSuggested diagnostic codes:
incompatible-types
invalid-conversion
unknown-variable
missing-external-data
incompatible-aggregate-types
unsupported-function
unsupported-directive51. 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:
{
"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:
- specification text,
- machine-readable conformance tests,
- reference implementation passing those tests.
An undocumented parser behavior is not CalcFile syntax.
Appendix A — Target documents
German budget
@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 totalProse
500 for flights + 90 for bags
Hotel: 120 EUR * 6
Food budget around 45 EUR * 10
totalContinuation
1000
- 19%
+ 50
/ 2Units
Distance = 200 km
Time = 2.5 hour
Distance / TimeAppendix B — Non-normative implementation architecture
Recommended reference pipeline:
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 resultRecommended implementation order:
- statuses + line classifier
- locale-aware tokenizer
- assignment + friendly variables
- prose extraction
ans- continuation
- percentages
- aggregates
- units
- currencies
- curated functions
- diagnostic/editor polish
Do not implement prose as a regex loop that deletes arbitrary words until Math.js accepts the remainder.