Parser API
Understanding the core parser that converts English code to JavaScript.
customParse()
The main entry point for converting English code to JavaScript
function
customParse(englishCode: string): string
Parameters
englishCode- The English-like source code to parse
Returns
Returns the generated JavaScript code as a string
ParseContext Interface
Context object passed to each construct parser
interface ParseContext {
line: string;
indentLevel: number;
inIfBlock: boolean;
inWhileLoop: boolean;
declaredVariables: Set<string>;
}
Properties
lineCurrent line being processed
indentLevelCurrent indentation depth for code blocks
inIfBlockWhether currently inside an if statement
inWhileLoopWhether currently inside a while loop
declaredVariablesSet of all declared variable names
ParseResult Interface
Result object returned by each construct parser
interface ParseResult {
jsCode: string;
indentLevel: number;
inIfBlock: boolean;
inWhileLoop: boolean;
declaredVariables: Set<string>;
handled: boolean;
}
Properties
jsCodeGenerated JavaScript code for this line
handledWhether the parser handled this line
...contextUpdated context for the next line
Parser Chain
Order in which construct parsers are executed
The parser tries each construct parser in the following order until one handles the line:
1parseIfStatements- if/else/endif blocks
2parseForLoops- for/endfor loops
3parseWhileLoops- while/endwhile loops
4parseArithmeticOperations- mathematical expressions
5parseVariableDeclarations- variable assignments
6parsePrintStatements- print commands
Error Handling
How the parser handles unsupported syntax
When no parser can handle a line, it's marked as unsupported:
if (!handled) {
jsCode += `${{getIndent(indentLevel)}// Unsupported: ${{line}`;
}
Note
Graceful degradation
Unsupported lines become comments, allowing partial compilation.