Blogs

  • Browse Blogs
  • My Blog
  • My Updates

Tags Help

  • View as cloud  | list

Similar Entries

photo

I still have one con...

Blog:  Yellow is the...
Tim Tripcony
Updated 
No Ratings 0     No Comments 0
photo

Blackberry JavaScrip...

Blog:  .Domino Frame...
Peter Presnell
Updated 
No Ratings 0     No Comments 0
photo

Every time you use w...

Blog:  Yellow is the...
Tim Tripcony
Updated 
No Ratings 0     No Comments 0
photo

JSFactory

Blog:  Yellow is the...
Tim Tripcony
Updated 
No Ratings 0     No Comments 0
photo

SnTT: Ajax history m...

Blog:  Yellow is the...
Tim Tripcony
Updated 
No Ratings 0     No Comments 0

Dogear Bookmarks

Archive

Links

  • URL of site's RSS feed State Machine Acitivity

State Machine

Blog Authors:  Xavier Mary  

Turing Machine sample in javascript

Xavier Mary  |    |  Tags:  javascript state machine  |  Comments (0)

Based on a work from Wolfram, a simple implementation of an 2,3 universal turing machine ...<br>
Here are the first 100 steps (cols) for this machine (use arrow to scroll)<br>

<script language=javascript>

var navigateur = {
 globalVar : {
 Mozilla : 0,
 moz :  0,
 ie4:  0,
 ns4:  0,
 op4:  0,
 layerRef: '',
 styleRef: '',
 posHDRx: '',
 posTRLx: '',
 posHDRy: '',
 posTRLy: '',
 msg:  '',
 accumulative_height: 0
 },
 Mozilla : 0,
 moz :  0,
 ie4:  0,
 ns4:  0,
 op4:  0,
 heightCheck : 0,
 widthCheck : 0,
 bF : 0,
 argumentArray : '',
 getSearchItem: function( itm) { var o='';
  if(this.argumentArray != '') {
   if(this.argumentArray[itm] != 'undefined' && this.argumentArray[itm] != '')
    return this.argumentArray[itm];
   return o;
  }
  var os1=document.location.search.toString();
  var toFind = ''+itm+'=';
  var r = os1.split('?');
  if(r.length>1)
   var r = r[1].split('&');
  else {
   this.argumentArray = {};
   return o;
  }
  this.argumentArray = {};
  for(var i=0; i< r.length; i++) {
   var r0 = r[i].split('=');
   if(r0.length==1)
    this.argumentArray[r0[0]] = '';
   else
    this.argumentArray[r0[0]] = r0[1];
   if(this.bF==1)
    alert(r0[0]+'\n'+r0[1]+'\n'+stringify(this.argumentArray))
  }
  if(this.argumentArray[itm] != 'undefined' && this.argumentArray[itm] != '')
   o= this.argumentArray[itm];
  if(this.bF==0) this.bF =0; else
  alert('[navigateur.js].getSearchItem!itm='+itm+'\n'+r.length+'\n'+stringify(this.argumentArray)+'\n'+(this.bF=0)+'\no='+o)
/*
  var i = os1.indexOf("extend";);
  if(i != -1) {
   var o1 = os1.substr(i+toFind.length,os1.length)
   i = o1.indexOf('&');
   if(i != -1) {
    o = o1.substr(0,i)
   }
  }
  //alert(os1+'\n toFind:'+toFind+'\no='+o+'\ni='+i);
*/
  return o;
 },
 argumentObject : '',
 makeArgumentObject : function(arg) {
 if(!arg || arg == 'undefined')
  var arg=document.location.search;
 var o='';
 var a='';
  o = arg.split('?'); // seul la suite du 1er ? est prise
  if(o.length>1) {
   o = o[1].split('&');
   if(o.length) {
    a = new Object();
    for(var i=0; i<o.length; i++) {
     var oo = new Array();
     if(o[i].length>1) {
      oo = o[i].split('=');
      if(oo.length>1) {
       a[oo[0]] = oo[1];
      }
     }
    }
   }
  }
  this.argumentObject = a;
  return a;
 },
 getArghh : function(arghh) {var o='';
  var i = document.location.search.indexOf(arghh+'=');
  if(i == -1) return '';
  var o2=document.location.search.substr(i+arghh.length+1,document.location.search.length);
  var j = o2.indexOf('&');
  if(j == -1)
 j= o2.length;
  o=o2.substr(0,j);
  //alert(o2+'\n'+arghh+"="+o)
  return o;
 },
 getDisplayWidth: function() {
 var w=0;
 if(this.globalVar.ns4 || (window && window.innerWidth))
  w=window.innerWidth;
 else // ie4
  w=document.body.offsetWidth;
 if(this.globalVar.ie4) w-=16;
 else if(this.globalVar.moz) w-=14;
 return w;
 },
 getDisplayHeight: function() {
 var h=0;
 if(this.globalVar.ns4 || (window && window.innerWidth))
  h=window.innerHeight;
 else // ie4
  h=document.body.offsetHeight;
 if(this.globalVar.ie4) h-=16;
 else if(this.globalVar.moz) h-=11;
 return h;
 },
 afficheCalque: function(calque) { eval(this.globalVar.layerRef + calque + this.globalVar.styleRef + '.visibility = "visible"'); },
 cacheCalque: function(calque) { eval(this.globalVar.layerRef + calque + this.globalVar.styleRef + '.visibility = "hidden"'); },
 moveXY: function(layer,x,y) { eval(this.globalVar.layerRef+layer+this.globalVar.posHDRx+x+this.globalVar.posTRLx+this.globalVar.layerRef+layer+this.globalVar.posHDRy+y+this.globalVar.posTRLy); },
 resizeXY: function(layer,x,y) {
  //moz
  if(this.globalVar.ie4 || this.globalVar.moz) {
   document.getElementById(layer).style.width=x;
   document.getElementById(layer).style.height=y;
  }
  else {
  //ns4
   document.layers[layer].resizeTo(x,y);
  }
 },

 getOffsetTop :function(el, offsetParent) {
 var offsetTop = 0;
 if (!offsetParent) offsetParent = null;
 do {
  offsetTop += el.offsetTop
 }
 while ((el = el.offsetParent) != offsetParent);
 return offsetTop;
 },
 getOffsetLeft :function(el, offsetParent) {
 var offsetLeft = 0;
 if (!offsetParent) offsetParent = null;
 do {
  offsetLeft += el.offsetLeft
 }
 while ((el = el.offsetParent) != offsetParent);
 return offsetLeft;
 },
 setRelativePosition :function(element, offsetElement, offsetTop, offsetLeft) {
 var offsetParent = null;
 try { offsetParent = element.offsetParent; } catch(e) {}

 var top = this.getOffsetTop(offsetElement, offsetParent) + offsetTop;
 if (top < 100) top = 100;

 var left = this.getOffsetLeft(offsetElement, offsetParent) + offsetLeft;
 if (left < 0) left = 0;

 element.style.top = top + 'px';
 element.style.left = left + 'px';
 },
 getDivXY : function(layer) { var x=0, y=0;
  var arr = [];
  if (this.globalVar.ie4) {
   _layer = document.all[layer];
   x=document.all[layer].style.left;
   y=document.all[layer].style.top;
  }
  else {
   _layer = document.getElementById(layer);
   x=document.getElementById(layer).style.left;
   y=document.getElementById(layer).style.top;
  }
  y = this.getOffsetTop(_layer)
  x = this.getOffsetLeft(_layer)
  arr[0]=x;
  arr[1]=y;
  //alert(layer + ', ' +x + ',' + y +', '+ arr)
  return arr;
 },

 changeStyle: function(layer,label,style) {
 var evalEXPR="";
  //moz
  if(this.globalVar.ie4 || this.globalVar.moz) {
   evalEXPR='document.getElementById(\''+layer+'\').style.'+label+'='+style+';';
   //eval(evalEXPR);
  }
  else {
  //ns4
   //eval('document.layers['+layer+'].'resizeTo(x,y);
  }
 },
 placeLayer: function(layer,x,y,largeur,hauteur) {
  this.cacheCalque(layer);
  this.moveXY(layer,x,y);
  this.resizeXY(layer,largeur,hauteur);
  this.afficheCalque(layer);
 },
 rewriteLayer: function(id, text) {
 var x=0;
 var y=0;
  if(this.globalVar.ie4) {
   this.cacheCalque(id);
   document.all[id].innerHTML = text;
   this.afficheCalque(id);
  }
  else if(this.globalVar.moz) {
   //if( typeof document.getElementById(id) != 'undefined' )
   try {
    document.getElementById(id).innerHTML = text;
   }
   catch(e) {
  this.globalVar.msg += 'rwLay err:'+id+'\n';
    }
  }
  else if(this.globalVar.ns4) {
   document.layers[id].document.open();
   document.layers[id].document.write(text);
   document.layers[id].document.close();
  }
  else {
   document.write("unsupported<br>\n";);
  }
 },
 checkResizeFix: function() {
  navigateur.widthCheck = navigateur.getDisplayWidth();
  navigateur.heightCheck = navigateur.getDisplayHeight();
  window.onResize = navigateur.resizeFix
 },
 resizeFix: function() {
  if (navigateur.widthCheck != navigateur.getDisplayWidth() || navigateur.heightCheck != navigateur.getDisplayHeight())
   document.location.href = document.location.href;
  setTimeout("navigateur.resizeFix()",1000);
  //if (navigateur.widthCheck != window.innerWidth || navigateur.heightCheck != window.innerHeight)
  // document.location.href = document.location.href
 },

 getHeight: function(id) {
     if (document.layers) return document.layers[id].document.height;
     else if (document.all) return document.all[id].clientHeight;
 },
 reposition: function(id) {
     if (document.layers)
         document.layers[id].top = this.globalVar.accumulative_height;
     else if (document.all)
         document.all[id].style.posTop = this.globalVar.accumulative_height;
     this.globalVar.accumulative_height += getHeight(id);
 },
 setCookie : function(Name, Value) {
   var ExpireDate = new Date();
   ExpireDate.setTime(ExpireDate.getTime() + (180 * 24 * 60 * 60 * 1000));
  document.cookie = Name + '=' + escape(Value) + '; expires=' + ExpireDate.toGMTString();
 },
 getCookie :function(Name){
   if (document.cookie.length > 0) {
      begin = document.cookie.indexOf(Name + '=');
      if (begin != -1) {
         begin += Name.length + 1;
         end = document.cookie.indexOf(';', begin);
         if (end == -1) end = document.cookie.length;
         return unescape(document.cookie.substring(begin, end));
     }
   }
   return null;
 },
 setWindowAttribut : function(w,h) {
  var attb="toolbar=yes,"
  attb+="location=yes,"
  attb+="directories=no,"
  attb+="status=yes,"
  attb+="menubar=no,"
  attb+="scrollbars=yes,"
  attb+="resizable=yes,"
  attb+="width="+w+","
  attb+="height="+h
  return attb;
 },
 popup_window :function(NewURL,w,h){ var o="";
  if(typeof w == 'undefined') w=640;
  if(typeof h == 'undefined') h=480;
  var testWND3=window.open(NewURL,'WSTEST',this.setWindowAttribut(w,h))
  //testWND3.document.write(o);
  //testWND3.document.write(document.formWSTEST.txt1.value);
  //testWND3.document.write('</body></html>');
  testWND3.focus();
 },

 describify : function(arg,root) { var i,o,v,u, racine;
 if(describify.arguments.length==1)
  racine = '';
 else
  racine = root;
 switch(typeof arg) {
  case 'object':
   if (arg) {
    var c=arg.constructor;
 switch(c) {
     case Array:
      o = '';
      for (i = 0; i < arg.length; ++i) {
       v = describify(arg[i],racine+'['+i+']');
       if (o) {
        o += ',<br>';
       }
       if (v !== u) {
        o += v;
       }
    else {
        o += 'null,';
       }
      }
      return ' <font color=#0099FF>'+'object'+'</font><font color=pink>'+ racine +'</font> = '+ '[<ul>' + o + '</ul>]';
      //return '[<ul>' + o + '</ul>]';
  default:
      if (typeof arg.toString != 'undefined') {
       o = '';
       for (i in arg) {
        v = describify(arg[i],racine);
        if (v !== u) {
         if (o) {
          o += ',';
         }
         o += '<b>'+describify(i,racine) + '</b>:' + v +'<br>';
        }
       }
       return '{' + o + '}<br>';
      } else {
       return;
      }
      return 'null';
    }
   }
   return 'null';
  case 'string':
   return ' <font color=green>'+'object'+'</font><font color=red>'+ racine +'</font> = '+ '"' + arg.replace(/(["\\])/g, '\\$1') + '"';
  case 'unknown':
  case 'undefined':
  case 'function':
   return u;
  default:
   return ' <font color=purple>'+'object'+'</font><font color=red>'+ racine +'</font> = ' + String(arg);
 }
 },
 stringifyDeco :function(arg) {
    var i, o, u, v;

    switch (typeof arg) {
    case 'object':
        if (arg) {
            if (typeof arg.length != 'undefined') {
            //if (arg.constructor == Array) {
                o = '';
                for (i = 0; i < arg.length; ++i) {
                    v = this.stringify(arg[i]);
                    if (o) {
                        o += ',';
                    }
                    if (v !== u) {
                        o += v;
                    } else {
                        o += 'null,';
                    }
                }
                return '[<ul>' + o + '</ul>]';
            } else if (typeof arg.toString != 'undefined') {
                o = '';
                for (i in arg) {
                    v = this.stringify(arg[i]);
                    if (v !== u) {
                        if (o) {
                            o += ',';
                        }
                        o += '<b>'+this.stringify(i) + '</b>:' + v +'<br>';
                    }
                }
                return '{' + o + '}<br>';
            } else {
                return;
            }
        }
        return 'null';
    case 'unknown':
    case 'undefined':
    case 'function':
        return u;
    case 'string':
        return '"' + arg.replace(/(["\\])/g, '\\$1') + '"';
    default:
        return String(arg);
    }
 },
 stringify : function(arg) {
    var i, o, u, v;

    switch (typeof arg) {
    case 'object':
        if (arg) {
            if (typeof arg.length != 'undefined') {
            //if (arg.constructor == Array) {
                o = '';
                for (i = 0; i < arg.length; ++i) {
                    v = this.stringify(arg[i]);
                    if (o) {
                        o += ',';
                    }
                    if (v !== u) {
                        o += v;
                    } else {
                        o += 'null,';
                    }
                }
                return '[' + o + ']';
            } else if (typeof arg.toString != 'undefined') {
                o = '';
                for (i in arg) {
                    v = this.stringify(arg[i]);
                    if (v !== u) {
                        if (o) {
                            o += ',';
                        }
                        o += ''+this.stringify(i) + ':' + v +'';
                    }
                }
                return '{' + o + '}';
            } else {
                return;
            }
        }
        return 'null';
    case 'unknown':
    case 'undefined':
    case 'function':
        return u;
    case 'string':
        return '"' + arg.replace(/(["\\])/g, '\\$1') + '"';
    default:
        return String(arg);
    }
 },
 stringifyNice : function(arg, t) { var t0='';
   if(typeof t == 'undefined') t='';
    var i, o, u, v;
    switch (typeof arg) {
    case 'object':
        if (arg) {
            if (typeof arg.length != 'undefined') {
            //if (arg.constructor == Array) {
                o = '';
    t0 = t;
    t +='\t';
                for (i = 0; i < arg.length; ++i) {
                    v = this.stringifyNice(arg[i], t);
                    if (o) {
                        o += ','; //\n'+t;
                    }
                    if (v !== u) {
                        o += v;
                    } else {
                        o += 'null,';
                    }
                }
                return ' [\n'+t + o + '\n'+t0+']';
                //return arg.constructor+'=='+Array+ '?'+(arg.constructor == Array )+ ' [\n'+t + o + '\n'+t0+']';
            } else if (typeof arg.toString != 'undefined') {
                o = '';
                for (i in arg) {
                    v = this.stringifyNice(arg[i]);
                    if (v !== u) {
                        if (o) {
                            o += ',';
                        }
                        o += ''+this.stringifyNice(i) + ':' + v +'';
                    }
                }
                return ' { ' + o + ' }\n';
            } else {
                return '...';
            }
        }
        return 'null';
    case 'unknown':
    case 'undefined':
    case 'function':
        return u;
    case 'string':
        return '"' + arg.replace(/(["\\])/g, '\\$1') + '"';
    default:
        return String(arg);
    }
 },


 init : function() {
  if(navigator.userAgent.substring(0,7) == "Mozilla";)
 this.globalVar.Mozilla=navigator.userAgent.substring(8,9);

  this.ie4= this.globalVar.ie4= ((navigator.appName == "Microsoft Internet Explorer";) && (parseInt(navigator.appVersion) >= 4 ));
  this.ns4= this.globalVar.ns4= ((navigator.appName == "Netscape";) && (parseInt(navigator.appVersion) >= 4 ));
  this.moz= this.globalVar.moz= ((document.getElementById) && !(this.globalVar.ie4))?1:0;
  this.op5= this.globalVar.op4= (navigator.userAgent.indexOf("Opera";)!= -1?1:0);
  this.Mozilla= this.globalVar.Mozilla= (navigator.userAgent.indexOf("Mozilla";)!= -1?1:0);
  if (this.globalVar.ie4) {
 this.globalVar.layerRef="document.all['";
 this.globalVar.styleRef="'].style";
 this.globalVar.posHDRx="'].style.left=";
 this.globalVar.posTRLx=";";
 this.globalVar.posHDRy="'].style.top =";
 this.globalVar.posTRLy=";";
  }
  else if(this.globalVar.moz) {
 this.globalVar.layerRef="document.getElementById('";
 this.globalVar.styleRef="').style";
 this.globalVar.posHDRx="').style.left=";
 this.globalVar.posTRLx=";";
 this.globalVar.posHDRy="').style.top =";
 this.globalVar.posTRLy=";";
  }
  else {
 this.globalVar.layerRef="document.layers['";
 this.globalVar.styleRef="']";
 this.globalVar.posHDRx="'].moveToAbsolute(";
 this.globalVar.posTRLx=",/*";
 this.globalVar.posHDRy="*/"; posTRLy=";);";
  }
 }
};
navigateur.init();
</script>

<table border=1 bgcolor=#CCCCCC><tr><td></td><td align=center><a href=javascript:moveUp(5); style="text-decoration:none"><img src=http://blogs.tap.ibm.com/weblogs/xavier.mary@fr.ibm.com/resource/template//arrow_up_blue.gif width=13 height=14 border=0></a></td><td></td></tr>
<tr><td><a href=javascript:moveLeft(10); style="text-decoration:none"><img src=http://blogs.tap.ibm.com/weblogs/xavier.mary@fr.ibm.com/resource/template/arrow_left_blue.gif width=14 height=13 border=0></a></td>
<td bgcolor=#666666>
<div id="result" name="result"></div>
</td><td><a href=javascript:moveRight(10); style="text-decoration:none"><img src=http://blogs.tap.ibm.com/weblogs/xavier.mary@fr.ibm.com/resource/template/arrow_right_blue.gif
 width=14 height=13 border=0></a></td></tr>
<tr><td></td><td align=center><a href=javascript:moveDown(5); style="text-decoration:none"><img src=http://blogs.tap.ibm.com/weblogs/xavier.mary@fr.ibm.com/resource/template/arrow_down_blue.gif width=13 height=14 border=0></a></td><td>
</td></tr></table>

<br>
<div id="result2" name="result2"></div>
<div id="result3" name="result3"></div>

<script language=javascript>
function func_start() {rules.animation=1;rules.show([0,1],[2],0)}
function func_stop() {rules.animation=0;}
</script>

<a href="javascript:func_start();">(start)</a>
<a href="javascript:func_stop();">(stop)</a>
<a href=javascript:rules.show([0,1],[2],4)>show(4)</a>
<a href=javascript:rules.show([0,1],[2],10)>show(10)</a>
<a href=javascript:rules.show([0,1],[2],20)>show(20)</a>
<a href=javascript:rules.show([0,1],[2],40)>show(40)</a>
<a href=javascript:rules.show([0,1],[2],100)>show(100)</a>
<a href=javascript:rules.show([0,1],[2],200)>show(200)</a>
<a href=javascript:rules.show([0,1],[2],400)>show(400)</a>
<a href=javascript:rules.show([26,1],[0,1,0,0,1,1,0,1,1,0,1,0,0,0,0,1,0,1,1,1,1,1,1,1,1,1,0,2,1],100)>show(400..500)</a>
<a href=javascript:rules.show([18,1],[0,1,0,0,1,0,0,1,0,1,1,1,1,0,1,1,0,1,2,1,0,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1],400))>show(800..1200)</a>
<br>