A "high-level" language for the Gameboy
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
2.9 KiB

  1. // AST factories
  2. {
  3. const attr = (name, args) => ({ name, args })
  4. const stmt = (stmt_type, args) => ({ type: "stmt", stmt_type, args })
  5. const decl = (decl_type, name, attrs, args) => ({ type: "decl", decl_type, name, attrs: attrs || [], args })
  6. const uexpr = (op, arg) => ({ type: "unary", op, arg })
  7. const bexpr = (op, left, right) => ({ type: "binary", op, left, right })
  8. const assocl = (head, tail) => tail.reduce((left, arg) => {
  9. const [op, right] = arg
  10. return bexpr(op, left, right)
  11. }, head)
  12. }
  13. Program = WS @(Decl / Stmt)* WS
  14. // Declarations
  15. Decl = VarDecl
  16. VarDecl = attrs:Attrs? type:Type name:Ident SEMI { return decl("var", name, attrs, { type }) }
  17. // Statements
  18. Stmt = GotoStmt / LabelStmt / AssignStmt
  19. GotoStmt = GOTO name:Ident SEMI { return stmt("goto", { name }) }
  20. LabelStmt = name:Ident COLON { return stmt("label", { name }) }
  21. AssignStmt = name:Ident ASSIGN expr:Expr SEMI { return stmt("assign", { name, expr }) }
  22. // Expressions
  23. Expr = BitOrExpr
  24. BitOrExpr = head:BitXorExpr tail:(op:BitOrOp e:BitXorExpr)* { return assocl(head, tail || []) }
  25. BitOrOp = PIPE { return "bit_or" }
  26. BitXorExpr = head:BitAndExpr tail:(op:BitXorOp e:BitAndExpr)* { return assocl(head, tail || []) }
  27. BitXorOp = CARET { return "bit_xor" }
  28. BitAndExpr = head:ShiftExpr tail:(op:BitAndOp e:ShiftExpr)* { return assocl(head, tail || []) }
  29. BitAndOp = AMPERSAND { return "bit_and" }
  30. ShiftExpr = head:AddExpr op:ShiftOp n:Number { return bexpr(op, head, n) }
  31. / AddExpr
  32. ShiftOp = LTLT { return "shift_left" }
  33. / GTGT { return "shift_right" }
  34. AddExpr = head:UnaryExpr tail:(op:AddOp e:UnaryExpr)* { return assocl(head, tail || []) }
  35. AddOp = PLUS { return "add" }
  36. / MINUS { return "subtract" }
  37. UnaryExpr = op:UnaryOp? e:BaseExpr { return op ? uexpr(op, e) : e }
  38. UnaryOp = TILDE { return "bit_negate" }
  39. / MINUS { return "arith_negate" }
  40. BaseExpr = Ident / Number / LPAREN @Expr RPAREN
  41. // Attributes
  42. Attrs = LATTR @AttrList RATTR
  43. AttrList = h:Attr t:(COMMA @Attr)* { return [h].concat(t) }
  44. / '' { return [] }
  45. Attr = name:Ident args:(LPAREN @AttrArgs RPAREN)? { return attr(name, args || []) }
  46. AttrArgs = h:AttrArg t:(COMMA @AttrArg)* { return [h].concat(t) }
  47. / '' { return [] }
  48. AttrArg = Ident / Number
  49. // Types
  50. Type = PrimitiveType
  51. PrimitiveType = S8 / U8 / U16
  52. // Terminals
  53. Ident = h:[a-zA-Z_] t:[0-9a-zA-Z_]* WS { return h + t.join('') }
  54. Number = digits:[0-9]+ WS { return parseInt(digits.join(''), 10) }
  55. / '0x' digits:[a-fA-F0-9]+ WS { return parseInt(digits.join(''), 16) }
  56. / '0b' digits:[01]+ WS { return parseInt(digits.join(''), 2) }
  57. // Terminal keywords
  58. GOTO = 'goto' WS
  59. S8 = @'s8' WS
  60. U8 = @'u8' WS
  61. U16 = @'u16' WS
  62. // Terminal symbols
  63. AMPERSAND = '&' WS
  64. ASSIGN = '<-' WS
  65. CARET = '^' WS
  66. COLON = ':' WS
  67. COMMA = ',' WS
  68. GTGT = '>>' WS
  69. LATTR = '[[' WS
  70. LPAREN = '(' WS
  71. LTLT = '<<' WS
  72. MINUS = '-' WS
  73. PIPE = '|' WS
  74. PLUS = '+' WS
  75. RATTR = ']]' WS
  76. RPAREN = ')' WS
  77. SEMI = ';' WS
  78. TILDE = '~' WS
  79. // Misc
  80. WS = [ \t\r\n]*