feat: add extern fn declarations for JS FFI
Adds `extern fn` syntax for declaring external JavaScript functions: extern fn getElementById(id: String): Element extern fn getContext(el: Element, kind: String): CanvasCtx = "getContext" pub extern fn alert(msg: String): Unit Changes across 11 files: - Lexer: `extern` keyword - AST: `ExternFnDecl` struct + `Declaration::ExternFn` variant - Parser: parse `extern fn` with optional `= "jsName"` override - Typechecker: register extern fn type signatures - Interpreter: ExternFn value with clear error on call - JS backend: emit extern fn calls using JS name (no _lux suffix) - C backend: silently skips extern fns - Formatter, linter, modules, symbol_table: handle new variant Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
17
src/ast.rs
17
src/ast.rs
@@ -221,6 +221,8 @@ pub enum Declaration {
|
||||
Trait(TraitDecl),
|
||||
/// Trait implementation: impl Trait for Type { ... }
|
||||
Impl(ImplDecl),
|
||||
/// Extern function declaration (FFI): extern fn name(params): ReturnType
|
||||
ExternFn(ExternFnDecl),
|
||||
}
|
||||
|
||||
/// Function declaration
|
||||
@@ -428,6 +430,21 @@ pub struct ImplMethod {
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// Extern function declaration (FFI)
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ExternFnDecl {
|
||||
pub visibility: Visibility,
|
||||
/// Documentation comment
|
||||
pub doc: Option<String>,
|
||||
pub name: Ident,
|
||||
pub type_params: Vec<Ident>,
|
||||
pub params: Vec<Parameter>,
|
||||
pub return_type: TypeExpr,
|
||||
/// Optional JS name override: extern fn foo(...): T = "jsFoo"
|
||||
pub js_name: Option<String>,
|
||||
pub span: Span,
|
||||
}
|
||||
|
||||
/// Type expressions
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum TypeExpr {
|
||||
|
||||
Reference in New Issue
Block a user