BDSIM
BDSIM is a Geant4 extension toolkit for simulation of particle transport in accelerator beamlines.
sym_table.cc
1/*
2Beam Delivery Simulation (BDSIM) Copyright (C) Royal Holloway,
3University of London 2001 - 2022.
4
5This file is part of BDSIM.
6
7BDSIM is free software: you can redistribute it and/or modify
8it under the terms of the GNU General Public License as published
9by the Free Software Foundation version 3 of the License.
10
11BDSIM is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with BDSIM. If not, see <http://www.gnu.org/licenses/>.
18*/
19#include "sym_table.h"
20
21#include <cstdlib>
22#include <iostream>
23
24#include "array.h"
25
26using namespace GMAD;
27
28Symtab::Symtab(std::string s):
29 name(s),
30 is_reserved(false),
31 type(symtabtype::NUMBER),
32 funcptr(nullptr),
33 value(0.0)
34{
35}
36
38{
39 array.clear();
40 for(unsigned int i=0;i<a->data.size();i++)
41 array.push_back(a->data[i]);
42 type = symtabtype::ARRAY;
43}
44
45void Symtab::Set(std::string a)
46{
47 str = a;
48 type = symtabtype::STRING;
49}
50
51
52void Symtab::Set(double a, bool reserved)
53{
54 value = a;
55 is_reserved = reserved;
56 type = symtabtype::NUMBER;
57}
58
60{
61 funcptr = a;
62 type = symtabtype::FUNCTION;
63}
64
65std::string Symtab::GetName()const
66{
67 return name;
68}
69
70Symtab::symtabtype Symtab::GetType()const
71{
72 return type;
73}
74
76{
77 return is_reserved;
78}
79
80double Symtab::GetNumber()const
81{
82 if (type!=symtabtype::NUMBER)
83 {
84 std::cout << "Symbol is not a number!" << std::endl;
85 exit(1);
86 }
87 return value;
88}
89
90std::string Symtab::GetString()const
91{
92 if (type!=symtabtype::STRING)
93 {
94 std::cout << "Symbol is not a string!" << std::endl;
95 exit(1);
96 }
97 return str;
98}
99
100std::list<double> Symtab::GetArray()const
101{
102 if (type!=symtabtype::ARRAY)
103 {
104 std::cout << "Symbol is not a array!" << std::endl;
105 exit(1);
106 }
107 return array;
108}
109
111{
112 if (type!=symtabtype::FUNCTION)
113 {
114 std::cout << "Symbol is not a function!" << std::endl;
115 exit(1);
116 }
117 return funcptr;
118}
119
121{
122 std::cout << "\t" << name << " = ";
123 std::list<double>::iterator it;
124 switch(type) {
125 case Symtab::symtabtype::NUMBER:
126 std::cout << value << std::endl;
127 break;
128
129 case Symtab::symtabtype::STRING:
130 std::cout << str << std::endl;
131 break;
132
133 case Symtab::symtabtype::ARRAY:
134 std::cout << "{";
135 for(it=array.begin();it!=array.end();++it)
136 {std::cout << " " << (*it) << " ";}
137 std::cout << "}" << std::endl;
138 break;
139
140 default:
141 break;
142 }
143}
Representation of arrays used in tokens.
Definition: array.h:40
std::vector< double > data
Representation of double array.
Definition: array.h:46
std::list< double > array
data
Definition: sym_table.h:88
std::list< double > GetArray() const
Get methods that check on type.
Definition: sym_table.cc:100
symtabtype GetType() const
Get type.
Definition: sym_table.cc:70
function funcptr
data
Definition: sym_table.h:85
double value
data
Definition: sym_table.h:86
function GetFunction() const
Set to function pointer.
Definition: sym_table.cc:110
double GetNumber() const
Set to double.
Definition: sym_table.cc:80
double(* function)(double)
typedef for function pointer
Definition: sym_table.h:45
void Print()
Print method.
Definition: sym_table.cc:120
symtabtype type
Enum type.
Definition: sym_table.h:83
bool is_reserved
Flag is parser symbol is a reserved name.
Definition: sym_table.h:81
std::string name
Name of parser symbol.
Definition: sym_table.h:79
void Set(Array *)
Set to Array value.
Definition: sym_table.cc:37
std::string str
data
Definition: sym_table.h:87
bool IsReserved() const
Check if symbol is a reserved name.
Definition: sym_table.cc:75
std::string GetName() const
Get name.
Definition: sym_table.cc:65
std::string GetString() const
Set to string.
Definition: sym_table.cc:90
Parser namespace for GMAD language. Combination of Geant4 and MAD.