C parser in pure JavaScript
TODO put this on npm
var cparse = require('./cparse.js');
var ast = cparse("void main() { int answer = 6 * 7; }");
console.log(JSON.stringify(ast, undefined, 4));
<script src="cparse.js"></script>
<script>
var ast = cparse("void main() { int answer = 6 * 7; }");
console.log(JSON.stringify(ast, undefined, 4));
</script>
Parsing code:
int answer = 6 * 7;
outputs following AST (the positions of each AST entry have been removed to reduce size):
[
{
"type": "GlobalVariableDeclaration",
"defType": {
"type": "Type",
"modifier": [],
"name": "int"
},
"name": "answer",
"value": {
"type": "BinaryExpression",
"operator": "*",
"left": {
"type": "Literal",
"value": 6,
},
"right": {
"type": "Literal",
"value": 7,
},
}
}
]
"THE BEER-WARE LICENSE":
Jakob Löw <[email protected]> wrote this code. As long as you retain this notice you
can do whatever you want with this stuff. If we meet some day, and you think
this stuff is worth it, you can buy me a beer in return.