|
|
@ -4,7 +4,12 @@ |
|
|
|
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 }) |
|
|
|
const bexpr = (op, left, right) => ({ type: "binary", op, left, right }) |
|
|
|
|
|
|
|
const assocl = (head, tail) => tail.reduce((left, arg) => { |
|
|
|
const [op, right] = arg |
|
|
|
return bexpr(op, left, right) |
|
|
|
}, head) |
|
|
|
} |
|
|
|
|
|
|
|
Program = WS @(Decl / Stmt)* WS |
|
|
@ -18,7 +23,10 @@ Stmt = @AssignStmt SEMI |
|
|
|
AssignStmt = name:Ident ASSIGN expr:Expr { return stmt("assign", { name, expr }) } |
|
|
|
|
|
|
|
// Expressions |
|
|
|
Expr = UnaryExpr |
|
|
|
Expr = AddExpr |
|
|
|
|
|
|
|
AddExpr = head:UnaryExpr tail:(op:AddOp e:UnaryExpr)* { return assocl(head, tail || []) } |
|
|
|
AddOp = PLUS { return "add" } |
|
|
|
|
|
|
|
UnaryExpr = op:UnaryOp? e:BaseExpr { return op ? uexpr(op, e) : e } |
|
|
|
UnaryOp = TILDE { return "bit_negate" } |
|
|
@ -51,6 +59,7 @@ U8 = @'u8' WS |
|
|
|
U16 = @'u16' WS |
|
|
|
|
|
|
|
// Terminal symbols |
|
|
|
PLUS = '+' WS |
|
|
|
TILDE = '~' WS |
|
|
|
MINUS = '-' WS |
|
|
|
SEMI = ';' WS |
|
|
|