Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Embed SymPy via Pyodide; rewrite parsers; modernize tooling (v0.1.0)
- SymPy 1.14 runs inside the extension via Pyodide (CPython on WASM) on
  a worker thread with timeouts, idle shutdown and crash recovery; the
  old python-subprocess mode remains as 'symplex.engine': 'system' with
  proper stream handling and a real timeout (dump_traceback_later
  without exit=True never actually stopped anything).
- JavaScript/TypeScript converter rewritten on the typed AST API: fixes
  pow() precedence, % and ** operators, zero-argument calls,
  multi-declarator statements, type annotations, Math.PI/Math.E; and
  unsupported syntax now yields a readable error instead of silently
  corrupted output ('a % b' used to become 'ab').
- Python converter rewritten with whole-word substitution: fixes
  variable names replaced inside other identifiers (x inside exp), the
  infinite loop on self-referential assignments that froze the editor,
  and crashes on bare final expressions.
- Tooling: TypeScript 5, ESLint 9 (tslint removed), engines ^1.90
  matched to @types/vscode, dead snippets/menu contributions removed,
  tests (npm test) covering every fixed bug plus an end-to-end
  embedded-SymPy evaluation.

Note: package-lock.json lands in the next commit together with the
final dependency set.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
  • Loading branch information
zalo and claude committed Aug 13, 2026
commit 238aae5fd070b6f555a42393b0b90303741cbeaf
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
out
node_modules
wheels
.vscode-test/
*.vsix
6 changes: 2 additions & 4 deletions .vscode/extensions.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
{
// See http://go.microsoft.com/fwlink/?LinkId=827846
// for the documentation about the extensions.json format
"recommendations": [
"ms-vscode.vscode-typescript-tslint-plugin"
"dbaeumer.vscode-eslint"
]
}
}
12 changes: 9 additions & 3 deletions .vscodeignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
.vscode/**
.vscode-test/**
out/test/**
src/**
out/test/**
scripts/**
.gitignore
.pylintrc
eslint.config.mjs
vsc-extension-quickstart.md
**/tsconfig.json
**/tslint.json
**/*.map
**/*.ts
**/*.vsix
node_modules/.bin/**
node_modules/@types/**
node_modules/eslint/**
node_modules/typescript-eslint/**
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Change Log

## 0.1.0 - Embedded SymPy + Parser Overhaul

- SymPy 1.14 now runs **inside the extension** via Pyodide (CPython on WebAssembly) — no Python installation required. The old subprocess mode is still available via `"symplex.engine": "system"`.
- Evaluations run on a worker thread with a configurable timeout (`symplex.timeoutSeconds`), so a wedged evaluation can no longer hang the editor.
- Rewrote the JavaScript/TypeScript converter on the typed AST API: fixes `pow()` precedence, `%`/`**` operators, zero-argument calls, multi-declarator statements, type annotations, `Math.PI`/`Math.E`, and unsupported syntax now produces a readable error instead of silently-corrupted output.
- Rewrote the Python converter with whole-word substitution: fixes variable names being replaced inside other identifiers (e.g. `x` inside `exp`), an infinite loop on self-referential assignments (`x = x + 1`), and crashes on bare final expressions.
- Modernized tooling: TypeScript 5, ESLint (tslint is dead), strict manifest, tests (`npm test`).

## 0.0.8 - Aesthetic Changes


Expand Down
30 changes: 23 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,36 @@ A Symbolic CAS that operates directly on Javascript and Python code.

## Requirements

Symplex requires that Python and `SymPy` are installed. `SymPy` can be installed with `pip install SymPy`.
None! SymPy 1.14 runs inside the extension via [Pyodide](https://pyodide.org) (CPython compiled to WebAssembly).

If you would rather use a locally installed Python (`pip install sympy`), set `"symplex.engine": "system"` — and point `"symplex.pythonPath"` at your interpreter if it isn't `python3`/`python` on your PATH.

## Settings

- `symplex.engine` — `embedded` (default, Pyodide) or `system` (local Python subprocess).
- `symplex.pythonPath` — interpreter used by the `system` engine.
- `symplex.timeoutSeconds` — how long a symbolic evaluation may run before it is cancelled (default 15).

## Known Issues

- Javascript parsing is somewhat fragile, this is due for a major refactor to better accomodate for the inconsistences in the nodegraph sourceFile output.
- Only variable assignment and basic arithmetic/trigonometric operations on real numbers are supported. No for-Loops, control-statements, etc.
- Only variable assignment and basic arithmetic/trigonometric operations on real numbers are supported. No for-Loops, control-statements, etc. Unsupported syntax now fails with an explanatory error instead of producing corrupted expressions.

## Adding new languages

To add a new language:
- Implement the conversion of a block of that language's code to a SymPy string (`convertToSympy()`) inside a new `*LANGUAGE*Support.ts` file.
- Throughout `extension.ts`, add a new case for your [language's identifier](https://code.visualstudio.com/docs/languages/identifiers), calling your `convertToSympy()` function.
- In `python/symplex.py`, add a case for your language's conversion (SymPy supports codegen in many languages already, so check to see if it's already there).
- Add it to the `package.json` as well and Test it by building the extension with `F5`!
- Implement the conversion of a block of that language's code to a SymPy string (`convertToSympy()`) in a new file under `src/converters/`.
- In `src/extension.ts`, add a case for your [language's identifier](https://code.visualstudio.com/docs/languages/identifiers) to `convertToSympy()`, and an entry to `CODE_STYLES`.
- In `python/symplex.py`, add an entry to `CODE_PRINTERS` (SymPy supports codegen in many languages already, so check to see if it's already there).
- Add it to the `package.json` menus/activation events as well, and test it by launching the extension with `F5`!

## Development

```bash
npm install
npm run compile # or: npm run watch
npm test # converter unit tests + an end-to-end embedded-SymPy test
npm run fetch-wheels # cache the SymPy wheels locally for offline/packaged use
```

### Future Work

Expand Down
14 changes: 14 additions & 0 deletions eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import tseslint from 'typescript-eslint';

export default tseslint.config(
...tseslint.configs.recommended,
{
files: ['src/**/*.ts'],
rules: {
'curly': 'warn',
'eqeqeq': 'warn',
'no-throw-literal': 'warn',
'@typescript-eslint/no-unused-vars': ['warn', { argsIgnorePattern: '^_' }]
}
}
);
69 changes: 43 additions & 26 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
"displayName": "Symplex",
"description": "A Computer Algebra System for your code",
"publisher": "zalo",
"version": "0.0.8",
"version": "0.1.0",
"license": "MIT",
"engines": {
"vscode": "^1.33.0"
"vscode": "^1.90.0"
},
"repository": {
"type": "git",
Expand Down Expand Up @@ -34,9 +34,7 @@
"activationEvents": [
"onLanguage:javascript",
"onLanguage:typescript",
"onLanguage:python",
"onCommand:symplex.Evaluate",
"onCommand:symplex.GetExpression"
"onLanguage:python"
],
"main": "./out/extension.js",
"contributes": {
Expand All @@ -53,44 +51,63 @@
"menus": {
"editor/context": [
{
"when": "editorHasSelection && resourceLangId =~ /(javascript|typescript|python)/",
"when": "editorHasSelection && resourceLangId =~ /^(javascript|typescript|python)$/",
"command": "symplex.Evaluate",
"group": "navigation@0"
},
{
"when": "editorHasSelection && resourceLangId =~ REPLACEWITHTHEABOVETODEBUGPARSING",
"when": "editorHasSelection && resourceLangId =~ /^(javascript|typescript|python)$/",
"command": "symplex.GetExpression",
"group": "navigation@1"
}
]
},
"snippets": [
{
"language": "javascript",
"path": "./snippetsDummy.json"
},
{
"language": "typescript",
"path": "./snippetsDummy.json"
},
{
"language": "python",
"path": "./snippetsDummy.json"
"configuration": {
"title": "Symplex",
"properties": {
"symplex.engine": {
"type": "string",
"enum": [
"embedded",
"system"
],
"default": "embedded",
"enumDescriptions": [
"Run SymPy inside the extension via Pyodide (WebAssembly). No Python installation required.",
"Run SymPy with a locally installed Python interpreter (requires `pip install sympy`)."
],
"description": "Which SymPy engine to use for symbolic evaluation."
},
"symplex.pythonPath": {
"type": "string",
"default": "",
"description": "Path to the Python interpreter used by the 'system' engine. Leave empty to use 'python3' (or 'python' on Windows) from PATH."
},
"symplex.timeoutSeconds": {
"type": "number",
"default": 15,
"description": "Maximum time in seconds a symbolic evaluation may run before it is cancelled."
}
}
]
}
},
"scripts": {
"vscode:prepublish": "npm run compile",
"vscode:prepublish": "npm run compile && npm run fetch-wheels",
"compile": "tsc -p ./",
"watch": "tsc -watch -p ./",
"postinstall": "node ./node_modules/vscode/bin/install"
"lint": "eslint src",
"fetch-wheels": "node ./scripts/fetchWheels.js",
"pretest": "npm run compile",
"test": "node --test \"out/test/*.test.js\""
},
"devDependencies": {
"@types/node": "^10.17.51",
"@types/vscode": "^1.52.0",
"tslint": "^5.20.1"
"@types/node": "^20.14.0",
"@types/vscode": "^1.90.0",
"eslint": "^9.9.0",
"typescript-eslint": "^8.0.0"
},
"dependencies": {
"typescript": "^3.1.1"
"pyodide": "^314.0.3",
"typescript": "^5.9.0"
}
}
140 changes: 78 additions & 62 deletions python/symplex.py
Original file line number Diff line number Diff line change
@@ -1,73 +1,89 @@
"""Symplex SymPy backend.

Importable (used by the extension's embedded Pyodide engine, which calls
`evaluate()` directly) and runnable as a CLI (used by the system-Python
engine): python symplex.py eval <language> <expression>
"""
import json
import sys

import sympy
import json
import faulthandler

# Sympy has direct support for ccode (C), cxxcode (C++), rcode (R),
# fcode (fortran), mathematica_code, jscode (javascript), julia_code,
# octave_code, rust_code, theano_code, and pycode
# More languages may be added with custom printers, or find/replace on these
def expressionToCode(expression, language):
'''Converts a SymPy Expression to a line of code in the target language'''
if (language == "python"):
return sympy.pycode(expression)
elif(language == "javascript" or language == "typescript"):
return sympy.jscode(expression)
elif(language == "c"):
return sympy.ccode(expression)
elif(language == "cpp"):
return sympy.cxxcode(expression)
elif(language == "r"):
return sympy.rcode(expression)
elif(language == "fortran"):
return sympy.fcode(expression)
elif(language == "mathematica"):
return sympy.mathematica_code(expression)
elif(language == "matlab" or language == "octave"):
return sympy.octave_code(expression)
elif(language == "rust"):
return sympy.rust_code(expression)
# elif(language == "theano"):
# return sympy.theano_code(expression)
# SymPy has direct support for ccode (C), cxxcode (C++), rcode (R),
# fcode (Fortran), mathematica_code, jscode (JavaScript), julia_code,
# octave_code, rust_code, and pycode.
# More languages may be added with custom printers, or find/replace on these.
CODE_PRINTERS = {
"python": sympy.pycode,
"javascript": sympy.jscode,
"typescript": sympy.jscode,
"c": sympy.ccode,
"cpp": sympy.cxxcode,
"r": sympy.rcode,
"fortran": sympy.fcode,
"mathematica": sympy.mathematica_code,
"matlab": sympy.octave_code,
"octave": sympy.octave_code,
"julia": sympy.julia_code,
"rust": sympy.rust_code,
}

def convertSymPyToDict(code, language):
'''Creates a JSONable list of lines of code from a SymPy Expression'''

# Select the first real solution
if(type(code) == list):
for root in code:
if len(root.atoms(sympy.I)) == 0:
code = root
break
elif(type(code) == str):
return { "returnString": code }
def expression_to_code(expression, language):
"""Converts a SymPy expression to a line of code in the target language."""
printer = CODE_PRINTERS.get(language, str)
return printer(expression)

pythonDict = {}
pythonDict["Variables"], pythonDict["Expression"] = sympy.cse(code)
for i, expr in enumerate(pythonDict["Variables"]):
pythonDict["Variables"][i] = {
"name" : str(expr[0]),
"expr" : str(expressionToCode(expr[1], language))
}
pythonDict["Expression"] = expressionToCode(pythonDict["Expression"][0], language)
return pythonDict

# Begin Parsing
faulthandler.enable()
def evaluate(language, expression_string):
"""Symbolically evaluates an expression string and returns a JSONable dict.

The result is either {"returnString": ...} for plain-text results (e.g.
latex()), or {"Variables": [{"name", "expr"}, ...], "Expression": ...}
where common subexpressions have been factored out into Variables.
"""
expression = sympy.sympify(expression_string)
if expression is None:
raise ValueError("SymPy could not interpret the expression: %r" % expression_string)

# Some operations (e.g. solve) return a list of solutions;
# select the first solution without imaginary parts.
if isinstance(expression, (list, tuple)):
real_roots = [root for root in expression if not root.atoms(sympy.I)]
if not real_roots:
return {"returnString": "No real solutions found. All solutions: %s"
% str(expression)}
expression = real_roots[0]
elif isinstance(expression, str):
# Operations like latex() return a raw string to insert verbatim.
return {"returnString": expression}

variables, reduced = sympy.cse(expression)
return {
"Variables": [
{"name": str(symbol), "expr": str(expression_to_code(subexpr, language))}
for symbol, subexpr in variables
],
"Expression": str(expression_to_code(reduced[0], language)),
}

try:
# Timeout after 10 seconds if it doesn't return
faulthandler.dump_traceback_later(10)

command = sys.argv[1]
language = sys.argv[2]
expression = sympy.sympify(sys.argv[3])
assert expression is not None, "SymPy Error: Cannot evaluate expression!"
def main(argv):
command, language, expression_string = argv[1], argv[2], argv[3]
if command != "eval":
raise ValueError("Unknown command: %r" % command)
print(json.dumps(evaluate(language, expression_string), indent=4))
sys.stdout.flush()

result = None
if(command == 'eval'):
print(json.dumps(convertSymPyToDict(expression, language), indent=4))
sys.stdout.flush()

finally:
faulthandler.cancel_dump_traceback_later()
if __name__ == "__main__":
import faulthandler
faulthandler.enable()
# Hard timeout: dump a traceback and exit if evaluation wedges.
# (dump_traceback_later without exit=True only prints — it doesn't stop anything.)
timeout = float(sys.argv[4]) if len(sys.argv) > 4 else 15.0
faulthandler.dump_traceback_later(timeout, exit=True)
try:
main(sys.argv)
finally:
faulthandler.cancel_dump_traceback_later()
17 changes: 17 additions & 0 deletions scripts/fetchWheels.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Downloads the SymPy (and mpmath) wheels into wheels/ so the packaged
// extension works offline. Run automatically by `vscode:prepublish`.
// If this is skipped, the extension still works: Pyodide downloads the
// wheels from its CDN into the user's global storage on first use.
const path = require('path');
const { loadPyodide } = require('pyodide');

(async () => {
const wheelsDir = path.join(__dirname, '..', 'wheels');
const pyodide = await loadPyodide({ packageCacheDir: wheelsDir });
await pyodide.loadPackage(['sympy']);
console.log('SymPy wheels cached in ' + wheelsDir);
process.exit(0);
})().catch(error => {
console.error('Failed to fetch wheels:', error);
process.exit(1);
});
Loading