Skip to main content

JsBackend

Struct JsBackend 

Source
pub struct JsBackend {
    pub module: JsModule,
    pub fn_map: HashMap<String, String>,
    pub fresh_counter: usize,
}
Expand description

JavaScript code generation backend.

Compiles LCNF function declarations to a JsModule containing ES2020+ JavaScript functions.

Fields§

§module: JsModule

The module being built.

§fn_map: HashMap<String, String>

Mapping from LCNF names to mangled JS names.

§fresh_counter: usize

Counter for generating fresh temporary variable names.

Implementations§

Source§

impl JsBackend

Source

pub fn new() -> Self

Create a new JS backend.

Source

pub fn fresh_var(&mut self) -> String

Generate a fresh temporary variable name: _t0, _t1, etc.

Source

pub fn mangle_name(&self, name: &str) -> String

Mangle an LCNF name into a valid JavaScript identifier.

Rules:

  • Replace . with _
  • Replace ' (prime) with _prime
  • Prefix reserved words with _
Source

pub fn compile_module(decls: &[LcnfFunDecl]) -> Result<String, String>

Top-level entry point: compile a slice of LCNF function declarations into a JavaScript module string.

Source

pub fn compile_decl(&mut self, decl: &LcnfFunDecl) -> Result<JsFunction, String>

Compile a single LCNF function declaration into a JsFunction.

Source

pub fn compile_expr( &mut self, expr: &LcnfExpr, stmts: &mut Vec<JsStmt>, ) -> Result<JsExpr, String>

Compile an LCNF expression into a sequence of JS statements, returning the JS expression that holds the result.

Source

pub fn compile_arg(&self, arg: &LcnfArg) -> JsExpr

Compile an LCNF argument (atomic value) into a JS expression.

Source

pub fn compile_lit(&self, lit: &LcnfLit) -> JsExpr

Compile an LCNF literal into a JS expression.

Trait Implementations§

Source§

impl Default for JsBackend

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.