diff --git a/src/include/rust.hh b/src/include/rust.hh index c7f3fd9..baa2fbc 100644 --- a/src/include/rust.hh +++ b/src/include/rust.hh @@ -11,20 +11,22 @@ type Cstr = char *; -/** The main function: does main things */ -fn rustpp__main () -> i32; mod termination { - class Termination { + struct ExitCode { + int code; fn return_value () -> int { - return 0; + return code; } }; } +/** The main function: does main things */ +fn rustpp__main () -> termination::ExitCode; + mod option { template - class Option: termination::Termination { + class Option { union { u8 None = 0; T Some; @@ -37,6 +39,10 @@ mod option { public: makeSelf (Option); + operator termination::ExitCode () { + return { (int) variant }; + } + static fn Some (T value) -> Self { Self op; op.value.Some = value; @@ -51,10 +57,6 @@ mod option { return op; } - fn return_value () -> int { - return self.is_some (); - } - fn is_some () -> bool { return variant == variant::Some; } @@ -117,6 +119,10 @@ mod result { public: makeSelf (Result); + operator termination::ExitCode () { + return { (int) variant }; + } + // TODO: destructor for Result static fn Ok (T value) -> Self { @@ -372,7 +378,8 @@ mod env { mod prelude { use slice::Slice; use option::Option, option::Some, option::None; - use result::Result; + use result::Result, result::Err, result::Ok; + use termination::ExitCode; } diff --git a/src/main.cc b/src/main.cc index 0ab0454..aedb934 100644 --- a/src/main.cc +++ b/src/main.cc @@ -3,7 +3,7 @@ // rust.hh changes language semantics, so must be included last! #include "rust.hh" -fn main () -> i32 { +fn main () -> ExitCode { let v = vec::Vec::new (); v.push (10); @@ -21,5 +21,5 @@ fn main () -> i32 { std::println ("{}", x); } - return 0; + return Err ("Oops!"); } diff --git a/src/runtime.cc b/src/runtime.cc index 7da472b..a6cfcb7 100644 --- a/src/runtime.cc +++ b/src/runtime.cc @@ -11,12 +11,15 @@ namespace { } fn main (int argc, char **argv) -> int { + int exitcode = 0; program_args = { (Cstr *) argv, (usize) argc }; try { - return rustpp__main (); + exitcode = rustpp__main ().return_value (); } catch (panic::Panic p) { std::println ("Thread exited with explicit panic: {}", p.what ()); + exitcode = -1; } + return exitcode; } // Environment