From de4da84ca3159ad0ebcf13729716be5f4d7bbc2a Mon Sep 17 00:00:00 2001 From: zinc Date: Thu, 15 Oct 2026 09:42:25 +0330 Subject: std: add std.Io (print, println, nl, put_*, put_f, get_c, eof, read, getline) with test, learn example and docs --- src/lib/std.zc | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/lsp/server.zc | 4 +-- 2 files changed, 80 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/lib/std.zc b/src/lib/std.zc index 745a921..b206888 100644 --- a/src/lib/std.zc +++ b/src/lib/std.zc @@ -415,6 +415,84 @@ struct Host { } } +// ---- standard input and output ---- + +// the terminal: printing to the standard output and reading the standard input. +// called as std.Io.name(args). the writers go through rt.put_c, so a capture +// (std.capture) sees their text too. the rt names (rt.put_n, rt.put_s and the +// rest) stay; these are the same operations, grouped with the library. +// zc has no varargs or overloading, so print takes a string, and numbers have +// their own writers: std.Io.print("n = "); std.Io.put_n(n); std.Io.nl(); +struct Io { + // writes a string. + // s: the text + // returns: 0 + static int print(char* s) { return rt.put_s(s); } + // writes a string and a line break. + // s: the text + // returns: 0 + static int println(char* s) { rt.put_s(s); return rt.put_c(10); } + // writes a line break. + // returns: 0 + static int nl() { return rt.put_c(10); } + // writes one character. + // c: the character + // returns: 0 + static int put_c(int c) { return rt.put_c(c); } + // writes a string, the same as print. + // s: the text + // returns: 0 + static int put_s(char* s) { return rt.put_s(s); } + // writes a number in decimal, with a minus sign when it is negative. + // n: the number + // returns: 0 + static int put_n(int n) { return rt.put_n(n); } + // writes a number in hexadecimal. + // n: the number + // returns: 0 + static int put_hex(int n) { return rt.put_hex(n); } + // writes a packed byte string. + // s: the byte string + // returns: 0 + static int put_bs(bstr s) { return rt.put_bs(s); } + // writes a float with the vm's float printer (six decimals, inf, nan). + // x: the value + // returns: 0 + static int put_f(float x) { asm { x @ #1220 } return 0; } + // reads one character. + // returns: its byte value, -1 at the end of the input + static int get_c() { return rt.get_c(); } + // true when the input has no more characters. + // returns: 1 at the end of the input, 0 otherwise + static int eof() { return rt.eof(); } + // reads up to n characters, one to a word, stopping early at the end of the input. + // buf: where they go, room for n words + // n: the most to read + // returns: how many were read, 0 at the end of the input + static int read(char* buf, int n) { + int i = 0, c; + while (i < n) { c = rt.get_c(); if (c < 0 && rt.eof()) break; buf[i] = c; i++; } + return i; + } + // reads one line into buf as a string, without its line break. a line longer + // than n - 1 characters is cut short and the rest is left for the next call. + // buf: where the line goes, room for n words, the 0 included + // n: the size of buf in words + // returns: the line's length, or -1 at the end of the input with nothing read + static int getline(char* buf, int n) { + int i = 0, c; + if (n < 1) return -1; + while (i < n - 1) { + c = rt.get_c(); + if (c < 0 && rt.eof()) { if (!i) { buf[0] = 0; return -1; } break; } + if (c == 10) break; + buf[i] = c; i++; + } + buf[i] = 0; + return i; + } +} + // ---- byte strings ---- // the same helpers as Text, for packed byte strings of type bstr (b"..." diff --git a/src/lsp/server.zc b/src/lsp/server.zc index 52e5e9a..bc2c967 100644 --- a/src/lsp/server.zc +++ b/src/lsp/server.zc @@ -2658,7 +2658,7 @@ char* module_description() { if (hname_is("run")) return "the resident compiler module: compile and evaluate source, inspect tokens and functions, and register syntax or receiver extensions."; if (hname_is("core")) return "the vm host interface: host gates, operand-stack addresses, byte-addressed memory access and program termination."; if (hname_is("lsp")) return "the language server module: document analysis, diagnostics, markdown hovers, completion, navigation and formatting."; - if (hname_is("std")) return "the embedded standard library: text and byte strings, numeric helpers, files, buffers, array algorithms and command-line arguments. no file is read and nothing imports it automatically."; + if (hname_is("std")) return "the embedded standard library: terminal input and output, text and byte strings, numeric helpers, files, buffers, array algorithms and command-line arguments. no file is read and nothing imports it automatically."; if (hname_is("icon")) return "structured runtime syntax transformations: token templates, balanced captures, expression and block edits, function publication and scalar execution inspection."; return 0; } @@ -2739,7 +2739,7 @@ int hover_instruction(int i) { if (fm_k[t] == 7) break; if (hcursor >= fm_s[t] && hcursor < fm_e[t]) { if (prev >= 0 && fm_is(i, prev, "include") && fm_is(i, t, "std")) { - hn = 0; return kw_put("include std;~compiles the embedded standard library here. no file is read and nothing imports it automatically. provides Text, Bytes, Host, Arena, Buf, capture, and command-line helpers."); + hn = 0; return kw_put("include std;~compiles the embedded standard library here. no file is read and nothing imports it automatically. provides Io, Text, Bytes, Host, Arena, Buf, capture, and command-line helpers."); } if (!raw || (prev >= 0 && fm_k[prev] == 6 && fm_o[prev] == 8)) return 0; op = -1; -- cgit v1.2.3