Metadata-Version: 2.4
Name: calc-rs-lang
Version: 0.3.4
Classifier: Programming Language :: Rust
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Intended Audience :: Developers
Classifier: Topic :: Software Development :: Libraries
Requires-Dist: marimo>=0.24.0 ; extra == 'notebook'
Requires-Dist: anywidget>=0.11.0 ; extra == 'notebook'
Requires-Dist: traitlets>=5.16.1 ; extra == 'notebook'
Requires-Dist: maturin>=1.15.0 ; extra == 'notebook'
Provides-Extra: notebook
License-File: LICENSE
Summary: A strongly typed, spreadsheet-style expression engine
Keywords: calculator,expression,spreadsheet,currency,rust
Author: Attica-oss
License-Expression: MIT
Requires-Python: >=3.10
Description-Content-Type: text/markdown; charset=UTF-8; variant=GFM
Project-URL: Repository, https://github.com/Attica-oss/calc_rs

<p align="center">
  <img src="docs/assets/banner.png" alt="calc — a scripting language for building things simply" width="100%">
</p>

<p align="center">
  <a href="https://github.com/Attica-oss/calc_rs/actions/workflows/ci.yml"><img src="https://github.com/Attica-oss/calc_rs/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
  <a href="https://pypi.org/project/calc-rs-lang/"><img src="https://img.shields.io/pypi/v/calc-rs-lang?label=pypi%20calc-rs-lang" alt="PyPI calc-rs-lang"></a>
  <a href="https://pypi.org/project/calc-desktop/"><img src="https://img.shields.io/pypi/v/calc-desktop?label=pypi%20calc-desktop" alt="PyPI calc-desktop"></a>
  <img src="https://img.shields.io/badge/python-3.10%2B-blue" alt="Python 3.10+">
  <img src="https://img.shields.io/badge/rust-edition%202024-orange?logo=rust" alt="Rust edition 2024">
  <a href="LICENSE"><img src="https://img.shields.io/github/license/Attica-oss/calc_rs" alt="MIT license"></a>
  <a href="https://molab.marimo.io/github/Attica-oss/calc_rs/blob/master/notebook/calc_notebook.py"><img src="https://marimo.io/molab-shield.svg" alt="Open the notebook in molab"></a>
</p>

# calc_rs

calc_rs is a strongly typed scripting language for calculations and structured
data. The core is written in Rust, and is available as a CLI, Rust library,
and Python extension module.

## Key features

- **Strongly typed**: Currency, percentages, dates, durations, tonnage, tables,
  and other domain values are distinct types rather than decorated numbers.

- **Calculations**: Designed for real-world calculations with units and
  domain-specific values, while retaining a concise expression-oriented
  syntax.

- **Data & Tables**: Tables are first-class values that can be created,
  transformed, queried, and persisted directly from the language — with
  grouped summaries, joins, window functions (`lag`, `running_sum`, `rank`),
  set operations and multi-key sorting.

- **Pipelines**: Compose transformations with `|>`, making data workflows read
  from top to bottom instead of nesting operations.

- **Schema-aware**: CSV, TSV, JSON Lines, Excel and OpenDocument files load
  with inferred or explicit schemas. Values are checked against their declared
  types rather than silently converted to text.

- **Maths**: Exact decimals and fractions (`fraction(1, 3) * 3` is `1/1`),
  trigonometry and logarithms, vectors and matrices (`linalg`), ratios,
  proportional allocation, and random numbers.

- **Dates and calendars**: Date ranges, month grids, business and working
  days, and public holidays from a configurable calendar.

- **From spreadsheets**: Most Excel worksheet functions have a calc
  equivalent, and `calc_rs doc --excel PMT` finds it.

- **Scripting**: Run individual expressions, `.calc` files, or multi-file
  projects with imports and modules.

- **Interactive**: A REPL with syntax highlighting, completion, signatures,
  multi-line expressions, and session state.

- **Editor Support**: Built-in Language Server support provides diagnostics,
  completion, and hover information.

