refactor: migrate examples to import Std::Io instead of raw extern func
- Create stdlib/Std/Io.bux with extern func declarations
- Rename C shim functions to short names (PrintLine, PrintInt, etc.)
- Update all 9 examples to use import Std::Io::{PrintLine, PrintInt};
- Remove manual extern func Std_Io_* declarations from examples
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
// Algebraic Enums - Enums with data (tagged unions)
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
extern func Std_Io_PrintInt(n: int);
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
|
||||
enum Result {
|
||||
Ok(int),
|
||||
@@ -15,14 +15,14 @@ func Main() -> int {
|
||||
r2.data.Err_0 = "error message";
|
||||
|
||||
if r1.tag == Result_Ok {
|
||||
Std_Io_PrintLine("r1 is Ok:");
|
||||
Std_Io_PrintInt(r1.data.Ok_0);
|
||||
Std_Io_PrintLine("");
|
||||
PrintLine("r1 is Ok:");
|
||||
PrintInt(r1.data.Ok_0);
|
||||
PrintLine("");
|
||||
}
|
||||
|
||||
if r2.tag == Result_Err {
|
||||
Std_Io_PrintLine("r2 is Err:");
|
||||
Std_Io_PrintLine(r2.data.Err_0);
|
||||
PrintLine("r2 is Err:");
|
||||
PrintLine(r2.data.Err_0);
|
||||
}
|
||||
|
||||
return 0;
|
||||
|
||||
+7
-7
@@ -1,6 +1,6 @@
|
||||
// Enums - Enumeration types
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
extern func Std_Io_PrintInt(n: int);
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
|
||||
enum Color {
|
||||
Red,
|
||||
@@ -24,11 +24,11 @@ func ColorName(c: Color) -> String {
|
||||
func Main() -> int {
|
||||
let myColor: Color = Color::Green;
|
||||
|
||||
Std_Io_PrintLine("My color is:");
|
||||
Std_Io_PrintLine(ColorName(myColor));
|
||||
Std_Io_PrintLine("Color value:");
|
||||
Std_Io_PrintInt(myColor as int);
|
||||
Std_Io_PrintLine("");
|
||||
PrintLine("My color is:");
|
||||
PrintLine(ColorName(myColor));
|
||||
PrintLine("Color value:");
|
||||
PrintInt(myColor as int);
|
||||
PrintLine("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Factorial - Recursive function
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
extern func Std_Io_PrintInt(n: int);
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
|
||||
func Factorial(n: int) -> int {
|
||||
if n <= 1 {
|
||||
@@ -10,15 +10,15 @@ func Factorial(n: int) -> int {
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
Std_Io_PrintLine("Factorials:");
|
||||
PrintLine("Factorials:");
|
||||
|
||||
var i: int = 1;
|
||||
while i <= 10 {
|
||||
let fact: int = Factorial(i);
|
||||
Std_Io_PrintInt(i);
|
||||
Std_Io_PrintLine("! = ");
|
||||
Std_Io_PrintInt(fact);
|
||||
Std_Io_PrintLine("");
|
||||
PrintInt(i);
|
||||
PrintLine("! = ");
|
||||
PrintInt(fact);
|
||||
PrintLine("");
|
||||
i = i + 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Fibonacci - Recursive function with while loop
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
extern func Std_Io_PrintInt(n: int);
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
|
||||
func Fibonacci(n: int) -> int {
|
||||
if n <= 1 {
|
||||
@@ -10,13 +10,13 @@ func Fibonacci(n: int) -> int {
|
||||
}
|
||||
|
||||
func Main() -> int {
|
||||
Std_Io_PrintLine("Fibonacci sequence:");
|
||||
PrintLine("Fibonacci sequence:");
|
||||
|
||||
var i: int = 0;
|
||||
while i < 10 {
|
||||
let fib: int = Fibonacci(i);
|
||||
Std_Io_PrintInt(fib);
|
||||
Std_Io_PrintLine("");
|
||||
PrintInt(fib);
|
||||
PrintLine("");
|
||||
i = i + 1;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Generics - Generic functions and structs
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
extern func Std_Io_PrintInt(n: int);
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
|
||||
func Max<T>(a: T, b: T) -> T {
|
||||
if a > b {
|
||||
@@ -14,13 +14,13 @@ func Main() -> int {
|
||||
let m1: int = Max<int>(10, 20);
|
||||
let m2: int = Max<int>(5, 3);
|
||||
|
||||
Std_Io_PrintLine("Max(10, 20) = ");
|
||||
Std_Io_PrintInt(m1);
|
||||
Std_Io_PrintLine("");
|
||||
PrintLine("Max(10, 20) = ");
|
||||
PrintInt(m1);
|
||||
PrintLine("");
|
||||
|
||||
Std_Io_PrintLine("Max(5, 3) = ");
|
||||
Std_Io_PrintInt(m2);
|
||||
Std_Io_PrintLine("");
|
||||
PrintLine("Max(5, 3) = ");
|
||||
PrintInt(m2);
|
||||
PrintLine("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
+2
-2
@@ -1,7 +1,7 @@
|
||||
// Hello World - Basic Bux program
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
func Main() -> int {
|
||||
Std_Io_PrintLine("Hello, Bux!");
|
||||
PrintLine("Hello, Bux!");
|
||||
return 0;
|
||||
}
|
||||
|
||||
+15
-15
@@ -1,6 +1,6 @@
|
||||
// Methods - Struct with methods using extend
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
extern func Std_Io_PrintInt(n: int);
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
|
||||
struct Rectangle {
|
||||
width: int;
|
||||
@@ -20,19 +20,19 @@ extend Rectangle {
|
||||
func Main() -> int {
|
||||
let rect: Rectangle = Rectangle { width: 10, height: 5 };
|
||||
|
||||
Std_Io_PrintLine("Rectangle:");
|
||||
Std_Io_PrintLine("Width = ");
|
||||
Std_Io_PrintInt(rect.width);
|
||||
Std_Io_PrintLine("");
|
||||
Std_Io_PrintLine("Height = ");
|
||||
Std_Io_PrintInt(rect.height);
|
||||
Std_Io_PrintLine("");
|
||||
Std_Io_PrintLine("Area = ");
|
||||
Std_Io_PrintInt(rect.Area());
|
||||
Std_Io_PrintLine("");
|
||||
Std_Io_PrintLine("Perimeter = ");
|
||||
Std_Io_PrintInt(rect.Perimeter());
|
||||
Std_Io_PrintLine("");
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Pattern Matching - Match expressions with algebraic enums
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
extern func Std_Io_PrintInt(n: int);
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
|
||||
enum Option {
|
||||
Some(int),
|
||||
@@ -20,13 +20,13 @@ func Main() -> int {
|
||||
|
||||
let opt2: Option = Option { tag: Option_None };
|
||||
|
||||
Std_Io_PrintLine("opt1 value: ");
|
||||
Std_Io_PrintInt(GetValue(opt1));
|
||||
Std_Io_PrintLine("");
|
||||
PrintLine("opt1 value: ");
|
||||
PrintInt(GetValue(opt1));
|
||||
PrintLine("");
|
||||
|
||||
Std_Io_PrintLine("opt2 value: ");
|
||||
Std_Io_PrintInt(GetValue(opt2));
|
||||
Std_Io_PrintLine("");
|
||||
PrintLine("opt2 value: ");
|
||||
PrintInt(GetValue(opt2));
|
||||
PrintLine("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
// Structs - Basic struct usage
|
||||
extern func Std_Io_PrintLine(s: String);
|
||||
extern func Std_Io_PrintInt(n: int);
|
||||
import Std::Io::{PrintLine, PrintInt};
|
||||
|
||||
|
||||
struct Point {
|
||||
x: int;
|
||||
@@ -17,13 +17,13 @@ func Main() -> int {
|
||||
let p2: Point = Point { x: 5, y: 15 };
|
||||
let sum: Point = AddPoints(p1, p2);
|
||||
|
||||
Std_Io_PrintLine("Point sum:");
|
||||
Std_Io_PrintLine("x = ");
|
||||
Std_Io_PrintInt(sum.x);
|
||||
Std_Io_PrintLine("");
|
||||
Std_Io_PrintLine("y = ");
|
||||
Std_Io_PrintInt(sum.y);
|
||||
Std_Io_PrintLine("");
|
||||
PrintLine("Point sum:");
|
||||
PrintLine("x = ");
|
||||
PrintInt(sum.x);
|
||||
PrintLine("");
|
||||
PrintLine("y = ");
|
||||
PrintInt(sum.y);
|
||||
PrintLine("");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
module Std::Io {
|
||||
|
||||
extern func PrintLine(s: String);
|
||||
extern func Print(s: String);
|
||||
extern func PrintInt(n: int);
|
||||
extern func PrintFloat(f: float64);
|
||||
extern func PrintBool(b: bool);
|
||||
extern func ReadLine() -> String;
|
||||
|
||||
}
|
||||
+6
-6
@@ -5,7 +5,7 @@
|
||||
#include <string.h>
|
||||
|
||||
/* PrintLine - print string with newline */
|
||||
void Std_Io_PrintLine(const char* s) {
|
||||
void PrintLine(const char* s) {
|
||||
if (s != NULL) {
|
||||
puts(s);
|
||||
} else {
|
||||
@@ -14,29 +14,29 @@ void Std_Io_PrintLine(const char* s) {
|
||||
}
|
||||
|
||||
/* Print - print string without newline */
|
||||
void Std_Io_Print(const char* s) {
|
||||
void Print(const char* s) {
|
||||
if (s != NULL) {
|
||||
printf("%s", s);
|
||||
}
|
||||
}
|
||||
|
||||
/* PrintInt - print integer */
|
||||
void Std_Io_PrintInt(int64_t n) {
|
||||
void PrintInt(int64_t n) {
|
||||
printf("%lld", (long long)n);
|
||||
}
|
||||
|
||||
/* PrintFloat - print float */
|
||||
void Std_Io_PrintFloat(double f) {
|
||||
void PrintFloat(double f) {
|
||||
printf("%g", f);
|
||||
}
|
||||
|
||||
/* PrintBool - print boolean */
|
||||
void Std_Io_PrintBool(int b) {
|
||||
void PrintBool(int b) {
|
||||
printf("%s", b ? "true" : "false");
|
||||
}
|
||||
|
||||
/* ReadLine - read line from stdin (simplified) */
|
||||
const char* Std_Io_ReadLine(void) {
|
||||
const char* ReadLine(void) {
|
||||
static char buffer[1024];
|
||||
if (fgets(buffer, sizeof(buffer), stdin) != NULL) {
|
||||
size_t len = strlen(buffer);
|
||||
|
||||
Reference in New Issue
Block a user