From 8e895dafcc94ce2475cff8e0e1e7d4969b02f58f Mon Sep 17 00:00:00 2001 From: hanez Date: Mon, 27 Apr 2026 04:25:14 +0200 Subject: [PATCH] More fun with scripts/play.fun. No core code logic changes. (0.41.5) --- CMakeLists.txt | 2 +- scripts/play | 39 +++++++++++++++++++++++++++++ scripts/play.fun | 65 +++++++++++++++++++++++++++++++++++++++++++++--- 3 files changed, 101 insertions(+), 5 deletions(-) create mode 100755 scripts/play diff --git a/CMakeLists.txt b/CMakeLists.txt index f36ca91..6d70407 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.10) -project(fun VERSION 0.41.4 LANGUAGES C) +project(fun VERSION 0.41.5 LANGUAGES C) set(CMAKE_C_STANDARD 99) set(CMAKE_C_STANDARD_REQUIRED ON) diff --git a/scripts/play b/scripts/play new file mode 100755 index 0000000..04faf0a --- /dev/null +++ b/scripts/play @@ -0,0 +1,39 @@ +#!/usr/bin/env bash +set -euo pipefail + +# Wrapper to run scripts/play.fun without requiring 'fun' on PATH. +# Honors FUN_BIN if provided, otherwise auto-detects a local build, then PATH. + +ROOT="$(cd "$(dirname "$0")"/.. && pwd)" +PLAY_FUN="$ROOT/scripts/play.fun" + +BIN="${FUN_BIN:-}" +if [[ -z "$BIN" ]]; then + # Try common local build locations + for cand in \ + "$ROOT/build/fun" \ + "$ROOT/build_debug/fun" \ + "$ROOT/build_release/fun" \ + "$ROOT/fun"; do + if [[ -x "$cand" ]]; then + BIN="$cand" + break + fi + done +fi + +if [[ -z "$BIN" ]]; then + if command -v fun >/dev/null 2>&1; then + BIN="fun" + else + echo "error: fun interpreter not found. Set FUN_BIN or build it (e.g., CMake)." >&2 + exit 2 + fi +fi + +# Ensure stdlib is discoverable unless user overrides +if [[ -z "${FUN_LIB_DIR:-}" ]]; then + export FUN_LIB_DIR="$ROOT/lib" +fi + +exec "$BIN" "$PLAY_FUN" "$@" diff --git a/scripts/play.fun b/scripts/play.fun index 1a846c4..61d7e02 100755 --- a/scripts/play.fun +++ b/scripts/play.fun @@ -18,6 +18,46 @@ #include #include +// Early argv scan so --help exits before any other side effects +raw_argv = argv() +yes_all = 0 +no_all = 0 +want_help = 0 +idx = 0 +argc = len(raw_argv) +while (idx < argc) + tok = raw_argv[idx] + if (tok == "--yes") + yes_all = 1 + else if (tok == "--no") + no_all = 1 + else if (tok == "--help") + want_help = 1 + idx = idx + 1 + +if (want_help == 1) + print("Usage: scripts/play.fun [--yes | --no] [examples-subdir]") + print("") + print("Options:") + print(" --yes Run all listed examples without asking") + print(" --no Do not run any examples (show list only)") + print(" --help Show this help and exit") + print("") + print("Behavior:") + print(" - Without a subdirectory argument: only runs .fun files directly in ./examples/") + print(" - With an examples-subdir: searches recursively under ./examples//") + print("") + print("Environment:") + print(" FUN_BIN Path to the fun interpreter (overrides auto-detection)") + print(" FUN_LIB_DIR Path to stdlib (defaults to ./lib)") + print("") + print("Examples:") + print(" scripts/play.fun") + print(" scripts/play.fun --yes") + print(" scripts/play.fun crypto") + print(" scripts/play.fun --no math") + exit(0) + // Set FUN_LIB_DIR to ensure stdlib is found // We try to use existing environment or default to ./lib lib_dir = env("FUN_LIB_DIR") @@ -93,11 +133,15 @@ fun get_examples(subdir) i = i + 1 return examples -args = parse_args(argv()) +args = parse_args(raw_argv) subdir = "" if (len(args["positionals"]) > 0) subdir = args["positionals"][0] +if (yes_all == 1 && no_all == 1) + print("Error: --yes and --no cannot be used together.") + exit(2) + examples = get_examples(subdir) if (len(examples) == 0) if (len(subdir) > 0) @@ -117,9 +161,22 @@ while (i < n) print("Example: " + example) // ask_yes_no returns 1 for yes, 0 for no - print("Do you want to run this example? [y/N]") - ans = str_to_lower(str_trim(input(""))) - if (ans == "y" || ans == "yes") + run_it = 0 + if (yes_all == 1) + // Auto-approve running + run_it = 1 + else if (no_all == 1) + // Auto-decline + run_it = 0 + else + print("Do you want to run this example? [y/N]") + ans = str_to_lower(str_trim(input(""))) + if (ans == "y" || ans == "yes") + run_it = 1 + else + run_it = 0 + + if (run_it == 1) print("Running: " + interpreter + " " + example) // Prepare command with environment variable