- **Embeddable**: Use the engine directly from Rust or Python.

- **Rust**: Written in Rust with the performance-critical engine implemented
  natively and exposed through a library API.

## Why calc_rs?

calc_rs is designed for programs where calculations and structured data are
central to the problem.

Instead of treating everything as an untyped number or string, values carry
their meaning and type:

```calc
let price = $12.50
let quantity = 3

price * quantity
// $37.50
```

The same language can work with structured data:

```calc
load("moves.csv")
  |> filter([status] != "Cancelled")
  |> inner_join(load("rates.csv"), "customer")
  |> extend("amount", [rate] * [tons])
  |> summarize(array("customer"),
       "tons", sum([tons]),
       "amount", sum([amount]),
       "moves", count())
  |> sort([amount], "desc")
```

```text
╭──────────┬──────┬───────────┬───────╮
│ customer │ tons │    amount │ moves │
├──────────┼──────┼───────────┼───────┤
│ IOT      │ 34.5 │ $1,569.75 │     2 │
│ CCS      │ 30.0 │ $1,200.00 │     1 │
╰──────────┴──────┴───────────┴───────╯
```

Row-order functions read neighbouring rows, per group:

```calc
load("moves.csv")
  |> sort([date])
  |> extend(array("container"),
       "previous_date", lag([date]),
       "running_tons", running_sum([tons]))
```

Schemas can be declared explicitly when the shape of the data matters:

```calc
struct Shipment {
  date: date,
  port: text,
  box: container,
  amount: currency
}

load("jan.csv", Shipment)
```

This lets the language check data as it enters the program rather than
discovering type problems later.

Numbers stay exact, and the maths library covers the rest:

```calc
(1 / 3) * 3                             // 0.9999999999999999999999999999
fraction(1, 3) * 3                      // 1/1

import "math" as math
math::allocate($100.00, array(1, 1, 1)) // [$33.34, $33.33, $33.33] — always sums to $100.00

import "linalg" as linalg
linalg::solve(matrix(array(2.0, 1.0), array(1.0, 3.0)), array(3.0, 5.0))  // [0.8, 1.4]

range(2026-01-31, 2026-06-01, 1mo)      // 31 Jan, 28 Feb, 31 Mar, 30 Apr, 31 May
```

## Philosophy

The goal of calc_rs is to provide a scripting language that:

- Keeps calculations concise without sacrificing strong typing.
- Treats domain-specific values as real types.
- Makes structured data and tables first-class language values.
- Makes data transformations readable through pipelines.
- Detects schema and type errors before they become silent data corruption.
- Works interactively as well as in scripts and multi-file projects.
- Can be embedded in both Rust and Python applications.
- Provides useful editor tooling without requiring a separate runtime.

calc_rs is written in Rust, giving the engine control over its data
representation and performance-critical execution while keeping the language
small and embeddable.

## Install

### CLI

```sh
uv tool install calc-rust
```

Then:

```sh
calc_rs '$5.00 * 3'
# $15.00
```

Or run without installing:

```sh
uvx --from calc-rust calc_rs '$5.00 * 3'
```

### Desktop app (Windows and macOS)

```sh
uv tool install calc-desktop
calc_desktop path/to/project
```

The app installs like the CLI — `uv` unpacks it into your user folder, so no
installer runs. On Windows it needs the WebView2 runtime (included in Windows
11). On Linux, build it from [`desktop/`](desktop/) instead.

### Python

```sh
pip install calc-rs-lang
```

```python
import calc_rs

calc_rs.evaluate("$5.00 * 3")
# Currency($15.00)
```

### Rust

```toml
[dependencies]
calc_rs = { git = "https://github.com/Attica-oss/calc_rs" }
```

## Documentation

- [Language Reference](docs/USAGE.md)
- [LSP / Editor Support](docs/LSP.md)
- [Examples](examples/)
- [Changelog](CHANGELOG.md)

## Development

```sh
cargo test
maturin develop --release
```

## License

MIT — see [LICENSE](LICENSE).

