File: cmd_symbol.cc

package info (click to toggle)
gpsim 0.32.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,644 kB
  • sloc: cpp: 121,258; asm: 54,223; ansic: 13,576; python: 9,708; sh: 4,695; makefile: 1,575; lex: 1,139; yacc: 854; pascal: 511; perl: 93; awk: 44; xml: 41
file content (176 lines) | stat: -rw-r--r-- 4,703 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/*
   Copyright (C) 1999 T. Scott Dattalo

This file is part of gpsim.

gpsim is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.

gpsim is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with gpsim; see the file COPYING.  If not, write to
the Free Software Foundation, 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.  */

#include <stdio.h>

#include <iostream>
#include <string>
#include <typeinfo>

#include "command.h"
#include "cmd_symbol.h"
#include "misc.h"
#include "../src/ValueCollections.h"
#include "../src/errors.h"
#include "../src/gpsim_object.h"
#include "../src/modules.h"
#include "../src/pic-instructions.h"
#include "../src/symbol.h"
#include "../src/ui.h"
#include "../src/value.h"


cmd_symbol c_symbol;

static cmd_options cmd_symbol_options[] =
{
  {nullptr, 0, 0}
};


cmd_symbol::cmd_symbol()
  : command("symbol", nullptr)
{
  brief_doc = "Add or display symbols";

  long_doc = "symbol [<symbol_name>]\n"
    "symbol <symbol_name>=<value>\n"
    "\n"
    "\tIf no options are supplied, the entire symbol table will be\n"
    "\tdisplayed. If only the symbol_name is provided, then only\n"
    "\tthat symbol will be displayed.\n"
    "\tIf a symbol_name that does not currently exist is equated\n"
    "\tto a value, then a new symbol will be added to the symbol table.\n"
    "\tThe type of symbol will be derived. To force a string value double\n"
    "\tdouble quote the value.\n"
    "\n"
    "\tValid symbol types:\n"
    "\t  Integer, Float, Boolean and String\n"
    "\n"
    "Examples:\n"
    "\tsymbol                     // display the symbol table\n"
    "\tsymbol GpsimIsGreat=true   // create a new constant symbol\n"
    "\n";

  op = cmd_symbol_options;
}

static std::string table;

void dumpOneSymbol(const SymbolEntry_t &sym)
{
  std::string out;

  Value *pVal = dynamic_cast<Value *>(sym.second);
  if (pVal != nullptr && typeid(*pVal) == typeid(LineNumberSymbol))
     return;

  if (table != "__global__")
      out = table + "." + sym.second->name();
  else
      out = sym.second->name();

  printf("%-25s Type: %s\n", out.c_str(), sym.second->showType().c_str());
}

void dumpSymbolTables(const SymbolTableEntry_t &st)
{
  table = st.first;
  (st.second)->ForEachSymbolTable(dumpOneSymbol);
}

void cmd_symbol::dump_all()
{
  std::cout << "\nSymbol table\n";
  globalSymbolTable().ForEachModule(dumpSymbolTables);
}

void cmd_symbol::dump_one(const char *sym_name)
{
  std::string sName(sym_name);
  Module *pM = globalSymbolTable().findModule(sName);

  if (pM)
    pM->getSymbolTable().ForEachSymbolTable(dumpOneSymbol);
  else
    dump_one(globalSymbolTable().find(sName));
  //get_symbol_table().dump_filtered(sName);
}
void cmd_symbol::dump_one(gpsimObject *s)
{
  if(s) {
    Module *pM  = dynamic_cast<Module *>(s);
    if (pM)
      pM->getSymbolTable().ForEachSymbolTable(dumpOneSymbol);
    else
      std::cout << s->toString() << '\n';
  }
}

void cmd_symbol::add_one(const char *sym_name, Expression *expr)
{
  Value * pVal = expr->evaluate();
  if (pVal) {
    pVal->new_name(sym_name);
    //pVal->setClearableSymbol(false);
    pVal->set_description("Derived from the command line.");
    if (!globalSymbolTable().addSymbol(pVal))
      delete pVal;
  }
}

void cmd_symbol::EvaluateAndDisplay(Expression *pExpr) {
  try {
    Value * pValue = pExpr->evaluate();
    GetUserInterface().DisplayMessage("%s\n", pValue->toString().c_str());
  }
  catch (const Error &pMessage)  {
    GetUserInterface().DisplayMessage("%s\n", pMessage.what());
  }
}

void cmd_symbol::dump(gpsimObject *s, ExprList_t*e)
{
  Value *pValue = dynamic_cast<Value *>(s);
  if (pValue) {
    IndexedSymbol sym(pValue, e);
    std::cout << sym.toString() << '\n';
  }
}

void cmd_symbol::Set(gpsimObject *s, ExprList_t*e, Expression *pExpr)
{
  Value *pValue = dynamic_cast<Value *>(s);
  if (pValue) {
    try {
      IIndexedCollection *pCollection = dynamic_cast<IIndexedCollection*>(s);
      if (pCollection == nullptr) {
        GetUserInterface().DisplayMessage("%s is not an indexed symbol\n",
                                          s->name().c_str());
      }
      else {
        pCollection->SetAt(e, pExpr);
      }
    }
    catch (const Error &Message)  {
      GetUserInterface().DisplayMessage("%s\n", Message.what());
    }
  }
}