Composing compositions

A note on how I parse compositions (see previous–2 post).

There are several ways to do this, I choose to post process the AST (Abstract Syntax Tree); I scan expressions for “:” and, if found, rewrite that node. Using the previous example:
    List(1,2,3).xplode():String(_):List(_)
The tree looks like:
Expression
    List(1,2,3)
        xplode()
    :
    String(_)
    :
    List(_)
and is converted to
DataRef
    List(
        String(
            List(1,2,3)
                xplode()

The core of algorithm is simple, being able to punt “can you compose this” to an individual node is nice (OO in action). Heterogeneous lists rock. Mutable data is nice. Static typing gets in your way. Special cases are not nice (and make up the majority of the code).

Code snippet (with some hand waving):
i := instance_left_of_:;
foreach dr in (Expression) {
   if (“:” != dr) break;
   dr = Expression.next();

   try {
     if(not dr.composeWith(i)) throw(Exception.NotFoundError)
   } catch(NotFoundError)
      { bitch(“Can’t compose %s (missing _?)”.fmt(dr),self); }
   i = dr;
}

If a node can compose what ever it is handed, it does, modifies itself and returns True. If it can’t, it returns False. If it doesn’t understand composeWith, the VM throws NotFoundError. This last feature (late binding) makes it really easy to extend what can be composed; just add a composeWith method to a node. Here are two examples:

DataRef:
fcn composeWith(x) {  // look for “_”: f().g(_).h(), f(1,_,3)
   objs.filter1(fcn(fc,x)
      { FcnCall.isInstanceOf(fc) and
        fc.composeWith(x) },x)  .toBool();
}

Function Call:
fcn composeWith(x){ // look for f(_), f(_.x) is bad
   if (False != (n := args.filter1n(
         fcn(dr){DataRef.isInstanceOf(dr) and
                 dr.objs[0]==”_”})))
   {
      if (args[n].objs.len() != 1) bitch(“_.* not good”,self);
      args[n] = x;
      return(True);
   }
   False
}

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s