gen_core_erlang
Types
pub type Definition =
#(FunctionDecl, Function)
Type that wraps the various c_erl()
subtypes into an expression.
These expressions are used as function bodies, in case clauses after ->
.
pub opaque type Expression
Type that wraps name and arity, such as loop/1
.
pub type FunctionDecl
pub type ModuleAttribute
Outermost Core Erlang term that can be passed to compile:forms(mod, [from_core])
.
pub type ModuleDef
Functions
pub fn c_apply_anonymous(
function: Expression,
arguments: List(Expression),
) -> Expression
Call a function, but not by name. Most likely, the function has been assigned to a variable
c_let([p_var("Fun")], f2e(c_fun(...)), c_call_anonymous(c_var("Fun", [])))
pub fn c_atom(name: String) -> Expression
Create an expression for the atom that has string representation name
pub fn c_binary(string: String) -> Expression
Create an Erlang binary for a Gleam string. Currently, there is no way to generate other binaries
pub fn c_call(
module: String,
name: String,
arguments: List(Expression),
) -> Expression
Call an exported function
pub fn c_clause(
patterns: List(Pattern),
expr: Expression,
) -> Clause
A single clause for a c_case
. Although you can pass a list of patterns,
it seems expr
always returns one value
pub fn c_clause_w_guard(
patterns: List(Pattern),
guard: Expression,
expr: Expression,
) -> Clause
Although we use the type Expression
, you should only pass those allowed in guards.
Maybe some day we can enforce this with a type, but that seems very hard
pub fn c_cons(head: Expression, tail: Expression) -> Expression
Concatenate two expressions. As per Erlang, the tail
argument
does not have to be a list. If you want to generate an empty list,
use c_nil
, if you want to generate a list from 1 or more items,
use make_list
pub fn c_fname(name: String, arity: Int) -> FunctionDecl
Create a function ‘name’ such as “loop/1”. To be used in module definitions (both exports and functions) as well as local function application
pub fn c_fun(
arguments: List(Pattern),
body: Expression,
) -> Function
If you need this as an Expression
, use f2e
pub fn c_let(
variables: List(Pattern),
argument: Expression,
body: Expression,
) -> Expression
Equivalent to
<P1, ..., Pn> = argument
body
where you can use a pattern line p_int("Num")
by its variable
`c_int(“Num”)
Note that all Pi need to be p_var
patterns. If you need other matches,
use c_case
with a single c_clause
pub fn c_module(
name: String,
exports: List(FunctionDecl),
attributes: List(ModuleAttribute),
definitions: List(#(FunctionDecl, Function)),
) -> ModuleDef
There should be a definition for each export. Other definitions are local functions. Note: For attributes, you currnely have to pass an empty list.
pub fn c_seq(arg: Expression, body: Expression) -> Expression
Execute two expression consecutively.
Erlang functions always return a result. If you need that result, use c_let
.
If you want to execute more than 2 expressions, use:
let assert Ok(expr) = list.reduce(expressions, c_seq)
pub fn list_elements(list: Expression) -> List(Expression)
Unspecified behaviour. Used for testing this library.
pub fn make_list(expressions: List(Expression)) -> Expression
Convenience function, equivalent to
c_cons(expr_1, .... c_cons(expr_n, c_nil()))
pub fn p_atom(name: String) -> Pattern
Create a pattern for the atom that has string representation name