|
|
@ -3,6 +3,8 @@ |
|
|
|
const attr = (name, args) => ({ name, args }) |
|
|
|
const stmt = (stmt_type, args) => ({ type: "stmt", stmt_type, args }) |
|
|
|
const decl = (decl_type, name, attrs, args) => ({ type: "decl", decl_type, name, attrs: attrs || [], args }) |
|
|
|
const uexpr = (op, arg) => ({ type: "unary", op, arg }) |
|
|
|
const bexpr = (op, arg1, arg2) => ({ type: "binary", op, arg1, arg2 }) |
|
|
|
} |
|
|
|
|
|
|
|
Program = WS @(Decl / Stmt)* WS |
|
|
@ -16,8 +18,13 @@ Stmt = @AssignStmt SEMI |
|
|
|
AssignStmt = name:Ident ASSIGN expr:Expr { return stmt("assign", { name, expr }) } |
|
|
|
|
|
|
|
// Expressions |
|
|
|
Expr = BaseExpr |
|
|
|
BaseExpr = Number |
|
|
|
Expr = UnaryExpr |
|
|
|
|
|
|
|
UnaryExpr = op:UnaryOp? e:BaseExpr { return op ? uexpr(op, e) : e } |
|
|
|
UnaryOp = TILDE { return "bit_negate" } |
|
|
|
/ MINUS { return "arith_negate" } |
|
|
|
|
|
|
|
BaseExpr = Ident / Number / LPAREN @Expr RPAREN |
|
|
|
|
|
|
|
// Attributes |
|
|
|
Attrs = LATTR @AttrList RATTR |
|
|
@ -44,6 +51,8 @@ U8 = @'u8' WS |
|
|
|
U16 = @'u16' WS |
|
|
|
|
|
|
|
// Terminal symbols |
|
|
|
TILDE = '~' WS |
|
|
|
MINUS = '-' WS |
|
|
|
SEMI = ';' WS |
|
|
|
ASSIGN = '<-' WS |
|
|
|
LPAREN = '(' WS |
|
|
|