Skip to content

Instantly share code, notes, and snippets.

@tuchida
tuchida / visitor.js
Created July 22, 2013 09:36
Nashorn Parser
function parse(str) {
var ir = Packages.jdk.nashorn.internal.ir;
var runtime = Packages.jdk.nashorn.internal.runtime;
var parser = Packages.jdk.nashorn.internal.parser;
var context = runtime.Context.getContext();
var source = new runtime.Source('test', str);
var node = new parser.Parser(context.getEnv(), source, new runtime.Context.ThrowErrorManager(), true).parse();
var lexContext = new ir.LexicalContext();
@tuchida
tuchida / goto.js
Last active December 20, 2015 01:58
Rhino GOTO
function traverseFn(node, fn) {
var parents = [];
(function f(node) {
fn(node, parents);
parents.push(node);
for (var c in Iterator(node)) {
f(c);
}
parents.pop();
})(node);
@tuchida
tuchida / brainfuck.js
Last active December 14, 2015 19:39 — forked from tuchida/brainfuck.js
macro bf {
case : { $body ... } => {
(function() {
var env = {
p: 0,
b: [],
buf: new Buffer(1)
};
bf_tokenizer[env]($body ...);
})();
@tuchida
tuchida / brainfuck.js
Created March 11, 2013 14:49
Brainfuck in sweet.js
macro bf {
case ($body ...) => {
eval('var p = 0, b = [], buf = new Buffer(1);');
_bf($body ...);
}
}
macro _bf {
case (>) => {
eval('p++;');
}
@tuchida
tuchida / asmfn.js
Created February 20, 2013 04:19
macro(sweet.js) for asm.js
macro asmfn {
case $name:ident ($params ...) { $body ... } => {
function $name(asmfn_params($params ...)) {
'use asm';
asmfn_convparams($params ...)
$body ...
}
}
// case $name:ident ($params ...): $ret_type { $body ... } => {
// macro ret {
@tuchida
tuchida / async.js
Created October 30, 2012 15:36
async/await in sweet.js
macro async {
case :{ $rest } => {
$rest
}
case :{ $head $rest ... } => {
$head
async: { $rest ... }
}
case :{ var $x:ident = await($yield:ident, $y ...); $rest ... } => {
(function($yield) {
macro forOf {
case (var $name:ident of $array) $body => {
(function(array) {
for (var i = 0, len = array.length; i < len; i++) {
var $name = array[i];
$body
}
})($array);
}
}