view anagram/guisupport/items.cpp @ 12:aab9ff6af791

Strengthen the build hack for non-DOS targets.
author David A. Holland
date Tue, 31 May 2022 00:58:42 -0400
parents 13d2b8934445
children
line wrap: on
line source

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

#include "dc.h"
#include "dict.h"
#include "items.h"
#include "myalloc.h"
#include "q1glbl.h"
#include "rule.h"
#include "stexpdc.h"
#include "stacks.h"
#include "token.h"
#include "ut.h"
#include "wm1.h"
#include "ws.h"

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


state_table_dc::item::item(unsigned ln) {
//  LOGSECTION("state_table_dc::item::item");
  int k = 0;
  unsigned lc = 0;
  unsigned *ndx = isht_dict->ndx;
  int *text = isht_dict->text;
  int *p;
//  LOGV(ln);
//  LOGV((int) text);
//  LOGV((int) ndx);
  while (lc <= ln) {
    lc += (text[ndx[k++]]-1)/2;
  }
//  LOGV(lc);
//  LOGV((int) p);
  p = &text[ndx[--k]];
  lc -= (*p++ -1)/2;
  p += 2*(first_flag = ln-lc);
  state = k;
//  LOGV((int) p);
  rule = *p++;
  index= *p;
//  LOGV(rule);
//  LOGV(index);
}

unsigned state_table_dc::item::token(void) {
  unsigned length;
  Rule r(rule);

  length = r->length();
  if (index >= length) {
    return 0;
  }
  //return lstptr(map_form_number[rule],tokens)[index];
  return r.token(index);
}

int build_item_list_ok(int tn) {
  LOGSECTION("build_item_list_ok");
  LOGV(tn);
  LOGV(ntkns);
  return tn && map_token_number[tn].non_terminal_flag;
}

dc_ref build_item_list(int tn) {
  //dc_ref window;
  if (tn == 0 || !map_token_number[tn].non_terminal_flag) {
    return dc_ref();
  }

  AgString head("Expansion Rules");
  AgString foot = AgString::format("T%03d: ", tn).concat(token_string(tn));
  return dc_ref(new expansion_rules_dc(tn, foot));
}

expansion_rules_dc::expansion_rules_dc(unsigned tn, const AgString foot)
  : dc("Expansion Rules", foot), token_number(tn)
{
  des->d_size.y = Token(tn)->expansionRuleList.size();
}

unsigned expansion_rules_dc::rule(unsigned ln) const {
  return Token(token_number).expansionRule(ln);
}

unsigned expansion_rules_dc::token(unsigned ln) const {
  int rn = rule(ln);
  if (map_form_number[rn].length() == 0) {
    return 0;
  }
  return Rule(rn).token(0);
}

void expansion_rules_dc::synchCursor(unsigned ln) const {
  unsigned rn = rule(ln);
  if (rn) {
    set_rule_line(rn);
  }
}

void expansion_rules_dc::getLine(unsigned ln) const{
  ics();
  append_item(rule(ln), 0);
}

int expansion_rules_dc::expansion_rules_ok(unsigned ln) {
  return build_item_list_ok(token(ln));
}

dc_ref expansion_rules_dc::expansion_rules(unsigned ln) {
  unsigned tn = token(ln);
  if (tn) {
    return build_item_list(token(ln));
  }
  return NULL;
}

int expansion_rules_dc::productions_ok(unsigned ln) {
  return productions_window_ok(token(ln));
}

dc_ref expansion_rules_dc::productions(unsigned ln) {
  unsigned tn = token(ln);
  if (tn) {
    return productions_window(token(ln));
  }
  return NULL;
}


dc_ref expansion_rules_dc::rule_context(unsigned ln) {
  unsigned rn = rule(ln);
  if (rn) {
    return rule_context_window(rn);
  }
  return NULL;
}

