P#-JS

raw JSON →
0.1.0 verified Fri May 01 auth: no javascript

An implementation of the P# programming language in JavaScript, including a transpiler from P# to JavaScript. Currently at version 0.1.0, this package provides tools to translate P# code (a probabilistic programming language) into executable JavaScript. As an early-stage release, it has limited documentation and may contain bugs. Key differentiators: it enables running P# programs in Node.js or browser environments, leveraging JavaScript's ecosystem. However, it is experimental and not suitable for production use.

error SyntaxError: Unexpected token 'export'
cause Using CommonJS require() on an ESM-only package.
fix
Use import statements or set type: 'module' in package.json.
error TypeError: transpile is not a function
cause Incorrect import style (default import instead of named import).
fix
Use import { transpile } from 'psharp-js'.
breaking API changes may occur without notice as the package is pre-1.0.0.
fix Pin to specific version and test thoroughly before upgrading.
deprecated No deprecated features currently; all features are preliminary.
fix None.
gotcha Transpiled code may not be secure; avoid running untrusted P# input.
fix Run in a sandboxed environment or only use trusted sources.
npm install psharp-js
yarn add psharp-js
pnpm add psharp-js

Transpile a simple P# program with a Bernoulli sample and execute the resulting JavaScript code.

import { transpile, run } from 'psharp-js';

// Example P# code
const psharpCode = `
def main() {
  x = sample Bernoulli(0.5);
  return x;
}
`;

// Transpile to JavaScript
const jsCode = transpile(psharpCode);
console.log('Transpiled code:', jsCode);

// Run the transpiled code (requires a random number generator environment)
const result = run(jsCode);
console.log('Result:', result);