Easy A
Would you like to react to this message? Create an account in a few clicks or log in to continue.


 
HomeHome  Latest imagesLatest images  SearchSearch  RegisterRegister  Log inLog in  

Share
 

 Micro Metalanguage Messages

View previous topic View next topic Go down 
AuthorMessage
Momment
Member
Member
Momment

Posts : 19
Join date : 2011-03-09

Micro Metalanguage Messages Empty
PostSubject: Micro Metalanguage Messages   Micro Metalanguage Messages EmptySat May 14, 2011 7:43 pm

It's easy to create your own micro language derivative, especially if you think of the metalanguage (javascript in this case) as a simple messaging tool.
The trick is to use strings to wrap your new language - this allows almost any form of expression for you commands.

This following simple example uses 5 simple rules.

Thses are the 5 rule instructions:
1) decide on what gross functionality categories you want to support (control, assignment, etc).
2) decide on the category commands you want to supply.
3) create one function to rule them all (laguage rules).
4) inside the language rules function, crete each language rule function.
4a) use the test method of a regular expression to identify your commands.
5) and... bow to the evil of eval .

Here's a simple example:
Code:

<!doctype html>
<html>
<head>
<title>
Micro Metalanguage Messages (MMM... It's Good!)
</title>
<style type='text/css'>
body {
   background: #23292e;
   color:#c5c5c5;
   margin: 20px;
   font-family: Helvetica, Arial, sans-serif;
}
textarea {
   background: #e8ed90;
   border: 1px solid #a9ae54;
   padding: 15px;
   width: 500px;
   font-weight: bold;
   font-size: 14px;
   color: #7c7834;
}
td span, pre {padding-left:40px;}
li {margin-bottom:10px;}
</style>
<script type="text/javascript">

/*    micro metalanguage messages.... (mmm... it's good)

   It's easy to create a micro metalanguage if you think of it as simple messaging.
   Yes, use strings to wrap your language - it allows almost any form of expression.

   simple 5 rule instructions:
   1) decide on what gross functionality categories you want to support (control, assignment, etc).
   2) decide on the category commands you want to supply.
   3) create one function to rule them all (laguage rules).
   4) inside the language rules function, crete each language rule function.
   4a) use the test method of a regular expression to identify your commands.
   5) and... bow to the evil of eval.
*/

//here's a simple example of using the 5 rules of mmm.
var glb_Metalanguage1=(function()
{
   if(typeof console == 'undefined')
   {
      console=
      {
         log:function(n){window.status=n}
      }
   }

   //  >> MMM Rules Set. This mmm supports:
   //> ** CONTROL **:
   //    for x to y, fncbody =>    use to perform fncbody mutiple times.
   //
   //> ** DISPLAY **:
   //    print text ===========>  print the text, no line return.
   //    printl text          =>  print the text, adds a line return.
   //    show var x ===========>  prints the value of the var, no line return.
   //    showl var x          =>  prints the value of the var, add a linre return.
   //    show object property =>  prints the value of the objects property.
   //    show object ==========>  prints all values of the object properties, adds  line returns.
   //
   //> ** ASSIGNMENT **
   //    use domobject number =>  assigns the dom obect identified by number (one-based) to show text.
   //    object has property  =>  assigns a property to an object. Create the object if doesn't exist.
   //    add number to object property => adds number/string to the objects property
   //
   //> ** ACTION **
   //    player1 fights player2 => Action! the fisrst player tries to hit the second player!
   //                  Returns the resulting fight desription as a string.
   //

   // *** Not a good code style - but at least it's kept simple.
   var meta = function(n,m)
   {
      //*** for <x> to <y> ***
      var arg1 = new RegExp(/for \w+ to \w+/);
      if(arg1.test(n))
      {
         var t = n.replace(/for (\w+) to (\w+)/,"$1|$2");
         var v = t.split("|");
         for(var i=parseInt(v[0]);i<parseInt(v[1]);i++)
         {
            eval(m);
         };
         return;
      }

      //*** print <text> ***
      var arg1 = new RegExp(/print [\s\S]*/);
      if(arg1.test(n))
      {
         var t = n.replace(/print ([\s\S]*)/,"$1");
         print(t);
         return;
      }

      //*** printl <text> ***
      var arg1 = new RegExp(/printl [\s\S]*/);
      if(arg1.test(n))
      {
         var t = n.replace(/printl ([\s\S]*)/,"$1");
         print(t+"\n");
         return;
      }

      //*** <object> has <property> ***
      var arg1 = new RegExp(/\w+ has \w+/);
      if(arg1.test(n))
      {
         var t = n.replace(/(\w+) has (\w+)/,"$1|$2");
         var v = t.split("|");
         eval("if(typeof "+v[0]+"=='undefined'){"+v[0]+"={}};"+v[0]+"."+v[1]+"=0");
         return;
      }

      //*** add <number> to <object> <property> ***
      var arg1 = new RegExp(/add -?\w+ to \w+ \w+/);
      if(arg1.test(n))
      {
         var t = n.replace(/add (-?\w+) to (\w+) (\w+)/,"$1|$2|$3");
         var v = t.split("|");
         var val = eval(v[1]+"."+v[2]+"+="+v[0]);
         return;
      }

      //*** show var <x> ***
      var arg1 = new RegExp(/show var \w+/);
      if(arg1.test(n))
      {
         var t = n.replace(/show var (\w+)/,"$1");
         var s = eval(t);
         print(s);
         return;
      }

      //*** showl var <x> ***
      var arg1 = new RegExp(/showl var \w+/);
      if(arg1.test(n))
      {
         var t = n.replace(/showl var (\w+)/,"$1");
         var s = eval(t);
         print(s+"\n");
         return;
      }

      //*** show <object> <property> ***
      var arg1 = new RegExp(/show \w+ \w+/);
      if(arg1.test(n))
      {
         var t = n.replace(/show (\w+) (\w+)/,"$1|$2");
         var v = t.split("|");
         var val = eval(v[0]+"."+v[1]);
         var s = v[0]+" "+v[1]+" = "+ val;
         print(s+"\n");
         return;
      }

      //*** show <object> ***
      var arg1 = new RegExp(/show \w+/);
      if(arg1.test(n))
      {
         var t = n.replace(/show (\w+)/,"$1");
         var s ="";
         var o = eval(t);
         for(var i in o)
         {
            s+=t+"->"+i+" = "+o[i]+"\n";
         }
         print(s);
         return;
      }

      //*** <player1> fights <player2> ***
      var arg1 = new RegExp(/\w+ fights \w+/);
      if(arg1.test(n))
      {
         var t = n.replace(/(\w+) fights (\w+)/,"$1|$2");
         var v = t.split("|");
         var d = d6(3);
         var p1=eval(v[0]);
         var p2=eval(v[1]);
         if(d<p1.skill)
         {
            var dm = d6(1);
            p2.health-=dm;
            return v[0]+" hit "+v[1]+" for "+ dm +" damage.";
         }
         return v[0] + " missed the " + v[1];
      }

      //*** use <domobject> <number> ***
      var arg1 = new RegExp(/use \w+ \w+/);
      if(arg1.test(n))
      {
         var t = n.replace(/use (\w+) (\w+)/,"$1|$2");
         var v = t.split("|");
         this.visob = document.getElementsByTagName(v[0])[v[1]-1];
         return;
      }
      alert(n+" is an unknown cmd");
   }

   function print(s)
   {
      if(this.visob==null)
      {
         console.log(s.replace(/\n/g,""));
      }else{
         if(this.visob.value)
         {
            this.visob.value += s;
         }else{
            this.visob.innerHTML += s.replace(/\n/g,"<br>");
         }
      }
   }

   meta.visob=null;

   return {
      meta:function(n,m){return meta(n,m)}
   }
})();


function d6(n)
{
   var r=0;
   for(var i=0;i<n;i++)
   {
      r+=Math.floor(Math.random()*6+1);
   }
   return r;
}

var x=0;
var dum="meta1";

window.onload = function()
{
   //use a short var
   var m = glb_Metalanguage1.meta;

   //Mix and match the simple meta with normal js.
   goblin={health:12,skill:6,gold:5};  //NOTICE: ** no var declaration = global **

   //m('use div 1');  //<- uncomment if you want - default text ouput target is console or status bar.

   m('printl this code is a simple demo of custom meta-commands');
   m('use textarea 1');    // <= sets the textarea to handle future text output.
   m('hero has health');
   m('for 1 to 20' , 'x+=2');

   x-=5;

   m('print x = ');
   m('showl var x');
   m('add '+x+' to hero health');
   m('add -13 to hero health');
   m('show hero health');
   m('printl *******');
   m('hero has strength');
   m('hero has skill');
   m('hero has gold');
   m('add '+d6(3)+' to hero skill');
   m('add '+d6(3)+' to hero strength');
   m('show hero');
   m('printl ------');
   m('show goblin');
   m('printl *******');
dum =   m('goblin fights hero');    // assign result for later use
   m('showl var dum')
   m('show hero');
   m('printl ------');
dum =  m('hero fights goblin');
   m('showl var dum')
   m('show goblin');   
   m('printl *******');
   m('use div 2');
   m('print (this is only one way to make a meta-language)');
}

</script>
</head>
<body>
<h1><font face='Lucida Calligraphy Italic,Monotype Corsiva,Footlight MT Light'>Micro Metalanguage Messages  (MMM... it's good!)</font></h1>
<h3>Yes this uses eval. Yes its evil to eval (...to the devil his own...). And, yes it's fun.</h3>
<h6>(uses simple messages)</h6>

<table><tr><td>
<div></div>
<textarea rows="18" cols="80"></textarea>
<div></div>
</td><td>
<span>MMM comand code used</span>
<pre>
   ...
   m('show hero');
   m('printl ------');
   m('show goblin');
   m('printl *******');
dum =   m('goblin fights hero');
   m('showl var dum')
   m('show hero');
   m('printl ------');
dum = m('hero fights goblin');
   m('showl var dum')
   m('show goblin');   
   m('printl *******');
   ...
</pre>
</td><td>
simple 5 rule instructions:

<ol><li>decide on what gross functionality categories you want to support (control, assignment, etc).</li>

<li>decide on the category commands you want to supply.</li>

<li>create one function to rule them all (laguage rules).</li>

<li>inside the language rules function, crete each language rule function.<br>*use the test method of a regular expression to identify your commands.</li>

<li>and... bow to the evil of eval.</li>
</ol>

</td></tr></table>
</body>
</html>

The evil of eval is acknowledged in the sample code.

Don't forget to have fun.
Back to top Go down
 

Micro Metalanguage Messages

View previous topic View next topic Back to top 
Page 1 of 1

Permissions in this forum:You cannot reply to topics in this forum
Easy A :: Micro Metalanguage Messages Edit-trash Useless :: Trash-