daily pastebin goal
56%
SHARE
TWEET

Untitled

a guest Oct 11th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.     A typical class in Mysidia Language.
  3.     Everything is an object, every object has a class, and every language token is a first class citizen.
  4.     All classes extends Object by default, all metaclasses extends Class by default.
  5.     Mysidia metaclasses are created automatically with "$(classname):Class", it is also possible to define custom metaclasses.
  6.     A namespace needs to be declared at the top of a file, followed by using statements.
  7.     A class may implements interfaces, all interface extends Interface by default.
  8.     Interfaces cannot have fields, they may only define method selector names, but not handler body.
  9.     Type annotation on messages is optional, there is also optional return type specification.
  10.     For single line method body, one can use => shortcut/syntactic sugar to make it more concise.
  11.     When passing a message to receiver object, it is possible to pass arguments by position, or use named arguments.
  12.     Upon receiving a message, the receiver object attempts to match it with method dictionary.
  13.     Message keys are expressed as $(selectorName):arg1Name:arg2Name:...argnName. Hence Mysidia supports pseudo method overloading, they are in fact different methods responding to different messages.
  14.     fields are private, methods are public, one needs to use methods to fetch field values.
  15.     Mysidia has no class fields/messages, they are actually instance fields/messages on the metaclass.
  16.     Exceptions may be thrown from any code, use block {} followed by a catch() method to handle them.  
  17.     Blocks are shortcut for closure, defined with { } with possible arguments wrapped in | |.
  18.     If Blocks are passed as only argument, it is possible to omit the parenthesis().  
  19. */
  20.  
  21. namespace Mysidia.Std.Util;
  22. using Mysidia.Std.Collection.*;
  23. using Mysidia.Std.Lang.*;
  24.  
  25. interface IDate{
  26.     methods{
  27.         getDay(), getMonth(), getYear()
  28.     }
  29. }
  30.  
  31. class Date implements IDate{
  32.  
  33.     fields{
  34.         day = 1, mon = 1, yr = 1970
  35.     }
  36.  
  37.     methods{
  38.        init(day, mon, yr){
  39.            this.day = day;
  40.            this.mon = mom;
  41.            this.yr = yr;
  42.        }
  43.  
  44.        getDay() => day;
  45.        getMonth() => mon;
  46.        getYear() => yr;
  47.        string toString() => "${yr}-${mon}-${day)}"; // a message with string return type  
  48.        after(Date date) => this < date;
  49.        before(Date date) => this > date;
  50.    }
  51.  
  52.    metaclass DateClass{
  53.        fields{
  54.            formatter = new DateFormatter();
  55.        }
  56.  
  57.        methods{
  58.            parse(string dateString) => parse(dateString, formatter);
  59.            parse(string dateString, IDateFormatter dateFormatter){
  60.                format = formatter.findFormat(dateString);
  61.                format.ifNull{ throw new DateFormatException("Supplied Date Format is invalid.") };
  62.                return format.createDate();                
  63.            }
  64.        }
  65.    }
  66. }
  67.  
  68. date = new Date(31, 8, 2018);
  69. Transcript.show(date); //prints "2018-8-31"
  70. date2 = Date.parse("2018-08-31");
  71. Transcript.show(date); //prints "2018-8-31"
  72. date3 = { Date.parse("abcdefz") }.catch(
  73.             on: DateFormatException,
  74.             do: {|dfe| Transcript.show(dfe.getDetails()) }
  75.         );
  76. //prints "Supplied Date Format is invalid"
  77.  
  78. Transcript.show(date.getClass()); //prints "class: Mysidia.Std.Util.Date"
  79. Transcript.show(Date.getClass()); //prints "metaclass: Mysidia.Std.Util.DateClass"
  80. Transcript.show(Date.getClass().getClass()); //prints "metaclass: Mysidia.Std.Lang.MetaClass"
  81. Transcript.show(Date.getClass().getClass().getClass()); //prints "metaclass: Mysidia.Standard.Lang.MetaClass:Class"
  82. Transcript.show(Date.getClass().getClass().getClass().getClass()); //prints "metaclass: Mysidia.Sd.Lang.MetaClass(Metaclass is the metaclass of all metaclasses, including MetaClass:Class)"
  83.  
  84. Transcript.show(date.getMetaClass()); //prints "metaclass: Mysidia.Std.Util.DateClass"
  85. Transcript.show(Date.getMetaClass()); //prints "metaclass: Mysidia.Std.Lang.MetaClass"
  86. Transcript.show(DateClass.getMetaClass()); //prints "metaclass: Mysidia.Std.Lang.MetaClass:Class"
  87. Transcript.show(MetaClass.getMetaClass()); //prints "metaclass: Mysidia.Standard.Lang.MetaClass(MetaClass is the metaclass of itself)"
  88. Transcript.show(MetaClass:Class.getMetaClass()); //prints "metaclass: Mysidia.Std.Lang.MetaClass:Class
  89.  
  90. Transcript.show(Date.getSuperClass()); //prints "class: Mysidia.Std.Lang.Object"
  91. Transcript.show(Date.getSuperClass().getSuperClass()); //prints "class: Mysidia.Std.Lang.UndefinedObject(object has no superclass, so it returns UndefinedObject/Null)"
  92. Transcript.show(Date.getSuperClass().getSuperClass().getSuperClass()) //prints "class: Mysidia.Std.Lang.Object(object is the root of all classes, so naturally UndefinedObject/Null as well)"
  93.  
  94. Transcript.show(Date.getSuperClass().getClass()); //prints "metaclass: Mysidia.Std.Lang.Object:Class"
  95. Transcript.show(Date.getSuperClass().getClass().getSuperClass()); //prints "class: Mysidia.Std.Lang.Class"
  96. Transcript.show(Date.getSuperClass().getClass().getSuperClass().getSuperClass()); //prints "class: Mysidia.Std.Lang.ClassDescription"
  97. Transcript.show(Date.getSuperClass().getClass().getSuperClass().getSuperClass()); //prints "class: Mysidia.Std.Lang.ClassDescription"
  98. Transcript.show(Date.getSuperClass().getClass().getSuperClass().getSuperClass().getSuperClass()); //prints class: Mysidia.Std.Lang.Behavior"
  99. Transcript.show(Date.getSuperClass().getClass().getSuperClass().getSuperClass().getSuperClass().getSuperClass()); // prints class: Mysidia.Std.Lang.Object"
  100.  
  101. Transcript.show(Date.getInterfaces()[0]); //prints "interface: Mysidia.Std.Util.IDate"
  102. Transcript.show(Date.getInterface(0)); //prints "interface: Mysidia.Std.Util.IDate"
  103. Transcript.show(Date.getInterfaces()); //prints "Array(1): (interface: Mysidia.Std.Lang.Interface)"
  104. Transcript.show(IDate.getSuperClass()); //prints "interface: Mysidia.Std.Lang.Interface"
  105. Transcript.show(IDate.getSuperClass()); //prints "class: Mysidia.Std.Lang.Object"
  106. Transcript.show(IDate.getClass()); //prints "class: Mysidia.Std.Lang.IDate:Class"
  107. Transcript.show(IDate.getMetaClass()); //prints "metaclass: Mysidia.Std.Lang.MetaClass"
  108. Transcript.show(IDate:Class.getMetaClass()); //prints "metaclass: Mysidia.Std.Lang.MetaClass:Class"
  109.  
  110. Date.toString = { "${this.mon}-${this.day}, ${this.yr}" };
  111. // dynamically modify the message handler toString for Date Class
  112. Transcript.show(date); //prints 8-31, 2018
  113. Date.setClass(Class:Class);
  114. // dynamically modify Date Object's metaclass(Date's class) to be the metaclass Class:Class
  115. date4 = { Date.Parse("2018-09-30") }.catch(
  116.             on: MessageNotUnderstoodException,
  117.             do: {|mnue| Transcript.show(mnue.getDetails()) }
  118.         );
  119. //prints "Receiver Mysidia.Std.Utility.Date cannot understand message with selector 'Parse' and arguments '2018-09-30'"
  120.  
  121. Date.init = {|mon, day, yr|
  122.                 this.mon = mon;
  123.                 this.day = day;
  124.                 this.yr = yr;
  125.             };
  126. // dynamically modify the constructor/new method on class Date, it now accepts month before date
  127. date5 = new Date(9, 30, 2018);
  128. Transcript.show(date5); //prints "2018-9-30"
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top