Advertisement
Guest User

Hash

a guest
Jul 16th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.88 KB | None | 0 0
  1. // Imports a the file begining are imported in all modules defined in the file.
  2. import std::io;
  3.  
  4. module Program {
  5.     // Any module that imports this module will also import std::core.
  6.     public import std::core;
  7.  
  8.     import std::random;
  9.     import "stdio.h";
  10.     import Program::Maths;
  11.  
  12.     // Main can be defined as any void global function.
  13.     Main(): std::entry_point -> {
  14.         Example::Get().val1 = 32.0f;
  15.         std::println($"{Example::Get().val1}, {Example::Get().val2}, {Example::Get().VAL3}");
  16.  
  17.         var add2Values = []<T: std::is_arithmetic>(a: T, b: T) -> {
  18.             return a + b;
  19.         };
  20.         var addValues = []<T...: std::is_arithmetic>(args: T...) -> {
  21.             return (args + ...);
  22.         };
  23.        
  24.         var pos0 = Vec2(0.5f, -9.75f);
  25.         var pos1 = Vec2(2.0f, 3.22f);
  26.         Vec2<float> pos2 = Vec2(-1, 5);
  27.  
  28.         if (pos0 * pos1 != 0.0f) {
  29.             std::println($"{pos0} * {pos1} != 0.0f");
  30.         }
  31.  
  32.         printf("Direct call to C header: %f\n". std::random(-100.0f, 100.0f));
  33.     }
  34.  
  35.     struct Example: Singleton<Example> {
  36.         val1 mutable: float32;
  37.         val2: int16 = -12;
  38.  
  39.         VAL3 static const: float64 = 26.924;
  40.  
  41.         init() -> {
  42.             std::println("Created Example Singleton!");
  43.         }
  44.  
  45.         deinit() -> {
  46.             std::println("Deleted Example Singleton!");
  47.         }
  48.  
  49.         getVal1() const -> val1;
  50.         setVal1(val1: int32) -> this.val1 = val1;
  51.     }
  52.    
  53.     struct Singleton<T>: std::non_copyable {
  54.         private instance static: T?;
  55.  
  56.         Get() static -> T* {
  57.             if(instance == null) {
  58.                 instance = T();
  59.             }
  60.  
  61.             return instance;
  62.         }
  63.     }
  64.  
  65.  
  66.     struct Animal: std::interface {
  67.         GetSound() const virtual -> std::string;
  68.     }
  69.    
  70.     struct Dog: Animal {
  71.         GetSound() const override -> std::string { return "Bark"; }
  72.     }
  73. }
  74.  
  75. // Only accessable in this target.
  76. private module Program::Maths {
  77.     import std::error;
  78.     import std::equality;
  79.     import std::math;
  80.  
  81.     // A 2 tuple vector containing arithmetic values.
  82.     struct Vec2<T: std::is_arithmetic> {
  83.         T x, y;
  84.  
  85.         init(x: T, y: T) -> {
  86.             this.x = x;
  87.             this.y = y;
  88.         }
  89.        
  90.         init<K: std::is_arithmetic>(x: K, y: K) -> {
  91.             this.x = (T)x;
  92.             this.y = (T)y;
  93.         }
  94.  
  95.         operator[](index: int) -> T {
  96.             switch(index) {
  97.             case 0:
  98.                 return x;
  99.             case 1:
  100.                 return y;
  101.             default:
  102.                 throw std::error::out_of_bounds("Index but be between 0 and 2!");
  103.             }
  104.         }
  105.  
  106.         operator bool() -> {
  107.             return this != 0;
  108.         }
  109.        
  110.         operator+<K>(rhs: Vec2<K>) -> {
  111.             return Vec2(x + rhs.x, y + rhs.y);
  112.         }
  113.  
  114.         operator*<K>(rhs: Vec2<K>) -> {
  115.             return Vec2(x * rhs.x, y * rhs.y);
  116.         }
  117.  
  118.         // Generates operators: <, <=, ==, !=, >=, >
  119.         operator<=><K>(rhs: Vec2<K>) override -> {
  120.             return [x, y] <=> [rhs.x, rhs.y];
  121.         }
  122.  
  123.         operator<=><K: std::is_arithmetic>(rhs: K) -> {
  124.             return [x, y] <=> [rhs, rhs];
  125.         }
  126.  
  127.         operator<<(stream: std::ostream*) -> {
  128.             return stream << '(' << x << ", " << y << ')';
  129.         }
  130.     }
  131. }
  132.  
  133.  
  134.  
  135. module std {
  136.     import std::compiler;
  137.     import std::platform;
  138.     import std::assert;
  139.  
  140.     // When a function or struct extents a metadata type, the type can be changed in a programmable way in compile time.
  141.  
  142.     $function entry_point {
  143.         $generate(source: std::reflect::function*) -> {
  144.             std::assert(!source.is_return_void(), "Entry point must not return a value");
  145.             std::assert(!source.parameters().empty(), "Entry point must not take any paramaters");
  146.             std::compiler::set_entry_point(source.id());
  147.         }
  148.     }
  149.  
  150.     $struct interface<T> {
  151.         ~T() -> {}
  152.  
  153.         $generate(source: std::reflect::struct*) -> {
  154.             foreach (var method in source.methods()) {
  155.                 std::assert(!method.is_public(), "Interfaces methods must be public");
  156.                 std::assert(!method.is_virtual(), "Interfaces methods must be virtual");
  157.             }
  158.  
  159.             std::assert(source.variables().empty(), "Interfaces must not contain data members");
  160.         }
  161.     }
  162.    
  163.     $struct non_copyable<T> {
  164.         T(T) delete;
  165.         T operator=(T) delete;
  166.  
  167.         $generate(source: std::reflect::struct*) -> {
  168.             //source.delete_copy_constructors();
  169.             //source.delete_copy_operators();
  170.         }
  171.     }
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement