implement stack-based interpreter to improve performance
See original GitHub issuecurrently sval is a tree-walking interpreter and has a ordinary performance. compile the code into some kinds of intermediate representation based on stack to improve performance.
I have a demo as followed. it may be difficult and take some time to migrate.
const { parse } = require('acorn')
const { simple } = require('acorn-walk')
const ast = parse('1 + 8 * 2 / 2 + 1')
const stack = []
simple(ast, {
Literal(node) {
stack.push(node.value)
},
BinaryExpression(node) {
const right = stack.pop()
const left = stack.pop()
const binaryOps = {
'+': () => left + right,
'-': () => left - right,
'*': () => left * right,
'/': () => left / right
}
const handler = binaryOps[node.operator]
if (handler) {
stack.push(handler())
} else {
throw new SyntaxError(`Unexpected token ${node.operator}`)
}
}
})
console.log(stack[0])
Issue Analytics
- State:
- Created 4 years ago
- Reactions:7
- Comments:13 (10 by maintainers)
Top Results From Across the Web
Cheap interpreter, part 6: faster stack machines
The easiest way to improve the performance of the interpreter we defined in part 4 is to pay attention to the data structures...
Read more >RegCPython: A Register-based Python Interpreter for Better ...
The stack architecture is considered simpler and smaller, while the register architecture is considered more efficient but more complex. CPython ...
Read more >How I developed a faster Ruby interpreter - Red Hat Developer
To implement a decent MIR-based JIT compiler, I decided earlier to develop some features with a design specific to CRuby. I use dynamically ......
Read more >Performance comparison between two virtual machine ...
machine implementing in a high level language. We observe that for a stack based machine the performance limit of the interpreter is likely...
Read more >Stack Caching for Interpreters
The dynamic method is based on having one ... of Ccode are compilation speed and exibility ... whether to use a stack or...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I will try to complete in August
fengari did a great job of running lua over js. mark for reference - Fengari