0.2.2 • Published 8 years ago

hrm-parser v0.2.2

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

hrm-parser

A parser for Human Resouce Machine programs

Screenshot

Human Resource Machine is a puzzle game. In each level, your boss gives you a job. Automate it by programming your little office worker! If you succeed, you'll be promoted up to the next level for another year of work in the vast office building. Congratulations!

NPM

npm install hrm-parser

Usage

Requires node 4.x - uses ES6

const parser = require( 'hrm-parser' )

//source is a string in the Human Resource Machine format
const source = getSource()

const parsed = parser( source )

It will output something like:

[ 
  [ "BUMPUP", 24 ],
  [ "INBOX" ],
  [ "JUMPZ", 9 ],
  [ "COPYTO", "[24]" ],
  [ "JUMP", 0 ],
  [ "COPYFROM", "[19]" ],
  [ "OUTBOX" ],
  [ "COPYFROM", "[24]" ],
  [ "COPYTO", "[19]" ],
  [ "BUMPDN", 24 ],
  [ "JUMPZ", 0 ],
  [ "COPYTO", 23 ],
  [ "COPYFROM", 23 ],
  [ "COPYTO", 19 ],
  [ "BUMPDN", 23 ],
  [ "JUMPZ", 5 ],
  [ "COPYFROM", "[23]" ],
  [ "SUB", "[19]" ],
  [ "JUMPN", 12 ],
  [ "JUMP", 14 ] 
]

If you want to handle errors yourself, or get metadata about comments, labels etc. you can use the async version:

parser( source, ( err, program, meta ) => {
  //...
})

program will be in the same format as above.

meta contains the following, all references to the program and line numbers are the program array above:

{
  //source comments - the key in the object is the line nubmer in the program
  //above which these comments appear
  "comments": {    
    "0": [
      "-- HUMAN RESOURCE MACHINE PROGRAM --"
    ]
  },
  
  //information about jumps - the key is the original label name without the 
  //trailing colon, to is the program line above which the jump's arrow points,
  //from is the program line where the jump originated
  "jumps": {
    "a": {
      "to": 5,
      "from": 14
    },
    "b": {
      "to": 5,
      "from": 11
    }
  },
  
  //information about images embedded in the program
  "images": {
  
    //the key in the comments object is the line number above which these 
    //comment images appear
    "comments": {
      "5": [
        "eJyrZmBgeCCVoGkkclTntoC3YQL/bJsE/sm+MwUfRUwUKU07LHm0UF/xeRGTmn2ujcb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lZmxNyFP7mBC3s9Df7LEjlzz3364S5JhFIyCQQYAmMqZXQ;"
      ]
    },
    
    //the key in the labels object is the floor tile which this label is 
    //assigned to
    "labels": {
      "16": "eJxTYGBgKBE2q5so0rQFyGQ4LcZw6rDkyQvzpE9eKFTYe1xf8fMBkHiO2JKtp8VmH+sUVzoN4v8Q/B4AosW1+ivydHdmqBieTdhrvCZ+q1lI6gSLhDxN69c9HdY712ZbNW25ZO6z+5bJ3uN7je+d+GN/8sJUp5MXNjm3nRN0gZg1CkbBKBg4AACGCzVc;",
      "17": "eJyTYmBguCz8KOKFeIB3iiSP5wOpVGcpWQaHzfLOTpnKhT42GlqlkZoBE3Zo7j0OVMrgo/S6x1xuRZe7xIqu2wIBExbxzZ/9hHfNEme+0tW3Bew38Yot2XpDdsnWPsXEzQfUpq+J1Hw8N1KzbhbDKBgFo2BQAgCttie8;"
    }
  }
}  

Similar projects

hrm-grammar uses a proper expression grammar and produces an AST

0.2.2

8 years ago

0.2.1

8 years ago

0.2.0

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

9 years ago

0.1.2

9 years ago

0.1.1

9 years ago

0.1.0

9 years ago