2025-02-04 21:55:57 +00:00
|
|
|
declare global {
|
|
|
|
interface ImportMeta {
|
|
|
|
main: boolean;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
import { Lexer, Parser, NGNKGenerator } from "./mod.ts";
|
|
|
|
|
2025-02-03 22:09:48 +00:00
|
|
|
export function add(a: number, b: number): number {
|
|
|
|
return a + b;
|
|
|
|
}
|
|
|
|
|
2025-02-04 21:55:57 +00:00
|
|
|
function testParser(input: string): void {
|
|
|
|
console.log("\n=== Testing Parser ===");
|
|
|
|
console.log("Input:", input);
|
|
|
|
|
|
|
|
try {
|
|
|
|
const lexer = new Lexer(input);
|
|
|
|
const tokens = lexer.tokenize();
|
|
|
|
console.log("Tokens:", tokens);
|
|
|
|
|
|
|
|
const parser = new Parser(tokens);
|
|
|
|
const ast = parser.parseExpressions();
|
|
|
|
console.log("AST:", JSON.stringify(ast, null, 2));
|
|
|
|
|
|
|
|
const generator = new NGNKGenerator();
|
|
|
|
const output = ast.map(node => generator.generate(node)).join("; ");
|
|
|
|
console.log("Generated Output:", output);
|
|
|
|
} catch (error) {
|
|
|
|
console.error("Error:", error.message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test cases
|
2025-02-03 22:09:48 +00:00
|
|
|
if (import.meta.main) {
|
2025-02-04 21:55:57 +00:00
|
|
|
// Test 1: Basic arithmetic
|
|
|
|
testParser("1 + 2");
|
|
|
|
|
|
|
|
// Test 2: Function call
|
|
|
|
testParser("sum[1; 2; 3]");
|
|
|
|
|
|
|
|
// Test 3: Complex expression
|
|
|
|
testParser("map[x; + 2] array[1; 2; 3]");
|
|
|
|
|
|
|
|
// Test 4: Unary operators
|
|
|
|
testParser("# array[1; 2; 3]");
|
|
|
|
|
|
|
|
// Test 5: Multiple expressions
|
|
|
|
testParser("x + 1 # array[1; 2] sum[4; 5; 6]");
|
|
|
|
|
|
|
|
// Test 6: Non-commutative operators
|
|
|
|
testParser("10 - 5");
|
|
|
|
|
|
|
|
// Test 7: Nested function calls
|
|
|
|
testParser("outer[inner[1; 2]; 3]");
|
|
|
|
|
|
|
|
// Test 8: Error case - incomplete expression
|
|
|
|
testParser("sum[1; 2;");
|
2025-02-03 22:09:48 +00:00
|
|
|
}
|