1
0
Fork 0
forked from fun/fun

Fixed bug with wrong line numbers in error messages. Some examples refactoring and bug fixes. (0.39.15)

This commit is contained in:
Johannes Findeisen 2026-03-30 20:51:04 +02:00
commit 3d3c11496f
23 changed files with 265 additions and 114 deletions

View file

@ -1,48 +0,0 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-11-26
*/
// Prepare sample DB from SQL if needed (requires sqlite3 CLI installed)
// Create it once with:
// sqlite3 ./database.sqlite < ./examples/data/database.sql
string db_path = "./database.sqlite"
number h = sqlite_open(db_path)
if h == 0
print("Failed to open DB: " + db_path)
exit(1)
rows = sqlite_query(h, "SELECT id, title, done, created_at FROM tasks ORDER BY id;")
print("Tasks (" + to_string(len(rows)) + "):")
for row in rows
string status = ""
if row["done"] == 1
status = ""
print("- [" + status + "] (#" + to_string(row["id"]) + ") " + to_string(row["title"]) + " " + to_string(row["created_at"]))
number rc = sqlite_exec(h, "INSERT INTO tasks (title, done) VALUES ('Try Fun + SQLite', 0);")
print("Insert rc=" + to_string(rc))
rows2 = sqlite_query(h, "SELECT count(*) AS cnt FROM tasks;")
print("Total tasks now: " + to_string(rows2[0]["cnt"]))
sqlite_close(h)
/* Example output:
Tasks (3):
- [✔] (#1) Write Fun + SQLite example 2025-11-26 23:22:04
- [✘] (#2) Ship optional feature flag 2025-11-26 23:22:04
- [✘] (#3) Celebrate with coffee 2025-11-26 23:22:04
Insert rc=0
Total tasks now: 4
*/

View file

@ -1,37 +0,0 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-10-13
*/
// Simple TCP echo server using built-in socket ops.
// Listens on localhost:12345 and echoes a single line/request back to each client.
port = 12345
backlog = 16
listen_fd = tcp_listen(port, backlog)
if (listen_fd <= 0)
print("tcp_listen failed")
exit(1)
print("Echo server listening on port " + to_string(port))
while (true)
client = tcp_accept(listen_fd)
if (client > 0)
data = sock_recv(client, 4096)
if (len(data) > 0)
// Echo back exactly what we received
sock_send(client, data)
sock_close(client)
// Not reached in this example, but here for completeness
sock_close(listen_fd)

View file

@ -1,32 +0,0 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-10-13
*/
// Echo server using the stdlib TcpServer class.
// Make sure FUN_LIB_DIR points to the ./lib directory containing io/socket.fun.
// Example:
// FUN_LIB_DIR="$(pwd)/lib" ./build/fun ./examples/tcp_echo_server_class.fun
include <io/socket.fun>
print("Starting echo server at localhost:12345")
server = TcpServer(12345, 16)
if (server.listen() <= 0)
print("Listen failed!")
exit(1)
else
print("Echo server (class) listening on port 12345")
// Serve clients forever, echoing back what they send.
server.serve_forever(4096)

View file

@ -1,59 +0,0 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-12-23
*/
#include <ui/tk.fun>
print("Initializing Fun File Manager...")
tk = TK()
tk.title("Fun File Manager")
// Current directory
dir = env("PWD")
if (dir == "")
dir = "."
print("Current directory: " + dir)
tk.label("path", "Current Dir: " + dir)
tk.pack("path")
// Files listbox
tk.listbox("files")
tk.pack("files")
// Populate listbox
fun refresh(tk, dir)
print("Refreshing file list for: " + dir)
tk.clear("files")
files = os_list_dir(dir)
print("Found " + to_string(len(files)) + " entries.")
for f in files
tk.insert("files", "end", f)
refresh(tk, dir)
// Refresh button
// Using tk.eval for button because tk.button currently exits the app.
tk.eval("button .refresh -text {Refresh} -command {puts {Refresh requested}}")
tk.pack("refresh")
// Exit button
tk.button("exit", "Exit")
tk.pack("exit")
print("Entering Tk loop...")
tk.loop()
print("Tk loop exited.")
/* Expected output:
A GUI... ;)
*/

View file

@ -1,33 +0,0 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-12-09
*/
// Demonstrates the Tk stdlib wrapper class using the new Tk opcodes.
include <ui/tk.fun>
tk = TK()
tk.title("Fun + Tk GUI")
tk.label("hello", "Hello, world!")
tk.pack("hello")
tk.button("ok", "OK")
tk.pack("ok")
// Enter GUI loop (no-op if built without FUN_WITH_TCLTK)
tk.loop()
/* Expected output:
A GUI... ;)
*/

View file

@ -1,38 +0,0 @@
#!/usr/bin/env fun
/*
* This file is part of the Fun programming language.
* https://fun-lang.xyz/
*
* Copyright 2025 Johannes Findeisen <you@hanez.org>
* Licensed under the terms of the Apache-2.0 license.
* https://opensource.org/license/apache-2-0
*
* Added: 2025-12-23
*/
#include <ui/tk.fun>
print("Testing os_list_dir...")
files = os_list_dir(".")
print("Found " + to_string(len(files)) + " files.")
if len(files) > 0
print("First file: " + files[0])
print("Testing tk_bind parsing...")
// We can't easily test Tk without an X server, but we can see if it crashes.
// If built with FUN_WITH_TCLTK, it should at least initialize.
// We use tk_eval to avoid full loop.
rc = tk_eval("set x 1")
print("tk_eval rc: " + to_string(rc))
if rc == 0
print("Tcl Result: " + tk_result())
/* Expected output:
Testing os_list_dir...
Found 23 files.
First file: build
Testing tk_bind parsing...
tk_eval rc: 0
Tcl Result: 1
*/