mirror of
https://github.com/wu736139669/Monkey-Test.git
synced 2026-08-06 06:14:53 +00:00
20 lines
475 B
JavaScript
20 lines
475 B
JavaScript
var esprima = require("esprima").parse;
|
|
var traverse = require("ast-traverse");
|
|
var ast = esprima("f(!x, y)");
|
|
|
|
var val;
|
|
try {
|
|
traverse(ast, {pre: function(node) {
|
|
if (node.type === "UnaryExpression" && node.operator === "!") {
|
|
val = node.argument;
|
|
throw 0;
|
|
}
|
|
}});
|
|
} catch(e) {
|
|
if (val === undefined) {
|
|
throw e; // re-throw if it wasn't our exception
|
|
}
|
|
}
|
|
|
|
console.dir(val); // { type: 'Identifier', name: 'x' }
|