|
@ -1,10 +1,30 @@ |
|
|
// AST factories |
|
|
// AST factories |
|
|
{ |
|
|
{ |
|
|
const attr = (name, args) => ({ name, args }) |
|
|
const attr = (name, args) => ({ name, args }) |
|
|
|
|
|
const stmt = (stmt_type, args) => ({ type: "stmt", stmt_type, args }) |
|
|
|
|
|
const decl = (decl_type, args) => ({ type: "decl", decl_type, args }) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Program = WS @(Decl / Stmt)* WS |
|
|
|
|
|
|
|
|
|
|
|
// Declarations |
|
|
|
|
|
Decl = VarDecl |
|
|
|
|
|
VarDecl = Attrs? type:Type name:Ident SEMI { return decl("var", { type, name }) } |
|
|
|
|
|
|
|
|
|
|
|
// Statements |
|
|
|
|
|
Stmt = @AssignStmt SEMI |
|
|
|
|
|
AssignStmt = name:Ident ASSIGN expr:Expr { return stmt("assign", { name, expr }) } |
|
|
|
|
|
|
|
|
|
|
|
// Expr |
|
|
|
|
|
Expr = BaseExpr |
|
|
|
|
|
BaseExpr = Number |
|
|
|
|
|
|
|
|
|
|
|
// Primitive types |
|
|
|
|
|
Type = PrimitiveType |
|
|
|
|
|
PrimitiveType = S8 / U8 / U16 |
|
|
|
|
|
|
|
|
// Attributes |
|
|
// Attributes |
|
|
Attrs = LATTR l:AttrList RATTR { return l } |
|
|
|
|
|
|
|
|
Attrs = LATTR @AttrList RATTR |
|
|
AttrList = h:Attr t:(COMMA @Attr)* { return [h].concat(t) } |
|
|
AttrList = h:Attr t:(COMMA @Attr)* { return [h].concat(t) } |
|
|
/ '' { return [] } |
|
|
/ '' { return [] } |
|
|
Attr = name:Ident args:(LPAREN @AttrArgs RPAREN)? { return attr(name, args || []) } |
|
|
Attr = name:Ident args:(LPAREN @AttrArgs RPAREN)? { return attr(name, args || []) } |
|
@ -15,8 +35,17 @@ AttrArg = Ident / Number |
|
|
// 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) } |
|
|
|
|
|
/ '0x' digits:[a-fA-F0-9]+ WS { return parseInt(digits.join(''), 16) } |
|
|
|
|
|
/ '0b' digits:[01]+ WS { return parseInt(digits.join(''), 2) } |
|
|
|
|
|
|
|
|
|
|
|
// Terminal keywords |
|
|
|
|
|
S8 = @'s8' WS |
|
|
|
|
|
U8 = @'u8' WS |
|
|
|
|
|
U16 = @'u16' WS |
|
|
|
|
|
|
|
|
// Terminal symbols |
|
|
// Terminal symbols |
|
|
|
|
|
SEMI = ';' WS |
|
|
|
|
|
ASSIGN = '<-' WS |
|
|
LPAREN = '(' WS |
|
|
LPAREN = '(' WS |
|
|
RPAREN = ')' WS |
|
|
RPAREN = ')' WS |
|
|
COMMA = ',' WS |
|
|
COMMA = ',' WS |
|
|