Newer
Older
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
#pragma once
// [config-parser.h]: configuration file parser
//
// -------------------------------------------------------------------------------------------- //
#include "mpi-com.h"
#include "str-com.h"
#include "cfg-var.h"
namespace nse
{
class ConfigParser {
public:
ConfigParser();
~ConfigParser();
bool run(const char* filename);
bool mpi_run(const char* filename, const MPI_Comm comm);
bool mpi_run(const char* filename);
bool is_varname(const char* name) const;
const cfgVariable get_variable(const int idx) const;
const cfgVariable get_variable(const char* name) const;
bool get_value(const char* name, int* value) const;
bool get_value(const char* name, float* value) const;
bool get_value(const char* name, double* value) const;
bool get_value(const char* name, char** value) const;
bool get_value(const char* name, std::string& value) const;
bool get_value(const char* name, bool* value) const;
bool mpi_is_varname(const char* name, const MPI_Comm comm) const;
template< typename T >
bool mpi_get_value(const char* name, T* value, const MPI_Comm comm) const;
bool mpi_get_value(const char* name, char** value, const MPI_Comm comm) const;
bool mpi_get_value(const char* name, std::string& value, const MPI_Comm comm) const;
void print() const;
private: // datatypes //
enum LEXEME_TYPE { // lexeme types //
IS_INVALID,
IS_NAME,
IS_ASSIGNMENT,
IS_BRACE_OPEN, IS_BRACE_CLOSE, // {}
IS_BRACKET_OPEN, IS_BRACKET_CLOSE, // ()
IS_SEMICOLON,
IS_VALUE,
IS_OP_ADD, IS_OP_SUB, // +,- [priority=1, assoc.=left]
IS_OP_MUL, IS_OP_DIV, IS_OP_MOD, // *,/,% [priority=2, assoc.=left]
IS_OP_PLUS, IS_OP_MINUS, // +,- [priority=3, assoc.=left]
IS_OP_EXP // ^ [priority=4, assoc.=right]
};
enum OP_ASSOCIATIVITY { IS_OP_LEFT, IS_OP_RIGHT }; // operation specifiers //
struct parserState { // helper struct to control parsing state //
parserState();
parserState(const parserState& state);
~parserState();
void truncate_name_space();
void append_name_space(const char* name);
int idx, level; // lexeme index and namespace level
char *name_space; // current namespace
private: // allocation data
int nalloc;
static const int c_alloc_init = 64;
};
struct rpnList { // helper struct for RPN expressions evaluation //
rpnList();
~rpnList();
bool convert(
parserState& state, // advancing state after delimiter [';']
const LEXEME_TYPE *lexeme_type, const FileParser& parser);
private: // interface //
void init();
void add(const int idx);
bool empty();
public:
int *expr; // lexeme index corresponding to element
int nexpr; // number of elements in expression
private: // allocation data
int nalloc;
static const int c_alloc_init = 64;
};
private: // static //
static bool is_valid_name(const char* lexeme); // valid variable name check //
// op - priority interface //
static bool is_op(const LEXEME_TYPE op);
static bool is_op_binary(const LEXEME_TYPE op);
static bool is_op_unary(const LEXEME_TYPE op);
static int op_priority(const LEXEME_TYPE op);
static OP_ASSOCIATIVITY op_associativity(const LEXEME_TYPE op);
static bool op_lt(const LEXEME_TYPE opA, const LEXEME_TYPE opB);
private: // processing //
bool add(const cfgVariable& rvalue); // adding variable to list //
const cfgVariable evaluate_rpn(
const rpnList& rpn,
parserState state, // using copy due to namespace operations
const LEXEME_TYPE *lexeme_type, const FileParser& parser) const;
bool run_lexical_analysis(LEXEME_TYPE *lexeme_type,
const FileParser& parser);
bool run_syntax_analysis(const LEXEME_TYPE *lexeme_type,
const FileParser& parser);
private: // data //
int nalloc_vars;
static const int c_alloc_init = 64;
int nvars;
cfgVariable *var;
};
}