rustpp_main: Fix exitcode semantics

This commit is contained in:
2025-07-11 16:59:44 -04:00
parent 3f0d6703ad
commit 1f9806a658
3 changed files with 23 additions and 13 deletions
+17 -10
View File
@@ -11,20 +11,22 @@
type Cstr = char *; type Cstr = char *;
/** The main function: does main things */
fn rustpp__main () -> i32;
mod termination { mod termination {
class Termination { struct ExitCode {
int code;
fn return_value () -> int { fn return_value () -> int {
return 0; return code;
} }
}; };
} }
/** The main function: does main things */
fn rustpp__main () -> termination::ExitCode;
mod option { mod option {
template <typename T> template <typename T>
class Option: termination::Termination { class Option {
union { union {
u8 None = 0; u8 None = 0;
T Some; T Some;
@@ -37,6 +39,10 @@ mod option {
public: public:
makeSelf (Option<T>); makeSelf (Option<T>);
operator termination::ExitCode () {
return { (int) variant };
}
static fn Some (T value) -> Self { static fn Some (T value) -> Self {
Self op; Self op;
op.value.Some = value; op.value.Some = value;
@@ -51,10 +57,6 @@ mod option {
return op; return op;
} }
fn return_value () -> int {
return self.is_some ();
}
fn is_some () -> bool { fn is_some () -> bool {
return variant == variant::Some; return variant == variant::Some;
} }
@@ -117,6 +119,10 @@ mod result {
public: public:
makeSelf (Result<T, E>); makeSelf (Result<T, E>);
operator termination::ExitCode () {
return { (int) variant };
}
// TODO: destructor for Result // TODO: destructor for Result
static fn Ok (T value) -> Self { static fn Ok (T value) -> Self {
@@ -372,7 +378,8 @@ mod env {
mod prelude { mod prelude {
use slice::Slice; use slice::Slice;
use option::Option, option::Some, option::None; use option::Option, option::Some, option::None;
use result::Result; use result::Result, result::Err, result::Ok;
use termination::ExitCode;
} }
+2 -2
View File
@@ -3,7 +3,7 @@
// rust.hh changes language semantics, so must be included last! // rust.hh changes language semantics, so must be included last!
#include "rust.hh" #include "rust.hh"
fn main () -> i32 { fn main () -> ExitCode {
let v = vec::Vec<i32>::new (); let v = vec::Vec<i32>::new ();
v.push (10); v.push (10);
@@ -21,5 +21,5 @@ fn main () -> i32 {
std::println ("{}", x); std::println ("{}", x);
} }
return 0; return Err<int, const char *> ("Oops!");
} }
+4 -1
View File
@@ -11,12 +11,15 @@ namespace {
} }
fn main (int argc, char **argv) -> int { fn main (int argc, char **argv) -> int {
int exitcode = 0;
program_args = { (Cstr *) argv, (usize) argc }; program_args = { (Cstr *) argv, (usize) argc };
try { try {
return rustpp__main (); exitcode = rustpp__main ().return_value ();
} catch (panic::Panic p) { } catch (panic::Panic p) {
std::println ("Thread exited with explicit panic: {}", p.what ()); std::println ("Thread exited with explicit panic: {}", p.what ());
exitcode = -1;
} }
return exitcode;
} }
// Environment // Environment