This repository has been archived by the owner on Sep 29, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
assembler.hpp
161 lines (116 loc) · 3.15 KB
/
assembler.hpp
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
/* assembler.hpp
*
* /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\
* --------------------------------------------------------
* This project contains an Assembler I made in C++
* for a project I am making, by re-inventing the wheel,
* and creating a 16-bit computer on an FPGA.
* --------------------------------------------------------
* \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
*
* Created by purpl3f0x on 7/1/18.
* Stavros Avramidis
*/
#ifndef ASSEMBLER_HPP
#define ASSEMBLER_HPP
// Boost libs
#include <boost/format.hpp>
#include <boost/fusion/adapted.hpp>
#include <boost/spirit/home/x3.hpp>
// STL libs (Yeah I know that's redundant)
#include <algorithm>
#include <bitset>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <sstream>
#include <stdexcept>
#include <string>
#include <vector>
#include "opcodes.hpp"
#include "rules.hpp"
#define SIZE 65536
#define WORD 16
#define OPCODE_SIZE 7
/*---------------namespace asmbl-------------*/
namespace asmbl {
namespace x3 = boost::spirit::x3;
namespace ascii = boost::spirit::x3::ascii;
using std::cout;
using std::endl;
using std::ifstream;
using std::ios;
using std::map;
using std::ofstream;
using std::stringstream;
using std::to_string;
using std::vector;
using x3::_attr;
using x3::alnum;
using x3::alpha;
using x3::char_;
using x3::double_;
using x3::eol;
using x3::int_;
using x3::lexeme;
using x3::lit;
using x3::parse;
using x3::phrase_parse;
using x3::skip;
using ascii::space;
class Assembler {
public:
Assembler(bool mode = false);
short Register(std::string s);
void setInputFile(std::string file);
bool parseString(const std::string &s) { return parser(s); }
bool parseFile();
bool translate(std::string outName = "");
std::string getErrors() const;
private:
struct instruction {
std::string opCode;
vector<std::string> arguments;
int size;
instruction();
void add(std::string s);
void add(vector<std::string> &v);
};
struct data_type {
std::string type;
std::string name;
std::string value;
};
struct error {
int line;
int index;
std::string type;
std::string message;
std::string line_text;
error(int line, int index, std::string type, std::string message, std::string line_text);
const std::string get();
};
int cur_line;
std::string cur_file;
bool onDebug;
std::vector<short> rom;
// All maps provιde binary value of each type
map<std::string, short> reg; // Registers
map<std::string, short> data; // Variable Stack
map<std::string, short> text; // Const Stack
vector<instruction> instructions;
vector<error> errors;
vector<std::string> out;
bool isEmptyLine(const std::string &line);
bool isData(const std::string &line);
bool isText(const std::string &line);
bool isStart(const std::string &line);
int to_int(std::string &s);
bool data_typeParser(const std::string &line, map<std::string, short> &stack);
bool lineParser(const std::string &line);
bool parser(const std::string &s);
bool parser(ifstream &fs);
};
} //end of assembler namespace
#endif // ASSEMBLER_H