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