mirror of
https://github.com/Ed94/Odin.git
synced 2026-08-06 07:38:48 +00:00
:: style procedure declarations; remove old parsing code
This commit is contained in:
+53
-53
@@ -34,14 +34,14 @@ FmtInfo :: struct {
|
||||
}
|
||||
|
||||
|
||||
proc make_string_buffer_from_slice(b: []u8) -> StringBuffer {
|
||||
make_string_buffer_from_slice :: proc(b: []u8) -> StringBuffer {
|
||||
return StringBuffer.Static{b};
|
||||
}
|
||||
|
||||
proc make_string_dynamic_buffer() -> StringBuffer {
|
||||
make_string_dynamic_buffer :: proc() -> StringBuffer {
|
||||
return StringBuffer.Dynamic{make([dynamic]u8)};
|
||||
}
|
||||
proc string_buffer_data(buf: ^StringBuffer) -> []u8 {
|
||||
string_buffer_data :: proc(buf: ^StringBuffer) -> []u8 {
|
||||
match b in buf {
|
||||
case StringBuffer.Static:
|
||||
return b.buf[..];
|
||||
@@ -50,7 +50,7 @@ proc string_buffer_data(buf: ^StringBuffer) -> []u8 {
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
proc string_buffer_data(buf: StringBuffer) -> []u8 {
|
||||
string_buffer_data :: proc(buf: StringBuffer) -> []u8 {
|
||||
match b in buf {
|
||||
case StringBuffer.Static:
|
||||
return b.buf[..];
|
||||
@@ -59,15 +59,15 @@ proc string_buffer_data(buf: StringBuffer) -> []u8 {
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
proc to_string(buf: StringBuffer) -> string {
|
||||
to_string :: proc(buf: StringBuffer) -> string {
|
||||
return string(string_buffer_data(buf));
|
||||
}
|
||||
|
||||
|
||||
proc write_string(buf: ^StringBuffer, s: string) {
|
||||
write_string :: proc(buf: ^StringBuffer, s: string) {
|
||||
write_bytes(buf, []u8(s));
|
||||
}
|
||||
proc write_bytes(buf: ^StringBuffer, data: []u8) {
|
||||
write_bytes :: proc(buf: ^StringBuffer, data: []u8) {
|
||||
match b in buf {
|
||||
case StringBuffer.Static:
|
||||
append(b.buf, ..data);
|
||||
@@ -75,7 +75,7 @@ proc write_bytes(buf: ^StringBuffer, data: []u8) {
|
||||
append(b.buf, ..data);
|
||||
}
|
||||
}
|
||||
proc write_byte(buf: ^StringBuffer, data: u8) {
|
||||
write_byte :: proc(buf: ^StringBuffer, data: u8) {
|
||||
match b in buf {
|
||||
case StringBuffer.Static:
|
||||
append(b.buf, data);
|
||||
@@ -83,7 +83,7 @@ proc write_byte(buf: ^StringBuffer, data: u8) {
|
||||
append(b.buf, data);
|
||||
}
|
||||
}
|
||||
proc write_rune(buf: ^StringBuffer, r: rune) {
|
||||
write_rune :: proc(buf: ^StringBuffer, r: rune) {
|
||||
if r < utf8.RUNE_SELF {
|
||||
write_byte(buf, u8(r));
|
||||
return;
|
||||
@@ -93,12 +93,12 @@ proc write_rune(buf: ^StringBuffer, r: rune) {
|
||||
write_bytes(buf, b[0..<n]);
|
||||
}
|
||||
|
||||
proc write_int(buf: ^StringBuffer, i: i128, base: int) {
|
||||
write_int :: proc(buf: ^StringBuffer, i: i128, base: int) {
|
||||
b: [129]u8;
|
||||
s := strconv.append_bits(b[0..<0], u128(i), base, true, 128, strconv.digits, 0);
|
||||
write_string(buf, s);
|
||||
}
|
||||
proc write_int(buf: ^StringBuffer, i: i64, base: int) {
|
||||
write_int :: proc(buf: ^StringBuffer, i: i64, base: int) {
|
||||
b: [129]u8;
|
||||
s := strconv.append_bits(b[0..<0], u128(i), base, true, 64, strconv.digits, 0);
|
||||
write_string(buf, s);
|
||||
@@ -106,7 +106,7 @@ proc write_int(buf: ^StringBuffer, i: i64, base: int) {
|
||||
|
||||
|
||||
|
||||
proc fprint(fd: os.Handle, args: ..any) -> int {
|
||||
fprint :: proc(fd: os.Handle, args: ..any) -> int {
|
||||
data: [_BUFFER_SIZE]u8;
|
||||
buf := make_string_buffer_from_slice(data[0..<0]);
|
||||
sbprint(&buf, ..args);
|
||||
@@ -115,7 +115,7 @@ proc fprint(fd: os.Handle, args: ..any) -> int {
|
||||
return len(res);
|
||||
}
|
||||
|
||||
proc fprintln(fd: os.Handle, args: ..any) -> int {
|
||||
fprintln :: proc(fd: os.Handle, args: ..any) -> int {
|
||||
data: [_BUFFER_SIZE]u8;
|
||||
buf := make_string_buffer_from_slice(data[0..<0]);
|
||||
sbprintln(&buf, ..args);
|
||||
@@ -123,7 +123,7 @@ proc fprintln(fd: os.Handle, args: ..any) -> int {
|
||||
os.write(fd, res);
|
||||
return len(res);
|
||||
}
|
||||
proc fprintf(fd: os.Handle, fmt: string, args: ..any) -> int {
|
||||
fprintf :: proc(fd: os.Handle, fmt: string, args: ..any) -> int {
|
||||
data: [_BUFFER_SIZE]u8;
|
||||
buf := make_string_buffer_from_slice(data[0..<0]);
|
||||
sbprintf(&buf, fmt, ..args);
|
||||
@@ -134,27 +134,27 @@ proc fprintf(fd: os.Handle, fmt: string, args: ..any) -> int {
|
||||
|
||||
|
||||
// print* procedures return the number of bytes written
|
||||
proc print (args: ..any) -> int { return fprint(os.stdout, ..args); }
|
||||
proc print_err (args: ..any) -> int { return fprint(os.stderr, ..args); }
|
||||
proc println (args: ..any) -> int { return fprintln(os.stdout, ..args); }
|
||||
proc println_err (args: ..any) -> int { return fprintln(os.stderr, ..args); }
|
||||
proc printf (fmt: string, args: ..any) -> int { return fprintf(os.stdout, fmt, ..args); }
|
||||
proc printf_err (fmt: string, args: ..any) -> int { return fprintf(os.stderr, fmt, ..args); }
|
||||
print :: proc(args: ..any) -> int { return fprint(os.stdout, ..args); }
|
||||
print_err :: proc(args: ..any) -> int { return fprint(os.stderr, ..args); }
|
||||
println :: proc(args: ..any) -> int { return fprintln(os.stdout, ..args); }
|
||||
println_err :: proc(args: ..any) -> int { return fprintln(os.stderr, ..args); }
|
||||
printf :: proc(fmt: string, args: ..any) -> int { return fprintf(os.stdout, fmt, ..args); }
|
||||
printf_err :: proc(fmt: string, args: ..any) -> int { return fprintf(os.stderr, fmt, ..args); }
|
||||
|
||||
|
||||
// aprint* procedures return a string that was allocated with the current context
|
||||
// They must be freed accordingly
|
||||
proc aprint(args: ..any) -> string {
|
||||
aprint :: proc(args: ..any) -> string {
|
||||
buf := make_string_dynamic_buffer();
|
||||
sbprint(&buf, ..args);
|
||||
return to_string(buf);
|
||||
}
|
||||
proc aprintln(args: ..any) -> string {
|
||||
aprintln :: proc(args: ..any) -> string {
|
||||
buf := make_string_dynamic_buffer();
|
||||
sbprintln(&buf, ..args);
|
||||
return to_string(buf);
|
||||
}
|
||||
proc aprintf(fmt: string, args: ..any) -> string {
|
||||
aprintf :: proc(fmt: string, args: ..any) -> string {
|
||||
buf := make_string_dynamic_buffer();
|
||||
sbprintf(&buf, fmt, ..args);
|
||||
return to_string(buf);
|
||||
@@ -163,15 +163,15 @@ proc aprintf(fmt: string, args: ..any) -> string {
|
||||
|
||||
// bprint* procedures return a string that was allocated with the current context
|
||||
// They must be freed accordingly
|
||||
proc bprint(buf: []u8, args: ..any) -> string {
|
||||
bprint :: proc(buf: []u8, args: ..any) -> string {
|
||||
sb := make_string_buffer_from_slice(buf[0..<0..<len(buf)]);
|
||||
return sbprint(&sb, ..args);
|
||||
}
|
||||
proc bprintln(buf: []u8, args: ..any) -> string {
|
||||
bprintln :: proc(buf: []u8, args: ..any) -> string {
|
||||
sb := make_string_buffer_from_slice(buf[0..<0..<len(buf)]);
|
||||
return sbprintln(&sb, ..args);
|
||||
}
|
||||
proc bprintf(buf: []u8, fmt: string, args: ..any) -> string {
|
||||
bprintf :: proc(buf: []u8, fmt: string, args: ..any) -> string {
|
||||
sb := make_string_buffer_from_slice(buf[0..<0..<len(buf)]);
|
||||
return sbprintf(&sb, fmt, ..args);
|
||||
}
|
||||
@@ -181,14 +181,14 @@ proc bprintf(buf: []u8, fmt: string, args: ..any) -> string {
|
||||
|
||||
|
||||
|
||||
proc fprint_type(fd: os.Handle, info: ^TypeInfo) {
|
||||
fprint_type :: proc(fd: os.Handle, info: ^TypeInfo) {
|
||||
data: [_BUFFER_SIZE]u8;
|
||||
buf := make_string_buffer_from_slice(data[0..<0]);
|
||||
write_type(&buf, info);
|
||||
os.write(fd, string_buffer_data(buf));
|
||||
}
|
||||
|
||||
proc write_type(buf: ^StringBuffer, ti: ^TypeInfo) {
|
||||
write_type :: proc(buf: ^StringBuffer, ti: ^TypeInfo) {
|
||||
if ti == nil -> return;
|
||||
|
||||
using TypeInfo;
|
||||
@@ -377,8 +377,8 @@ proc write_type(buf: ^StringBuffer, ti: ^TypeInfo) {
|
||||
}
|
||||
|
||||
|
||||
proc _parse_int(s: string, offset: int) -> (result: int, offset: int, ok: bool) {
|
||||
proc is_digit(r: rune) -> bool #inline {
|
||||
_parse_int :: proc(s: string, offset: int) -> (result: int, offset: int, ok: bool) {
|
||||
is_digit :: proc(r: rune) -> bool #inline {
|
||||
return '0' <= r && r <= '9';
|
||||
}
|
||||
|
||||
@@ -398,8 +398,8 @@ proc _parse_int(s: string, offset: int) -> (result: int, offset: int, ok: bool)
|
||||
return result, offset+i, i != 0;
|
||||
}
|
||||
|
||||
proc _arg_number(fi: ^FmtInfo, arg_index: int, format: string, offset, arg_count: int) -> (index, offset: int, ok: bool) {
|
||||
proc parse_arg_number(format: string) -> (int, int, bool) {
|
||||
_arg_number :: proc(fi: ^FmtInfo, arg_index: int, format: string, offset, arg_count: int) -> (index, offset: int, ok: bool) {
|
||||
parse_arg_number :: proc(format: string) -> (int, int, bool) {
|
||||
if len(format) < 3 {
|
||||
return 0, 1, false;
|
||||
}
|
||||
@@ -430,7 +430,7 @@ proc _arg_number(fi: ^FmtInfo, arg_index: int, format: string, offset, arg_count
|
||||
return arg_index, offset+width, false;
|
||||
}
|
||||
|
||||
proc int_from_arg(args: []any, arg_index: int) -> (int, int, bool) {
|
||||
int_from_arg :: proc(args: []any, arg_index: int) -> (int, int, bool) {
|
||||
num := 0;
|
||||
new_arg_index := arg_index;
|
||||
ok := true;
|
||||
@@ -456,7 +456,7 @@ proc int_from_arg(args: []any, arg_index: int) -> (int, int, bool) {
|
||||
}
|
||||
|
||||
|
||||
proc fmt_bad_verb(using fi: ^FmtInfo, verb: rune) {
|
||||
fmt_bad_verb :: proc(using fi: ^FmtInfo, verb: rune) {
|
||||
assert(verb != 'v');
|
||||
write_string(buf, "%!");
|
||||
write_rune(buf, verb);
|
||||
@@ -471,7 +471,7 @@ proc fmt_bad_verb(using fi: ^FmtInfo, verb: rune) {
|
||||
write_byte(buf, ')');
|
||||
}
|
||||
|
||||
proc fmt_bool(using fi: ^FmtInfo, b: bool, verb: rune) {
|
||||
fmt_bool :: proc(using fi: ^FmtInfo, b: bool, verb: rune) {
|
||||
match verb {
|
||||
case 't', 'v':
|
||||
write_string(buf, b ? "true" : "false");
|
||||
@@ -481,7 +481,7 @@ proc fmt_bool(using fi: ^FmtInfo, b: bool, verb: rune) {
|
||||
}
|
||||
|
||||
|
||||
proc fmt_write_padding(fi: ^FmtInfo, width: int) {
|
||||
fmt_write_padding :: proc(fi: ^FmtInfo, width: int) {
|
||||
if width <= 0 -> return;
|
||||
|
||||
pad_byte: u8 = fi.space ? ' ' : '0';
|
||||
@@ -491,7 +491,7 @@ proc fmt_write_padding(fi: ^FmtInfo, width: int) {
|
||||
for _ in 0..<count -> write_byte(fi.buf, pad_byte);
|
||||
}
|
||||
|
||||
proc _fmt_int(fi: ^FmtInfo, u: u128, base: int, is_signed: bool, bit_size: int, digits: string) {
|
||||
_fmt_int :: proc(fi: ^FmtInfo, u: u128, base: int, is_signed: bool, bit_size: int, digits: string) {
|
||||
_, neg := strconv.is_integer_negative(u128(u), is_signed, bit_size);
|
||||
|
||||
BUF_SIZE :: 256;
|
||||
@@ -563,7 +563,7 @@ proc _fmt_int(fi: ^FmtInfo, u: u128, base: int, is_signed: bool, bit_size: int,
|
||||
__DIGITS_LOWER := "0123456789abcdefx";
|
||||
__DIGITS_UPPER := "0123456789ABCDEFX";
|
||||
|
||||
proc fmt_rune(fi: ^FmtInfo, r: rune, verb: rune) {
|
||||
fmt_rune :: proc(fi: ^FmtInfo, r: rune, verb: rune) {
|
||||
match verb {
|
||||
case 'c', 'r', 'v':
|
||||
write_rune(fi.buf, r);
|
||||
@@ -572,7 +572,7 @@ proc fmt_rune(fi: ^FmtInfo, r: rune, verb: rune) {
|
||||
}
|
||||
}
|
||||
|
||||
proc fmt_int(fi: ^FmtInfo, u: u128, is_signed: bool, bit_size: int, verb: rune) {
|
||||
fmt_int :: proc(fi: ^FmtInfo, u: u128, is_signed: bool, bit_size: int, verb: rune) {
|
||||
match verb {
|
||||
case 'v': _fmt_int(fi, u, 10, is_signed, bit_size, __DIGITS_LOWER);
|
||||
case 'b': _fmt_int(fi, u, 2, is_signed, bit_size, __DIGITS_LOWER);
|
||||
@@ -596,7 +596,7 @@ proc fmt_int(fi: ^FmtInfo, u: u128, is_signed: bool, bit_size: int, verb: rune)
|
||||
}
|
||||
}
|
||||
|
||||
proc _pad(fi: ^FmtInfo, s: string) {
|
||||
_pad :: proc(fi: ^FmtInfo, s: string) {
|
||||
if !fi.width_set {
|
||||
write_string(fi.buf, s);
|
||||
return;
|
||||
@@ -611,7 +611,7 @@ proc _pad(fi: ^FmtInfo, s: string) {
|
||||
}
|
||||
}
|
||||
|
||||
proc fmt_float(fi: ^FmtInfo, v: f64, bit_size: int, verb: rune) {
|
||||
fmt_float :: proc(fi: ^FmtInfo, v: f64, bit_size: int, verb: rune) {
|
||||
match verb {
|
||||
// case 'e', 'E', 'f', 'F', 'g', 'G', 'v':
|
||||
// case 'f', 'F', 'v':
|
||||
@@ -653,7 +653,7 @@ proc fmt_float(fi: ^FmtInfo, v: f64, bit_size: int, verb: rune) {
|
||||
fmt_bad_verb(fi, verb);
|
||||
}
|
||||
}
|
||||
proc fmt_string(fi: ^FmtInfo, s: string, verb: rune) {
|
||||
fmt_string :: proc(fi: ^FmtInfo, s: string, verb: rune) {
|
||||
match verb {
|
||||
case 's', 'v':
|
||||
write_string(fi.buf, s);
|
||||
@@ -673,7 +673,7 @@ proc fmt_string(fi: ^FmtInfo, s: string, verb: rune) {
|
||||
}
|
||||
}
|
||||
|
||||
proc fmt_pointer(fi: ^FmtInfo, p: rawptr, verb: rune) {
|
||||
fmt_pointer :: proc(fi: ^FmtInfo, p: rawptr, verb: rune) {
|
||||
match verb {
|
||||
case 'p', 'v':
|
||||
// Okay
|
||||
@@ -688,7 +688,7 @@ proc fmt_pointer(fi: ^FmtInfo, p: rawptr, verb: rune) {
|
||||
_fmt_int(fi, u, 16, false, 8*size_of(rawptr), __DIGITS_UPPER);
|
||||
}
|
||||
|
||||
proc fmt_enum(fi: ^FmtInfo, v: any, verb: rune) {
|
||||
fmt_enum :: proc(fi: ^FmtInfo, v: any, verb: rune) {
|
||||
if v.type_info == nil || v.data == nil {
|
||||
write_string(fi.buf, "<nil>");
|
||||
return;
|
||||
@@ -759,7 +759,7 @@ proc fmt_enum(fi: ^FmtInfo, v: any, verb: rune) {
|
||||
}
|
||||
|
||||
|
||||
proc fmt_value(fi: ^FmtInfo, v: any, verb: rune) {
|
||||
fmt_value :: proc(fi: ^FmtInfo, v: any, verb: rune) {
|
||||
if v.data == nil || v.type_info == nil {
|
||||
write_string(fi.buf, "<nil>");
|
||||
return;
|
||||
@@ -926,7 +926,7 @@ proc fmt_value(fi: ^FmtInfo, v: any, verb: rune) {
|
||||
}
|
||||
}
|
||||
|
||||
proc fmt_complex(fi: ^FmtInfo, c: complex128, bits: int, verb: rune) {
|
||||
fmt_complex :: proc(fi: ^FmtInfo, c: complex128, bits: int, verb: rune) {
|
||||
match verb {
|
||||
case 'f', 'F', 'v':
|
||||
r, i := real(c), imag(c);
|
||||
@@ -943,15 +943,15 @@ proc fmt_complex(fi: ^FmtInfo, c: complex128, bits: int, verb: rune) {
|
||||
}
|
||||
}
|
||||
|
||||
proc _u128_to_lo_hi(a: u128) -> (lo, hi: u64) { return u64(a), u64(a>>64); }
|
||||
proc _i128_to_lo_hi(a: u128) -> (lo: u64 hi: i64) { return u64(a), i64(a>>64); }
|
||||
_u128_to_lo_hi :: proc(a: u128) -> (lo, hi: u64) { return u64(a), u64(a>>64); }
|
||||
_i128_to_lo_hi :: proc(a: u128) -> (lo: u64 hi: i64) { return u64(a), i64(a>>64); }
|
||||
|
||||
|
||||
proc do_foo(fi: ^FmtInfo, f: f64) {
|
||||
do_foo :: proc(fi: ^FmtInfo, f: f64) {
|
||||
fmt_string(fi, "Hellope$%!", 'v');
|
||||
}
|
||||
|
||||
proc fmt_arg(fi: ^FmtInfo, arg: any, verb: rune) {
|
||||
fmt_arg :: proc(fi: ^FmtInfo, arg: any, verb: rune) {
|
||||
if arg == nil {
|
||||
write_string(fi.buf, "<nil>");
|
||||
return;
|
||||
@@ -1006,7 +1006,7 @@ proc fmt_arg(fi: ^FmtInfo, arg: any, verb: rune) {
|
||||
|
||||
|
||||
|
||||
proc sbprint(buf: ^StringBuffer, args: ..any) -> string {
|
||||
sbprint :: proc(buf: ^StringBuffer, args: ..any) -> string {
|
||||
fi: FmtInfo;
|
||||
prev_string := false;
|
||||
|
||||
@@ -1023,7 +1023,7 @@ proc sbprint(buf: ^StringBuffer, args: ..any) -> string {
|
||||
return to_string(buf^);
|
||||
}
|
||||
|
||||
proc sbprintln(buf: ^StringBuffer, args: ..any) -> string {
|
||||
sbprintln :: proc(buf: ^StringBuffer, args: ..any) -> string {
|
||||
fi: FmtInfo;
|
||||
fi.buf = buf;
|
||||
|
||||
@@ -1036,7 +1036,7 @@ proc sbprintln(buf: ^StringBuffer, args: ..any) -> string {
|
||||
return to_string(buf^);
|
||||
}
|
||||
|
||||
proc sbprintf(b: ^StringBuffer, fmt: string, args: ..any) -> string {
|
||||
sbprintf :: proc(b: ^StringBuffer, fmt: string, args: ..any) -> string {
|
||||
fi: FmtInfo;
|
||||
arg_index: int = 0;
|
||||
end := len(fmt);
|
||||
|
||||
Reference in New Issue
Block a user