Skip to content

Commit

Permalink
Dynamic protocols.
Browse files Browse the repository at this point in the history
  • Loading branch information
lerno committed Oct 5, 2023
1 parent 4cc30c0 commit 49c4595
Show file tree
Hide file tree
Showing 106 changed files with 2,367 additions and 2,006 deletions.
4 changes: 2 additions & 2 deletions lib/std/collections/enummap.c3
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fn void EnumMap.init(&self, ValueType init_value)
}
}

fn usz! EnumMap.to_format(&self, Formatter* formatter) @dynamic
fn usz! EnumMap.to_format(&self, Formatter* formatter) : Printable
{
usz n = formatter.print("{ ")!;
foreach (i, &value : self.values)
Expand All @@ -25,7 +25,7 @@ fn usz! EnumMap.to_format(&self, Formatter* formatter) @dynamic
return n;
}

fn String EnumMap.to_string(&self, Allocator* using = mem::heap()) @dynamic
fn String EnumMap.to_string(&self, Allocator* using = mem::heap()) : Printable
{
return string::printf("%s", *self);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/std/collections/enumset.c3
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ fn EnumSet EnumSet.xor_of(&self, EnumSet s)
$endif
}

fn usz! EnumSet.to_format(&set, Formatter* formatter) @dynamic
fn usz! EnumSet.to_format(&set, Formatter* formatter) : Printable
{
usz n = formatter.print("[")!;
bool found;
Expand All @@ -140,7 +140,7 @@ fn usz! EnumSet.to_format(&set, Formatter* formatter) @dynamic
return n;
}

fn String EnumSet.to_string(&set, Allocator* using = mem::heap()) @dynamic
fn String EnumSet.to_string(&set, Allocator* using = mem::heap()) : Printable
{
return string::printf("%s", *set);
}
Expand Down
10 changes: 5 additions & 5 deletions lib/std/collections/list.c3
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import std::io;
import std::math;

def ElementPredicate = fn bool(Type *type);
def ElementTest = fn bool(Type *type, any context);
def ElementTest = fn bool(Type *type, any* context);
const ELEMENT_IS_EQUATABLE = types::is_equatable_type(Type);
const ELEMENT_IS_POINTER = Type.kindof == POINTER;

Expand Down Expand Up @@ -42,7 +42,7 @@ fn void List.tinit(&self, usz initial_capacity = 16)
self.init(initial_capacity, mem::temp()) @inline;
}

fn usz! List.to_format(&self, Formatter* formatter) @dynamic
fn usz! List.to_format(&self, Formatter* formatter) : Printable
{
switch (self.size)
{
Expand All @@ -62,7 +62,7 @@ fn usz! List.to_format(&self, Formatter* formatter) @dynamic
}
}

fn String List.to_string(&self, Allocator* using = mem::heap()) @dynamic
fn String List.to_string(&self, Allocator* using = mem::heap()) : Printable
{
return string::printf("%s", *self);
}
Expand Down Expand Up @@ -286,12 +286,12 @@ macro usz List._remove_if(&self, ElementPredicate filter, bool $invert) @local
return size - self.size;
}

fn usz List.remove_using_test(&self, ElementTest filter, any context)
fn usz List.remove_using_test(&self, ElementTest filter, any* context)
{
return self._remove_using_test(filter, false, context);
}

fn usz List.retain_using_test(&self, ElementTest filter, any context)
fn usz List.retain_using_test(&self, ElementTest filter, any* context)
{
return self._remove_using_test(filter, true, context);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/std/collections/object.c3
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ struct Object
}


