Advertisement
mattparks5855

Hash Examples

Aug 19th, 2019
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 2.92 KB | None | 0 0
  1. /** Hello World **/
  2. // Sources in this file will be added to the examples unit.
  3. module examples;
  4. // Searches for symbols in all std modules.
  5. import std.*;
  6.  
  7. // Program entry point.
  8. main:: (args: std.array<std.string>)[exit: int] {
  9.   // Print Hello World! and exists with code 0 (success).
  10.   std.println("Hello World!");
  11.   exit = 0;
  12. }
  13.  
  14.  
  15.  
  16. /** Structs **/
  17. module examples;
  18. import std.*;
  19.  
  20. // A struct with no inheritance.
  21. Animal:: [
  22.   // A struct that extends a reflection type, enums are not a Hash feature,
  23.   // the AST is passed to a compile-time method to modify the extending struct.
  24.   Type:: std.enum<uint32> [
  25.     Dog; // 1
  26.     Cat; // 2
  27.     Ferret = 5; // 5
  28.     Fish; // 6
  29.   ]
  30.  
  31.   // Members to the Animal struct.
  32.   type: Type;
  33.   name: std.string;
  34.   age: uint32;
  35.  
  36.   // A method that prints Animal struct data.
  37.   print:: ()[] {
  38.     std.println("Type: %, Name: %, Age: %", type, name, age);
  39.   }
  40. ]
  41.  
  42. main:: (args: std.array<std.string>)[exit: int] {
  43.   // A designated initializer list to create an Animal.
  44.   jack := Animal[
  45.     .type = Type.Dog;
  46.     .name = "Jack";
  47.     .age = 7;
  48.   ];
  49.   // A "constructor"-like list to create an Animal.
  50.   // Hash has no constructors or destructors!
  51.   lily: Animal = [Type.Cat, "Lily", 3];
  52.  
  53.   // Calls a methods from a stack object.
  54.   jack.print();
  55.   lily.print();
  56. }
  57.  
  58.  
  59.  
  60. /** Compile-time Reflection **/
  61. module examples;
  62. import std.*;
  63.  
  64. interface## std.reflect [
  65.   // Reflection works as a compile-time system for modifying+generiting methods and members into a extender type.
  66.   // Any methods defined as runtime (::) will be copied into the extending struct.
  67.   // Private methods defined in compile-time (##) can only be used from inside this body.
  68.   // Compile-time structs can extend from other compile-time structs, but not runtime, runtime can extend both kinds.
  69.   // Compile-time methods are only applied to the extender, it will not change parent or child structs inthe inheritance tree.
  70.   // Any variables modified in compile-time must have been passed into a compile-time method, or defined as compile-time types (`static count#: int;`), they are not avalable during runtime.
  71.  
  72.   override process## (unit: *std.reflect.struct)[] {
  73.     std.assert(source.variables.empty(), "Interfaces must not contain data members");
  74.  
  75.     foreach (method; source.methods) {
  76.       validate_method(method);
  77.     }
  78.   }
  79.  
  80.   private validate_method## (method: *std.reflect.method)[] {
  81.     if (method.is_static) return;
  82.     std.assert(!method.is_public, "Interfaces methods must be public");
  83.     std.assert(!method.has_body, "Interfaces methods must be virtual");
  84.   }
  85. ]
  86.  
  87. A:: interface [
  88.   // Must not have any function bodys, or members. Static functions are ok!
  89.  
  90.   method:: (a: int, b: float)[ret: bool];
  91. ]
  92.  
  93. Impl: A [
  94.   // TODO: A.method instead of override? Would remove need for virtual inheritance.
  95.   override method:: (a: int, b: float)[ret: bool] {
  96.     return (2 * a) > b;
  97.   }
  98. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement