Curated source subset assembled by klammertext-dev's doc/make_dist.sh: the Klammermachine (mac), the Standard Klammer Set (sks), the commands (com), editor plugins and install guides (doc), a test subset (tst), and lib/bin placeholders. Builds with 'make -C com'. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
309 lines
9.5 KiB
C++
309 lines
9.5 KiB
C++
#include <regex>
|
|
#include <iostream>
|
|
|
|
#include "show.h"
|
|
#include "file.h"
|
|
#include "error.h"
|
|
#include "state.h"
|
|
#include "log.h"
|
|
#include "util.h"
|
|
|
|
int State::class_id = 0;
|
|
|
|
bool Var::defined()
|
|
{
|
|
return !m_name.empty();
|
|
}
|
|
|
|
std::vector<std::string> Frame::names() const
|
|
{
|
|
std::vector<std::string> result;
|
|
result.reserve(m_vars.size());
|
|
std::transform(m_vars.begin(), m_vars.end(), std::back_inserter(result),
|
|
[](const auto& pair) { return pair.first; });
|
|
return result;
|
|
}
|
|
|
|
void Frame::set(std::string name, std::string value,
|
|
std::string delim, std::string desc, Locator loc)
|
|
{
|
|
Var v(name, value, delim, desc, loc);
|
|
m_vars[name] = v;
|
|
}
|
|
|
|
std::pair<Var, bool> Frame::get(std::string name)
|
|
{
|
|
std::pair<Var, bool> result {Var(), false};
|
|
if (m_vars.contains(name)) {
|
|
result = {m_vars[name], true};
|
|
}
|
|
return result;
|
|
}
|
|
|
|
// State
|
|
|
|
|
|
void State::open_frame(std::string name)
|
|
{
|
|
Frame f(name);
|
|
// m_frames.push_back(f);
|
|
m_frames.emplace(m_frames.begin(), f);
|
|
}
|
|
|
|
|
|
void State::close_frame()
|
|
{
|
|
if (m_frames.empty()) {
|
|
throw Internal_error("No frame to close", Locator());
|
|
}
|
|
// Preserve altered machine state:
|
|
string_map machine_state {};
|
|
for (auto [name, var] : m_frames[0].m_vars) {
|
|
if (contains(name, "K_")) {
|
|
machine_state[name] = var.m_value;
|
|
}
|
|
}
|
|
m_frames.erase(m_frames.begin());
|
|
for (auto [name, value] : machine_state) {
|
|
set(name, value, true);
|
|
}
|
|
}
|
|
|
|
void State::set(std::string name, std::string value, bool update,
|
|
std::string delim, std::string desc, Locator loc)
|
|
{
|
|
if (m_frames.empty()) {
|
|
std::stringstream ss {};
|
|
ss << "No open frame to set " << q_(name) << " to " << q_(value);
|
|
throw Internal_error(ss.str(), Locator());
|
|
}
|
|
auto [current, exists] = m_frames[0].get(name);
|
|
|
|
if (exists && current.m_value != klammerstate::no_value && !update) {
|
|
std::stringstream ss {};
|
|
ss << "Variable " << q_(name) << " is already defined at " << current.m_loc.desc()
|
|
<< ". Use ':replace <new-value>' to replace the current value of "
|
|
<< q_(current.m_value) << ".";
|
|
throw Argument_error(ss.str(), current.m_loc);
|
|
}
|
|
m_frames[0].set(name, value, delim, desc, loc);
|
|
}
|
|
|
|
void State::set(std::map<std::string, std::string> varmap)
|
|
{
|
|
for (auto [k, v] : varmap) {
|
|
set(k, v);
|
|
}
|
|
}
|
|
|
|
|
|
void State::replace(std::string name, std::string value, bool error_if_not_defined)
|
|
{
|
|
if (error_if_not_defined && !get(name).defined()) {
|
|
std::stringstream ss {};
|
|
ss << "Cannot replace value of nonexistent variable " << q_(name) << " with " << q_(value);
|
|
throw Argument_error(ss.str(), Locator());
|
|
}
|
|
set(name, value, true);
|
|
}
|
|
|
|
void State::add_environment_frame()
|
|
{
|
|
open_frame(klammerstate::shell_environment_name);
|
|
for (auto [name, value] : environment_variables()) {
|
|
set(name, value);
|
|
}
|
|
}
|
|
|
|
Var State::get(std::string name, bool error_if_not_defined, Locator loc)
|
|
{
|
|
for (auto f : m_frames) {
|
|
auto [result, found] = f.get(name);
|
|
if (found) {
|
|
return result;
|
|
}
|
|
}
|
|
if (error_if_not_defined) {
|
|
msg() << describe();
|
|
throw Argument_error("Variable " + q_(name) + " not defined", loc);
|
|
} else {
|
|
return Var();
|
|
}
|
|
}
|
|
|
|
std::string State::value(std::string name, bool error_if_not_defined, Locator loc)
|
|
{
|
|
return get(name, error_if_not_defined, loc).m_value;
|
|
}
|
|
|
|
std::string State::subst(std::string text, bool quote_values)
|
|
{
|
|
(void)K::log(3);
|
|
std::string result = text;
|
|
std::regex varpat(R"(\*(\w+)\*)");
|
|
for (std::sregex_iterator iter(text.begin(), text.end(), varpat), end; iter != end; ++iter) {
|
|
std::string match = iter->str();
|
|
std::string var = (*iter)[1].str();
|
|
//std::cout << "Found: " << iter->str() << sp_arrow << (*iter)[1].str() << "\n";
|
|
// std::cout << "Found: " << match << sp_arrow << var << "\n";
|
|
|
|
//std::string value = get(var).m_value;
|
|
|
|
auto var_value = value(var, false);
|
|
auto printable = q_(var_value);
|
|
if (var_value != klammerstate::no_value) {
|
|
if (quote_values) {
|
|
var_value = q_(var_value);
|
|
}
|
|
result = string_replace(result, match, var_value);
|
|
} else {
|
|
// throw Argument_error("Variable " + printable + " is not defined");
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
void State::subst(katom_iter begin, katom_iter end)
|
|
{
|
|
(void)K::log(3);
|
|
std::regex varpat(R"((.*?)\*(\w+)\*(.*))");
|
|
for (auto ki = begin; ki < end ; ki++) {
|
|
// msg() << kall << ktype << *ki << "\n";
|
|
std::smatch match;
|
|
if (std::regex_match(ki->m_text, match, varpat)
|
|
&& ki->m_type == katom_t::karg) {
|
|
//auto [var, found] = get(match[1]);
|
|
auto var_value = value(match[2], true, begin->m_loc);
|
|
if (var_value != klammerstate::no_value) {
|
|
msg() << "Found subst: " << match[1] << sp_arrow << var_value << "\n";
|
|
std::stringstream ss {};
|
|
ss << match[1] << var_value << match[3];
|
|
ki->m_text = ss.str(); // match[1] + var_value + match[3];
|
|
} else {
|
|
throw Argument_error("Variable " + q_(match[1]) + " is not defined", begin->m_loc);
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
void prohibit_change_of_description(
|
|
std::string name, bool defined, std::string old_desc, std::string new_desc, Locator old_loc, Locator loc)
|
|
{
|
|
if (defined && !old_desc.empty() && !new_desc.empty()) {
|
|
std::stringstream ss {};
|
|
ss << "The " << q_(name) << " variable's description is already defined";
|
|
if (new_desc != old_desc) {
|
|
ss << "; the description cannot be changed to " << q_(new_desc)
|
|
<< " from " << q_(old_desc);
|
|
}
|
|
ss << " at " << old_loc.desc() << ".";
|
|
|
|
throw Argument_error(ss.str(), loc);
|
|
}
|
|
}
|
|
|
|
|
|
// Parameter_set m_parameters = Parameter_set("name :value :append :replace :delim :desc");
|
|
|
|
void State::parse_state_katoms(katom_iter begin, katom_iter end, katom_list katoms)
|
|
{
|
|
(void)K::log(3, *begin, *(end-1));
|
|
// std::cout << "parse_katoms: " << std::pair(begin + 1, end - 1) << "\n";
|
|
auto [positional, optional, rest] = argument_split(begin + 1, end - 1);
|
|
auto args = m_parameters.value_map(positional, optional, rest, begin->m_loc);
|
|
|
|
// std::cout << std::setfill(' ') << "\nArgument values:\n" << args;
|
|
|
|
Var current = get(args["name"]);
|
|
bool defined = current.defined();
|
|
std::string delim = args["delim"];
|
|
delim = delim.empty() ? " " : delim;
|
|
|
|
prohibit_change_of_description(
|
|
args["name"], defined, current.m_desc, args["desc"], current.m_loc, begin->m_loc);
|
|
|
|
if (defined && !args["replace"].empty()) {
|
|
replace(args["name"], args["replace"]);
|
|
} else if (defined && !args["append"].empty()) {
|
|
replace(args["name"], current.m_value + delim + args["append"]);
|
|
} else if (!args["value"].empty()) {
|
|
set(args["name"], args["value"], false, delim, args["desc"], begin->m_loc);
|
|
}
|
|
|
|
modify_type(katom_t::replaced, begin, end);
|
|
auto next_iter = end;
|
|
ignore_whitespace(next_iter, katoms);
|
|
}
|
|
|
|
std::vector<std::string> State::all_names()
|
|
{
|
|
std::vector<std::string> result {};
|
|
for (Frame f : m_frames) {
|
|
for (auto [name, var] : f.m_vars) {
|
|
// std::cout << "Name: " << name << "\n";
|
|
result.push_back(name);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
std::string State::python_code()
|
|
{
|
|
(void)K::log(3);
|
|
std::vector<std::string> names = all_names();
|
|
std::string margin = " ";
|
|
std::stringstream ss {};
|
|
|
|
ss << "import sys\n";
|
|
for (auto d : sks_dirs()) {
|
|
auto python_files = pathnames_with_extension(d, "py");
|
|
if (!python_files.empty()) {
|
|
ss << "sys.path.append('" << d << "')\n";
|
|
}
|
|
}
|
|
if (!m_frames.empty()) {
|
|
int name_length = max_length(names);
|
|
ss << "class K:\n"
|
|
<< margin << std::left << std::setw(name_length) << "K_eval_id" << " = "
|
|
<< State::class_id++ << "\n";
|
|
for (const auto& name : names) {
|
|
// auto [var_value, argtype] = value_type(name);
|
|
// ss << argtype.python_value(name, var_value, name_length) << "\n";
|
|
std::string v = value(name);
|
|
v = string_replace(v, "\\", "\\\\");
|
|
v = string_replace(v, "\"", "\\\"");
|
|
std::string var_value = qq_(v);
|
|
ss << margin << std::left << std::setw(name_length) << name << " = " << var_value << "\n";
|
|
}
|
|
}
|
|
// msg() << ss.str() << "\n";
|
|
return ss.str();
|
|
}
|
|
|
|
|
|
std::string State::describe(bool show_environment, int margin_size) const
|
|
{
|
|
std::string margin(margin_size, ' ');
|
|
int i = m_frames.size() - 1;
|
|
std::stringstream ss {};
|
|
for (auto f : m_frames) {
|
|
int width = max_key_length(f.m_vars);
|
|
ss << margin << "Frame " << i-- << ": " << f.m_name << "\n";
|
|
if ((f.m_name != klammerstate::shell_environment_name) ||
|
|
(show_environment && f.m_name == klammerstate::shell_environment_name)) {
|
|
for (auto [key, value] : f.m_vars) {
|
|
std::string print_value = value.m_value;
|
|
if (print_value == klammerstate::no_value) {
|
|
print_value = "<no-value>";
|
|
}
|
|
ss << margin << " " << std::setw(width) << std::left << key << " "
|
|
<< abbrev(print_value) << "\n";
|
|
}
|
|
}
|
|
}
|
|
ss << "\n";
|
|
return ss.str();
|
|
}
|