Created
June 23, 2018 15:04
-
-
Save Leandros/07d8e14da7809b6ea93bf560a92f0125 to your computer and use it in GitHub Desktop.
Zlang Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vec3; | |
import std::Concepts; | |
import std::Gc; | |
func Square(x: int) -> int { | |
return x * x; | |
} | |
/* PrintTest is overloaded on different concepts. */ | |
func PrintTest(x: Concepts::Number) { | |
Print("Number is {}\n", x); | |
} | |
func PrintTest(x: Concepts::Any) { | |
Print("<unknown> is {}\n", x); | |
} | |
/* Create a new concept for a PrintTest specialization. */ | |
concept Vec = Vec3<float> | Vec3<int>; | |
func PrintTest(x: Vec) { | |
Print("Vec3 is Vec3<{}>({}, {}, {})\n", | |
typeof(x), x.x, x.y, x.z); | |
} | |
func main() { | |
PrintLn("Hello, World!"); | |
const x = 5; | |
const result = Square(x); | |
PrintNumber(result); | |
/* GC'ed Vector, allocated on the heap. */ | |
const vec1 = Gc::New Vec3<float>(1, 1, 0); | |
/* Vector allocated on the stack. */ | |
const vec2 = vec1 + Vec3<float>(1, 0, 1); | |
Assert(vec == Vec3<float>(2, 1, 1), "whoops!"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@export(T=float) | |
@export(T=int) | |
struct Vec3 { | |
x: T; | |
y: T; | |
z: T; | |
} | |
func Vec3::+(other: Vec3 *) -> Vec3 { | |
return Vec3( | |
this.x + other.x, | |
this.y + other.y, | |
this.z + other.z); | |
} | |
func Vec3::+=(other: Vec3 *) -> Vec3 * { | |
this.x += other.x; | |
this.y += other.y; | |
this.z += other.z; | |
return this; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment