test: expand golden C-codegen tests from 2 to 8 examples
Add golden tests for: structs, generics, algebraic_enums, enums, methods, strings. Each test captures the expected C output from the bootstrap compiler for regression detection. Also remove leftover 'PARSER FIX V2 ACTIVE' debug print from parser.bux.
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
[Package]
|
||||
Name = "algebraic_enums"
|
||||
Version = "0.1.0"
|
||||
Type = "bin"
|
||||
|
||||
[Build]
|
||||
Output = "Bin"
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,29 @@
|
||||
// Algebraic Enums - Enums with data (tagged unions)
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
|
||||
enum Result {
|
||||
Ok(int),
|
||||
Err(String)
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
let r1: Result = Result { tag: Result_Ok };
|
||||
r1.data.Ok_0 = 42;
|
||||
|
||||
let r2: Result = Result { tag: Result_Err };
|
||||
r2.data.Err_0 = "error message";
|
||||
|
||||
if r1.tag == Result_Ok {
|
||||
PrintLine("r1 is Ok:");
|
||||
PrintInt(r1.data.Ok_0);
|
||||
PrintLine("");
|
||||
}
|
||||
|
||||
if r2.tag == Result_Err {
|
||||
PrintLine("r2 is Err:");
|
||||
PrintLine(r2.data.Err_0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
[Package]
|
||||
Name = "enums"
|
||||
Version = "0.1.0"
|
||||
Type = "bin"
|
||||
|
||||
[Build]
|
||||
Output = "Bin"
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,34 @@
|
||||
// Enums - Enumeration types
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
|
||||
enum Color {
|
||||
Red,
|
||||
Green,
|
||||
Blue
|
||||
}
|
||||
|
||||
func ColorName(c: Color) -> String {
|
||||
if c == Color::Red {
|
||||
return "Red";
|
||||
}
|
||||
if c == Color::Green {
|
||||
return "Green";
|
||||
}
|
||||
if c == Color::Blue {
|
||||
return "Blue";
|
||||
}
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
let myColor: Color = Color::Green;
|
||||
|
||||
PrintLine("My color is:");
|
||||
PrintLine(ColorName(myColor));
|
||||
PrintLine("Color value:");
|
||||
PrintInt(myColor as int);
|
||||
PrintLine("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
[Package]
|
||||
Name = "generics"
|
||||
Version = "0.1.0"
|
||||
Type = "bin"
|
||||
|
||||
[Build]
|
||||
Output = "Bin"
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,26 @@
|
||||
// Generics - Generic functions and structs
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
|
||||
func Max<T>(a: T, b: T) -> T {
|
||||
if a > b {
|
||||
return a;
|
||||
} else {
|
||||
return b;
|
||||
}
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
let m1: int = Max<int>(10, 20);
|
||||
let m2: int = Max<int>(5, 3);
|
||||
|
||||
PrintLine("Max(10, 20) = ");
|
||||
PrintInt(m1);
|
||||
PrintLine("");
|
||||
|
||||
PrintLine("Max(5, 3) = ");
|
||||
PrintInt(m2);
|
||||
PrintLine("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
[Package]
|
||||
Name = "methods"
|
||||
Version = "0.1.0"
|
||||
Type = "bin"
|
||||
|
||||
[Build]
|
||||
Output = "Bin"
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,38 @@
|
||||
// Methods - Struct with methods using extend
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
|
||||
struct Rectangle {
|
||||
width: int;
|
||||
height: int;
|
||||
}
|
||||
|
||||
extend Rectangle {
|
||||
func Area(self: Rectangle) -> int {
|
||||
return self.width * self.height;
|
||||
}
|
||||
|
||||
func Perimeter(self: Rectangle) -> int {
|
||||
return 2 * (self.width + self.height);
|
||||
}
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
let rect: Rectangle = Rectangle { width: 10, height: 5 };
|
||||
|
||||
PrintLine("Rectangle:");
|
||||
PrintLine("Width = ");
|
||||
PrintInt(rect.width);
|
||||
PrintLine("");
|
||||
PrintLine("Height = ");
|
||||
PrintInt(rect.height);
|
||||
PrintLine("");
|
||||
PrintLine("Area = ");
|
||||
PrintInt(rect.Area());
|
||||
PrintLine("");
|
||||
PrintLine("Perimeter = ");
|
||||
PrintInt(rect.Perimeter());
|
||||
PrintLine("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
[Package]
|
||||
Name = "strings"
|
||||
Version = "0.1.0"
|
||||
Type = "bin"
|
||||
|
||||
[Build]
|
||||
Output = "Bin"
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,34 @@
|
||||
// Strings - String manipulation with Std::String
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
import Std::String::{String_Len, String_Eq, String_Concat};
|
||||
|
||||
func Main() -> int {
|
||||
let hello: String = "Hello";
|
||||
let world: String = "World";
|
||||
|
||||
PrintLine("String operations:");
|
||||
|
||||
// Length
|
||||
PrintLine("Length of 'Hello':");
|
||||
PrintInt(String_Len(hello));
|
||||
PrintLine("");
|
||||
|
||||
// Concatenation
|
||||
let greeting: String = String_Concat(hello, ", ");
|
||||
let greeting2: String = String_Concat(greeting, world);
|
||||
let full: String = String_Concat(greeting2, "!");
|
||||
|
||||
PrintLine("Concatenated:");
|
||||
PrintLine(full);
|
||||
|
||||
// Equality
|
||||
if String_Eq(hello, "Hello") {
|
||||
PrintLine("'Hello' equals 'Hello'");
|
||||
}
|
||||
|
||||
if !String_Eq(hello, world) {
|
||||
PrintLine("'Hello' does not equal 'World'");
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
[Package]
|
||||
Name = "structs"
|
||||
Version = "0.1.0"
|
||||
Type = "bin"
|
||||
|
||||
[Build]
|
||||
Output = "Bin"
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,29 @@
|
||||
// Structs - Basic struct usage
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
|
||||
struct Point {
|
||||
x: int;
|
||||
y: int;
|
||||
}
|
||||
|
||||
func AddPoints(a: Point, b: Point) -> Point {
|
||||
let result: Point = Point { x: a.x + b.x, y: a.y + b.y };
|
||||
return result;
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
let p1: Point = Point { x: 10, y: 20 };
|
||||
let p2: Point = Point { x: 5, y: 15 };
|
||||
let sum: Point = AddPoints(p1, p2);
|
||||
|
||||
PrintLine("Point sum:");
|
||||
PrintLine("x = ");
|
||||
PrintInt(sum.x);
|
||||
PrintLine("");
|
||||
PrintLine("y = ");
|
||||
PrintInt(sum.y);
|
||||
PrintLine("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user