mattparks5855

Hash Syntax

Jul 18th, 2019
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Swift 1.28 KB | None | 0 0
  1. /// Test1
  2. test1:: () -> {
  3.     alias f32: float32;
  4.  
  5.     f := new f32(10.97); // *float
  6.     d := new f32(-6.1); // *float
  7.  
  8.     fr := &f;
  9.     fr += -3.0;
  10.  
  11.     swap(f, d);
  12.     delete(d, f);
  13. }
  14.  
  15. swap:: <T>(lhs: *T, rhs: *T) -> {
  16.     tmp := &lhs;
  17.     lhs = &rhs;
  18.     rhs = tmp;
  19. }
  20.  
  21. /// Test2
  22. test2:: () -> {
  23.     u: U;
  24.     u.y = 'p';
  25.     assert(u.x == 112);
  26. }
  27.  
  28. U:: union {
  29.     x: int;
  30.     y: char;
  31. }
  32.  
  33. /// Test3
  34. test3:: () -> {
  35.     f: foo; // foo
  36.  
  37.     j: int[3]; // int[3]
  38.     k := int[](1, 2, 3); // int[3]
  39.     l := new int[](1, 2, 3); // *int[3]
  40.     f.matches(*k);
  41.     matches(*f, l);
  42.  
  43.     if (typeof(a) == typeof(b)) {
  44.         printf("Hello world!\n");
  45.     }
  46. }
  47.  
  48. A:: struct {
  49.     prefix := "Foo";
  50. }
  51.  
  52. // No method body so it's virtual
  53. matches:: ($this: *A, lhs: *int[], l: int) -> bool;
  54.  
  55. B:: struct {
  56. }
  57.  
  58. add_val:: <T>($this: *B, lhs: ?T, i: T) -> T;
  59.  
  60. foo:: struct {
  61.     // Subtype polymorphism
  62.     using a: A;
  63.     using b: B;
  64. }
  65.  
  66. // Must have the same function declaration as A::matches, with the implementors $this type.
  67. matches:: ($this: *foo, lhs: *int[], l: int) -> bool {
  68.     prefix := a::prefix;
  69.     ret lhs.count == l;
  70. }
  71.  
  72. add_val:: <T>($this: *foo, lhs: ?T, i: T) -> T {
  73.     if (lhs) {
  74.         return &lhs + i;
  75.     }
  76.     return i;
  77. }
  78.  
  79. exten printf:: (format: *int8, ...) -> void;
  80.  
  81. /// Main
  82. main:: () -> {
  83.     test1();
  84.     test2();
  85.     test3();
  86. }
Advertisement
Add Comment
Please, Sign In to add comment