fn usz! Object.to_format(&self, Formatter* formatter) @dynamic
fn usz! Object.to_format(&self, Formatter* formatter) : Printable
{
switch (self.type)
{
Expand Down
4 changes: 2 additions & 2 deletions lib/std/collections/priorityqueue.c3
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,12 @@ fn Type PrivatePriorityQueue.peek_at(&self, usz index) @operator([])
return self.heap[index];
}

fn usz! PrivatePriorityQueue.to_format(&self, Formatter* formatter) @dynamic
fn usz! PrivatePriorityQueue.to_format(&self, Formatter* formatter) : Printable
{
return self.heap.to_format(formatter);
}

fn String PrivatePriorityQueue.to_string(&self, Allocator* using = mem::heap()) @dynamic
fn String PrivatePriorityQueue.to_string(&self, Allocator* using = mem::heap()) : Printable
{
return self.heap.to_string(using);
}
8 changes: 4 additions & 4 deletions lib/std/collections/range.c3
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ fn Type Range.get(&self, usz index) @operator([])
return self.start + (Type)index;
}

fn String Range.to_string(&self, Allocator* using = mem::heap()) @dynamic
fn String Range.to_string(&self, Allocator* using = mem::heap()) : Printable
{
return string::printf("[%s..%s]", self.start, self.end);
}

fn usz! Range.to_format(&self, Formatter* formatter) @dynamic
fn usz! Range.to_format(&self, Formatter* formatter) : Printable
{
return formatter.printf("[%s..%s]", self.start, self.end)!;
}
Expand All @@ -56,12 +56,12 @@ fn bool ExclusiveRange.contains(&self, Type value) @inline
return value >= self.start && value < self.end;
}

fn usz! ExclusiveRange.to_format(&self, Formatter* formatter) @dynamic
fn usz! ExclusiveRange.to_format(&self, Formatter* formatter) : Printable
{
return formatter.printf("[%s..<%s]", self.start, self.end)!;
}

fn String ExclusiveRange.to_string(&self, Allocator* using = mem::heap()) @dynamic
fn String ExclusiveRange.to_string(&self, Allocator* using = mem::heap()) : Printable
{
return string::printf("[%s..<%s]", self.start, self.end);
}
Expand Down
17 changes: 16 additions & 1 deletion lib/std/core/builtin.c3
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ macro void @swap(&a, &b) @builtin
* @ensure @typeis(return, $Type*)
* @return! CastResult.TYPE_MISMATCH
**/
macro anycast(any v, $Type) @builtin
macro anycast(any* v, $Type) @builtin
{
if (v.type != $Type.typeid) return CastResult.TYPE_MISMATCH?;
return ($Type*)v.ptr;
Expand Down Expand Up @@ -190,6 +190,21 @@ macro void unsupported(String string = "Unsupported function invoked") @builtin
$$unreachable();
}

macro any_make(void* ptr, typeid type) @builtin
{
return $$any_make(ptr, type);
}

macro any.retype_to(&self, typeid type)
{
return $$any_make(self.ptr, type);
}

macro any.as_inner(&self)
{
return $$any_make(self.ptr, self.type.inner);
}

