implement str.from_i64

This commit is contained in:
2025-11-22 20:01:10 +01:00
parent 06c979f177
commit 73cd71c8e4
6 changed files with 47 additions and 27 deletions

View File

@@ -5,7 +5,7 @@ A very cool language
## Features
* Clean indentation-based syntax
* Compiles to x86_64 Assembly
* ~~No libc required~~ (SOON; still used for `malloc,realloc,free,snprintf,system,gethostbyname`)
* ~~No libc required~~ (SOON; still used for memory allocation and DNS resolution)
* Produces tiny static executables (~50KB with musl)
* Sometimes works
* Has the pipe operator

View File

@@ -105,8 +105,7 @@ section .text
"
);
// take that rustfmt
for name in "malloc,realloc,free,snprintf,system,gethostbyname".split(",") {
for name in &["malloc", "realloc", "free", "system", "gethostbyname"] {
emit!(&mut self.output, "extern {}", name);
emit!(&mut self.output, "c.{} equ {}", name, name);
}

View File

@@ -104,7 +104,7 @@ func str.len[s: String] : I64
func str.copy[s: String] : String
let size: I64 = str.len(s) + 1
let dup: String = mem.alloc(size)
for i in 0..size+1
for i in 0..size
str.set(dup, i, s[i])
return dup
@@ -203,10 +203,31 @@ func str.reverse[s: String] : String
str.set(out, len, 0)
return out
// not sure this covers all wacky edge cases
func str.from_i64[n: I64] : String
let x: String = mem.alloc(21)
c.snprintf(x, 21, "%ld", n)
return x
if n == 0
let s: String = mem.alloc(2)
str.set(s, 0, '0')
str.set(s, 1, 0)
return s
let neg: Bool = n < 0
if neg
n = -n
let buf: String = mem.alloc(21)
let i: I64 = 0
while n > 0
let d: U8 = n % 10
str.set(buf, i, '0' + d)
n = n / 10
i = i + 1
if neg
str.set(buf, i, '-')
i = i + 1
str.set(buf, i, 0)
let s: String = str.reverse(buf)
mem.free(buf)
return s
func str.parse_i64[s: String] : I64
let len: I64 = str.len(s)