forked from paule32/forth2asm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
214 lines (189 loc) · 6.54 KB
/
common.h
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// --------------------------------------------------------------------------------
// MIT License
//
// Copyright (c) 2018 Jens Kallup
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
// --------------------------------------------------------------------------------
#ifndef COMMON_H
#define COMMON_H
#pragma once
#include <iostream> // std::cout
#include <vector>
#include <string> // std::string
#include <sstream> // std::stringstream
#include <iomanip> // std::setw
#include <regex>
#include <cstring>
extern int yyerror(const char*);
extern int yylex();
extern void yyset_in (FILE*);
extern void yyset_out(FILE*);
extern void yyset_lineno(int);
extern int yyget_lineno();
// my own namespace: non-standard !!
namespace ext {
const int CVT_NONE = 0;
const int CVT_ASM = 2;
const int CVT_PASCAL = 1; // default ?
enum ForthFlags {
FLAG_NULL = 0, // empty
FLAG_SUBROUTINE, // subroutine
FLAG_PARAMETER, // parameter
FLAG_PROGRAM, // program
FLAG_VARIABLE // variable
};
struct forth_labels {
::std::string id;
::std::stringstream id_stream;
forth_labels() { }
forth_labels(const forth_labels &o) {
id = o.id;
id_stream.clear();
id_stream << o.id_stream.str();
}
forth_labels & operator=(const forth_labels &o) {
id = o.id;
id_stream.clear();
id_stream << o.id_stream.str();
}
};
struct forth_programs {
::std::string id;
::std::stringstream id_stream;
forth_programs() { }
forth_programs(const forth_programs &o) {
id = o.id;
id_stream.clear();
id_stream << o.id_stream.str();
}
forth_programs & operator=(const forth_programs &o) {
id = o.id;
id_stream.clear();
id_stream << o.id_stream.str();
}
};
struct forth_variables {
::std::string id;
int id_type = 0;
forth_variables() {}
forth_variables(const forth_variables &o) {
id = o.id;
id_type = o.id_type;
}
forth_variables & operator=(const forth_variables &o) {
id = o.id;
id_type = o.id_type; return
*this;
}
};
struct forth_namespace {
::std::string id;
::std::vector<forth_namespace> id_parameter;
::std::vector<forth_variables> id_variables;
::std::vector<forth_programs> id_programs;
::std::vector<forth_labels> id_labels;
int id_index = 0;
ForthFlags id_type = ForthFlags::FLAG_NULL;
forth_namespace() {}
forth_namespace(::std::string s1) : id(s1) {}
forth_namespace( const forth_namespace &o) { id = o.id; }
forth_namespace & operator=(const forth_namespace &o) { id = o.id; }
};
::std::vector<forth_namespace> forth_memory;
::std::vector<forth_namespace>::iterator forth_memory_iterator;
::std::string forth_procedure;
int forth_memory_position = 0;
FILE * file_pile[2048];
int convert_mode = CVT_PASCAL;
int current_id = 0;
int current_flag = ForthFlags::FLAG_NULL;
int yyget_line_gap = 0;
int yymet_line_gap = 0;
int tmp_top_of_stack = 0;
int tmp_parameter = 1;
::std::stringstream tmp_string_stream;
::std::string yypadding(::std::string c) {
::std::stringstream ss;
if (convert_mode == CVT_ASM) { ss
<< ";"
<< ::std::setw(yymet_line_gap)
<< ::yyget_lineno()
<< ": " << c; } else
if (convert_mode == CVT_PASCAL) { ss
<< "(* "
<< ::std::setw(yymet_line_gap)
<< ::yyget_lineno()
<< ": " << c
<< "*)" ;
}
return ss.str();
}
size_t strlen(const char * _str) {
size_t i = 0;
while(_str[i++]);
return i;
}
::std::string string2upper(::std::string s) {
char *p = (char*)s.c_str();
while (*p) {
*p = toupper(*p);
++p;
}
return ::std::string(p);
}
::std::string string2lower(std::string s) {
char *p = (char*)s.c_str();
while (*p) {
*p = toupper(*p);
++p;
}
return ::std::string(p);
}
void removeCharsFromString(::std::string &str, char* charsToRemove) {
for (unsigned int i = 0; i < strlen(charsToRemove); ++i) {
str.erase(remove(str.begin(),str.end(),charsToRemove[i]),str.end());
}
}
void replaceStr(::std::string &data, ::std::string toSearch, ::std::string replaceStr)
{
// Get the first occurrence
size_t pos = data.find(toSearch);
// Repeat till end is reached
while( pos != std::string::npos)
{
// Replace this occurrence of Sub String
data.replace(pos, toSearch.size(), replaceStr);
// Get the next occurrence from the current position
pos = data.find(toSearch, pos + replaceStr.size());
}
}
::std::vector< ::std::string> split(const ::std::string& s, char delimiter)
{
::std::vector< ::std::string> tokens;
::std::string token;
::std::istringstream tokenStream(s);
while (::std::getline(tokenStream, token, delimiter))
{
tokens.push_back(token);
}
return tokens;
}
}
#endif