/**
* @param expr "the expression to cast"
* @param $Type "the type to cast to"
Expand Down
2 changes: 1 addition & 1 deletion lib/std/core/types.c3
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fault ConversionResult
/**
* @require $Type.kindof.is_int() || $Type.kindof == TypeKind.ENUM "Argument was not an integer"
**/
macro any_to_int(any v, $Type)
macro any_to_int(any* v, $Type)
{
typeid any_type = v.type;
TypeKind kind = any_type.kindof;
Expand Down
31 changes: 18 additions & 13 deletions lib/std/io/formatter.c3
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@ import libc;

const int PRINTF_NTOA_BUFFER_SIZE = 256;

fn String any.to_string(void* value, Allocator *using) @interface;
fn usz! any.to_format(void* value, Formatter* formatter) @interface;
protocol Printable
{
fn String to_string(&self, Allocator *using);
fn usz! to_format(&self, Formatter* formatter);
}

fault PrintFault
{
Expand Down Expand Up @@ -69,7 +72,7 @@ fn usz! Formatter.out(&self, char c) @private
return 1;
}

macro usz! Formatter.print_with_function(&self, any arg)
fn usz! Formatter.print_with_function(&self, Printable* arg)
{
if (&arg.to_format)
{
Expand Down Expand Up @@ -104,7 +107,7 @@ macro usz! Formatter.print_with_function(&self, any arg)
}


fn usz! Formatter.out_str(&self, any arg) @private
fn usz! Formatter.out_str(&self, any* arg) @private
{
switch (arg.type.kindof)
{
Expand All @@ -116,7 +119,7 @@ fn usz! Formatter.out_str(&self, any arg) @private
case FAULT:
return self.out_substr((*(anyfault*)arg.ptr).nameof);
case ANY:
return self.out_str(*(any*)arg);
return self.out_str(*(any**)arg);
case OPTIONAL:
unreachable();
case SIGNED_INT:
Expand Down Expand Up @@ -146,14 +149,16 @@ fn usz! Formatter.out_str(&self, any arg) @private
return self.out_substr(*(bool*)arg.ptr ? "true" : "false");
default:
}
usz! n = self.print_with_function(arg);
usz! n = self.print_with_function((Printable*)arg);
if (catch err = n)
{
case SearchResult.MISSING:
break;
default:
return err?;
} else {
}
else
{
return n;
}
switch (arg.type.kindof)
Expand All @@ -179,7 +184,7 @@ fn usz! Formatter.out_str(&self, any arg) @private
{
return self.out_substr(((DString*)arg).str_view());
}
return self.out_str(any { arg.ptr, arg.type.inner });
return self.out_str(arg.as_inner());
case POINTER:
PrintFlags flags = self.flags;
uint width = self.width;
Expand Down Expand Up @@ -211,7 +216,7 @@ fn usz! Formatter.out_str(&self, any arg) @private
for (usz i = 0; i < alen; i++)
{
if (i != 0) len += self.out_substr(", ")!;
len += self.out_str(any { ptr, inner })!;
len += self.out_str(any_make(ptr, inner))!;
ptr += size;
}
len += self.out(']')!;
Expand All @@ -236,7 +241,7 @@ fn usz! Formatter.out_str(&self, any arg) @private
for (usz i = 0; i < vlen; i++)
{
if (i != 0) len += self.out_substr(", ")!;
len += self.out_str(any { ptr, inner })!;
len += self.out_str(any_make(ptr, inner))!;
ptr += size;
}
len += self.out_substr(">]")!;
Expand Down Expand Up @@ -267,7 +272,7 @@ fn usz! Formatter.out_str(&self, any arg) @private
for (usz i = 0; i < slen; i++)
{
if (i != 0) len += self.out_substr(", ")!;
len += self.out_str(any { ptr, inner })!;
len += self.out_str(any_make(ptr, inner))!;
ptr += size;
}
len += self.out(']')!;
Expand All @@ -286,7 +291,7 @@ fn void! out_null_fn(void* data @unused, char c @unused) @private



fn usz! Formatter.vprintf(&self, String format, any[] anys)
fn usz! Formatter.vprintf(&self, String format, any*[] anys)
{
if (!self.out_fn)
{
Expand Down Expand Up @@ -353,7 +358,7 @@ fn usz! Formatter.vprintf(&self, String format, any[] anys)
// evaluate specifier
uint base = 0;
if (variant_index >= anys.len) return PrintFault.MISSING_ARG?;
any current = anys[variant_index++];
any* current = anys[variant_index++];
switch (c)
{
case 'd':
Expand Down
18 changes: 9 additions & 9 deletions lib/std/io/formatter_private.c3
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ fn usz! Formatter.adjust(&self, usz len) @local
return self.pad(' ', self.width, len);
}

fn uint128! int_from_any(any arg, bool *is_neg) @private
fn uint128! int_from_any(any* arg, bool *is_neg) @private
{
*is_neg = false;
if (arg.type.kindof == TypeKind.POINTER)
Expand All @@ -18,7 +18,7 @@ fn uint128! int_from_any(any arg, bool *is_neg) @private
}
if (arg.type.kindof == TypeKind.DISTINCT)
{
return int_from_any(any { arg.ptr, arg.type.inner }, is_neg);
return int_from_any(arg.as_inner(), is_neg);
}
switch (arg)
{
Expand Down Expand Up @@ -60,7 +60,7 @@ fn uint128! int_from_any(any arg, bool *is_neg) @private
}
}

fn FloatType! float_from_any(any arg) @private
fn FloatType! float_from_any(any* arg) @private
{
$if env::F128_SUPPORT:
if (arg.type == float128.typeid) return (FloatType)*((float128*)arg.ptr);
Expand All @@ -70,7 +70,7 @@ fn FloatType! float_from_any(any arg) @private
$endif
if (arg.type.kindof == TypeKind.DISTINCT)
{
return float_from_any(any { arg.ptr, arg.type.inner });
return float_from_any(arg.as_inner());
}
switch (arg)
{
Expand Down Expand Up @@ -585,14 +585,14 @@ fn usz! Formatter.ntoa_format(&self, String buf, usz len, bool negative, uint ba
}


fn usz! Formatter.ntoa_any(&self, any arg, uint base) @private
fn usz! Formatter.ntoa_any(&self, any* arg, uint base) @private
{
bool is_neg;
uint128 val = int_from_any(arg, &is_neg)!!;
return self.ntoa(val, is_neg, base) @inline;
}

fn usz! Formatter.out_char(&self, any arg) @private
fn usz! Formatter.out_char(&self, any* arg) @private
{
usz len = 1;
uint l = 1;
Expand Down Expand Up @@ -646,21 +646,21 @@ fn void! printf_advance_format(usz format_len, usz *index_ptr) @inline @private
if (val >= format_len) return FormattingFault.UNTERMINATED_FORMAT?;
}

fn any! next_any(any* args_ptr, usz args_len, usz* arg_index_ptr) @inline @private
fn any*! next_any(any** args_ptr, usz args_len, usz* arg_index_ptr) @inline @private
{
if (*arg_index_ptr >= args_len) return FormattingFault.MISSING_ARG?;
return args_ptr[(*arg_index_ptr)++];
}

fn int! printf_parse_format_field(
any* args_ptr, usz args_len, usz* args_index_ptr,
any** args_ptr, usz args_len, usz* args_index_ptr,
char* format_ptr, usz format_len, usz* index_ptr) @inline @private
{
char c = format_ptr[*index_ptr];
if (c.is_digit()) return simple_atoi(format_ptr, format_len, index_ptr);
if (c != '*') return 0;
printf_advance_format(format_len, index_ptr)!;
any val = next_any(args_ptr, args_len, args_index_ptr)!;
any* val = next_any(args_ptr, args_len, args_index_ptr)!;
if (!val.type.kindof.is_int()) return FormattingFault.INVALID_WIDTH_ARG?;
uint! intval = types::any_to_int(val, int);
return intval ?? FormattingFault.INVALID_WIDTH_ARG?;
Expand Down
4 changes: 2 additions & 2 deletions lib/std/io/path.c3
Original file line number Diff line number Diff line change
Expand Up @@ -449,12 +449,12 @@ fn void Path.free(self)
}


fn usz! Path.to_format(&self, Formatter* formatter) @dynamic
fn usz! Path.to_format(&self, Formatter* formatter) : Printable
{
return formatter.print(self.str_view());
}

fn String Path.to_string(&self, Allocator* using = mem::heap()) @dynamic
fn String Path.to_string(&self, Allocator* using = mem::heap()) : Printable
{
return self.str_view().copy(using);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/std/io/stream.c3
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ fn usz! Stream.available(&self) @inline
return IoError.NOT_SEEKABLE?;
}

fn usz! Stream.read_any(&self, any ref)
fn usz! Stream.read_any(&self, any* ref)
{
return self.read_all(ref.ptr[:ref.type.sizeof]);
}
Expand All @@ -94,7 +94,7 @@ fn usz! Stream.read_any(&self, any ref)
* @require ref.ptr != null
* @ensure return == ref.type.sizeof
*/
fn usz! Stream.write_any(&self, any ref)
fn usz! Stream.write_any(&self, any* ref)
{
return self.write_all(ref.ptr[:ref.type.sizeof]);
}
Expand Down
Loading

0 comments on commit 49c4595

Please sign in to comment.