-
Notifications
You must be signed in to change notification settings - Fork 1
Autumn Standard Library Documentation
Autumn is a Lisp-like language with S-expressions. All code is wrapped in parentheses, with the first element being the operator or function name. The language is designed for game programming with built-in support for physics and collision detection.
Below is a formal presentation of the grammar for both expressions (Expr) and statements (Stmt), integrating the relevant token types (e.g.,
-
$\textit{Expr}$ denotes an expression node. -
$\textit{Stmt}$ denotes a statement node. -
$\textit{Token}[X]$ indicates a token whose$\texttt{TokenType}$ is$X$ . -
$\textit{T*}$ indicates a list of elements of type$T$ . - Braces
$( \dots )$ enclose parameter lists for each construct.
Expressions:
Assign ::= "=" (name : Token[IDENTIFIER]) (value : Expr)
Binary ::= op : { PLUS, MINUS, STAR, SLASH, MODULO } (left : Expr) (right : Expr)
Call ::= (callee : Expr) (arguments : Expr)* | ((callee))
Grouping ::= (expression : Expr)
Literal ::= Token[NUMBER | STRING | TRUE | FALSE | NIL]
Logical ::= (op : { AND, OR }) (left : Expr) (right : Expr)
Get ::= ".." (object : Expr) (name : Token[IDENTIFIER])
Unary ::= (op : { BANG, MINUS }) (right : Expr)
Lambda ::= "-->" (params : Token[IDENTIFIER]*) (right : Expr)
Variable ::= (name : Token[IDENTIFIER])
TypeExpr ::= (name : Token[IDENTIFIER])
TypeDecl ::= ":" (name : Token[IDENTIFIER]) (typeexpr : TypeExpr)
ListTypeExpr ::= "List" (typeexpr : TypeExpr)
ListVarExpr ::= "list" (varExprs : Expr)*
IfExpr ::= "if" (condition : Expr) "then" (elseBranch : Expr)
Let ::= "Let" (exprs : Expr)*
InitNext ::= "initnext" (initializer : Expr) (nextExpr : Expr)
Statements:
Object ::= "object" (name : Token[IDENTIFIER]) (fields : TypeExpr*) (Cell : Expr)*
Expression ::= (expression : Expr)
OnStmt ::= "on" (condition : Expr) (expr : Expr)
We provide the script in tools/generate_ast.cpp to generate and modify this grammar quickly.
-
Single-character tokens:
$\texttt{LEFT\_PAREN}$ $($ ,$\texttt{RIGHT\_PAREN}$ $)$ ,$\texttt{LEFT\_BRACE}$ $\{$ ,$\texttt{RIGHT\_BRACE}$ $\}$ ,$\texttt{COMMA}$ $\texttt{,}$ ,$\texttt{DOT}$ $.$ ,$\texttt{MINUS}$ $-$ ,$\texttt{PLUS}$ $+$ ,$\texttt{SLASH}$ $/$ ,$\texttt{STAR}$ $*$ ,$\texttt{COLON}$ $:$ ,$\texttt{MODULO}$ $%$ . -
Multi-character tokens:
$\texttt{BANG\_EQUAL}$ $!=$ ,$\texttt{EQUAL\_EQUAL}$ $==$ ,$\texttt{GREATER\_EQUAL}$ $\ge$ ,$\texttt{LESS\_EQUAL}$ $\le$ ,$\texttt{MAPTO}$ $\rightarrow$ . -
Literals:
$\texttt{IDENTIFIER}$ (variable names),$\texttt{STRING}$ ,$\texttt{NUMBER}$ ,$\texttt{TRUE}$ ,$\texttt{FALSE}$ ,$\texttt{NIL}$ . -
Keywords:
$\texttt{PROGRAM}$ $\text{program}$ ,$\texttt{AND}$ (either & or$\texttt{and}$ ,$\texttt{OBJECT}$ $\texttt{object}$ ,$\texttt{ELSE}$ $\texttt{else}$ ,$\texttt{FUN}$ $\texttt{fn}$ ,$\texttt{IF}$ $\texttt{if}$ ,$\texttt{THEN}$ $\texttt{then}$ ,$\texttt{ON}$ $\texttt{on}$ ,$\texttt{OR}$ (either$\texttt{or}$ or |),$\texttt{LET}$ $\texttt{let}$ ,$\texttt{INITNEXT}$ .
For modifying Token (adding token types, etc. please look at TokenType header file
Below is a compact set of examples illustrating each expression type and statement type from the grammar. Every snippet is wrapped in a (program ...) form for completeness, but in real usage, you would mix and match these constructs as needed.
Expression Examples
- Assign
(program
(= x 42) ; Assigning the literal 42 to variable x
)- Explanation: The expression
(= x 42)assigns the value 42 to a variable named x.
- Binary
(program
(on true
(let
(= sum (+ 2 3)) ; Binary expr: (+ 2 3)
(print sum)
true
)
)
)- Explanation:
(+ 2 3)is a binary expression using the + operator.
- Call
(program
(: foo (fn (a b) (* a b))) ; A simple lambda function for multiplication
(= result (foo 10 5)) ; Call expr: (foo 10 5)
)- Explanation:
(foo 10 5)calls the function stored in foo with arguments 10 and 5.
- Get
(program
(object Player (: health Number) (Cell 0 0 "blue"))
(: player Player)
(= player (Player (Position 5 5)))
(= currentHealth (.. player health)) ; Get expr: (.. player health)
)- Explanation:
(.. player health)accesses the health field from the object player.
- Grouping
(program
(= grouped ( ( + 1 (* 2 3) ) )) ; Grouping expr around (+ 1 (* 2 3))
)- Explanation: Grouping is demonstrated by wrapping expressions in parentheses (though in Autumn’s Lisp-like syntax, parentheses are already used heavily). The outer parentheses around
(+ 1 (* 2 3))illustrate grouping.
- Literal
(program
(= str "hello world") ; Literal string
(= num 123) ; Literal number
(= boolValue true) ; Literal boolean
)- Explanation: Literal values can be strings, numbers, booleans, or nil.
- Logical
(program
(= x 3)
(= y 10)
(= check (& (> x 2) (== y 10))) ; Logical expr: (& (> x 2) (== y 10))
)- Explanation: This checks the conjunction (&) of two comparisons: x > 2 and y == 10.
- Set
(program
(object Agent (: speed Number) (Cell 0 0 "red"))
(: myAgent Agent)
(= myAgent (Agent (Position 4 4)))
(= (.. myAgent speed) 99) ; Set expr: updates the 'speed' field of 'myAgent'
)- Explanation: (= (.. myAgent speed) 99) modifies the speed field of myAgent.
- Unary
(program
(: value Number)
(= value 5)
(= negValue (- value)) ; Unary minus
(= notAlive (! alive)) ; Unary bang (if alive is a bool)
)- Explanation:
- (- value) is the unary negation of value.
- (! alive) would be a unary logical NOT, assuming alive is a boolean variable.
- Lambda
(program
(= adder (fn (a b) (+ a b))) ; A lambda function taking two params a, b
(= result (adder 7 8)) ; Calls the lambda -> 15
)- Explanation: (fn (a b) (+ a b)) is a lambda expression assigned to adder.
- Variable
(program
(: x Number) ; Declare variable x
(= x 10) ; Variable usage
(on true (print x)) ; x is used directly
)- Explanation: x is a simple variable referred to by name.
- TypeDecl
(program
(: MyCustomType Bool)
)- Explanation: Declares a new type MyCustomType as a list of some type T (In this case, Bool). (Requires having typevariable Bool declared somewhere.)
- ListTypeExpr
(program
(: MyType (List Bool)) ; Interpreted as "List of Number"
)
- Explanation: (listtypeexpr Number) means a type expression for a list of Number.
14. ListVarExpr
```sexp
(program
(= items (list 1 2 3 4)) ; Creates a list of literal values
)- Explanation: (list 1 2 3 4) is a typical “ListVarExpr” usage, returning a list expression of values.
- IfExpr
(program
(= x 5)
(= result (if (> x 3) then "x is large" else "x is small")) ; IfExpr
)- Explanation: (if (> x 3) ... ...) is the conditional expression returning one of two branches.
- Let
(program
(on true
(let
(= a 1)
(= b 2)
(print (+ a b)) ; -> prints 3
true
)
)
)- Explanation: A let expression creates local bindings (a, b) and then executes sub-expressions, and return value of the final expression evaluation
- InitNext
(program
(: counter Number)
(= counter (initnext 0 ( + (prev counter) 1 )))
)- Explanation: On initialization, counter is 0. Each subsequent timestep, it sets counter to (prev counter) + 1.
- Object
(program
(object Player (: health Number) (Cell 0 0 "blue"))
(object Enemy (: damage Number) (Cell 0 0 "red"))
)- Explanation: Defines two object types, Player and Enemy, each with fields and a base Cell.
- Expression (Statement)
(program
(+ 1 2)
(print "Hello from an expression stmt")
)- Explanation: An Expression statement simply evaluates an expression where a statement is allowed (e.g., top-level or inside a block).
- OnStmt
(program
(= x 0)
(on (< x 5)
(= x (+ x 1))
)
)- Explanation: onStmt triggers the expression body whenever the condition (< x 5) evaluates to true. Here, it increments x until it reaches
Below is a comprehensive set of documentation for every function defined in the Autumn Stdlib, as well as brief documentation for functions/calls referenced but not defined within the snippet.
-
Description: Moves the given object
objby the vectordir. -
Parameters:
-
obj: The object to move (must have anoriginproperty). -
dir: APositionobject specifying the displacement in x and y.
-
-
Returns: A new version of
objwhoseoriginhas been updated toorigin + dir.
-
Description: Moves the given object
objexactly one unit to the right (x + 1). -
Parameters:
-
obj: The object to move.
-
-
Returns: The updated object with the x-coordinate of
originincremented by 1.
-
Description: Moves the given object
objexactly one unit to the left (x - 1). -
Parameters:
-
obj: The object to move.
-
-
Returns: The updated object with the x-coordinate of
origindecremented by 1.
-
Description: Moves the given object
objexactly one unit up (y - 1). -
Parameters:
-
obj: The object to move.
-
-
Returns: The updated object with the y-coordinate of
origindecremented by 1.
-
Description: Moves the given object
objexactly one unit down (y + 1). -
Parameters:
-
obj: The object to move.
-
-
Returns: The updated object with the y-coordinate of
originincremented by 1.
-
Description: Moves
objby(x, y)only if the resulting position is free, excluding any collision checks with the originalobjitself. -
Parameters:
-
obj: The object to move. -
x: The change in the x-direction. -
y: The change in the y-direction.
-
-
Returns: A new version of
objif the position is free, otherwise returns the originalobjwithout movement.
-
Description: Moves
objby(x, y)if the resulting position is free, excluding collisions withobjandobj2. -
Parameters:
-
obj: The primary object to move. -
x: The change in the x-direction. -
y: The change in the y-direction. -
obj2: A second object to exclude from collision checks.
-
- Returns: The updated object if the position is free, otherwise the original object.
-
Description: Rotates
obj(using an external functionrotate) only if the rotated object:- Remains within bounds.
- Does not collide with anything except the original
obj.
-
Parameters:
-
obj: The object to rotate.
-
- Returns: The rotated object if rotation is valid, otherwise the original object.
-
Description: Computes a new
Positionby addingdirtopos. -
Parameters:
-
pos: APositionobject. -
dir: APositionspecifying how much to move in x and y.
-
-
Returns: A new
Positionwith updated coordinates.
-
Description: Returns a new
Positionmoved one unit to the right frompos. -
Parameters:
-
pos: The initialPosition.
-
-
Returns:
Position(pos.x + 1, pos.y).
-
Description: Returns a new
Positionmoved one unit to the left. -
Parameters:
-
pos: The initialPosition.
-
-
Returns:
Position(pos.x - 1, pos.y).
-
Description: Returns a new
Positionmoved one unit up. -
Parameters:
-
pos: The initialPosition.
-
-
Returns:
Position(pos.x, pos.y - 1).
-
Description: Returns a new
Positionmoved one unit down. -
Parameters:
-
pos: The initialPosition.
-
-
Returns:
Position(pos.x, pos.y + 1).
-
Description: Returns the absolute value of
x. -
Parameters:
-
x: A number.
-
-
Returns: Absolute value of
x.
-
Description: Determines the sign of
x. -
Parameters:
-
x: A number (integer, as Autumn does not have float yet).
-
-
Returns:
-
-1ifx < 0 -
0ifx == 0 -
1ifx > 0
-
-
Description: An alias for
concat. In this code,vcatis set toconcat. - Parameters: Varies depending on usage, typically a list of lists to concatenate.
- Returns: A single concatenated list.
-
Description: Computes the difference in coordinates between
pos2andpos1. -
Parameters:
-
pos1: The firstPosition. -
pos2: The secondPosition.
-
-
Returns: A new
Position=(pos2 - pos1), i.e.(pos2.x - pos1.x, pos2.y - pos1.y).
-
Description: Generates a list of
Positionobjects for the rectangular region spanning from(pos1.x, pos1.y)to(pos2.x, pos2.y), excluding the upper bound if the language follows typicalrangeconventions. -
Parameters:
-
pos1: The top-leftPosition. -
pos2: The bottom-rightPosition.
-
-
Returns: A flattened list of
Positionobjects representing each coordinate in the rectangle.
-
Description: An alias for
deltaPos. Computes the displacement between two positions. -
Parameters:
-
pos1: The firstPosition. -
pos2: The secondPosition.
-
-
Returns: A
Positionrepresenting(pos2 - pos1).
-
Description: Computes the coordinate difference between two elements by comparing their
positionproperty. -
Parameters:
-
e1: An element with aposition. -
e2: Another element with aposition.
-
-
Returns: The result of
deltaPos(e1.position, e2.position).
-
Description: Computes the difference between the
originof two objects. -
Parameters:
-
obj1: First object with anorigin. -
obj2: Second object with anorigin.
-
-
Returns: A
Positionrepresenting(obj2.origin - obj1.origin).
-
Description: Checks if two elements (
e1ande2) are adjacent on a grid, meaning the sum of the absolute difference in x plus y coordinates is1. -
Parameters:
-
e1: An element with aposition. -
e2: Another element with aposition.
-
-
Returns: Boolean
trueif they are horizontally or vertically adjacent, otherwisefalse.
-
Description: Checks if two positions are within
unitSizeadjacency in a grid sense. Here, adjacency is defined byabs(delta.x) + abs(delta.y) <= unitSize. -
Parameters:
-
p1: APosition. -
p2: AnotherPosition. -
unitSize: The threshold for adjacency.
-
-
Returns: Boolean
trueif withinunitSize, elsefalse.
-
Description: Checks if
obj1andobj2are adjacent, using the same logic asadjacentPosson theirorigin. -
Parameters:
-
obj1: First object. -
obj2: Second object. -
unitSize: Adjacency threshold.
-
-
Returns: Boolean
trueif withinunitSize, elsefalse.
-
Description: Checks if two positions are adjacent including diagonals, i.e.
abs(delta.x) <= 1andabs(delta.y) <= 1. -
Parameters:
-
p1: APosition. -
p2: APosition.
-
-
Returns: Boolean
trueif they are adjacent in any of the 8 surrounding squares, elsefalse.
-
Description: Uses
adjacentPossDiagto check diagonal adjacency between two objects. -
Parameters:
-
obj1: First object. -
obj2: Second object.
-
-
Returns: Boolean
trueif diagonally (or directly) adjacent, elsefalse.
-
Description: Finds all objects in the global
allObjs()list that are adjacent toobj1(or each object inobj1, ifobj1is a list) givenunitSize. -
Parameters:
-
obj1: The object or list of objects. -
unitSize: The threshold for adjacency.
-
-
Returns: A list of objects from
allObjs()that are adjacent.
-
Description: Finds all objects in the global
allObjs()list that are adjacent toobj(or each object inobj, ifobjis a list) including diagonals. -
Parameters:
-
obj: The object or list of objects.
-
- Returns: A list of objects considered adjacent diagonally or orthogonally.
-
Description: Checks if
objis adjacent to any object inobjsgivenunitSize. -
Parameters:
-
obj: The object to check. -
objs: A list of objects. -
unitSize: Adjacency threshold.
-
-
Returns: Boolean
trueif adjacency is found with at least one inobjs, elsefalse.
-
Description: Filters a list of objects, returning only those that have been "clicked" (based on an external
clickedfunction). -
Parameters:
-
objs: A list of objects to check.
-
-
Returns: A list of objects for which
clicked(obj) == true.
- Description: Computes the squared distance between two positions.
-
Parameters:
-
pos1: APosition. -
pos2: AnotherPosition.
-
-
Returns:
(delta.x * delta.x + delta.y * delta.y), wheredelta = pos2 - pos1.
-
Description: Computes a "unit vector" (in grid sense) from
obj's origin totarget's origin. If both x and y need to move and are both nonzero, it defaults to moving only along x first (i.e.(sign_x, 0)). -
Parameters:
-
obj: The "source" object. -
target: The "destination" object.
-
-
Returns: A
Position(sign_x, sign_y)representing direction, with a special rule if both are nonzero.
-
Description: Similar to
unitVector, but the target is a bare positionposinstead of an object. -
Parameters:
-
obj: The "source" object (has anorigin). -
pos: A directPosition.
-
-
Returns: A
Position(sign_x, sign_y)using the same logic asunitVector.
-
Description: Returns the maximum of
aandb. -
Parameters:
-
a: A number. -
b: A number.
-
-
Returns: The larger of
aandb.
-
Description: Returns the minimum of
aandb. -
Parameters:
-
a: A number. -
b: A number.
-
-
Returns: The smaller of
aandb.
-
Description: From a list of objects, finds the one with the smallest squared distance to
obj. -
Parameters:
-
obj: The reference object. -
listObjs: A list of objects.
-
-
Returns: The single closest object in
listObjs. ReturnsobjiflistObjsis empty.
-
Description: From a list of positions, finds the position closest (in squared distance) to
obj's origin. -
Parameters:
-
obj: The reference object (with anorigin). -
listPoss: A list ofPositions.
-
-
Returns: The closest
Positionby squared distance. Returnsobj.originiflistPossis empty.
-
Description: Produces a list of renderable elements for
obj. Ifobjis a list, concatenates all of their renderValues. If a single object, returns(.. obj render). -
Parameters:
-
obj: An object or list of objects that can be rendered.
-
-
Returns: A list of rendered elements (or concatenated lists if
objis a list).
-
Description: Checks if any element in
elems1shares the same position as any element inelems2. -
Parameters:
-
elems1: A list of elements (each with aposition). -
elems2: Another list of elements.
-
-
Returns: Boolean
trueif at least one position overlaps, otherwisefalse.
-
Description: Checks if a given
poscoincides with thepositionof any element inelems. -
Parameters:
-
pos: APosition. -
elems: A list of elements (each with aposition).
-
-
Returns: Boolean
trueif any element matchespos, elsefalse.
-
Description: Checks if
posis contained in the list of positionsposs. -
Parameters:
-
pos: APosition. -
poss: A list ofPositionobjects.
-
-
Returns: Boolean
trueifposis inposs, elsefalse.
-
Description: Determines if
obj1andobj2have any intersecting elements by comparing their rendered values. -
Parameters:
-
obj1: An object (or list of objects). -
obj2: Another object (or list of objects).
-
-
Returns: Boolean
trueif their rendered elements overlap.
-
Description: Checks if all rendered elements of
objoccupy free positions (i.e., no collisions). -
Parameters:
-
obj: An object or list of objects to check for collisions.
-
-
Returns: Boolean
trueif none of the positions are blocked, elsefalse.
-
Description: Checks if element
eis in listl. -
Parameters:
-
e: Element to find. -
l: A list.
-
-
Returns: Boolean
trueifeis found inl, otherwisefalse.
-
Description: Checks if all rendered elements of
objare free, ignoring collisions withprev_obj’s rendered elements. -
Parameters:
-
obj: The object to check. -
prev_obj: The object whose elements we ignore in collision checks.
-
-
Returns: Boolean
trueif none of the new positions (beyond those occupied byprev_obj) are blocked, otherwisefalse.
-
Description: Checks if a single position
posis free or is intersecting only withobj. -
Parameters:
-
pos: APosition. -
obj: The object to ignore in collision checks.
-
-
Returns: Boolean
trueifposis free or only occupied byobj.
-
Description: Checks positions in the range
[start, end)along the x-axis (y=0 in the code snippet) to see if they are free, excluding theobj's positions. -
Parameters:
-
start: Start of the x-range. -
end: End of the x-range (exclusive if using typicalrange). -
obj: The object to exclude from collisions.
-
-
Returns: Boolean
trueif all those positions are free except for whereobjis, elsefalse.
-
Description: Moves
objone unit to the left if it is within bounds and free (excluding the object’s own current space). -
Parameters:
-
obj: The object to move.
-
-
Returns: The updated
objif valid, otherwise returnsobjunchanged.
-
Description: Moves
objone unit to the right if it is within bounds and free. -
Parameters:
-
obj: The object to move.
-
-
Returns: The updated
objif valid, otherwiseobj.
-
Description: Moves
objone unit up if it is within bounds and free. -
Parameters:
-
obj: The object to move.
-
-
Returns: The updated
objif valid, otherwiseobj.
-
Description: Moves
objone unit down if it is within bounds and free. -
Parameters:
-
obj: The object to move.
-
-
Returns: The updated
objif valid, otherwiseobj.
-
Description: This is simply an alias set to
moveDownNoCollision. Typically used in contexts where an object moves like a “solid” block. -
Parameters:
-
obj: The object to move down.
-
-
Returns: The updated
objif valid, otherwise the originalobj.
-
Description: Finds the closest "hole" (position) among
holesand attempts to moveobjtoward it usingnextLiquidMoveClosestHole. -
Parameters:
-
obj: The object representing a liquid droplet (or similar). -
holes: A list of positions representing available holes.
-
-
Returns: The moved
objif a hole is found and the path is free, or the originalobjotherwise.
-
Description: Simulates liquid movement:
- If
objcan move down freely and isn’t at the bottom, it moves down. - Else, it scans the next row for "holes" and attempts to move toward the closest one.
- If
-
Parameters:
-
obj: The liquid-like object to move.
-
-
Returns: The updated
objafter applying liquid movement logic.
-
Description: Moves
objtowardclosestHoleby computing a direction viaunitVectorObjPosand verifying free space. -
Parameters:
-
obj: The object representing a liquid droplet. -
closestHole: APositionrepresenting the hole’s location.
-
-
Returns: The updated
objif movement is valid, otherwise the originalobj.
Below are brief descriptions of native functions or constructs:
-
updateObj(obj, propertyName, newValue)
Updates the objectobjby setting itspropertyNametonewValue. Returns a new or updated version ofobj. -
Position(x, y)
Represents a coordinate pair with fieldsxandy. Used extensively for movement and geometry. -
isFreePos(pos)
Checks if the positionposis free (i.e., not occupied or blocked) in the game/grid world. -
isWithinBounds(obj)
Checks ifobj(or possibly its rendered elements) lies within the valid boundaries of the grid/board. -
rotate(obj)
Rotatesobjin some manner (e.g., rotating the shape or positions). Used byrotateNoCollision. -
isList(value)
Tests whethervalueis recognized as a list in this language. -
allObjs()
Returns a list of all objects that exist in the current environment or game state. -
addObj oldList newObjAdd thenewObjto theoldList. -
removeObj oldList oldObjRemove theoldObjfrom theoldList. -
clicked(obj)
Checks ifobjwas clicked (mouse/touch event). Used byobjClicked. -
map(function, list)
Appliesfunctionto each element oflist, returning a new list of results. -
filter(function, list)
Retains only those elements fromlistfor whichfunction(element)returnstrue. -
any(function, list)
Returnstrueiffunction(element)istruefor at least one element inlist, otherwisefalse. -
foldl(function, initValue, list)
Left-fold (reduce): iterates throughlist, applyingfunction(acc, elem), carrying forward an accumulator. -
head(list)
Returns the first element oflist. -
at(list, idx)
Returns theidxth element in the list. -
tail(list)
Returns the last element oflist. -
concat(listOfLists)
Flattens or concatenates a list of lists into a single list. -
range(start, end)
Returns a list of integers fromstartup to but not includingend(based on typical usage in the code). -
print(value)
Outputsvaluefor debugging or logging purposes. -
Logical Operators (used in the code):
-
and,or,not(shown as(! ...))
Standard logical operations for combining boolean expressions. -
if (condition) then (expr1) else (expr2)
Standard conditional expression.
-