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