view anagram/agcore/configparam.cpp @ 21:1c9dac05d040

Add lint-style FALLTHROUGH annotations to fallthrough cases. (in the parse engine and thus the output code) Document this, because the old output causes warnings with gcc10.
author David A. Holland
date Mon, 13 Jun 2022 00:04:38 -0400
parents 13d2b8934445
children
line wrap: on
line source

/*
 * AnaGram, A System for Syntax Directed Programming
 * Copyright 1993-2002 Parsifal Software. All Rights Reserved.
 * See the file COPYING for license and usage terms.
 *
 * configparam.cpp
 */

#include "agstring.h"
#include "configparam.h"
#include "rpk.h"
#include "rule.h"
#include "stacks.h"

//#define INCLUDE_LOGGING
#include "log.h"


ConfigParam::ConfigParam(const char *n)
  : name(n)
{}

void ConfigParam::setIntegerValue(const int, ErrorHandler &e) {
  LOGSECTION("ConfigParam::setValue(int)");
  char buf[200];
  sprintf(buf, "Parameter takes string value: %s", name);
  e.badParam(buf);
}

void ConfigParam::setStringValue(const AgString, ErrorHandler &e) {
  LOGSECTION("ConfigParam::setValue(AgString)");
  char buf[200];
  sprintf(buf, "Parameter takes integer value: %s", name);
  e.badParam(buf);
}

void ConfigParam::set(int k, ErrorHandler &e) {
  LOGSECTION("ConfigParam::set(int)");
  LOGV(string_base);
  AgString name = buildAgString();
  LOGV(name);
  strlwr(name.pointer());
  LOGV(name);
  int i;
  for (i = 0; i < paramCount; i++) {
    if (name != table[i]->name) {
      continue;
    }
    table[i]->setIntegerValue(k, e);
    return;
  }
  char buf[500];
  sprintf(buf, "Parameter not defined: %s", name.pointer());
  e.badParam(buf);
}

void ConfigParam::set(ErrorHandler &e) {
  LOGSECTION("ConfigParam::set()");
  AgString param = buildAgString();

  AgString name = buildAgString();
  strlwr(name.pointer());
  LOGV(name) LCV(param);
  int i;
  for (i = 0; i < paramCount; i++) {
    if (name != table[i]->name) continue;
    table[i]->setStringValue(param, e);
    return;
  }
  char buf[500];
  sprintf(buf, "Parameter not defined: %s", name.pointer());
  e.badParam(buf);
}

void ConfigParam::resetAll() {
  LOGSECTION("ConfigParam::resetAll");
  int i;
  LOGV(paramCount);
  for (i = 0; i < paramCount; i++) {
    //LOGV(table[i]->asString());
    table[i]->reset();
    LOGV(table[i]->asString());
  }
}

void ConfigParam::initAll() {
  LOGSECTION("ConfigParam::initAll");
  int i;
  LOGV(paramCount);
  for (i = 0; i < paramCount; i++) {
    LOGV(i) LCV(table[i]->asString());
    table[i]->init();
    LOGV(table[i]->asString());
  }
}

void IntegerParam::setIntegerValue(const int x, ConfigParam::ErrorHandler &) {
  parameter = x;
}

void ConfigSwitch::setIntegerValue(const int x, ConfigParam::ErrorHandler &) {
  parameter = x;
}

void ConfigSwitch::setStringValue(const AgString s, ConfigParam::ErrorHandler &e) {
  char *pointer = s.pointer();
  strlwr(pointer);
  if (s == "off") {
    parameter = 0;
  }
  else if (s == "on") {
    parameter = 1;
  }
  else {
    char buf[100];
    sprintf(buf, "Switch takes on/off values only: %s", name);
    e.badParam(buf);
  }
}

void StringParam::setStringValue(const AgString s, ConfigParam::ErrorHandler &) {
  parameter = s;
}

void TextParam::setStringValue(const AgString s, ConfigParam::ErrorHandler &) {
  parameter = s;
}

CastParam::CastParam(const char *name, int &p)
  : Param<int>(name, p)
{}

void CastParam::setStringValue(const AgString s, ConfigParam::ErrorHandler &e) {
  LOGSECTION("CastParam::setValue(AgString)");
  Cast type(s);
  if (strcmp(name, "default token type") == 0 && type.wrapperRequired()) {
    e.badParam("Token with wrapper cannot be default token type");
    return;
  }
  parameter = type;
}

AgString CastParam::asString() {
  LOGSECTION("CastParam::asString()");
  LOGV(parameter);
  LOGV((int) Cast(parameter)) LCV((AgString) Cast(parameter));

  if (parameter) {
    return Cast(parameter).name();
  }
  else {
    return AgString("UNDEFINED");
  }
}

TokenParam::TokenParam(const char *name, int &p)
  : Param<int>(name, p)
{
  LOGSECTION("TokenParam::TokenParam");
  LOGV(name) LCV(p);
}

void TokenParam::setStringValue(const AgString s, ConfigParam::ErrorHandler &) {
  LOGSECTION("TokenParam::setValue(AgString)");
  parameter = id_token(Symbol(s.pointer()));
}

AgString TokenParam::asString() {
  LOGSECTION("TokenParam::asString");
  LOGV(parameter) LCV(Token::count());
  Token token(parameter);
  //AgString name = Token(parameter)->token_name->string;
  AgString name = token.isNotNull() ? token->token_name->string : 
    AgString("UNDEFINED");
  LOGV(name);
  //if (name.size() == 0) name = "UNDEFINED";
  return name;
}

AgString ConfigSwitch::asString() {
  return AgString(parameter ? "ON" : "OFF");
}

AgString StringParam::asString() {
  char buf[200];
  if (parameter.exists()) {
    sprintf(buf, "\"%s\"", parameter.pointer());
    return AgString(buf);
  }
  return AgString("UNDEFINED");
}

AgString TextParam::asString() {
  if (parameter.exists()) return parameter;
  return AgString("UNDEFINED");
}

AgString IntegerParam::asString() {
  char buf[20];
  sprintf(buf, "%d", parameter);
  return AgString(buf);
}