Advertisement
mattparks5855

Hash

Jul 16th, 2019
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.37 KB | None | 0 0
  1. import std::io;
  2. // Any module that imports this module will also import std::core.
  3. import std::core public;
  4.  
  5. import std::random;
  6. import "stdio.h";
  7. import Program::Maths;
  8.  
  9. // Build steps can be written directly in the program!
  10. // No cmake or IDE specific build steps, called directly from the hash.exe compiler.
  11. $project Program1: std::project_generate {
  12.     $func generate(project: std::project*, target: std::build_target) override -> {
  13.         // A unique ID assigned to this project, generated from a website.
  14.         project.set_unique_id(0x371EE67);
  15.         // This version tag is used to generate a comparable version ID.
  16.         project.set_version("0.1.0");
  17.         // Other data can be written into a EXE.
  18.         project.set_copyright("Copyright 2019 Matthew Albrecht");
  19.         project.set_authors("Matthew Albrecht");
  20.         project.set_url("https://github.com/hash/hash.git");
  21.         project.set_icons(null);
  22.        
  23.         project.set_output_name("Project1");
  24.         project.set_type(std:::project::type::executable);
  25.         project.set_entry_point(Program::Main);
  26.        
  27.         // Libraries will set build order, these may be system libs, or C libs.
  28.         project.add_library("Library1", 0x2D74EA55, std::project::public);
  29.  
  30.         // Could set this projects compiler to another system version.
  31.         //project.set_compiler(std::project::get_compilers().first());
  32.  
  33.         // If a GNU compiler (Clang/GNU) extra build args can be added.
  34.         if (project.compiler & std::compiler::gnu) {
  35.             project.add_build_args("-Wall -Wextra -Wno-unknown-pragmas");
  36.  
  37.             if (target == std::build_target::debug) {
  38.                 project.add_build_args("-O1 -fvisibility-inlines-hidden");
  39.             } else if (target == std::build_target::release) {
  40.                 project.add_build_args("-O3 -fno-rtti");
  41.             }
  42.         }
  43.     }
  44. }
  45.  
  46. module Program {
  47.     // Main can be defined as any void global function.
  48.     func Main(): std::entry_point -> {
  49.         Example::Get().val1 = 32.0f;
  50.         std::println($"{Example::Get().val1}, {Example::Get().val2}, {Example::Get().VAL3}");
  51.        
  52.         add2Int32s := (a: int32, b: int32) -> int32 {
  53.             return a + b;
  54.         };
  55.         add2Values := <T: std::is_arithmetic>(a: T, b: T)[this] -> {
  56.             return a + b;
  57.         };
  58.         addValues := <T...: std::is_arithmetic>(args: T...) -> {
  59.             return (args + ...);
  60.         };
  61.        
  62.         // Ranged for between 0 and 99.
  63.         foreach (var i; 0..100) {
  64.         }
  65.        
  66.         // A more typical way to write a for loop.
  67.         for (i: int32; i < 100; i++) {
  68.         }
  69.  
  70.         // Normal while loop.
  71.         while (std::io::getin()) {
  72.         }
  73.  
  74.         // Do while loop.
  75.         do {
  76.         } while (std::io::getin());
  77.        
  78.         pos0 := Vec2(0.5f, -9.75f);
  79.         pos1 := Vec2(2.0f, 3.22f);
  80.  
  81.         pos2: Vec2<float>;
  82.         pos2 = Vec2(-1, 5);
  83.  
  84.         if (pos0 * pos1 != 0.0f) {
  85.             std::println($"{pos0} * {pos1} != 0.0f");
  86.         }
  87.  
  88.         printf("Direct call to C header: %f\n". std::random(-100.0f, 100.0f));
  89.     }
  90.  
  91.  
  92.     struct EnumAnimal : std::enum<int32> {
  93.         undefined, // 0
  94.         dog = 1,   // 1
  95.         cat,       // 2
  96.         frog = 8,  // 8
  97.         fish;      // 9
  98.     }
  99.     struct EnumBits : std::enum_bits<uint32> {
  100.         bit1,      // 1
  101.         bit2,      // 2
  102.         bit3,      // 4
  103.         bit5 = 16, // 16
  104.         bit6;      // 32
  105.     }
  106.     // (EnumBits.bit1 | EnumBits.bit2) | EnumBits.bit1 == true
  107.  
  108.  
  109.     // This-module-only statement
  110.     // Protected functions can be used in this namespace anywhere in this assembly.
  111.     func Internal0() protected -> {
  112.     }
  113.    
  114.     // This-module-only expression
  115.     // Private functions can only be used in this file.
  116.     func Internal0(): std::for_platform<std::platform::windows | std::platform::linux> private -> int32 {
  117.         return -10;
  118.     }
  119.  
  120.  
  121.     struct Example: Singleton<Example> {
  122.         val1: mutable float32;
  123.         val2 := Internal0();
  124.  
  125.         VAL3 static const: float64 = 26.924;
  126.         val4, val5, val6 : int32;
  127.  
  128.         func init() -> {
  129.             std::println("Created Example Singleton!");
  130.         }
  131.  
  132.         func deinit() -> {
  133.             std::println("Deleted Example Singleton!");
  134.         }
  135.  
  136.         func getVal1() const -> val1;
  137.         func setVal1(val1: int32) -> this.val1 = val1;
  138.  
  139.         // While being compiled methods are turned into functions:
  140.         // func getVal1(this: Example)[] const -> this.val1;
  141.         // func setVal1(this: Example, val1: int32) -> this.val1 = val1;
  142.     }
  143.    
  144.     struct Singleton<T>: std::non_copyable {
  145.         instance private static: T?;
  146.  
  147.         func Get() static -> T* {
  148.             if (instance == null) {
  149.                 instance = T();
  150.             }
  151.  
  152.             return instance;
  153.         }
  154.     }
  155.  
  156.  
  157.     struct Animal: std::pure_interface {
  158.         GetSound() const virtual -> std::string;
  159.     }
  160.    
  161.     struct Dog: Animal {
  162.         GetSound() const override -> std::string { return "Bark"; }
  163.     }
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement