osiris-educational-transpiler
v1.0.28
Published
Transpiler intended for educational purpose that translates currently python code into javascript so that it can be run on a browser .
Readme
Osiris - Educational Transpiler
Osiris is a transpiler that converts Python code to JavaScript.It also allows transpiled code execution within a web worker. It is an educational tool for those who want to learn about programming language transpilation.
Installation
npm i osiris-educational-transpilerUsage
Python Osiris using eval()
Create an instance of the Osiris class, pass the Python code, and execute it:
import OsirisLogo from "osiris-educational-transpiler/logo";
import OsirisPython from "osiris-educational-transpiler/Python";
const transpiler = new Osiris("python");
const result = transpiler.passCode('print("Hello, World!")');
if (result.success) {
console.log('Transpiled Code:', result.code);
eval(result.code)
} else {
console.error('Transpilation Error:', result.error);
}
Using python with Logo running code on web worker (Only works on browser)
To use Osiris with Python, import the Python transpiler and set up the execution environment:
import OsirisPython from "osiris-educational-transpiler/Python";
const pythonTranspiler = new OsirisPython();
// Register an event handler to capture output
pythonTranspiler.setEventHandler((event) => {
if (event.type === 'output') {
console.log(event.output);
} else if (event.type === 'error') {
console.error('Runtime Error:', event.error);
}
});
// Optional: set a timeout to prevent infinite loops
pythonTranspiler.setExecutionTimeout(300000); // 5 minutes
// Transpile and execute
const result = pythonTranspiler.sendCode('print("Hello, Python!")');
if (result.success) {
pythonTranspiler.runCode();
} else {
console.error(result.errors);
}Using Osiris with Logo (Only works on browser)
To use Osiris with Logo, import the Logo transpiler and configure it with the appropriate canvas ID where turtle graphics will be rendered.
import OsirisLogo from "osiris-educational-transpiler/logo";
const logoTranspiler = new OsirisLogo();
// Set the canvas ID where turtle graphics will be drawn
logoTranspiler.configureLanguage('canvasId', 'myCanvas');
// Register an event handler for execution output
logoTranspiler.setEventHandler((event) => {
if (event.type === 'output') {
console.log(event.output);
}
});
// Transpile and execute
const result = logoTranspiler.sendCode('repeat 4 [forward 100 right 90]');
if (result.success) {
// Ensure the canvas exists in your HTML
const canvas = document.createElement('canvas');
canvas.id = 'myCanvas';
canvas.width = 800;
canvas.height = 800;
document.body.appendChild(canvas);
logoTranspiler.runCode();
} else {
console.error(result.errors);
}
```js
import OsirisLogo from "osiris-educational-transpiler/logo";
import OsirisPython from "osiris-educational-transpiler/Python";
const transpiler = new Osiris("python");
const result = transpiler.passCode('print("Hello, World!")');
if (result.success) {
console.log('Transpiled Code:', result.code);
eval(result.code)
} else {
console.error('Transpilation Error:', result.error);
}
API
new Osiris()
Creates a transpiler instance for the desired language (e.g., "python" or "logo").
sendCode(code: string): object
Transpiles source code to semantically equivalent JavaScript. Returns:
success: boolean indicating whether the transpilation was successful.code: transpiled JavaScript code (if successful).errors: array of error messages (if failed).
configureLanguage(key: string, value: any)
Sets a language-specific configuration property.
Example: setting 'canvasId' for Logo turtle graphics.
setEventHandler(handler: function)
Registers a callback function to handle runtime events (e.g., output, errors, graphics commands).
setExecutionTimeout(timeoutMs: number)
Sets the maximum code execution time in milliseconds. Default is 300000 (5 minutes).
runCode()
Executes the last transpiled code inside a sandboxed Web Worker.
sendIO(userInput: string)
Sends user input to the running program (for handling things like input()).
supportedLanguages(): Array<string>
Returns a list of supported languages.
