rustpp: Try to fix for loop semantics

This commit is contained in:
2025-07-11 16:34:49 -04:00
parent b6440e6f74
commit 3f0d6703ad
4 changed files with 75 additions and 42 deletions
+60 -31
View File
@@ -1,12 +1,12 @@
#pragma once
// all C/++ headers must be imported first
#include <cstdlib>
#include <print> // IWYU pragma: export
#include "panic.hh" // IWYU pragma: export
#include "primitives.hh" // IWYU pragma: export
// no reorder
// keywords.hh must come last; changes language semantics with macros
#include "keywords.hh" // IWYU pragma: export
type Cstr = char *;
@@ -37,37 +37,41 @@ mod option {
public:
makeSelf (Option<T>);
pub static fn Some (T value) -> Self {
static fn Some (T value) -> Self {
Self op;
op.value.Some = value;
op.variant = variant::Some;
return op;
}
pub static fn None (T *_type = (T *) (0)) -> Self {
static fn None (T *_type = (T *) (0)) -> Self {
Self op;
op.value.None = 0;
op.variant = variant::None;
return op;
}
pub fn return_value () -> int {
fn return_value () -> int {
return self.is_some ();
}
pub fn is_some () -> bool {
fn is_some () -> bool {
return variant == variant::Some;
}
pub fn is_none () -> bool {
fn is_none () -> bool {
return variant == variant::None;
}
pub operator bool () {
operator bool () {
return this->is_some ();
}
pub fn unwrap () -> T {
fn unwrap_unchecked () -> T {
return value.Some;
}
fn unwrap () -> T {
match (variant) {
arm (variant::Some, return value.Some);
arm (variant::None, panic ("Called Option::unwrap() on a None value"));
@@ -75,7 +79,7 @@ mod option {
}
template <typename U>
pub fn map (U (f) (T)) -> Option<U> {
fn map (U (f) (T)) -> Option<U> {
match (variant) {
arm (variant::Some, return Some (f (value.Some)));
arm (variant::None, return None<U> ());
@@ -115,42 +119,68 @@ mod result {
// TODO: destructor for Result
pub static fn Ok (T value) -> Self {
static fn Ok (T value) -> Self {
Self result;
result.value.Ok = value;
result.variant = variant::Ok;
return result;
}
pub static fn Err (E error) -> Self {
static fn Err (E error) -> Self {
Self result;
result.value.Err = error;
result.variant = variant::Err;
return result;
}
pub fn is_ok () -> bool {
fn is_ok () -> bool {
return variant == variant::Ok;
}
pub fn is_err () -> bool {
fn is_err () -> bool {
return variant == variant::Err;
}
pub fn unwrap () -> T {
fn ok () -> option::Option<T> {
match (variant) {
arm (variant::Ok, return option::Some (value.Ok));
arm (variant::Err, return option::None<T> ());
}
}
fn err () -> option::Option<E> {
match (variant) {
arm (variant::Err, return option::Some (value.Err));
arm (variant::Ok, return option::None<T> ());
}
}
fn unwrap () -> T {
match (variant) {
arm (variant::Ok, return value.Ok);
arm (variant::Err, panic ("Called Result::unwrap() on Err value"));
}
}
pub fn unwrap_err () -> E {
fn unwrap_err () -> E {
match (variant) {
arm (variant::Ok, panic ("Called Result::unwrap_err() on Ok value"));
arm (variant::Err, return value.Err);
}
}
};
/// Constructs a Result with variant Err and value `Err(value)`
template <typename T, typename E>
fn Err (E error) -> Result<T, E> {
return Result<T, E>::Err (error);
}
/// Constructs a Result with variant Ok and value `Ok(value)`
template <typename T, typename E>
fn Ok (T value) -> Result<T, E> {
return Result<T, E>::Ok (value);
}
}
mod ptr {
@@ -159,12 +189,11 @@ mod ptr {
T *value;
makeSelf (Nonnull<T>);
pub static fn __rustpp__new (T *value) -> option::Option<Self> {
switch (value) {
case nullptr:
return option::Some ({ value });
default:
return option::None (&value);
static fn rustpp__new (T *value) -> option::Option<Self> {
if (value) {
return option::Some ((Self) { value });
} else {
return option::None<Self> ();
}
}
@@ -200,7 +229,7 @@ mod iter {
mod slice {
template <typename T>
class Iter: public iter::Iterator<T *> {
class Iter: public iter::Iterator<ptr::Nonnull<T>> {
T *head;
T *tail;
@@ -209,15 +238,15 @@ mod slice {
public:
makeSelf (Iter<T>);
static fn __rustpp__new (T *head, usize len) -> Self {
static fn rustpp__new (T *head, usize len) -> Self {
return { head, head + len };
}
fn next () -> option::Option<T *> override {
fn next () -> option::Option<ptr::Nonnull<T>> override {
if (head < tail)
return option::Some (head++);
return ptr::Nonnull<T>::rustpp__new (head++);
else
return option::None<T *> ();
return option::None<ptr::Nonnull<T>> ();
}
fn into_iter () -> Self & {
@@ -232,7 +261,7 @@ mod slice {
makeSelf (Slice<T>);
fn into_iter () -> Iter<T> {
return Iter<T>::__rustpp__new (self.ptr, self.len);
return Iter<T>::rustpp__new (self.ptr, self.len);
}
};
}
@@ -260,7 +289,7 @@ mod vec {
return option::Some (*head++);
}
if (allocation) {
free(allocation);
free (allocation);
allocation = nullptr;
}
return option::None<T> ();
@@ -276,7 +305,7 @@ mod vec {
makeSelf (Vec<T>);
static fn __rustpp__new () -> Self {
static fn rustpp__new () -> Self {
return {};
}
@@ -347,4 +376,4 @@ mod prelude {
}
using mod prelude;
use mod prelude;