Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wa-lang
wa
提交
2bb7def2
wa
项目概览
wa-lang
/
wa
大约 1 年 前同步成功
通知
68
Star
655
Fork
45
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
wa
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
2bb7def2
编写于
7月 30, 2023
作者:
chai2010
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
完善 runtime 的 print 函数
上级
46c94732
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
254 addition
and
29 deletion
+254
-29
waroot/examples/runtime_print/main.wa
waroot/examples/runtime_print/main.wa
+84
-0
waroot/src/runtime/heap.wat.ws
waroot/src/runtime/heap.wat.ws
+6
-2
waroot/src/runtime/print.wa
waroot/src/runtime/print.wa
+156
-0
waroot/src/runtime/runtime.wa
waroot/src/runtime/runtime.wa
+7
-0
waroot/src/runtime/runtime_wasi.wa
waroot/src/runtime/runtime_wasi.wa
+1
-27
未找到文件。
waroot/examples/runtime_print/main.wa
0 → 100644
浏览文件 @
2bb7def2
// 版权 @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()
}
waroot/src/runtime/heap.wat.ws
浏览文件 @
2bb7def2
...
...
@@ -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
)
waroot/src/runtime/print.wa
0 → 100644
浏览文件 @
2bb7def2
// 版权 @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)
}
waroot/src/runtime/runtime.wa
浏览文件 @
2bb7def2
...
...
@@ -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)
}
waroot/src/runtime/runtime_wasi.wa
浏览文件 @
2bb7def2
...
...
@@ -114,7 +114,7 @@ func waPuts(ptr: i32, len: i32) {
#wa:linkname $runtime.waPrintF64
func waPrintF64(v: f64) {
print
_f
64(v)
print
F
64(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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录