Skip to content

Instantly share code, notes, and snippets.

@kosamari kosamari/parser.js
Last active Oct 11, 2016

Embed
What would you like to do?
parser function for sbn compiler
function parser (tokens) {
var AST = {
type: 'Drawing',
body: []
}
// extract a token at a time as current_token. Loop until we are out of tokens.
while (tokens.length > 0){
var current_token = tokens.shift()
// Since number token does not do anything by it self, we only analyze syntax when we find a word.
if (current_token.type === 'word') {
switch (current_token.value) {
case 'Paper' :
var expression = {
type: 'CallExpression',
name: 'Paper',
arguments: []
}
// if current token is CallExpression of type Paper, next token should be color argument
var argument = tokens.shift()
if(argument.type === 'number') {
expression.arguments.push({ // add argument information to expression object
type: 'NumberLiteral',
value: argument.value
})
AST.body.push(expression) // push the expression object to body of our AST
} else {
throw 'Paper command must be followed by a number.'
}
break
case 'Pen' :
...
case 'Line':
...
}
}
}
return AST
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.