Interpreter API

Understanding the JavaScript execution environment and how the language interpreter works.

executeJS()
Execute generated JavaScript code in a safe environment
function
executeJS(jsCode: string): Promise<string>

Parameters

  • jsCode - The JavaScript code string to execute

Returns

Promise that resolves to the captured console output as a string

Complete Interpretation Workflow
How English code becomes executed JavaScript

Step-by-Step Process

1
English Input

User writes code in natural English syntax

x is equal to 10
print x
2
Parsing

customParse() converts English to JavaScript

let x = 10;
console.log(x);
3
Execution

executeJS() runs the JavaScript and captures output

10

Integration Example

import { customParse, executeJS } from '@/language/interpreter';
const englishCode = `
age is equal to 25
if age is greater than 18 then
print "Adult"
else
print "Minor"
endif
`;
// Parse English to JavaScript
const jsCode = customParse(englishCode);
// Execute and get result
const output = await executeJS(jsCode);
console.log(output); // "Adult"
Error Handling
How the interpreter deals with errors and edge cases

Parse Errors

// Input: Unsupported syntax
do something magical
// Generated JavaScript:
// Unsupported: do something magical

Unsupported lines become comments, allowing partial execution of valid code.

Runtime Errors

// JavaScript that would cause error:
let result = x / 0; // Division by zero
console.log(undefined_variable); // Undefined variable
// Error is caught and returned as output
// User sees the error message instead of crash

Runtime errors are caught and displayed to the user instead of crashing the application.

Security and Limitations
Understanding the execution environment
Note

Browser Environment

Code executes in the browser's JavaScript context with access to browser APIs.

Limitation

No Sandboxing

Currently no sandboxing is implemented. Code has access to the global scope.

Feature

Output Capture

Console output is captured and returned rather than going to browser console.

Safety

Error Recovery

Errors don't crash the application and console.log is properly restored.

Performance Considerations
Understanding execution performance

Execution Speed

The interpreter adds minimal overhead. Most time is spent in:

  • • Parsing English syntax (typically <1ms for small programs)
  • • JavaScript execution (depends on your code complexity)
  • • Console output capture (minimal overhead)

Memory Usage

Memory usage is primarily from:

  • • Generated JavaScript code strings
  • • Captured output arrays
  • • Variable storage in JavaScript runtime

Optimization Tips

Tip

Parse once, execute multiple times

Cache parsed JavaScript if running the same code repeatedly.

Tip

Limit output capture

Very large amounts of console output can impact performance.

Extension Points
How to extend the interpreter functionality

Custom Output Handling

// Custom output processor
function customExecuteJS(jsCode: string) {
const output: string[] = [];
const originalLog = console.log;
console.log = (...args) => {
// Custom formatting here
output.push(formatOutput(args));
};
// ... rest of execution
}

Error Customization

// Custom error handling
try {
eval(jsCode);
} catch (error) {
// Custom error formatting
return formatError(error);
}