Hey I have this script:
I’m trying to run it on [url]http://www.w3schools.com/js/tryit.asp?filename=tryjs_browser
[CODE]
<script type=”text/javascript”>
function matchParen(str){
num_rp = null;
var count;
for(count=0; num_rp!==0; count++){
if(str[count]=='(‘) ++num_rp;
else if(str[count]==’)’) –num_rp;
}
return count;
}
/*filler stuff for test purposes*/
function Dodec(f){ return true; }
Boolean.prototype.cond = function(q){return !this || q}
k=’a’, x=’b’, y=’d’, n=’k’, r=’k’;
/*filler end*/
ex = “Dodec(k)→((Dodec(x)→Dodec(y))→(Dodec(n)→Dodec(r)))”;
while(ex.match(‘→’)){
m = ex.match(/→.+/).toString();
pos = matchParen(m)+ex.search(‘→’)+6;
ex = ex.replace(‘→’,'<b>.cond(</b>’);
ex = ex.substring(0,pos)+'<b>)</b>’+ex.substring(pos);
document.write(ex+”<BR />”);
}
ex = ex.replace(/<.?b>/g,”);
</script>
Also, if you know how to optimize the code that would really be helpful.
Additionally, any other methods on creating a new “operator” would be insightful. I’m just using OO design for now, but I’m trying to create a conditional operator ‘→’ so that:
[CODE]
P Q P→Q (Algorithm: !P || Q)
T T T
T F F
F T T
F F T