Created
June 15, 2012 06:53
-
-
Save kuboon/2935085 to your computer and use it in GitHub Desktop.
Forth for control jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<html> | |
<head> | |
<title>jQuery forth test</title> | |
<link rel="stylesheet" href="http://code.jquery.com/ui/1.8.21/themes/base/jquery-ui.css" type="text/css" media="all" /> | |
<link rel="stylesheet" href="http://static.jquery.com/ui/css/demo-docs-theme/ui.theme.css" type="text/css" media="all" /> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> | |
<script src="http://code.jquery.com/ui/1.8.21/jquery-ui.min.js" type="text/javascript"></script> | |
<script src="http://jquery-ui.googlecode.com/svn/tags/latest/ui/minified/i18n/jquery-ui-i18n.min.js" type="text/javascript"></script> | |
</head> | |
<body> | |
<textarea style="width:100%; height: 20em;" id="src">: dialog $:1 .dialog ; "</div>" 1 2 + 3 * "<div>" + + dialog</textarea> | |
<input type="button" value="run" onclick="run();return false;" /> | |
<textarea style="width:100%; height: 20em;" id="stdout"></textarea> | |
<script> | |
$(function(){ | |
}); | |
var words={}; | |
var stack=[]; | |
function run(){ | |
var src = $("#src").val(), | |
$stdout = $("#stdout").html(), | |
arr = src.split(" "), | |
pp = 0; | |
while(true){ | |
if(arr.length<=pp)break; | |
var word = arr[pp++]; | |
if(word==":"){ | |
var name = arr[pp++]; | |
var func_arr = []; | |
while( word = arr[pp++] ){ | |
if(word == ";")break; | |
func_arr.push(word); | |
} | |
words[name]=func_arr; | |
}else if(word=="."){ | |
$stdout.append(" ").append(stack.pop()); | |
}else if(word=="+"){ | |
stack.push(stack.pop()+stack.pop()); | |
}else if(word=="-"){ | |
stack.push(stack.pop()-stack.pop()); | |
}else if(word=="*"){ | |
stack.push(stack.pop()*stack.pop()); | |
}else if(word=="/"){ | |
stack.push(stack.pop()/stack.pop()); | |
}else if(word.match(/^\d+$/)){ | |
stack.push(parseInt(word)); | |
}else if(word.match(/^"(.+)"$/)){ | |
stack.push(RegExp.$1); | |
}else if(words.hasOwnProperty(word)){ | |
arr = words[word].concat(arr.slice(pp)); | |
pp = 0; | |
}else if(word.match(/^(.+):(\d+)$/)){ | |
stack.push(call_function(RegExp.$1, RegExp.$2)); | |
}else{ | |
stack.push(call_function(word, 0)); | |
} | |
} | |
} | |
function call_function(func_name, num_args){ | |
var args = [], _this=window; | |
while(num_args--){ | |
args.push(stack.pop()); | |
} | |
if(func_name[0]=="."){ | |
_this = stack.pop(); | |
func_name = func_name.slice(1); | |
} | |
var func = _this[func_name]; | |
return func.apply(_this, args); | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment