From beb2a8c5bddd6b8f3a9fc4a91ccd1a55f4b882c8 Mon Sep 17 00:00:00 2001 From: Will Anderson Date: Mon, 4 May 2026 11:00:24 -0500 Subject: [PATCH] lexer + parser + codegen: try/catch statement try { ... } catch (name: Type) { ... } is now a first-class El statement. Lexer: `try` and `catch` are now keywords (Try, Catch token kinds). Parser: TryCatch AST node with try_body, catch_name, catch_body. codegen-js: emits try { ... } catch (name) { ... } directly -- correct for all browser error handling patterns. codegen.el (C backend): emits the try body with a comment; exception handling is a no-op since C has no analogous mechanism. Programs using try/catch should compile with --target=js. The catch variable type annotation is parsed and skipped (same treatment as all other type annotations in El). --- el-compiler/src/codegen-js.el | 13 +++++++++++++ el-compiler/src/codegen.el | 9 +++++++++ el-compiler/src/lexer.el | 2 ++ el-compiler/src/parser.el | 34 ++++++++++++++++++++++++++++++++++ 4 files changed, 58 insertions(+) diff --git a/el-compiler/src/codegen-js.el b/el-compiler/src/codegen-js.el index 6cd27ea..7717eaa 100644 --- a/el-compiler/src/codegen-js.el +++ b/el-compiler/src/codegen-js.el @@ -812,6 +812,19 @@ fn js_cg_stmt(stmt: Map, indent: String, declared: [String]) -> [St if kind == "TypeDef" { return declared } if kind == "EnumDef" { return declared } if kind == "Import" { return declared } + + if kind == "TryCatch" { + let try_body = stmt["try_body"] + let catch_name: String = stmt["catch_name"] + let catch_body = stmt["catch_body"] + js_emit_line(indent + "try {") + js_cg_stmts(try_body, indent + " ", native_list_clone(declared)) + js_emit_line(indent + "} catch (" + catch_name + ") {") + js_cg_stmts(catch_body, indent + " ", native_list_clone(declared)) + js_emit_line(indent + "}") + return declared + } + // ExternFn: the function exists in the JS environment (loaded via