Language Syntax Reference
Overview
Morphyn is a declarative language with minimal syntax. Programs consist of entity declarations with fields and event handlers.
Comments
Three comment styles are supported:
Entity Declaration
Field Declaration
Basic Fields
Pool Fields
Event Handlers
Without Parameters
With Parameters
Actions
Data Flow (Arrow)
Check (Guard)
# Check with an inline action
check condition: action
check hp > 0: emit alive
check state == "idle": emit can_move
# Check without an inline action (guard)
# If false, the event execution is stopped
check i < 0
Emit (Event Dispatch)
emit event_name
emit event_name(arg1, arg2)
emit target.event_name
emit self.destroy
emit log("message", value)
emit input("prompt: ", "fieldName")
Sync Emit (Immediate Call with Return Value)
Executes an event synchronously and assigns the result to a field. The result is the last value assigned inside the called event.
emit event_name(args) -> field
emit Entity.event_name(args) -> field
emit Entity.event_name(args) -> pool.at[idx]
Subscriptions
Subscribe to events of another entity:
entity Logger {
event init {
when Player.death : onPlayerDeath
}
event onPlayerDeath {
emit log("Player died!")
}
}
Field Change Subscriptions
Subscribe to value changes of a field:
watch fieldName : handlerEvent # watch own field
watch TargetEntity.fieldName : handlerEvent # watch field on another entity
unwatch fieldName : handlerEvent
unwatch TargetEntity.fieldName : handlerEvent
The handler receives (oldValue, newValue) as arguments.
Only fires when the value actually changes.
entity Player {
has hp: 100
event init {
watch hp : onHpChanged
}
event onHpChanged(old, new) {
emit log("hp:", old, "->", new)
check new <= 0: emit die
}
}
Block Actions
Built-in Functions
| Function | Description | Example |
|---|---|---|
log |
Print to console | emit log("HP:", hp) |
input |
Read line from console into field | emit input("Name: ", "name") |
unity |
Call Unity callback | emit unity("PlaySound", "hit") |
input
Reads a line from console and writes the result into a field. The field name must be passed as a string literal in quotes.
Operators
Arithmetic
| Operator | Description | Example |
|---|---|---|
+ |
Addition | hp + 10 |
- |
Subtraction | hp - 5 |
* |
Multiplication | damage * 2 |
/ |
Division | armor / 3 |
% |
Modulo | level % 5 |
Comparison
| Operator | Description | Example |
|---|---|---|
== |
Equal | hp == 100 |
!= |
Not equal | state != "dead" |
> |
Greater than | hp > 0 |
< |
Less than | hp < max |
>= |
Greater or equal | level >= 10 |
<= |
Less or equal | mana <= 0 |
Logic
| Operator | Description | Example |
|---|---|---|
and |
Logical AND | hp > 0 and alive |
or |
Logical OR | idle or walk |
not |
Logical NOT | not dead |
Flow
| Operator | Description | Example |
|---|---|---|
-> |
Data flow | value -> field |
Keywords
| Keyword | Purpose |
|---|---|
entity |
Declare entity |
has |
Declare field |
event |
Declare event handler |
emit |
Send event, call sync event, or call built-in function |
check |
Conditional guard |
pool |
Collection type |
when |
Subscribe to another entity's event |
unwhen |
Unsubscribe from another entity's event |
watch |
Subscribe to field value changes |
unwatch |
Unsubscribe from field value changes |
true |
Boolean true |
false |
Boolean false |
null |
Null value |