int expansion_rules_dc::set_elements_ok(unsigned ln) {
  unsigned tn = token(ln);
  if (tn) {
    return token_set_window_ok(token(ln));
  }
  return 0;
}

dc_ref expansion_rules_dc::set_elements(unsigned ln) {
  unsigned tn = token(ln);
  if (tn) {
    return token_set_window(token(ln));
  }
  return NULL;
}

int expansion_rules_dc::usage_ok(unsigned ln) {
  unsigned tn = token(ln);
  return token_usage_window_ok(tn);
}

dc_ref expansion_rules_dc::usage(unsigned ln) {
  unsigned tn = token(ln);
  return token_usage_window(tn);
}

dc_ref expand_specific_item(dc_ref , int fn, int fx){
  int tn;
  Rule rule(fn);

  if (fx < 0 || fx >= rule->length()) {
    return dc_ref();
  }
  tn = rule.token(fx);
  return build_item_list(tn);
}

static int state_table_tabs[] = {7,0};

void state_table_dc::getLine(unsigned ln) const {
  item x(ln);
  ics();
  if (x.first_flag == 0) {
    apprintf("S%03d:", x.state);
  }
  acs('\t');
  append_item(x.rule, x.index);
}

void state_table_dc::synchCursor(unsigned ln) const {
  set_rule_line(item(ln).rule);
}

state_table_dc::state_table_dc(void)
  : dc("State Definition Table")
{
  int dp  = 0,k = isht_dict->nsx;
  const int *ndx = (int*)isht_dict->ndx, *text = (int*)isht_dict->text;


  tab_stops = state_table_tabs;
  columnHeadTitle = "State\tCharacteristic Rules";
  while (k--) dp += (text[*ndx++]-1);
  dp /=2;
  ok_ptr(des);
  des->d_size.y = dp;
}

dc_ref state_table_dc::aux_trace(unsigned ln) {
  return aux_trace_window(item(ln).state);
}

int state_table_dc::expansion_rules_ok(unsigned ln){
  item x(ln);
  unsigned tn = x.token();
  return build_item_list_ok(tn);
}

dc_ref state_table_dc::expansion_rules(unsigned ln){
  item x(ln);
  unsigned tn = x.token();
  if (tn == 0) {
    return NULL;
  }
  return build_item_list(tn);
}

int state_table_dc::keywords_ok(unsigned ln) {
  return keywords_window_ok(item(ln).state);
}

dc_ref state_table_dc::keywords(unsigned ln) {
  return keywords_window(item(ln).state);
}

int state_table_dc::previous_states_ok(unsigned ln) {
  return previous_states_window_ok(item(ln).state);
}

dc_ref state_table_dc::previous_states(unsigned ln) {
  return previous_states_window(item(ln).state);
}

int state_table_dc::productions_ok(unsigned ln) {
  return productions_window_ok(item(ln).token());
}

dc_ref state_table_dc::productions(unsigned ln) {
  return productions_window(item(ln).token());
}

int state_table_dc::reduction_states_ok(unsigned ln) {
  item x(ln);
  return reduction_states_window_ok(x.rule,x.index);
}

dc_ref state_table_dc::reduction_states(unsigned ln) {
  item x(ln);
  return reduction_states_window(x.state,x.rule,x.index);
}

dc_ref state_table_dc::rule_context(unsigned ln){
  item x(ln);
  return rule_context_window(x.rule);
}

int state_table_dc::set_elements_ok(unsigned ln) {
  return token_set_window_ok(item(ln).token());
}

dc_ref state_table_dc::set_elements(unsigned ln) {
  return token_set_window(item(ln).token());
}

dc_ref state_table_dc::state_expansion(unsigned ln) {
  return state_expansion_window(item(ln).state);
}

int state_table_dc::usage_ok(unsigned ln) {
  return token_usage_window_ok(item(ln).token());
}

dc_ref state_table_dc::usage(unsigned ln) {
  return token_usage_window(item(ln).token());
}