Advertisement
mattparks5855

Hash New Spec

Aug 16th, 2019
864
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Groovy 3.15 KB | None | 0 0
  1. module tests;
  2. import cstd.io;
  3. import std.math;
  4. import export std.error;
  5.  
  6. main:: (argc: int, argv: [][]char) [ret: int] {
  7.     [x, y] := rng(1245);
  8.  
  9.     val := 3;
  10.     app(*val);
  11.  
  12.     if (root := sqrt(-22.0); is_type<std.error>(root)) {
  13.         // Invalid sqrt.
  14.     }
  15.  
  16.     // Can ignore error return.
  17.     root := sqrt(4.0);
  18.     v: float = root + 2.0;
  19.  
  20.     // Optional return.
  21.     root2 := sqrt2(16.0);
  22.     if (@root2) {
  23.         v2: float = root2 + 4.0;
  24.     }
  25.  
  26.     fp: FunctionPointer;
  27.     fp.add2 = <T> (a: T, b: T) [ret: T] {
  28.         return a + (2 * b);
  29.     };
  30.     cstd.printf("%i\n", fp.doIt(-10, 2));
  31.  
  32.     d: D<float64>;
  33.     d.print();
  34.  
  35.     dog := animals.dog;
  36.  
  37.     ret = 0;
  38. }
  39.  
  40. rng:: (seed: int) [x: float, y: float] {
  41.     x = (seed % 3 * 22 / 2) * 0.1f;
  42.     y = ((seed << 2) % 2 * 33 / 4) * 0.3f;
  43. }
  44.  
  45. // You don't need to write void in the return args.
  46. app<T>:: (val: *T) [void] {
  47.     while (true) {
  48.         if (val > 100) {
  49.             // Breaks from the function, val is returned.
  50.             return;
  51.         }
  52.  
  53.         val *= 2;
  54.     }
  55.  
  56.     // A type that is also a pointer/optional is used exactly like a normal type,
  57.     // To get the pointer/optional-bool you use the @ prefix-operator.
  58.     cstd.printf("app.val: %p\n", @val);
  59.  
  60.     val2: T;
  61.     // OK: copys val int val2.
  62.     val2 = val;
  63.  
  64.     val3: *T;
  65.     // ERROR: T* and T do not implicitly match.
  66.     //val3 = val;
  67.     // OK: copys the pointer address of val into val3.
  68.     val3 = @val;
  69.  
  70.     val4: **T;
  71.     // OK: Gets a pointer to the val pointer.
  72.     val4 = *@val;
  73.     // ERROR: Cannot assign int32 to T*.
  74.     //val4 = 3;
  75.     // OK: Derefs *T down to T.
  76.     &val4 = 3;
  77. }
  78.  
  79. // Varient return type, only one can be set at a time.
  80. sqrt<T>:: (val: T) ![ret: T, err: std.error] {
  81.     if (val >= 0) {
  82.         ret = cstd.sqrt(val);
  83.     } else {
  84.         err = std.math_error("Cannot take sqrt of a negative number!");
  85.     }
  86. }
  87.  
  88. sqrt2<T>:: (val: T) [ret: ?T] {
  89.     if (val > 0) {
  90.         ret = cstd.sqrt(val);
  91.     } else {
  92.         ret = nil;
  93.     }
  94. }
  95.  
  96. FunctionPointer:: {
  97.     add2: std.function<(int, int), [int]>;
  98.  
  99.     doIt:: (a: int, b: int) [ret: int] {
  100.         ret = add2(a, b);
  101.     }
  102. }
  103.  
  104. A:: [
  105.     funA:: (int) [bool];
  106. ]
  107.  
  108. B<T>:: A [
  109.     // funA is does not need a redefinition.
  110.     funB:: (bool) [T];
  111. ]
  112.  
  113. C:: A [
  114.     funC:: () [float];
  115. ]
  116.  
  117. D<K>:: B<K>, C, A [
  118.     private {
  119.         constVal := 4;
  120.     }
  121.  
  122.     // Compile-time auto type decution for method paramaters and returns.
  123.     getConstVal:: () [ret] { ret = constVal; }
  124.     setConstVal:: (val) [] { constVal = val; }
  125.  
  126.     funA:: (val: int) [ret: bool] {
  127.         ret = val > 7 && val % 2 != 0;
  128.     }
  129.     funB:: (val: bool) [ret: K] {
  130.         ret = val ? 3.1415 : 9.8282;
  131.     }
  132.     funC:: () [ret: float] {
  133.         ret = constVal * 2.0;
  134.     }
  135.     print:: () [] {
  136.         val: K = funC() * funB(funA());
  137.         cstd.println(cal);
  138.     }
  139. ]
  140.  
  141. enum<T: int32>$$ (unit: *std.struct) [
  142.     assert(unit.methods.empty());
  143.     previousVal := 1;
  144.  
  145.     // A foreach syntax is needed.
  146.     for (it := unit.members.begin(); it < unit.members.end(); ++it) {
  147.         // A variable/members not having a type is not a syntax error,
  148.         // only fails to compile if not found by end of preprossesor.
  149.         assert(member.auto_type());
  150.         member.set_type<T>();
  151.  
  152.         if (!member.assigned_val()) {
  153.             member.set_val<T>(++previousVal);
  154.         }
  155.  
  156.         previousVal = member.val<T>();
  157.     }
  158. ]
  159.  
  160. animals:: enum<int16> [
  161.     dog;
  162.     cat = 3;
  163.     fish;
  164.     bird;
  165. ]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement