An output policy for the three commands, and @cond as a true special form
A snapshot of the development tree. The substantial changes since the last one:
COMMAND OUTPUT POLICY. The three commands display text in exactly three cases,
and each owns a stream: LOGGING under "-v" greater than 0 and an ERROR before
termination go to STDERR; OUTPUT THE USER ASKED FOR goes to STDOUT. For ktext
that output is a document, so "ktext doc.kt -d | ..." is now safe -- logging
used to share the stream and land inside the document. A bare command prints
its usage and succeeds rather than failing. Colour is emitted only to a
terminal, per stream, and NO_COLOR is honoured.
"-v 1" reports every decision whose outcome you could not have read off your own
input: the klammerset that was loaded and from which file, a font's directory, a
":files" name's file, how "-o" was expanded. Higher levels are the trace.
The commands no longer warn and continue: an anomaly is an error, described with
its location. Two exceptions remain, each for a stated reason -- a condition
that is expected and temporary by design, and a judgment that is a heuristic
rather than exact.
@cond IS NOW A TRUE SPECIAL FORM, resolved at APPLICATION time rather than when
the file is read. Two consequences for a writer:
* a state variable reaches the predicate. "@@@state Flag :value true @@@
@cond *Flag* | T | F @" renders "T"; it used to see the literal "*Flag*" and
silently take the false branch. The document now behaves like a klammer
body, whose arguments are bound before its conditionals are decided.
* nothing in a discarded branch happens -- it is not read, not evaluated, not
expanded. An @eval in the branch not taken used to run anyway.
Its predicate relation is total and strict: true, True, 1; false, False, 0, and
empty; anything else is an error at the @cond rather than silently false.
@eval REACHING OUTSIDE. ":shell" and ":haskell" now keep the command's standard
error out of the document (it appears under "-v 1") and treat a nonzero exit as
an error naming what the command reported. A command that exits nonzero on
purpose -- "grep" finding no match -- says so with "|| true".
KLAMMER SETS. Several combine: "--klammersets a b c" loads all three in the
order given, sharing one namespace, with the definition modes deciding
collisions. "none" means none and may not be combined with other symbols. A
klammerset with symbol X is declared in a file X/X.k, which is what lets two
sets require the same third set without loading it twice.
TESTS. Four new suites: the kdiag command's interface, the @eval primitive's
contract with the outside world, and verbosity at both tiers. Three suites
that could not run on macOS at all now do.
Assembled from dev commit 6c8ee6c22fca.
This commit is contained in:
118
com/kdiag.cpp
118
com/kdiag.cpp
@@ -3,6 +3,7 @@
|
||||
#include <map>
|
||||
|
||||
#include "util.h"
|
||||
#include "check.h"
|
||||
#include "argument_set.h"
|
||||
#include "argv.h"
|
||||
#include "argument.h"
|
||||
@@ -19,7 +20,11 @@ int main(int argc, char* argv[])
|
||||
set_verbose_level(argc, argv);
|
||||
|
||||
Argv args {};
|
||||
args.req("input", "Klammertext input text", "'text'");
|
||||
// Optional: "kdiag --machine" with no input shows the Machine's initial
|
||||
// state, which is a question about the machine rather than about any
|
||||
// document -- and the one thing a programmer wants before feeding it
|
||||
// anything.
|
||||
args.req("input", "Klammertext input text", "'text'", false);
|
||||
args.flag("type", "Show katom types in subscript");
|
||||
args.flag("index", "Show the list index of the katom");
|
||||
args.flag("text", "Show text katoms with selected attributes");
|
||||
@@ -37,13 +42,27 @@ int main(int argc, char* argv[])
|
||||
args.flag("literal", "Process literal: ^'...'^");
|
||||
args.flag("ignore", "Process ignored: #, ##, #[...]#");
|
||||
args.flag("ws", "Process whitespace: #-, #+, #/");
|
||||
args.flag("klammer", "Process klammer definitions: @@<name> ... @@");
|
||||
args.flag("process", "Process all");
|
||||
args.flag("klammer", "Register klammer definitions: @@<name> ... @@. Their katoms "
|
||||
"are consumed, so they appear only with --replaced.");
|
||||
args.flag("system", "Register machine definitions: @@@target, @@@argtype, @@@state, "
|
||||
"@@@klammerset. Their katoms are consumed, so they appear only "
|
||||
"with --replaced. The two tiers are ORDERED: a klammer names a "
|
||||
"target, so --klammer alone cannot register a klammer whose "
|
||||
"target the input itself declares. That is not an error here: "
|
||||
"the definition is skipped and its katoms stay visible, which is "
|
||||
"the report.");
|
||||
args.flag("process", "Process all, both tiers of definition included");
|
||||
args.flag("check", "Check every klammer application in the input and in the "
|
||||
"body of every defined klammer");
|
||||
args.flag("machine", "Show the state of the Klammermachine after all processing");
|
||||
args.opt("v", "'verbosity'", "degree", "0", "'verbosity'");
|
||||
|
||||
if (show_usage(argc, argv)) {
|
||||
// A bare command is a REQUEST for information, not a failure:
|
||||
// usage goes to stdout (it is the result being asked for) and the
|
||||
// exit status is 0, so "kdiag && echo ok" reports what happened.
|
||||
args.usage(file_basename(argv[0]));
|
||||
exit(1);
|
||||
exit(0);
|
||||
}
|
||||
auto p = [&](const std::string& name) { return args.get(name) == "true"; };
|
||||
|
||||
@@ -57,19 +76,48 @@ int main(int argc, char* argv[])
|
||||
show_rewrite_rules = true;
|
||||
}
|
||||
|
||||
// kdiag deliberately has no --klammersets: a klammerset is reached here
|
||||
// the way the input reaches anything else, with "@read sks/sks.k @".
|
||||
// The symbol mechanism is a higher-level convenience, and a debugger
|
||||
// must not depend on the machinery it debugs -- if symbol resolution
|
||||
// breaks, kdiag must still run, and the two paths to the same file can
|
||||
// then be compared.
|
||||
Machine machine;
|
||||
// kdiag has no target argument, and evaluating needs one: Eval::eval
|
||||
// re-reads its result under K_target, so with the variable unset every
|
||||
// @eval died with "Variable "K_target" not defined" -- an error naming
|
||||
// something the user never wrote. A command that does not specify a
|
||||
// target evaluates under the GENERAL target (Andy, 2026-08-15), which
|
||||
// is also what kdiag means: it loads no klammerset, so nothing
|
||||
// target-specific is in scope to begin with.
|
||||
machine.m_state.set("K_target", Target_registry::general_name);
|
||||
machine.m_state.set("K_input_dir", fs::current_path().string());
|
||||
std::string input = args.as_string("input");
|
||||
std::string command = construct_command_pathname(argv[0]);
|
||||
katom_list katoms {};
|
||||
if (p("process")) {
|
||||
katoms = machine.process(input, command);
|
||||
} else {
|
||||
katoms = machine.process(
|
||||
//args.as_string("input"), construct_command_pathname(argv[0]),
|
||||
input, command,
|
||||
p("nonascii"), p("literal"), p("ignore"), p("ws"), p("klammer"),
|
||||
p("eval"), p("cond"), p("read"));
|
||||
}
|
||||
// ONE authoritative katom list. process() returns the katoms but
|
||||
// registers nothing -- read() is what appends to m_katoms and extracts
|
||||
// the definitions -- so kdiag does that itself, in the order read()
|
||||
// uses, and then displays m_katoms. Displaying its own copy instead
|
||||
// would show the definition katoms unconsumed, because extraction
|
||||
// marks them "replaced" in the Machine's list, not in a copy.
|
||||
bool all = p("process");
|
||||
katom_list processed = all
|
||||
? machine.process(input, command)
|
||||
: machine.process(input, command,
|
||||
p("nonascii"), p("literal"), p("ignore"), p("ws"),
|
||||
p("klammer"), p("eval"), p("cond"), p("read"));
|
||||
machine.m_katoms.insert(
|
||||
machine.m_katoms.end(), processed.begin(), processed.end());
|
||||
// Machine definitions first: a klammer definition names a target, so
|
||||
// the target must be registered before the klammer that uses it.
|
||||
// Tolerant: a definition that cannot be registered because the other
|
||||
// tier was not run is skipped, not fatal -- its katoms stay visible.
|
||||
// kdiag dissects structure and typing, which means allowing modes of
|
||||
// processing that would be errors in ktext.
|
||||
if (all || p("system")) machine.extract_machine_definitions(true);
|
||||
if (all || p("klammer")) machine.extract_klammer_definitions(true);
|
||||
katom_list& katoms = machine.m_katoms;
|
||||
|
||||
if (p("spans")) {
|
||||
describe_spans(katoms);
|
||||
} else {
|
||||
@@ -79,7 +127,9 @@ int main(int argc, char* argv[])
|
||||
if (p("text")) std::cout << kall;
|
||||
if (p("all")) std::cout << kall << kreplaced << kignored;
|
||||
if (p("replaced")) std::cout << kreplaced;
|
||||
std::cout << katoms << "\n";
|
||||
if (!p("check")) {
|
||||
std::cout << katoms << "\n";
|
||||
}
|
||||
}
|
||||
|
||||
if (p("args")) {
|
||||
@@ -105,11 +155,45 @@ int main(int argc, char* argv[])
|
||||
std::cout << std::right << std::setw(label_width) << "rest:" << " " << rest << "\n";
|
||||
}
|
||||
}
|
||||
int check_errors = 0;
|
||||
if (p("check")) {
|
||||
check_errors = report_diagnostics(check_machine(machine, katoms, "*"), std::cout);
|
||||
// The commonest cause of a wall of "not defined" is that nothing
|
||||
// was registered. The advice belongs here rather than in the
|
||||
// catch block: --check REPORTS, it does not throw, so an exception
|
||||
// handler never sees this case. The condition is the CAUSE, not a
|
||||
// proxy for it.
|
||||
if (check_errors > 0 && machine.m_klammers.m_klammers.empty()) {
|
||||
std::cout << "\nNo klammers are registered, so every application is "
|
||||
"undefined.\nkdiag loads no klammerset: read one in the input, "
|
||||
"as \"@read sks/sks.k @\",\nand add --process (or --system "
|
||||
"--klammer --read) to register what it defines.\n";
|
||||
}
|
||||
}
|
||||
// Last, and after everything else: the state shown is the result of
|
||||
// whatever the other arguments did. This is why --check defers its
|
||||
// exit status rather than returning as soon as it finds errors.
|
||||
if (p("machine")) {
|
||||
std::cout << machine;
|
||||
}
|
||||
if (check_errors > 0) {
|
||||
std::cout << black;
|
||||
return 1;
|
||||
}
|
||||
|
||||
}
|
||||
catch (Error& e) {
|
||||
// The advice must name a way in that kdiag actually has. It named
|
||||
// "--klammersets sks" until 2026-08-15, a flag the 2026-08-14 redesign
|
||||
// deliberately removed from this command -- a debugger must not depend
|
||||
// on the machinery it debugs -- so the one remedy offered was one that
|
||||
// errors. Same wording as the --check hint above, for the same reason.
|
||||
std::string advice = "";
|
||||
if (e.m_type == "target")
|
||||
advice = "To include the Standard Klammer Set, add flag \"--sks\".";
|
||||
if (e.m_type == "target" || e.m_type == "definition") {
|
||||
advice = "kdiag loads no klammerset: read one in the input, as "
|
||||
"\"@read sks/sks.k @\", and add --process to register what it "
|
||||
"defines.";
|
||||
}
|
||||
e.print_message(advice);
|
||||
// Nonzero, as ktext does: a command that prints an error and exits 0
|
||||
// reports success, and a script cannot tell the difference.
|
||||
|
||||
Reference in New Issue
Block a user