Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Imports a the file begining are imported in all modules defined in the file.
- import std::io;
- module Program {
- // Any module that imports this module will also import std::core.
- public import std::core;
- import std::random;
- import "stdio.h";
- import Program::Maths;
- // Main can be defined as any void global function.
- Main(): std::entry_point -> {
- Example::Get().val1 = 32.0f;
- std::println($"{Example::Get().val1}, {Example::Get().val2}, {Example::Get().VAL3}");
- var add2Values = []<T: std::is_arithmetic>(a: T, b: T) -> {
- return a + b;
- };
- var addValues = []<T...: std::is_arithmetic>(args: T...) -> {
- return (args + ...);
- };
- var pos0 = Vec2(0.5f, -9.75f);
- var pos1 = Vec2(2.0f, 3.22f);
- Vec2<float> pos2 = Vec2(-1, 5);
- if (pos0 * pos1 != 0.0f) {
- std::println($"{pos0} * {pos1} != 0.0f");
- }
- printf("Direct call to C header: %f\n". std::random(-100.0f, 100.0f));
- }
- struct Example: Singleton<Example> {
- val1 mutable: float32;
- val2: int16 = -12;
- VAL3 static const: float64 = 26.924;
- init() -> {
- std::println("Created Example Singleton!");
- }
- deinit() -> {
- std::println("Deleted Example Singleton!");
- }
- getVal1() const -> val1;
- setVal1(val1: int32) -> this.val1 = val1;
- }
- struct Singleton<T>: std::non_copyable {
- private instance static: T?;
- Get() static -> T* {
- if(instance == null) {
- instance = T();
- }
- return instance;
- }
- }
- struct Animal: std::interface {
- GetSound() const virtual -> std::string;
- }
- struct Dog: Animal {
- GetSound() const override -> std::string { return "Bark"; }
- }
- }
- // Only accessable in this target.
- private module Program::Maths {
- import std::error;
- import std::equality;
- import std::math;
- // A 2 tuple vector containing arithmetic values.
- struct Vec2<T: std::is_arithmetic> {
- T x, y;
- init(x: T, y: T) -> {
- this.x = x;
- this.y = y;
- }
- init<K: std::is_arithmetic>(x: K, y: K) -> {
- this.x = (T)x;
- this.y = (T)y;
- }
- operator[](index: int) -> T {
- switch(index) {
- case 0:
- return x;
- case 1:
- return y;
- default:
- throw std::error::out_of_bounds("Index but be between 0 and 2!");
- }
- }
- operator bool() -> {
- return this != 0;
- }
- operator+<K>(rhs: Vec2<K>) -> {
- return Vec2(x + rhs.x, y + rhs.y);
- }
- operator*<K>(rhs: Vec2<K>) -> {
- return Vec2(x * rhs.x, y * rhs.y);
- }
- // Generates operators: <, <=, ==, !=, >=, >
- operator<=><K>(rhs: Vec2<K>) override -> {
- return [x, y] <=> [rhs.x, rhs.y];
- }
- operator<=><K: std::is_arithmetic>(rhs: K) -> {
- return [x, y] <=> [rhs, rhs];
- }
- operator<<(stream: std::ostream*) -> {
- return stream << '(' << x << ", " << y << ')';
- }
- }
- }
- module std {
- import std::compiler;
- import std::platform;
- import std::assert;
- // When a function or struct extents a metadata type, the type can be changed in a programmable way in compile time.
- $function entry_point {
- $generate(source: std::reflect::function*) -> {
- std::assert(!source.is_return_void(), "Entry point must not return a value");
- std::assert(!source.parameters().empty(), "Entry point must not take any paramaters");
- std::compiler::set_entry_point(source.id());
- }
- }
- $struct interface<T> {
- ~T() -> {}
- $generate(source: std::reflect::struct*) -> {
- foreach (var method in source.methods()) {
- std::assert(!method.is_public(), "Interfaces methods must be public");
- std::assert(!method.is_virtual(), "Interfaces methods must be virtual");
- }
- std::assert(source.variables().empty(), "Interfaces must not contain data members");
- }
- }
- $struct non_copyable<T> {
- T(T) delete;
- T operator=(T) delete;
- $generate(source: std::reflect::struct*) -> {
- //source.delete_copy_constructors();
- //source.delete_copy_operators();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement