Skip to content

Instantly share code, notes, and snippets.

@pasaran
Created November 6, 2012 12:40
Show Gist options
  • Save pasaran/4024392 to your computer and use it in GitHub Desktop.
Save pasaran/4024392 to your computer and use it in GitHub Desktop.
Работа с ast
function code(ast) {
switch (ast.id) {
case 'or':
case 'and':
case 'eq':
case 'rel':
case 'add':
case 'mul':
return code(ast.left) + ast.op + code(ast.right);
case 'not':
case 'minus':
return ast.op + '(' + code(ast.expr) + ')';
case 'subexpr':
return '(' + code(ast.expr) + ')';
case 'number':
return ast.value;
case 'var_':
return 'vars["' + ast.name + '"]';
}
}
function type(ast) {
switch (ast.id) {
case 'or':
case 'and':
case 'eq':
case 'rel':
case 'not':
return 'boolean';
case 'add':
case 'mul':
case 'minus':
case 'number':
case 'var_':
return 'scalar';
case 'subexpr':
return type(ast.expr);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment