|
@ -2,27 +2,23 @@ |
|
|
{ |
|
|
{ |
|
|
const attr = (name, args) => ({ name, args }) |
|
|
const attr = (name, args) => ({ name, args }) |
|
|
const stmt = (stmt_type, args) => ({ type: "stmt", stmt_type, args }) |
|
|
const stmt = (stmt_type, args) => ({ type: "stmt", stmt_type, args }) |
|
|
const decl = (decl_type, args) => ({ type: "decl", decl_type, args }) |
|
|
|
|
|
|
|
|
const decl = (decl_type, name, attrs, args) => ({ type: "decl", decl_type, name, attrs: attrs || [], args }) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
Program = WS @(Decl / Stmt)* WS |
|
|
Program = WS @(Decl / Stmt)* WS |
|
|
|
|
|
|
|
|
// Declarations |
|
|
// Declarations |
|
|
Decl = VarDecl |
|
|
Decl = VarDecl |
|
|
VarDecl = Attrs? type:Type name:Ident SEMI { return decl("var", { type, name }) } |
|
|
|
|
|
|
|
|
VarDecl = attrs:Attrs? type:Type name:Ident SEMI { return decl("var", name, attrs, { type }) } |
|
|
|
|
|
|
|
|
// Statements |
|
|
// Statements |
|
|
Stmt = @AssignStmt SEMI |
|
|
Stmt = @AssignStmt SEMI |
|
|
AssignStmt = name:Ident ASSIGN expr:Expr { return stmt("assign", { name, expr }) } |
|
|
AssignStmt = name:Ident ASSIGN expr:Expr { return stmt("assign", { name, expr }) } |
|
|
|
|
|
|
|
|
// Expr |
|
|
|
|
|
|
|
|
// Expressions |
|
|
Expr = BaseExpr |
|
|
Expr = BaseExpr |
|
|
BaseExpr = Number |
|
|
BaseExpr = Number |
|
|
|
|
|
|
|
|
// Primitive types |
|
|
|
|
|
Type = PrimitiveType |
|
|
|
|
|
PrimitiveType = S8 / U8 / U16 |
|
|
|
|
|
|
|
|
|
|
|
// Attributes |
|
|
// Attributes |
|
|
Attrs = LATTR @AttrList RATTR |
|
|
Attrs = LATTR @AttrList RATTR |
|
|
AttrList = h:Attr t:(COMMA @Attr)* { return [h].concat(t) } |
|
|
AttrList = h:Attr t:(COMMA @Attr)* { return [h].concat(t) } |
|
@ -32,6 +28,10 @@ AttrArgs = h:AttrArg t:(COMMA @AttrArg)* { return [h].concat(t) } |
|
|
/ '' { return [] } |
|
|
/ '' { return [] } |
|
|
AttrArg = Ident / Number |
|
|
AttrArg = Ident / Number |
|
|
|
|
|
|
|
|
|
|
|
// Types |
|
|
|
|
|
Type = PrimitiveType |
|
|
|
|
|
PrimitiveType = S8 / U8 / U16 |
|
|
|
|
|
|
|
|
// Terminals |
|
|
// Terminals |
|
|
Ident = h:[a-zA-Z_] t:[0-9a-zA-Z_]* WS { return h + t.join('') } |
|
|
Ident = h:[a-zA-Z_] t:[0-9a-zA-Z_]* WS { return h + t.join('') } |
|
|
Number = digits:[0-9]+ WS { return parseInt(digits.join(''), 10) } |
|
|
Number = digits:[0-9]+ WS { return parseInt(digits.join(''), 10) } |
|
|