Skip to content
FransebasPublic

About

Language interpreter for Tomin scripts

Resources

Stars

1 star

Watchers

0 watching

Forks

Repository files navigation

Tomin

A DSL (PortfolioScript) for describing ETF/portfolio allocation strategies, with an ANTLR4 grammar and a TypeScript interpreter. Published on npm as tomin-script (the tomin name was already taken). Ships two entry points:

  • tomin-script — the Node build. Full data layer, including the local history DB provider (node:sqlite) and the vendor API providers.
  • tomin-script/web — the browser build. The data layer is browser-safe: an OPFS-backed sqlite-wasm cache plus a WebDataProvider that proxies to your own backend instead of holding vendor API keys.

Installing it also gives you a tomin CLI binary (the package's bin entry keeps the shorter name even though the npm package is tomin-script).

Install

npm install tomin-script

Browser usage requires @sqlite.org/sqlite-wasm

The web build (tomin-script/web) stores its ticker cache in the browser via @sqlite.org/sqlite-wasm. It is declared as an optional peer dependency — Node-only consumers don't need it, but if you import tomin-script/web you must install it yourself so your app controls the version and how the WASM asset is bundled:

npm install @sqlite.org/sqlite-wasm

Bundler setup for the web build

@sqlite.org/sqlite-wasm loads a separate sqlite3.wasm file at runtime, resolving its URL relative to its own script. Bundlers that pre-bundle or rewrite dependencies can break that relative path, so the WASM fails to load (commonly a 403 in the dev server). This is standard for the package — its own demos carry the same setup. Apply the one-time config for your bundler:

Vite

// vite.config.ts
import { defineConfig } from "vite";

export default defineConfig({
  optimizeDeps: {
    // Keep Vite's dep optimizer from rewriting the package, so its runtime
    // path to sqlite3.wasm stays correct.
    exclude: ["@sqlite.org/sqlite-wasm"],
  },
});

If the package is hoisted/linked outside your app root and you hit an @fs "not allowed to serve files outside of the project root" error, also widen the dev-server allowlist:

server: { fs: { allow: [".."] } },

Webpack / Next.js

Ensure .wasm is treated as an async WebAssembly asset and served:

// next.config.js
module.exports = {
  webpack: (config) => {
    config.experiments = { ...config.experiments, asyncWebAssembly: true };
    return config;
  },
};

Note: No cross-origin isolation (COOP/COEP) headers are required. The web build uses sqlite-wasm's SAHPool VFS, which does not rely on SharedArrayBuffer.

Usage

// Browser
import { createWebTomin } from "tomin-script/web";

const etf = await createWebTomin({ /* WebDataProvider options */ });
// Node
import { Tomin } from "tomin-script";

const etf = await Tomin.create(/* resolver options */);

Language notes

Variable scoping

PortfolioScript has a single, flat (global) scope: a variable assigned with name = expr lives for the rest of the script. This holds no matter where the assignment appears — a variable assigned inside an if or for each body is not local to that block; it persists after the block ends, just as in Python:

if cond:
  temp = computeSomething()   // "temp" is readable after the `end`
end
temp

There is one deliberate exception.

for each loop variables are scoped to the loop

The loop variable in for each x in ... : ... end is local to the loop: it does not exist before the loop and is removed again once the loop finishes, so it never leaks its last value into the surrounding script. This mirrors how for (let x ...) behaves in JavaScript, Rust, and C++, even though every other binding here is global.

To keep this unambiguous, the loop variable's name must be free when the loop starts — reusing the name of an existing variable (or a builtin) is an error rather than a silent shadow:

total = 0
for each total in HOLDINGS:   // error: 'total' is already defined
  ...
end

Two consequences follow from the "must be free" rule:

  • Sequential loops may reuse the same loop-variable name freely, because each loop cleans the name up when it ends.
  • Nested loops may not reuse the same name — the inner loop sees the outer loop's variable as still-live and errors. Use distinct names when nesting.

Because other variables are global, a name behaves differently depending on how it was introduced: for each t ... leaves no t behind, whereas t = 3 persists. That asymmetry is intentional.

Development

See CLAUDE.md for the grammar workflow, the dev CLI, and the synthetic test-ticker fixtures.

About

Language interpreter for Tomin scripts

Resources

Stars

1 star

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages