LM lmscript.io Examples Book
Hands-On Learning · 58 Verified Core Scripts

The 10-Part LMScript Examples Book

Organized in progressive order from lmctl-mono/lmscript/examples/. Every example can be checked with lmscript --check, linted with lmscript --lint, and executed directly.

Part I — First Steps, Variables, Lists & Maps

Covers basic output (println), arithmetic, booleans, 0-based lists (xs[0], negative indexing xs[-1]), and key-value maps (m.key and m["key"]).

Part I — hello_collections.lms (download)
greeting = "Hello, LMScript!";
langs    = ["lmscript", "lmcode", "lmaws"];
release  = { version: "0.2.38", dslb: 6 };

println(greeting);
println("First: " + langs[0] + " | Last: " + langs[-1]);
println(f"Release {release.version} (DSLB v{release.dslb})");

Part II — Python-Style Strings, Regex Substrings, Stateful pos() Tokenizer & Path Literals

Demonstrates raw strings (r"..."), format strings (f"..."), triple-quoted blocks, regex containment & substring extraction (regex_test, regex_find, regex_matches, regex_match, regex_find_all, regex_replace), Perl-like stateful pos() tokenizers (pos, set_pos, regex_find_pos, regex_match_pos, regex_test_pos), and first-class paths.

Part II — regex_tokenizer.lms (download · python-strings.lms)
# 1. Check if string matches (contains) pattern & extract 1 or more matched substrings
log = "2026-10-17 ERROR disk 91%\n2026-10-18 WARN cpu 72%\n2026-10-18 ERROR mem 88%";
println(regex_test(log, r"ERROR"));        # true
println(regex_find(log, r"\d+%"));         # "91%"
println(regex_matches(log, r"\d+%"));      # ["91%", "72%", "88%"]

# 2. Stateful Perl-like pos() tokenizer over a list of [kind, pattern] rules
const RULES = [
  ["WS",     r"\s+"],
  ["NUMBER", r"\d+(?:\.\d+)?"],
  ["IDENT",  r"[A-Za-z_][A-Za-z0-9_]*"],
  ["OP",     r"==|!=|<=|>=|[+\-*/=()<>]"],
  ["SEMI",   r";"]
];

fn tokenize(src) {
  pos(src, 0);
  let tokens = [];
  while (pos() < len(src)) {
    let start = pos();
    let matched = false;
    for (rule in RULES) {
      let text = regex_find_pos(src, rule[1]);
      if (text != null) {
        if (rule[0] != "WS") { push(tokens, { kind: rule[0], text: text, at: start }); }
        matched = true;
        break;
      }
    }
    if (!matched) {
      return { ok: false, error_pos: pos(), rest: slice(src, pos()) };
    }
  }
  return { ok: true, tokens: tokens, final_pos: pos() };
}

res = tokenize("total = 42 + tax_rate * 10.5;");
println(f"tokens={len(res.tokens)} final_pos={res.final_pos}");

Part III — Control Flow, Loops & Functions

Covers if (...) { ... } else if (...) { ... } else { ... }, for (v in xs) { ... }, for (k in map) { ... } (map keys), numeric for (i = 0; i < 10; i = i + 1), while, and higher-order functions (functions are declared at top level and passed as values; there are no nested closures).

Part III — functions_and_loops.lms (download)
# Part III — Functions, Recursion, Higher-Order Functions & Loops
fn double(x) {
  return x * 2;
}

fn apply_twice(f, val) {
  return f(f(val));
}

results = [];
for (i = 1; i <= 4; i = i + 1) {
  push(results, apply_twice(double, i));
}
println("Sequence: " + json_stringify(results));

Part IV — Embedded Data Blocks (_json_, _csv_, _toml_, _props_, _graphql_)

Data blocks are validated at parse/check time and produce opaque Data for JSON, CSV, and TOML (use .value() for native values); props produces a map and GraphQL produces text.

Part IV — data_blocks.lms (download)
endpoints = _csv_ heading=true;
name,method,path
health,GET,/healthz
deploy,POST,/v1/deploy
end _csv_

for (ep in endpoints.value()) {
  println(f"{ep.method} {ep.path} ({ep.name})");
}

Part V — File System, _cmd_ Blocks & Process Execution

Safe command execution with _cmd_ argv blocks, system(...) status inspection, system(argv) output capture, and dry-run-aware file mutations.

Part V — cmd_and_files.lms (download)
ver_cmd = _cmd_;
lmscript --version
end _cmd_

res = system(ver_cmd);
println("Exit: " + str(res.code) + " | Output: " + trim(res.stdout));

Part VI — Forward Templates (expand) & Reverse Templates (template_extract)

Render config files and prompts with expand(tpl, map), and invert the same template to extract text fields back out of logs or command output with template_extract_text(tpl, text).

Part VI — reverse_template.lms (download)
# Part VI — Forward expand() and Reverse template_extract_text() / template_extract()
tpl  = "site=${site};env=${env};end";
line = expand(tpl, {site: "lmscript.io", env: "prod"});
println("Rendered  : " + line);

rev = template_extract_text(tpl, line);
require(rev.matched, "expected reverse template text match");
println("Extracted : " + json_stringify(rev.value));

rows = template_extract([{kind: "host", name: "$name"}], [
  {kind: "host", name: "lmaws"},
  {kind: "skip", name: "none"},
  {kind: "host", name: "lmcad"}
]);
println("Projected : " + json_stringify(rows.value));

Part VII — SQLite Databases, HTTP Client & XML/HTML Parsing

Query local SQLite files with literal SQL and bound parameters, fetch text via http_get, and inspect _xml_ Data using xml_select.

Part VII — sqlite_and_xml.lms (download)
# Part VII — SQLite with a temporary local database
db = temp_file();
sql_exec(db, "CREATE TABLE hosts (name TEXT, lang TEXT)");
sql_exec(db, "INSERT INTO hosts VALUES (?, ?), (?, ?)", ["lmaws", "java", "lmcad", "cpp"]);
rows = sql_read(db, "SELECT name, lang FROM hosts ORDER BY name");
delete_file(db);
println(json_stringify(rows));

Part VIII — Modules (import), Packages & CLI PROPS

Structure multi-file toolchains using import, and CLI PROPS / --pref definitions.

Part IX — Reliability: recover, resumepoint & parallel_map

Select explicit recover / resumepoint entries when rerunning a script, reload any needed application state explicitly (recovery saves no state and catches no errors), and fan out work across concurrent workers.

Part X — Embedded _lmcode_ Bytecode Hosts & Unified AI Agents

Compile statically typed _lmcode_ blocks to DSLB v6 bytecode, execute them via run_lmcode, and coordinate multi-turn AI coding agents with agent_open and agent_turn.

Catalog of 58 Core LMScript Example Scripts

The 58 scripts in the main reading sequence from lmctl-mono/lmscript/examples/index.md, in book order. The separate xml_tree.lms example is linked at the end of that repository book. The Part column follows the core sequence: 1 Basics, 2 Data, 3 Text, 4 Files, 5 Processes, 6 Agents, 7 Concurrency & recovery, 8 Checking & tracing, 9 Modules & data blocks (Part 10 is the reference and language comparisons).

58 examples
#PartScript NameCore Feature Demonstrated
1Part 1hello.lmsThe smallest script: variables, println, an f-string; every statement ends with ;
2Part 1values_and_scope.lmsSix value kinds, strict ==, truthiness, block-owned names, function-local assignment
3Part 1control_flow.lmsif / else if / else, while, counted for, for (x in list|map|string), break, continue, cond ? a : b
4Part 1functions.lmsfn with hoisting and recursion, functions as values, let (a, b) = [1, 2];
5Part 1config_consts.lmsTop-level const bindings spliced with ${} into _lmcode_ blocks; frozen lists/maps and clone()
6Part 1strict_access_methods.lmsMissing keys/indices are errors; m.get(key, default), m.has(key), .len(), .keys()
7Part 1errors_and_exit.lmsrequire, assert, warning, exit(code) on the passing path
8Part 1errors_and_exit_failing.lmserror(...) records and continues, failing the run with exit 1 at the end
9Part 1argv.lmsEverything after the script name is data in ARGV (alias args)
10Part 2inline_data.lmsjson_parse, csv_parse, csv_stringify on raw triple-quoted text
11Part 201_dsl_heredoc.lmsRaw and dedented _lmcode_ heredoc strings preserving UTF-8, quotes and interpolation-looking text
12Part 2lists.lmsNegative indexing, mutating helpers and their return values, sort/unique/zip/chunk/range/enumerate, clone()
13Part 2maps_and_json.lmskeys/values/entries, merge, delete_key, nested set(), json_parse/json_stringify, json_parse_safe, pluck/filter_by/find_by
14Part 2math_and_predicates.lmsabs/ceil/floor/round/sqrt/pow/min/max/clamp, is_* predicates, if_null/coalesce, getstr/getnum/getbool/getlist
15Part 3strings.lmsQuoting forms (raw r"...", triple), trim/split/join/replace/pad/substring/char_at, UTF-16 units vs code points
16Part 3formatting.lmsf-strings, format("{} {name}"), sprintf/printf with width and precision
17Part 3regex.lmsregex_test, regex_find, regex_matches, regex_match, regex_find_all, regex_replace with $1, split_regex, stateful pos()
18Part 3regex_tokenizer.lmsPerl-like stateful pos() + regex_find_pos lexer over an ordered list of [kind, pattern] rules
19Part 3expand.lmsexpand() resolves ${name} from a vars map, ${PROPS.path} and ${ARGV[n]}
20Part 3reverse_templates.lmstemplate_extract tree projection and row filtering, template_extract_text exact text holes, match envelopes
21Part 3agent_reply_helpers.lmslast_line_as_status, kv_from_last_paragraph, parse_file_blocks/format_file_blocks, write_files, read_tree, split_command
22Part 4files.lmsText, lines, JSON and CSV read/write, append, mkdir/list_dir/glob, copy/move/delete, path helpers, temp_file
23Part 4files_more.lmswrite_file_exclusive, remove_file, rmdir, clear/size on a map
24Part 4path_literals.lmspath"..." literals: the written path in OS form, with no filesystem lookup
25Part 4env_and_time.lmsenv/set_env/getenv, which, now/timestamp/date/strftime, sleep_ms
26Part 5commands.lmscmd COMMAND argv construction, system(argv, timeout) with no shell, cmd_render quoting
27Part 5system_results.lms{stdout, stderr, code, signal, timed_out, duration_ms, pid}, timeouts, {cwd, timeoutMs, stdin} options
28Part 5cmd_blocks.lmsMulti-line _cmd_ argv blocks with ${} fields, run with system(...)
29Part 5run_script.lmsrun_script runs another script in-process: {success, exit_code, stdout, output, value, errors}
30Part 5run_script_child.lmsThe child script launched by run_script.lms (its own ARGV and output)
31Part 6agents_offline_mock.lmsTeamfile, lm_lint, lm_seed, lm_members, lm_prompt, lm_batch_* result shapes, offline with ClaudeMock
32Part 6agent_unified.lmsUnified agent host offline: agent_providers, agent_open, agent_turn, agent_info, agent_history, agent_close
33Part 6agent_observe.lmslm_running, lm_ratelimit_readings, lm_terminal, prompt_human_config
34Part 6book_live.lmsShared helpers imported by the live agent examples
35Part 6agent_prompt.lmsLive: one agent_prompt({provider, model}, prompt) with session_id, token buckets and price
36Part 6agent_teamfile.lmsLive: write a teamfile, lm_lint, lm_seed, lm_members, lm_prompt(teamfile, alias, prompt)
37Part 6agent_fanout.lmsLive: parallel_map around agent_prompt sends one prompt to several agents concurrently
38Part 6batchscript.lmsLive: batch over a teamfile with lm_read_prompts, lm_batch_open, lm_batch_turn, lm_batch_close
39Part 6llm_classify.lmsLive: one-shot llm_prompt classification with validated JSON output
40Part 6llm_agent.lmsLive: multi-turn shell agent with session history and restricted tool calling over llm_prompt
41Part 6tool_agent.lmsLive: workspace agent built from tool_specs, tool_parse, tool_call and llm_prompt
42Part 6providers_check.lmsLive: iterate llm_providers(), discover llm_models(), test completions
43Part 7recover_steps.lmsrecover { resumepoint NAME: ... }, RESUMEPOINT, --resumepoint PATH
44Part 7resource_batch.lmsA resource handle closes automatically when its owning scope exits
45Part 7resource_escape.lmsA resource handle cannot escape the function that owns it
46Part 7resource_signal.lmsResource handles are released on SIGINT/SIGTERM
47Part 7parallel_sync.lmsparallel([f(), g()]) with ordered results and sync x = ... binding locks
48Part 7parallel_map.lmsparallel_map(items, fn[, limit]) bounded fan-out (default 8, maximum 64)
49Part 8checking_and_dry_run.lms--check, --lint, --dry-run PLAN output and --check-files
50Part 8trace.lmslmscript --trace FILE JSON Lines execution traces, summarized by event kind
51Part 9git_summary.lmsGit through system(["git", ...]) argv; check and dry-run launch no process
52Part 9github_query.lmsThe gh source module: import "../modules/gh.lms"; and gh.gh_graphql(query, vars)
53Part 9graphql_query.lmsA checked _graphql_ block passed as one argv element
54Part 9imports_and_packages.lmsimport "file.lms"; and package NAME; files exposing NAME.member
55Part 9book_greetings.lmsThe package file imported by imports_and_packages.lms
56Part 9data_blocks_csv_json.lms_csv_ and _json_ blocks with per-value ${expr} substitution after parsing
57Part 9data_blocks_toml_props.lms_toml_ (TOML 1.0) and _props_ (Java .properties) blocks
58Part 9properties_and_pref.lmsPROPS, has_property, get_property, get_properties, set_property, --pref FILE overlay