blob: 9297b6e913d1f11ad0416c0e03346d11cbb58456 (
plain)
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
|
/**
* @module cprogram
* @author Guenther Neuwirth (0626638), Manuel Mausz (0728348)
* @brief class for parsing and saving a program
* @date 11.05.2009
*/
#include <fstream>
#include <vector>
#include <string>
#include <boost/tokenizer.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include "cprogram.h"
using namespace std;
using namespace boost;
CProgram::CProgram(const std::string& progfile) :
m_programfile(progfile)
{
parse();
dump(std::cout);
}
/*----------------------------------------------------------------------------*/
CProgram::~CProgram()
{
}
/*----------------------------------------------------------------------------*/
void CProgram::parse()
{
/* open and read file */
ifstream file(m_programfile.c_str(), ios::in);
// if (!file)
// throw ParserError("Unable to open scriptfile '" + m_scriptfile + "'.");
int cur_line_nr = 0;
while (!file.eof() && file.good())
{
string cur_line;
/* read file per line */
getline(file, cur_line);
if (cur_line.empty())
continue;
trim(cur_line);
/* ignore comments */
if (cur_line.find_first_of('#') == 0)
continue;
/*remove commas from current line */
unsigned int pos;
while (( pos = cur_line.find_first_of(",") ) != string::npos)
cur_line.erase(pos, 1);
/* add source line*/
m_progsource.push_back(vector<string>());
algorithm::split( m_progsource.back(), cur_line, is_any_of(" \t"));
/* jump addr as line number */
if(m_progsource[cur_line_nr][0] == "label")
{
int size = m_progsource[cur_line_nr][01].size() - 1;
string label(m_progsource[cur_line_nr][01].substr(0, size));
m_jumpaddr[label] = cur_line_nr;
}
cur_line_nr++;
}
file.close();
}
/*----------------------------------------------------------------------------*/
#ifdef DEBUG
void CProgram::dump(std::ostream& out)
{
out << endl << "Program file:" << endl << m_programfile << endl;
out << endl << "Program source:" << endl;
for (int i = 0; i < (int) m_progsource.size();i++)
{
for(int x = 0; x < (int) m_progsource[i].size();x++)
out << m_progsource[i][x] << " ";
out << std::endl;
}
out << endl << "Jump addresses:" << endl;
map<string, unsigned int>::iterator it;
for (it = m_jumpaddr.begin(); it != m_jumpaddr.end(); it++)
out << (*it).first << " " << (*it).second << endl;
}
#endif
|