From 92c5cd59f5c475821173275c284e694fc5dae550 Mon Sep 17 00:00:00 2001 From: dimgigov Date: Sun, 31 May 2026 02:01:28 +0300 Subject: [PATCH] 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 --- examples/algebraic_enums.bux | 14 +++++++------- examples/enums.bux | 14 +++++++------- examples/factorial.bux | 14 +++++++------- examples/fibonacci.bux | 10 +++++----- examples/generics.bux | 16 ++++++++-------- examples/hello.bux | 4 ++-- examples/methods.bux | 30 +++++++++++++++--------------- examples/pattern_matching.bux | 16 ++++++++-------- examples/structs.bux | 18 +++++++++--------- stdlib/Std/Io.bux | 10 ++++++++++ stdlib/io.c | 12 ++++++------ 11 files changed, 84 insertions(+), 74 deletions(-) create mode 100644 stdlib/Std/Io.bux diff --git a/examples/algebraic_enums.bux b/examples/algebraic_enums.bux index 86aa4bc..ac0ac74 100644 --- a/examples/algebraic_enums.bux +++ b/examples/algebraic_enums.bux @@ -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; diff --git a/examples/enums.bux b/examples/enums.bux index fd62ac4..cd94316 100644 --- a/examples/enums.bux +++ b/examples/enums.bux @@ -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; } diff --git a/examples/factorial.bux b/examples/factorial.bux index 03d59f1..ee24f50 100644 --- a/examples/factorial.bux +++ b/examples/factorial.bux @@ -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; } diff --git a/examples/fibonacci.bux b/examples/fibonacci.bux index f30886e..d94e949 100644 --- a/examples/fibonacci.bux +++ b/examples/fibonacci.bux @@ -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; } diff --git a/examples/generics.bux b/examples/generics.bux index 1c96e3c..ae27518 100644 --- a/examples/generics.bux +++ b/examples/generics.bux @@ -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(a: T, b: T) -> T { if a > b { @@ -14,13 +14,13 @@ func Main() -> int { let m1: int = Max(10, 20); let m2: int = Max(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; } diff --git a/examples/hello.bux b/examples/hello.bux index e539cae..b776c66 100644 --- a/examples/hello.bux +++ b/examples/hello.bux @@ -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; } diff --git a/examples/methods.bux b/examples/methods.bux index 544642f..7efa8ca 100644 --- a/examples/methods.bux +++ b/examples/methods.bux @@ -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; } diff --git a/examples/pattern_matching.bux b/examples/pattern_matching.bux index 5162980..d3f1b96 100644 --- a/examples/pattern_matching.bux +++ b/examples/pattern_matching.bux @@ -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; } diff --git a/examples/structs.bux b/examples/structs.bux index 2d02d30..d4da35b 100644 --- a/examples/structs.bux +++ b/examples/structs.bux @@ -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; } diff --git a/stdlib/Std/Io.bux b/stdlib/Std/Io.bux new file mode 100644 index 0000000..c819829 --- /dev/null +++ b/stdlib/Std/Io.bux @@ -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; + +} diff --git a/stdlib/io.c b/stdlib/io.c index a8240cf..b9ed6d8 100644 --- a/stdlib/io.c +++ b/stdlib/io.c @@ -5,7 +5,7 @@ #include /* 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);