Skip to content

Instantly share code, notes, and snippets.

@yutannihilation
Last active August 16, 2024 03:39
Show Gist options
  • Save yutannihilation/eb3f3db5cd4a2f388bffd5bf856985c3 to your computer and use it in GitHub Desktop.
Save yutannihilation/eb3f3db5cd4a2f388bffd5bf856985c3 to your computer and use it in GitHub Desktop.

Syntax

EOL : '\n' | ';'
Program :
   Statement*

Statements

Note: some statement can be placed only on the global level, but probably it's simple to accept on parsing and then reject?

Statement :
   EOL
 | LetStatement
 | ExpressionStatement
 | FunctionDeclaration
 | MacroDeclaration

Global-only statement

FunctionDeclaration :
   (TBD)
MacroDeclaration :
   (TBD)

General statement

LetStatement :
   'let' LeftVariable ( ':' TypeSpec )? '=' Expression EOL
ExpressionStatement :
   Expression EOL

Expression

Expression :
   LiteralExpression
 | VariableExpression
 | OperatorExpression
 | CallExpression
 | PipeExpression
 | LambdaExpression
 | MacroExpandExpression
 | TupleExpression
 | TupleIndexExpression
 | ReturnExpression
 | GroupedExpression
 | BlockExpression
 | IfExpression

Literal (e.g. 1.0 and true)

LiteralExpression :
   FloatLiteral
 | IntegerLiteral
 | BooleanLiteral
 | StringLiteral

Variable (e.g. x)

VariableExpression :
   Identifier
 | "self"
 | "now"

Operator

OperatorExpression :
   UnaryOperatorExpression
 | BinaryOperatorExpression
 | AssignExpression

Unary op (e.g. -1.0)

UnaryOperatorExpression :
   '-' Expression
 | '!' Expression

Binary op (e.g. x + y)

BinaryOperatorExpression :
   Expression '+'  Expression
 | Expression '-'  Expression
 | Expression '*'  Expression
 | Expression '/'  Expression
 | Expression '%'  Expression
 | Expression '^'  Expression
 | Expression '==' Expression
 | Expression '!=' Expression
 | Expression '<'  Expression
 | Expression '<=' Expression
 | Expression '>'  Expression
 | Expression '>=' Expression
 | Expression '||' Expression
 | Expression '&&' Expression

Assignment (e.g. x = 1.0)

TODO: should this be treated as statement?

AssignExpression :
   Identifier '=' Expression

Call (e.g. f())

CallExpression :
   Identifier '(' CallParams? ')' ( '@' DelaySpec )?
CallParams :
   Expression ( ',' Expression )* ','?
DelaySpec :
   Expression

Pipe (e.g. x |> f())

PipeExpression :
   Expression '|>' CallExpression

Lambda (e.g. |x| x + 1)

LambdaExpression :
   '|' LambdaParams '|' Expression
LambdaParams :
   LambdaArg ( ',' LambdaArg )* ','?
LambdaArg :
   Identifier ( ':' TypeSpec )

Macro expansion (e.g. foo!(x, y))

MacroExpandExpression :
   (TBD)

Tuple (e.g. (1.0, 2.0))

Note: single tuple doesn't allow trailing comma to distingish it from GroupedExpression

TupleExpression :
   '(' Expression ',' ')'
 | '(' Expression ( ',' Expression )+ ','? ')'

Tuple index (e.g. x.1)

TupleIndexExpression :
   (TBD)

Return (e.g. return)

ReturnExpression :
   'return' Expression

Grouped expression (e.g. (1 + 1 + 1))

GroupedExpression :
   '(' Expression ')'

Block expression (e.g. {x = 1; x * 2})

BlockExpression :
   '{' Statement* Expression? '}'

Note: Expression is probably needed to handle when there's no linebreak like the following case. But, this can be more nicer.

{ 10 }

{ let x = 10
  x * 20 }

If (e.g. if (cond) { 1.0 } else { 0.0 })

IfExpression :
   'if' '(' Expression ')' Expression ( 'else' Expression )?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment