github.com/goplus/llgo@v0.8.3/cl/_testdata/print/in.go (about) 1 package main 2 3 import ( 4 "unsafe" 5 6 "github.com/goplus/llgo/internal/runtime/c" 7 ) 8 9 func gwrite(b []byte) { 10 if len(b) == 0 { 11 return 12 } 13 c.Printf(c.Str("%s"), b) 14 } 15 16 func printbool(v bool) { 17 if v { 18 printstring("true") 19 } else { 20 printstring("false") 21 } 22 } 23 24 func printfloat(v float64) { 25 switch { 26 case v != v: 27 printstring("NaN") 28 return 29 case v+v == v && v > 0: 30 printstring("+Inf") 31 return 32 case v+v == v && v < 0: 33 printstring("-Inf") 34 return 35 } 36 37 const n = 7 // digits printed 38 var buf [n + 7]byte 39 buf[0] = '+' 40 e := 0 // exp 41 if v == 0 { 42 if 1/v < 0 { 43 buf[0] = '-' 44 } 45 } else { 46 if v < 0 { 47 v = -v 48 buf[0] = '-' 49 } 50 51 // normalize 52 for v >= 10 { 53 e++ 54 v /= 10 55 } 56 for v < 1 { 57 e-- 58 v *= 10 59 } 60 61 // round 62 h := 5.0 63 for i := 0; i < n; i++ { 64 h /= 10 65 } 66 v += h 67 if v >= 10 { 68 e++ 69 v /= 10 70 } 71 } 72 73 // format +d.dddd+edd 74 for i := 0; i < n; i++ { 75 s := int(v) 76 buf[i+2] = byte(s + '0') 77 v -= float64(s) 78 v *= 10 79 } 80 buf[1] = buf[2] 81 buf[2] = '.' 82 83 buf[n+2] = 'e' 84 buf[n+3] = '+' 85 if e < 0 { 86 e = -e 87 buf[n+3] = '-' 88 } 89 90 buf[n+4] = byte(e/100) + '0' 91 buf[n+5] = byte(e/10)%10 + '0' 92 buf[n+6] = byte(e%10) + '0' 93 gwrite(buf[:]) 94 } 95 96 func printuint(v uint64) { 97 var buf [100]byte 98 i := len(buf) 99 for i--; i > 0; i-- { 100 buf[i] = byte(v%10 + '0') 101 if v < 10 { 102 break 103 } 104 v /= 10 105 } 106 gwrite(buf[i:]) 107 } 108 109 func printint(v int64) { 110 if v < 0 { 111 printstring("-") 112 v = -v 113 } 114 printuint(uint64(v)) 115 } 116 117 var minhexdigits = 0 118 119 func printhex(v uint64) { 120 const dig = "0123456789abcdef" 121 var buf [100]byte 122 i := len(buf) 123 for i--; i > 0; i-- { 124 buf[i] = dig[v%16] 125 if v < 16 && len(buf)-i >= minhexdigits { 126 break 127 } 128 v /= 16 129 } 130 i-- 131 buf[i] = 'x' 132 i-- 133 buf[i] = '0' 134 gwrite(buf[i:]) 135 } 136 137 func printsp() { 138 printstring(" ") 139 } 140 141 func printnl() { 142 printstring("\n") 143 } 144 145 func printstring(s string) { 146 gwrite(bytes(s)) 147 } 148 149 type slice struct { 150 array unsafe.Pointer 151 len int 152 cap int 153 } 154 155 type stringStruct struct { 156 str unsafe.Pointer 157 len int 158 } 159 160 func stringStructOf(sp *string) *stringStruct { 161 return (*stringStruct)(unsafe.Pointer(sp)) 162 } 163 164 func bytes(s string) (ret []byte) { 165 rp := (*slice)(unsafe.Pointer(&ret)) 166 sp := stringStructOf(&s) 167 rp.array = sp.str 168 rp.len = sp.len 169 rp.cap = sp.len 170 return 171 } 172 173 func main() { 174 printstring("llgo") 175 printnl() 176 printuint(1024) 177 printnl() 178 printhex(0x1234abcf) 179 printnl() 180 prinxor(1) 181 printnl() 182 prinsub(100) 183 printnl() 184 prinusub(1<<64 - 1) 185 printnl() 186 prinfsub(100.1) 187 printnl() 188 printany(float32(1e9)) 189 printnl() 190 printany(float64(2e9)) 191 printnl() 192 var b bool = true 193 if b == true && b != false { 194 println("check bool", b) 195 } 196 n1 := 0b1001 197 n2 := 0b0011 198 println("check &^", n1&^n2 == 0b1000, n2&^n1 == 0b0010) 199 println(true, false, 'a', 'A', rune('δΈ'), 200 int8(1), int16(2), int32(3), int64(4), 5, 201 uint8(1), uint16(2), uint32(3), uint64(4), uintptr(5), 202 "llgo") 203 } 204 205 func println(args ...any) { 206 for i, v := range args { 207 if i != 0 { 208 printstring(" ") 209 } 210 printany(v) 211 } 212 printnl() 213 } 214 215 func printany(v any) { 216 switch v := v.(type) { 217 case bool: 218 printbool(v) 219 case int: 220 printint(int64(v)) 221 case int8: 222 printint(int64(v)) 223 case int16: 224 printint(int64(v)) 225 case int32: 226 printint(int64(v)) 227 case int64: 228 printint(int64(v)) 229 case uint: 230 printuint(uint64(v)) 231 case uint8: 232 printuint(uint64(v)) 233 case uint16: 234 printuint(uint64(v)) 235 case uint32: 236 printuint(uint64(v)) 237 case uint64: 238 printuint(uint64(v)) 239 case uintptr: 240 printuint(uint64(v)) 241 case float32: 242 printfloat(float64(v)) 243 case float64: 244 printfloat(float64(v)) 245 case string: 246 printstring(v) 247 } 248 } 249 250 func prinxor(n int64) { 251 printint(^n) 252 } 253 254 func prinsub(n int64) { 255 printint(-n) 256 } 257 258 func prinusub(n uint64) { 259 printuint(-n) 260 } 261 262 func prinfsub(n float64) { 263 printfloat(-n) 264 }