TaggedObject

Discussions about extensions and frameworks

TaggedObject

Postby oliver » Sun Apr 03, 2011 6:53 am

Tagged objects were missing in MorphEngine.
For yet another custom type study aid, here's the full code to implement them:
Code: Select all
var TaggedObject = {
   type: "tagged",
   typeHP: 12,
   isLoaded: true,
   separator: ":",

   instance: function(s, obj) { // the object's constructor function
      this.type = TaggedObject.type;
      this.name = s;
      this.obj = obj;
      this.toString = function() { return String(this.obj); }; // to permit auto-conversion to String in JS
   },
   fromNameAndObj: function(name, obj) { return new TaggedObject.instance(name, obj); },
   toObj: function(obj) { return obj.obj; },

   toString: function(obj) {
      return this.separator + obj.name + this.separator + String(obj.obj);
   },
   toHTML: function(obj) {
      var parts = this.toString(obj).split(this.separator);
      var HTMLforType = parts[1] + this.separator + " " + parts[2];
      HTMLforType += calculator.HTMLforTypeBadge(obj.type, display.isLarge() ? 60 : 50);
      return HTMLforType;
   },
   isStringRepresentation: function(x) { return (!x.indexOf(TaggedObject.separator) && x.split(TaggedObject.separator).length == 3); },
   fromString: function(str) {
      var args = str.split(TaggedObject.separator);
      return this.fromNameAndObj(args[1], args[2]);
   },
                                                
   // operators and functions
   "detag": function(x) { return this.toObj(x); },
   eval: function(x) { return this.toObj(x); },
                                                
   onload: function() {
      calculator.functions["@tag"] = function(obj, tag) { return TaggedObject.fromNameAndObj(calculator.unquote(String(tag)), obj); };
                                                
      // function name aliases
      calculator.function_aliases["\u2192TAG"] = "@tag";
      calculator.function_aliases["DTAG"] = "detag";
   }
};
calculator.registerType(TaggedObject);                                                


Programs in the Bench folder are the first users of this new type. As tagged objects shed their tags when invoked with an operator they don't define, you're able to do normal calculations with annotated benchmark result figures. (For example, you can subtract two run-time, even though objects of this type don't define a "-" (minus)-operator.)

Any object, or "data type", can be tagged (annotated).
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Return to Extensions

Who is online

Users browsing this forum: No registered users and 1 guest

cron