sqlite3, fizzbuzz examples

This commit is contained in:
2025-11-22 21:06:00 +01:00
parent 73cd71c8e4
commit b24bfc0241
4 changed files with 45 additions and 2 deletions

View File

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

10
examples/fizzbuzz.zr Normal file
View File

@@ -0,0 +1,10 @@
func main[] : I64
for i in 1..40
if i % 15 == 0
io.println("FizzBuzz")
else if i % 5 == 0
io.println("Buzz")
else if i %3 == 0
io.println("Fizz")
else
io.println_i64(i)

33
examples/sqlite_todo.zr Normal file
View File

@@ -0,0 +1,33 @@
// needs to be compiled with -m -C="-lsqlite3"
func main[] : I64
extern sqlite3_open
extern sqlite3_exec
extern sqlite3_prepare_v2
extern sqlite3_bind_text
extern sqlite3_step
extern sqlite3_errmsg
extern sqlite3_finalize
let rc: I64 = 0
let db: Ptr = mem.alloc(8)
let stmt: Ptr = mem.alloc(8)
rc = sqlite3_open("todo.db", db)
if rc
dbg.panic("failed to open db")
rc = sqlite3_exec(mem.read64(db), "CREATE TABLE IF NOT EXISTS todo(id INTEGER PRIMARY KEY AUTOINCREMENT, task TEXT NOT NULL);", 0, 0, 0)
if rc
dbg.panic(sqlite3_errmsg(mem.read64(db)))
sqlite3_prepare_v2(mem.read64(db), "INSERT INTO todo(task) VALUES(?);", -1, stmt, 0)
sqlite3_bind_text(mem.read64(stmt), 1, "World domination", -1, 0)
if sqlite3_step(mem.read64(stmt)) != 101
dbg.panic(sqlite3_errmsg(mem.read64(db)))
io.println("Task added!")
sqlite3_finalize(mem.read64(stmt))

View File

@@ -1,5 +1,5 @@
func dbg.panic[msg: String] : Void
io.print("PANIC:")
io.print("PANIC: ")
io.println(msg)
os.exit(1)