Working on print
This commit is contained in:
@@ -2,8 +2,7 @@
|
|||||||
|
|
||||||
First doable version:
|
First doable version:
|
||||||
|
|
||||||
- [ ] Any stuff working well
|
- [ ] Test and bulletproof any, slices
|
||||||
- [ ] Var args working
|
|
||||||
|
|
||||||
In the future
|
In the future
|
||||||
|
|
||||||
|
|||||||
@@ -8,7 +8,58 @@ AnyArguments :: (values: []Any)
|
|||||||
Assert(*(values[0].data->*S64) == 10)
|
Assert(*(values[0].data->*S64) == 10)
|
||||||
Assert(*(values[1].data->*S64) == 20)
|
Assert(*(values[1].data->*S64) == 20)
|
||||||
|
|
||||||
// printf :: #foreign (string: *char, args: ..): int
|
/**
|
||||||
|
* C++ version 0.4 char* style "itoa":
|
||||||
|
* Written by Lukás Chmela
|
||||||
|
* Released under GPLv3.
|
||||||
|
*/
|
||||||
|
ItoaLookupTable := "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"
|
||||||
|
Itoa :: (value: S64, result: *U8, base: S64): *U8
|
||||||
|
// check that the base if valid
|
||||||
|
if (base < 2) || (base > 36)
|
||||||
|
*result = 0 // '
|
||||||
|
return result
|
||||||
|
|
||||||
|
ptr := result
|
||||||
|
ptr1 := result
|
||||||
|
tmp_char: U8
|
||||||
|
tmp_value: S64
|
||||||
|
|
||||||
|
for value != 0
|
||||||
|
tmp_value = value
|
||||||
|
value /= base
|
||||||
|
|
||||||
|
|
||||||
|
// @todo: FIX FIX FIX String is untyped
|
||||||
|
*ptr++ = ItoaLookupTable[35 + (tmp_value - value * base)]
|
||||||
|
|
||||||
|
// Apply negative sign
|
||||||
|
if tmp_value < 0
|
||||||
|
*ptr++ = '-'
|
||||||
|
*ptr-- = 0
|
||||||
|
for ptr1 < ptr
|
||||||
|
tmp_char = *ptr
|
||||||
|
*ptr-- = *ptr1
|
||||||
|
*ptr1++ = tmp_char
|
||||||
|
return result
|
||||||
|
|
||||||
|
FormatString :: (buffer: *U8, buffer_len: U64, string: String, args: ..)
|
||||||
|
arg_counter := 0
|
||||||
|
for i := 0, i < Len(string), i+=1
|
||||||
|
if string[i] == '%'
|
||||||
|
Assert(arg_counter < Len(args), "Passed too many [%] to the string formating function")
|
||||||
|
arg := args[arg_counter++]
|
||||||
|
|
||||||
|
if arg.type == S64
|
||||||
|
value := *(arg.data->*S64)
|
||||||
|
itoa_buff: [64]U8
|
||||||
|
p := Itoa(value, &itoa_buff[0], 10)
|
||||||
|
for *p != 0
|
||||||
|
buffer[buffer_len++] = *p++
|
||||||
|
else;; Assert(false)
|
||||||
|
else
|
||||||
|
buffer[buffer_len++] = string[i]
|
||||||
|
|
||||||
|
|
||||||
main :: (): int
|
main :: (): int
|
||||||
a := 10
|
a := 10
|
||||||
@@ -24,12 +75,8 @@ main :: (): int
|
|||||||
Assert(*(c.data->*S64) == 30)
|
Assert(*(c.data->*S64) == 30)
|
||||||
d := VariadicArguments("Test", {b,a})
|
d := VariadicArguments("Test", {b,a})
|
||||||
Assert(*(d.data->*S64) == b)
|
Assert(*(d.data->*S64) == b)
|
||||||
e := VariadicArguments("Test", {b,a})
|
|
||||||
|
|
||||||
Assert(*(values[0].data->*S64) == 10)
|
Assert(*(values[0].data->*S64) == 10)
|
||||||
Assert(*(values[1].data->*S64) == 20)
|
Assert(*(values[1].data->*S64) == 20)
|
||||||
|
|
||||||
// @todo: maybe this is better? Assert(*cast(*S64)values[0].data == 10)
|
|
||||||
|
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
@@ -63,55 +63,3 @@ Time :: (): F64
|
|||||||
Assert(err != 0)
|
Assert(err != 0)
|
||||||
result := query->F64 / PerformanceFrequency
|
result := query->F64 / PerformanceFrequency
|
||||||
return result
|
return result
|
||||||
|
|
||||||
/**
|
|
||||||
* C++ version 0.4 char* style "itoa":
|
|
||||||
* Written by Lukás Chmela
|
|
||||||
* Released under GPLv3.
|
|
||||||
*/
|
|
||||||
Itoa :: (value: S64, result: *U8, base: S64): *U8
|
|
||||||
// check that the base if valid
|
|
||||||
if (base < 2) || (base > 36)
|
|
||||||
*result = 0 // '
|
|
||||||
return result
|
|
||||||
|
|
||||||
ptr := result
|
|
||||||
ptr1 := result
|
|
||||||
tmp_char: U8
|
|
||||||
tmp_value: S64
|
|
||||||
|
|
||||||
for value != 0
|
|
||||||
tmp_value = value
|
|
||||||
value /= base
|
|
||||||
*ptr++ = "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" [35 + (tmp_value - value * base)]
|
|
||||||
|
|
||||||
// Apply negative sign
|
|
||||||
if tmp_value < 0
|
|
||||||
*ptr++ = '-'
|
|
||||||
*ptr-- = 0
|
|
||||||
for ptr1 < ptr
|
|
||||||
tmp_char = *ptr
|
|
||||||
*ptr-- = *ptr1
|
|
||||||
*ptr1++ = tmp_char
|
|
||||||
return result
|
|
||||||
|
|
||||||
|
|
||||||
Print :: (string: String, args: ..)
|
|
||||||
buffer: [1024]U8
|
|
||||||
buffer_len: S64
|
|
||||||
|
|
||||||
arg_counter := 0
|
|
||||||
for i := 0, i < Len(string), i+=1
|
|
||||||
if string[i] == '%'
|
|
||||||
Assert(arg_counter < Len(args), "Passing too many [%] to a print lambda")
|
|
||||||
arg := args[arg_counter++]
|
|
||||||
|
|
||||||
if arg.type == S64
|
|
||||||
value := *(arg.data->*S64)
|
|
||||||
itoa_buff: [64]U8
|
|
||||||
p := itoa(value, &itoa_buff[0], 10)
|
|
||||||
for *p != 0
|
|
||||||
buffer[buffer_len++] = *p++
|
|
||||||
else;; Assert(false)
|
|
||||||
else
|
|
||||||
buffer[buffer_len++] = string[i]
|
|
||||||
Reference in New Issue
Block a user