API reference / @jsprismarine/molang
@jsprismarine/molang
Molang parser, compiler and evaluator for Bedrock behavior packs. Parses the full Molang grammar with a Pratt parser, compiles each expression once per (source, version) into a JavaScript closure, and evaluates it over a typed scope (entity, block permutation, item, world, placement, context.*, variable.*, temp.*).
import { Molang } from '@jsprismarine/molang';
const molang = new Molang();
const compiled = molang.compile('query.is_baby ? 0.02 : 0.001', { minEngineVersion: [1, 21, 0] });
const value = compiled.evaluate({ entity: myEntityContext });Operator precedence is versioned by the containing pack's min_engine_version, matching the engine's rule changes at 1.18.10 (ternary associativity) and 1.18.20 (&& over ||, comparison over equality). Unknown query.* and math.* identifiers are reported through the compile diagnostics and evaluate to 0.0 at run time, as the engine does.
Examples
These expressions are exported as MOLANG_EXAMPLES, each with the scopes it is shown under and the value it produces. The test suite compiles every one with no diagnostics and checks the values, and fails if this section drifts from the list, so they are safe to copy.
import { MOLANG_EXAMPLES, Molang } from '@jsprismarine/molang';
const molang = new Molang();
for (const example of MOLANG_EXAMPLES) {
const compiled = molang.compile(example.source);
for (const { label, scope, expected } of example.cases) {
console.log(example.title, label, compiled.evaluate(scope), expected);
}
}A chance that depends on age
A ternary picks between two numbers, as an ambient sound probability does.
query.is_baby ? 0.02 : 0.001a baby: 0.02; an adult: 0.001.
A value kept in range
The math functions take and return numbers; clamp(value, min, max) bounds a variant index.
math.clamp(query.variant, 0, 3)variant 2: 2; variant 9: 3.
A spawn condition
Booleans are 1 and 0, so conditions combine with &&, || and !.
query.head_is_in_water && !query.is_on_groundswimming: 1; standing in the shallows: 0.
A string property
Strings compare with == and !=, and query.property reads an entity property.
query.property('example:color') == 'red'red: 1; blue: 0.
Worn-out tools
An item expression reads durability; the result is 1 once less than a tenth is left.
query.remaining_durability / query.max_durability < 0.1almost broken: 1; nearly new: 0.
Interpolating between two numbers
math.lerp(start, end, t) blends linearly.
math.lerp(10, 20, 0.25)no scope needed: 12.5.
A reward that grows with level
Arithmetic follows the usual precedence, so the multiplication happens before the cap.
math.min(10 + query.player_level * 5, 100)level 4: 30; level 40: 100.
A terrain condition
Worldgen queries take coordinates and read the placement context.
query.heightmap(0, 0) > 62above sea level: 1; under water: 0.
A complex expression with a loop
Statements end in ;, v. is variable., and a complex expression yields what return gives it.
v.total = 0; loop(5, { v.total = v.total + 2; }); return v.total;five rounds of two: 10.
Stopping a loop early
break leaves the loop; the loop is capped at 1024 rounds either way.
v.n = 0; loop(100, { v.n = v.n + 1; (v.n >= 7) ? break; }); return v.n;stops at seven: 7.
Classes
Interfaces
ArrowNode
Defined in: ast.ts:63
The arrow operator left->right; evaluates right against the entity left resolves to.
Properties
kind
kind:
"arrow"
Defined in: ast.ts:64
left
left:
ExpressionNode
Defined in: ast.ts:65
right
right:
ExpressionNode
Defined in: ast.ts:66
AssignNode
Defined in: ast.ts:77
An assignment to variable.* or temp.* storage.
Properties
kind
kind:
"assign"
Defined in: ast.ts:78
target
target:
ReferenceNode
Defined in: ast.ts:79
value
value:
ExpressionNode
Defined in: ast.ts:80
BinaryNode
Defined in: ast.ts:47
A binary operation.
Properties
kind
kind:
"binary"
Defined in: ast.ts:48
left
left:
ExpressionNode
Defined in: ast.ts:50
operator
operator:
BinaryOperator
Defined in: ast.ts:49
right
right:
ExpressionNode
Defined in: ast.ts:51
BlockContext
Defined in: scope.ts:54
Block permutation state for query.block_state and tag queries on blocks.
Methods
blockState()?
optionalblockState(name):string|number|boolean|undefined
Defined in: scope.ts:56
The value of the named block state, or undefined when the permutation lacks it.
Parameters
name
string
Returns
string | number | boolean | undefined
tags()?
optionaltags(): readonlystring[]
Defined in: scope.ts:58
The block's tags, for query.any_tag and query.all_tags.
Returns
readonly string[]
BlockNode
Defined in: ast.ts:98
A braced statement list { a = 1; b = 2; }.
Properties
kind
kind:
"block"
Defined in: ast.ts:99
statements
statements: readonly
StatementNode[]
Defined in: ast.ts:100
CallNode
Defined in: ast.ts:30
A call such as math.clamp(a, b, c) or query.anger_level(this).
Properties
args
args: readonly
ExpressionNode[]
Defined in: ast.ts:33
callee
callee:
ReferenceNode
Defined in: ast.ts:32
kind
kind:
"call"
Defined in: ast.ts:31
CompileOptions
Defined in: Molang.ts:10
Options for one compilation.
Properties
minEngineVersion?
optionalminEngineVersion?:EngineVersion
Defined in: Molang.ts:15
The min_engine_version of the pack that contains the expression. Selects the versioned precedence and evaluation rules; the latest rules apply when omitted.
CompiledMolang
Defined in: compiler.ts:74
A compiled Molang expression: the parse diagnostics and a closure to evaluate it.
Properties
ast?
optionalast?:ProgramNode
Defined in: compiler.ts:78
The parsed program, or undefined when the source failed to parse.
diagnostics
diagnostics: readonly
Diagnostic[]
Defined in: compiler.ts:80
Problems found at parse or bind time; the closure still evaluates, erroring parts as 0.
evaluate
evaluate: (
scope) =>MolangValue
Defined in: compiler.ts:82
Evaluates the expression over the scope. Booleans come back as 0 and 1.
Parameters
scope
Returns
source
source:
string
Defined in: compiler.ts:76
The source text the expression was compiled from.
Diagnostic
Defined in: diagnostics.ts:9
A problem found while parsing or binding an expression. The engine raises content errors for these and still evaluates the expression to 0.0; this package mirrors that and reports the problem here instead of throwing.
Properties
code
code:
DiagnosticCode
Defined in: diagnostics.ts:10
message
message:
string
Defined in: diagnostics.ts:11
EntityContext
Defined in: scope.ts:27
Entity state the entity queries read. Implement the members your entities can answer.
Properties
variables?
optionalvariables?:Map<string,MolangValue>
Defined in: scope.ts:50
Read-write variable.* storage on the entity; not persisted.
Methods
angerLevel()?
optionalangerLevel():number
Defined in: scope.ts:29
The warden's anger level, [0, n).
Returns
number
equipmentCount()?
optionalequipmentCount():number
Defined in: scope.ts:31
Armor pieces equipped, 0 to 5, hands excluded.
Returns
number
hadComponentGroup()?
optionalhadComponentGroup(name):boolean
Defined in: scope.ts:33
Whether the entity was loaded from data saved with the named component group.
Parameters
name
string
Returns
boolean
hasAnyFamily()?
optionalhasAnyFamily(families):boolean
Defined in: scope.ts:35
Whether the entity belongs to any of the given type families.
Parameters
families
readonly string[]
Returns
boolean
headIsInWater()?
optionalheadIsInWater():boolean
Defined in: scope.ts:37
Whether the entity's head is in water.
Returns
boolean
isBaby()?
optionalisBaby():boolean
Defined in: scope.ts:38
Returns
boolean
isOnGround()?
optionalisOnGround():boolean
Defined in: scope.ts:39
Returns
boolean
isUsingItem()?
optionalisUsingItem():boolean
Defined in: scope.ts:40
Returns
boolean
lastHitByPlayer()?
optionallastHitByPlayer():boolean
Defined in: scope.ts:42
Whether a player dealt the last hit.
Returns
boolean
playerLevel()?
optionalplayerLevel():number
Defined in: scope.ts:44
The player's experience level, or 0 for non-players.
Returns
number
property()?
optionalproperty(name):string|number|undefined
Defined in: scope.ts:46
The value of the named entity property, or undefined when it does not exist.
Parameters
name
string
Returns
string | number | undefined
variant()?
optionalvariant():number
Defined in: scope.ts:48
The entity's variant index.
Returns
number
ForEachNode
Defined in: ast.ts:111
for_each(variable, array, expression).
Properties
array
array:
ExpressionNode
Defined in: ast.ts:114
body
body:
ExpressionNode
Defined in: ast.ts:115
kind
kind:
"for_each"
Defined in: ast.ts:112
variable
variable:
ReferenceNode
Defined in: ast.ts:113
IndexNode
Defined in: ast.ts:70
Array access target[index].
Properties
index
index:
ExpressionNode
Defined in: ast.ts:73
kind
kind:
"index"
Defined in: ast.ts:71
target
target:
ExpressionNode
Defined in: ast.ts:72
ItemContext
Defined in: scope.ts:62
Item stack state for the durability and tag queries.
Methods
maxDurability()?
optionalmaxDurability():number
Defined in: scope.ts:63
Returns
number
remainingDurability()?
optionalremainingDurability():number
Defined in: scope.ts:64
Returns
number
tags()?
optionaltags(): readonlystring[]
Defined in: scope.ts:66
The item's tags, for query.any_tag and query.all_tags.
Returns
readonly string[]
LoopControlNode
Defined in: ast.ts:93
break and continue inside a loop body. They may appear where an expression is expected, as in (v.x > 5) ? break, and evaluate to 0 outside a loop.
Properties
kind
kind:
"break"|"continue"
Defined in: ast.ts:94
LoopNode
Defined in: ast.ts:104
loop(count, expression); the engine caps count at 1024.
Properties
body
body:
ExpressionNode
Defined in: ast.ts:107
count
count:
ExpressionNode
Defined in: ast.ts:106
kind
kind:
"loop"
Defined in: ast.ts:105
MathFunction
Defined in: math.ts:2
A Molang math function: a pure numeric function over already-evaluated arguments.
Properties
evaluate
evaluate: (
args) =>number
Defined in: math.ts:4
Evaluates the function. Missing arguments read as 0, as Molang errors evaluate to 0.
Parameters
args
readonly number[]
Returns
number
MolangArray
Defined in: scope.ts:18
An actor array, as produced by queries the arrow and for_each constructs consume.
Properties
elements
elements: readonly
MolangValue[]
Defined in: scope.ts:20
kind
kind:
"array"
Defined in: scope.ts:19
MolangExample
Defined in: examples.ts:12
A Molang expression a pack author could write, with the scopes it is shown to work under.
Properties
cases
readonlycases: readonlyMolangExampleCase[]
Defined in: examples.ts:20
description
readonlydescription:string
Defined in: examples.ts:16
Where an author would put it and what it does.
minEngineVersion?
readonlyoptionalminEngineVersion?: readonly [number,number,number]
Defined in: examples.ts:19
The pack's min_engine_version when the expression depends on one.
source
readonlysource:string
Defined in: examples.ts:17
title
readonlytitle:string
Defined in: examples.ts:14
A short name, used as the heading in the README.
MolangExampleCase
Defined in: examples.ts:4
One evaluation of an example: what the scope answers and the value that comes out.
Properties
expected
readonlyexpected:MolangValue
Defined in: examples.ts:8
label
readonlylabel:string
Defined in: examples.ts:6
What is different about this scope, in a few words.
scope
readonlyscope:MolangScope
Defined in: examples.ts:7
MolangOptions
Defined in: Molang.ts:19
Options for a Molang engine.
Properties
queries?
optionalqueries?:QueryRegistry
Defined in: Molang.ts:21
The query registry to bind query.* names against; a fresh vanilla registry by default.
MolangScope
Defined in: scope.ts:95
Everything one evaluation can see. Queries declare which of these members they need; the compiler reports a query bound against a scope that was declared without its context.
Properties
block?
optionalblock?:BlockContext
Defined in: scope.ts:97
context?
optionalcontext?:ReadonlyMap<string,MolangValue>
Defined in: scope.ts:102
Read-only values the game supplies for the expression, such as context.other.
entity?
optionalentity?:EntityContext
Defined in: scope.ts:96
item?
optionalitem?:ItemContext
Defined in: scope.ts:98
placement?
optionalplacement?:PlacementContext
Defined in: scope.ts:100
variables?
optionalvariables?:Map<string,MolangValue>
Defined in: scope.ts:104
variable.* storage when no entity carries it, as for feature-rule engine variables.
world?
optionalworld?:WorldContext
Defined in: scope.ts:99
MolangScopeRef
Defined in: scope.ts:12
A reference to another evaluation scope, used by the arrow operator and this. The engine's actor references map to this: context.other->query.remaining_durability evaluates its right side against the referenced scope.
Properties
kind
kind:
"scope"
Defined in: scope.ts:13
scope
scope:
MolangScope
Defined in: scope.ts:14
MolangStruct
Defined in: scope.ts:2
A struct value, implicitly defined by usage such as v.location.x = 1;.
Properties
kind
kind:
"struct"
Defined in: scope.ts:3
members
members:
Map<string,MolangValue>
Defined in: scope.ts:4
MolangVersionRules
Defined in: version.ts:13
The language rules that change with a pack's min_engine_version.
The engine versions each Molang expression by the manifest of the pack that contains it. Two changes affect parsing: at 1.18.10 the ternary became right-associative, and at 1.18.20 && started binding tighter than || and comparisons tighter than equality. One change affects evaluation: at 1.19.60 division by a dynamically determined negative value stopped dividing by its absolute value.
Properties
andBeforeOr
andBeforeOr:
boolean
Defined in: version.ts:17
&& binds tighter than || and comparison tighter than equality from 1.18.20.
divideByNegativeValue
divideByNegativeValue:
boolean
Defined in: version.ts:19
Division by a non-constant negative divisor uses the real divisor from 1.19.60.
ternaryRightAssociative
ternaryRightAssociative:
boolean
Defined in: version.ts:15
A ? B : C ? D : E groups as A ? B : (C ? D : E) from 1.18.10.
NumberNode
Defined in: ast.ts:2
A numeric literal. Molang numbers are floats; true and false lex to 1 and 0.
Properties
kind
kind:
"number"
Defined in: ast.ts:3
value
value:
number
Defined in: ast.ts:4
PlacementContext
Defined in: scope.ts:80
Feature placement state for the worldgen queries.
Methods
aboveTopSolid()?
optionalaboveTopSolid(x,z):number
Defined in: scope.ts:84
The height of the block above the highest solid block at (x, z).
Parameters
x
number
z
number
Returns
number
hasBiomeTag()?
optionalhasBiomeTag(tag,x?,y?,z?):boolean
Defined in: scope.ts:88
Whether the biome at the placement position (or the given one) has the tag.
Parameters
tag
string
x?
number
y?
number
z?
number
Returns
boolean
heightmap()?
optionalheightmap(x,z):number
Defined in: scope.ts:82
The world-surface height (Y) at (x, z).
Parameters
x
number
z
number
Returns
number
noise()?
optionalnoise(x,z):number
Defined in: scope.ts:86
A Perlin noise value near [-1, 1] for (x, z).
Parameters
x
number
z
number
Returns
number
ProgramNode
Defined in: ast.ts:143
A parsed Molang expression. A simple expression has one statement and no ; and returns its value; a complex expression runs its statements and returns 0 unless a return runs.
Properties
complex
complex:
boolean
Defined in: ast.ts:145
kind
kind:
"program"
Defined in: ast.ts:144
statements
statements: readonly
StatementNode[]
Defined in: ast.ts:146
QueryDefinition
Defined in: queries.ts:7
A registered query: the context it needs and its implementation.
Properties
context
context:
QueryContextKind
Defined in: queries.ts:9
Which part of the scope the query reads; loaders can validate fields against it.
evaluate
evaluate: (
scope,args) =>MolangValue
Defined in: queries.ts:11
Evaluates the query over already-evaluated arguments.
Parameters
scope
args
readonly MolangValue[]
Returns
ReferenceNode
Defined in: ast.ts:18
A dotted reference such as query.is_baby, variable.a.b or math.pi. domain is the normalized first segment (aliases q, v, t and c are expanded) and path the remaining segments.
Properties
domain
domain:
"query"|"math"|"variable"|"temp"|"context"|"geometry"|"material"|"texture"|"array"
Defined in: ast.ts:20
kind
kind:
"reference"
Defined in: ast.ts:19
path
path: readonly
string[]
Defined in: ast.ts:21
ReturnNode
Defined in: ast.ts:84
return <expression>; ends the whole expression with the value.
Properties
kind
kind:
"return"
Defined in: ast.ts:85
value
value:
ExpressionNode
Defined in: ast.ts:86
StringNode
Defined in: ast.ts:8
A single-quoted string literal with its case preserved.
Properties
kind
kind:
"string"
Defined in: ast.ts:9
value
value:
string
Defined in: ast.ts:10
TernaryNode
Defined in: ast.ts:55
A ? B : C, or the binary conditional A ? B when otherwise is undefined.
Properties
condition
condition:
ExpressionNode
Defined in: ast.ts:57
kind
kind:
"ternary"
Defined in: ast.ts:56
otherwise?
optionalotherwise?:ExpressionNode
Defined in: ast.ts:59
then
then:
ExpressionNode
Defined in: ast.ts:58
ThisNode
Defined in: ast.ts:25
this, the value the expression ultimately writes to.
Properties
kind
kind:
"this"
Defined in: ast.ts:26
Token
Defined in: lexer.ts:11
A lexed token. value is lowercased except for string contents, which keep their case.
Properties
kind
kind:
TokenKind
Defined in: lexer.ts:12
start
start:
number
Defined in: lexer.ts:15
Offset of the token's first character in the source text.
value
value:
string
Defined in: lexer.ts:13
UnaryNode
Defined in: ast.ts:37
A unary operation, !a or -a.
Properties
kind
kind:
"unary"
Defined in: ast.ts:38
operand
operand:
ExpressionNode
Defined in: ast.ts:40
operator
operator:
"!"|"-"
Defined in: ast.ts:39
WorldContext
Defined in: scope.ts:70
Level state the world queries read.
Methods
day()?
optionalday():number
Defined in: scope.ts:73
Returns
number
moonBrightness()?
optionalmoonBrightness():number
Defined in: scope.ts:75
Returns
number
moonPhase()?
optionalmoonPhase():number
Defined in: scope.ts:74
Returns
number
timeOfDay()?
optionaltimeOfDay():number
Defined in: scope.ts:72
Time of day in [0, 1): midnight 0.0, sunrise 0.25, noon 0.5, sunset 0.75.
Returns
number
timeStamp()?
optionaltimeStamp():number
Defined in: scope.ts:76
Returns
number
Type Aliases
BinaryOperator
BinaryOperator =
"+"|"-"|"*"|"/"|"<"|"<="|">"|">="|"=="|"!="|"&&"| "||" |"??"
Defined in: ast.ts:44
The binary operators of the language, including ??.
DiagnosticCode
DiagnosticCode =
"syntax-error"|"unknown-query"|"unknown-math-function"|"client-only-resource"
Defined in: diagnostics.ts:2
What a diagnostic reports.
EngineVersion
EngineVersion = readonly [
number,number,number]
Defined in: version.ts:2
A pack min_engine_version triple, as found in a manifest header.
ExpressionNode
ExpressionNode =
NumberNode|StringNode|ReferenceNode|ThisNode|CallNode|UnaryNode|BinaryNode|TernaryNode|ArrowNode|IndexNode|AssignNode|BlockNode|LoopNode|ForEachNode|LoopControlNode
Defined in: ast.ts:119
Any expression form.
Keyword
Keyword = typeof
KEYWORDS[number]
Defined in: lexer.ts:5
One of KEYWORDS.
MathFunctionName
MathFunctionName = keyof typeof
MATH_FUNCTIONS
Defined in: math.ts:168
The name of a known math.* function.
MolangValue
MolangValue =
number|string|MolangStruct|MolangScopeRef|MolangArray
Defined in: scope.ts:24
A runtime Molang value. Numbers carry booleans as 0.0 and 1.0.
QueryContextKind
QueryContextKind =
"entity"|"block"|"item"|"world"|"placement"|"tags"|"pure"
Defined in: queries.ts:4
The scope member a query reads. tags resolves to the item when present, else the block.
StatementNode
StatementNode =
ExpressionNode|ReturnNode
Defined in: ast.ts:137
A statement in a complex expression.
TokenKind
TokenKind =
"number"|"string"|"identifier"|"keyword"|"operator"
Defined in: lexer.ts:8
The lexical category of a token.
VanillaQueryName
VanillaQueryName = keyof typeof
VANILLA_QUERIES
Defined in: queries.ts:134
The name of a registered vanilla server-side query.
Variables
KEYWORDS
constKEYWORDS: readonly ["return","loop","for_each","break","continue","this","true","false"]
Defined in: lexer.ts:2
Keywords the parser treats specially. Everything else that looks like a word is an identifier.
LATEST_RULES
constLATEST_RULES:MolangVersionRules
Defined in: version.ts:35
The rules the latest engine applies; used when a caller gives no version.
MATH_FUNCTIONS
constMATH_FUNCTIONS:object
Defined in: math.ts:95
The 61 documented math.* functions with Bedrock's semantics: trigonometry in degrees, sign(0) is -1, mod keeps the dividend's sign like C fmod, die_roll sums num random floats, and the 30 easing functions map (start, end, t) along the Penner curves.
Type Declaration
abs
readonlyabs:MathFunction
acos
readonlyacos:MathFunction
asin
readonlyasin:MathFunction
atan
readonlyatan:MathFunction
atan2
readonlyatan2:MathFunction
ceil
readonlyceil:MathFunction
clamp
readonlyclamp:MathFunction
copy_sign
readonlycopy_sign:MathFunction
cos
readonlycos:MathFunction
die_roll
readonlydie_roll:MathFunction
die_roll_integer
readonlydie_roll_integer:MathFunction
ease_in_back
readonlyease_in_back:MathFunction
ease_in_bounce
readonlyease_in_bounce:MathFunction
ease_in_circ
readonlyease_in_circ:MathFunction
ease_in_cubic
readonlyease_in_cubic:MathFunction
ease_in_elastic
readonlyease_in_elastic:MathFunction
ease_in_expo
readonlyease_in_expo:MathFunction
ease_in_out_back
readonlyease_in_out_back:MathFunction
ease_in_out_bounce
readonlyease_in_out_bounce:MathFunction
ease_in_out_circ
readonlyease_in_out_circ:MathFunction
ease_in_out_cubic
readonlyease_in_out_cubic:MathFunction
ease_in_out_elastic
readonlyease_in_out_elastic:MathFunction
ease_in_out_expo
readonlyease_in_out_expo:MathFunction
ease_in_out_quad
readonlyease_in_out_quad:MathFunction
ease_in_out_quart
readonlyease_in_out_quart:MathFunction
ease_in_out_quint
readonlyease_in_out_quint:MathFunction
ease_in_out_sine
readonlyease_in_out_sine:MathFunction
ease_in_quad
readonlyease_in_quad:MathFunction
ease_in_quart
readonlyease_in_quart:MathFunction
ease_in_quint
readonlyease_in_quint:MathFunction
ease_in_sine
readonlyease_in_sine:MathFunction
ease_out_back
readonlyease_out_back:MathFunction
ease_out_bounce
readonlyease_out_bounce:MathFunction
ease_out_circ
readonlyease_out_circ:MathFunction
ease_out_cubic
readonlyease_out_cubic:MathFunction
ease_out_elastic
readonlyease_out_elastic:MathFunction
ease_out_expo
readonlyease_out_expo:MathFunction
ease_out_quad
readonlyease_out_quad:MathFunction
ease_out_quart
readonlyease_out_quart:MathFunction
ease_out_quint
readonlyease_out_quint:MathFunction
ease_out_sine
readonlyease_out_sine:MathFunction
exp
readonlyexp:MathFunction
floor
readonlyfloor:MathFunction
hermite_blend
readonlyhermite_blend:MathFunction
inverse_lerp
readonlyinverse_lerp:MathFunction
lerp
readonlylerp:MathFunction
lerprotate
readonlylerprotate:MathFunction
ln
readonlyln:MathFunction
max
readonlymax:MathFunction
min
readonlymin:MathFunction
min_angle
readonlymin_angle:MathFunction
mod
readonlymod:MathFunction
pi
readonlypi:MathFunction
pow
readonlypow:MathFunction
random
readonlyrandom:MathFunction
random_integer
readonlyrandom_integer:MathFunction
round
readonlyround:MathFunction
sign
readonlysign:MathFunction
sin
readonlysin:MathFunction
sqrt
readonlysqrt:MathFunction
trunc
readonlytrunc:MathFunction
MOLANG_EXAMPLES
constMOLANG_EXAMPLES: readonlyMolangExample[]
Defined in: examples.ts:32
Expressions that compile and evaluate on this engine, each with the values it produces.
The README shows exactly these sources, and examples.test.ts fails when the two differ or when an example stops producing its values, so none of them can quietly rot. A tool that wants real Molang to try - an editor, a playground, a documentation page - can take them from here.
VANILLA_QUERIES
constVANILLA_QUERIES:object
Defined in: queries.ts:26
The 21 queries the vanilla behavior pack uses server-side, keyed without the query. prefix. Each declares the scope context it reads.
Type Declaration
above_top_solid
readonlyabove_top_solid:object
above_top_solid.context
readonlycontext:"placement"='placement'
above_top_solid.evaluate
readonlyevaluate: (scope,args) =>number
Parameters
scope
args
readonly MolangValue[]
Returns
number
all_tags
readonlyall_tags:object
all_tags.context
readonlycontext:"tags"='tags'
all_tags.evaluate
readonlyevaluate: (scope,args) =>0|1
Parameters
scope
args
readonly MolangValue[]
Returns
0 | 1
anger_level
readonlyanger_level:object
anger_level.context
readonlycontext:"entity"='entity'
anger_level.evaluate
readonlyevaluate: (scope) =>number
Parameters
scope
Returns
number
any_tag
readonlyany_tag:object
any_tag.context
readonlycontext:"tags"='tags'
any_tag.evaluate
readonlyevaluate: (scope,args) =>0|1
Parameters
scope
args
readonly MolangValue[]
Returns
0 | 1
block_state
readonlyblock_state:object
block_state.context
readonlycontext:"block"='block'
block_state.evaluate
readonlyevaluate: (scope,args) =>string|number
Parameters
scope
args
readonly MolangValue[]
Returns
string | number
equipment_count
readonlyequipment_count:object
equipment_count.context
readonlycontext:"entity"='entity'
equipment_count.evaluate
readonlyevaluate: (scope) =>number
Parameters
scope
Returns
number
had_component_group
readonlyhad_component_group:object
had_component_group.context
readonlycontext:"entity"='entity'
had_component_group.evaluate
readonlyevaluate: (scope,args) =>0|1
Parameters
scope
args
readonly MolangValue[]
Returns
0 | 1
has_any_family
readonlyhas_any_family:object
has_any_family.context
readonlycontext:"entity"='entity'
has_any_family.evaluate
readonlyevaluate: (scope,args) =>0|1
Parameters
scope
args
readonly MolangValue[]
Returns
0 | 1
has_biome_tag
readonlyhas_biome_tag:object
has_biome_tag.context
readonlycontext:"placement"='placement'
has_biome_tag.evaluate
readonlyevaluate: (scope,args) =>0|1
Parameters
scope
args
readonly MolangValue[]
Returns
0 | 1
head_is_in_water
readonlyhead_is_in_water:object
head_is_in_water.context
readonlycontext:"entity"='entity'
head_is_in_water.evaluate
readonlyevaluate: (scope) =>0|1
Parameters
scope
Returns
0 | 1
heightmap
readonlyheightmap:object
heightmap.context
readonlycontext:"placement"='placement'
heightmap.evaluate
readonlyevaluate: (scope,args) =>number
Parameters
scope
args
readonly MolangValue[]
Returns
number
is_baby
readonlyis_baby:object
is_baby.context
readonlycontext:"entity"='entity'
is_baby.evaluate
readonlyevaluate: (scope) =>0|1
Parameters
scope
Returns
0 | 1
is_on_ground
readonlyis_on_ground:object
is_on_ground.context
readonlycontext:"entity"='entity'
is_on_ground.evaluate
readonlyevaluate: (scope) =>0|1
Parameters
scope
Returns
0 | 1
is_using_item
readonlyis_using_item:object
is_using_item.context
readonlycontext:"entity"='entity'
is_using_item.evaluate
readonlyevaluate: (scope) =>0|1
Parameters
scope
Returns
0 | 1
last_hit_by_player
readonlylast_hit_by_player:object
last_hit_by_player.context
readonlycontext:"entity"='entity'
last_hit_by_player.evaluate
readonlyevaluate: (scope) =>0|1
Parameters
scope
Returns
0 | 1
max_durability
readonlymax_durability:object
max_durability.context
readonlycontext:"item"='item'
max_durability.evaluate
readonlyevaluate: (scope) =>number
Parameters
scope
Returns
number
noise
readonlynoise:object
noise.context
readonlycontext:"placement"='placement'
noise.evaluate
readonlyevaluate: (scope,args) =>number
Parameters
scope
args
readonly MolangValue[]
Returns
number
player_level
readonlyplayer_level:object
player_level.context
readonlycontext:"entity"='entity'
player_level.evaluate
readonlyevaluate: (scope) =>number
Parameters
scope
Returns
number
property
readonlyproperty:object
property.context
readonlycontext:"entity"='entity'
property.evaluate
readonlyevaluate: (scope,args) =>string|number
Parameters
scope
args
readonly MolangValue[]
Returns
string | number
remaining_durability
readonlyremaining_durability:object
remaining_durability.context
readonlycontext:"item"='item'
remaining_durability.evaluate
readonlyevaluate: (scope) =>number
Parameters
scope
Returns
number
variant
readonlyvariant:object
variant.context
readonlycontext:"entity"='entity'
variant.evaluate
readonlyevaluate: (scope) =>number
Parameters
scope
Returns
number
Functions
compileProgram()
compileProgram(
source,program,rules,queries):CompiledMolang
Defined in: compiler.ts:86
Compiles a parsed program into a closure over a scope.
Parameters
source
string
program
rules
queries
Returns
parse()
parse(
source,rules):ProgramNode
Defined in: parser.ts:283
Parses Molang source text into an AST under the given versioned rules.
Parameters
source
string
rules
Returns
Throws
When the text is not a valid Molang expression.
rulesForEngineVersion()
rulesForEngineVersion(
version):MolangVersionRules
Defined in: version.ts:26
Derives the versioned language rules from a pack's min_engine_version.
Parameters
version
Returns
tokenize()
tokenize(
source):Token[]
Defined in: lexer.ts:43
Splits Molang source text into tokens. Molang is case-insensitive except inside strings, so identifiers and keywords are lowercased here and string contents are preserved.
Parameters
source
string
Returns
Token[]
Throws
When the text contains an unexpected character or an unterminated string.