提交 2bb7def2 编写于 作者: chai2010's avatar chai2010

完善 runtime 的 print 函数

上级 46c94732
// 版权 @2023 凹语言 作者。保留所有权利。
#wa:linkname runtime.printBytes
func runtime_printBytes(b: []byte)
#wa:linkname runtime.printString
func runtime_printString(s: string)
#wa:linkname runtime.printSpace
func runtime_printSpace
#wa:linkname runtime.printNewline
func runtime_printNewline
#wa:linkname runtime.printBool
func runtime_printBool(b: bool)
#wa:linkname runtime.printF64
func runtime_printF64(f: f64)
#wa:linkname runtime.printU64
func runtime_printU64(v: u64)
#wa:linkname runtime.printI64
func runtime_printI64(v: i64)
#wa:linkname runtime.printHex
func runtime_printHex(v: u64)
#wa:linkname runtime.printSlice
func runtime_printSlice(x: []byte)
const (
MaxInt64 = 1<<63 - 1
MinInt64 = -1 << 63
MaxUint64 = 1<<64 - 1
)
func main {
runtime_printBytes([]byte{ 'a', 'b', '\n'})
runtime_printString("hello\n")
runtime_printString("== printBool ==\n")
runtime_printBool(true)
runtime_printSpace()
runtime_printBool(false)
runtime_printNewline()
runtime_printString("== printF64 ==\n")
runtime_printF64(123.45)
runtime_printNewline()
runtime_printString("== printI64 ==\n")
runtime_printI64(12345)
runtime_printNewline()
runtime_printI64(MinInt64)
runtime_printNewline()
runtime_printI64(MaxInt64)
runtime_printNewline()
runtime_printString("== printU64 ==\n")
runtime_printU64(12345)
runtime_printNewline()
runtime_printU64(0)
runtime_printNewline()
runtime_printU64(MaxUint64)
runtime_printNewline()
runtime_printU64(MaxUint64/2)
runtime_printNewline()
runtime_printString("== printHex ==\n")
runtime_printHex(0)
runtime_printNewline()
runtime_printHex(127)
runtime_printNewline()
runtime_printHex(255)
runtime_printNewline()
runtime_printString("== printSlice ==\n")
runtime_printSlice([]byte{'a', 'b', 'c'})
runtime_printNewline()
runtime_printSlice([]byte("cba123"))
runtime_printNewline()
}
......@@ -204,9 +204,13 @@
)
(func $$wa.runtime.i32_ref_to_ptr (param $b i32) (param $d i32) (result i32) ;;result = ptr
local.get $d
local.get $d
)
(func $$wa.runtime.slice_to_ptr (param $b i32) (param $d i32) (param $l i32) (param $c i32) (result i32) ;;result = ptr
local.get $d
local.get $d
)
(func $$wa.runtime.string_to_ptr (param $b i32) (param $d i32) (param $l i32) (result i32) ;;result = ptr
local.get $d
)
// 版权 @2023 凹语言 作者。保留所有权利。
func printBytes(b: []byte) {
if n := len(b); n > 0 {
puts(refToPtr_byteSlice(b), i32(n))
return
}
}
func printString(s: string) {
if n := len(s); n > 0 {
puts(refToPtr_string(s), i32(n))
return
}
}
func printSpace {
printString(" ")
}
func printNewline {
printString("\n")
}
func printBool(v: bool) {
if v {
printString("true")
} else {
printString("false")
}
}
func printF64(v: f64) {
switch {
case v != v:
printString("NaN")
return
case v+v == v && v > 0:
printString("+Inf")
return
case v+v == v && v < 0:
printString("-Inf")
return
}
const n = 7 // digits printed
buf: [n + 7]byte
buf[0] = '+'
e := 0 // exp
if v == 0 {
if 1/v < 0 {
buf[0] = '-'
}
} else {
if v < 0 {
v = -v
buf[0] = '-'
}
// normalize
for v >= 10 {
e++
v /= 10
}
for v < 1 {
e--
v *= 10
}
// round
h := 5.0
for i := 0; i < n; i++ {
h /= 10
}
v += h
if v >= 10 {
e++
v /= 10
}
}
// format +d.dddd+edd
for i := 0; i < n; i++ {
s := int(v)
buf[i+2] = byte(s + '0')
v -= float64(s)
v *= 10
}
buf[1] = buf[2]
buf[2] = '.'
buf[n+2] = 'e'
buf[n+3] = '+'
if e < 0 {
e = -e
buf[n+3] = '-'
}
buf[n+4] = byte(e/100) + '0'
buf[n+5] = byte(e/10)%10 + '0'
buf[n+6] = byte(e%10) + '0'
printBytes(buf[:])
}
func printU64(v: u64) {
buf: [100]byte
i := len(buf)
for i--; i > 0; i-- {
buf[i] = byte(v%10 + '0')
if v < 10 {
break
}
v /= 10
}
printBytes(buf[i:])
}
func printI64(v: i64) {
if v < 0 {
printString("-")
v = -v
}
printU64(u64(v))
}
func printHex(v: u64) {
const dig = "0123456789abcdef"
buf: [100]byte
i := len(buf)
for i--; i > 0; i-- {
buf[i] = dig[v%16]
if v < 16 {
break
}
v /= 16
}
i--
buf[i] = 'x'
i--
buf[i] = '0'
printBytes(buf[i:])
}
func printSlice(s: []byte) {
print("[", len(s), "/", cap(s), "]")
printHex(u64(refToPtr_byteSlice(s)))
}
func printEface() {
// todo(chai2010)
}
func printIface() {
// todo(chai2010)
}
......@@ -149,6 +149,9 @@ func I32_ref_to_ptr(t: *i32) => i32
#wa:linkname $wa.runtime.slice_to_ptr
func U8_slice_to_ptr(t: []byte) => i32
#wa:linkname $wa.runtime.string_to_ptr
func U8_string_to_ptr(s: string) => i32
func refToPtr_i32(p: *i32) => i32 {
return I32_ref_to_ptr(p)
}
......@@ -156,3 +159,7 @@ func refToPtr_i32(p: *i32) => i32 {
func refToPtr_byteSlice(t: []byte) => i32 {
return U8_slice_to_ptr(t)
}
func refToPtr_string(s: string) => i32 {
return U8_string_to_ptr(s)
}
......@@ -114,7 +114,7 @@ func waPuts(ptr: i32, len: i32) {
#wa:linkname $runtime.waPrintF64
func waPrintF64(v: f64) {
print_f64(v)
printF64(v)
}
func print_i32(i: i32) {
......@@ -158,29 +158,3 @@ func print_f64(f: f64) {
}
print_i32(fpart_1000)
}
/*
void ftoa(float n, char* res, int afterpoint)
{
// Extract integer part
int ipart = (int)n;
// Extract floating part
float fpart = n - (float)ipart;
// convert integer part to string
int i = intToStr(ipart, res, 0);
// check for display option after point
if (afterpoint != 0) {
res[i] = '.'; // add dot
// Get the value of fraction part upto given no.
// of points after dot. The third parameter
// is needed to handle cases like 233.007
fpart = fpart * pow(10, afterpoint);
intToStr((int)fpart, res + i + 1, afterpoint);
}
}
*/
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册