添加符号表
This commit is contained in:
parent
ff38dc4b7c
commit
0e426a1a1b
@ -4,7 +4,6 @@
|
|||||||
#include <map>
|
#include <map>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <set>
|
#include <set>
|
||||||
#include <string>
|
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
@ -12,7 +11,6 @@ using std::map;
|
|||||||
using std::pair;
|
using std::pair;
|
||||||
using std::set;
|
using std::set;
|
||||||
using std::shared_ptr;
|
using std::shared_ptr;
|
||||||
using std::string;
|
|
||||||
using std::vector;
|
using std::vector;
|
||||||
using std::weak_ptr;
|
using std::weak_ptr;
|
||||||
|
|
||||||
@ -26,8 +24,8 @@ struct Struct {
|
|||||||
weak_ptr<Struct> fa;
|
weak_ptr<Struct> fa;
|
||||||
vector<shared_ptr<TemplateType>> c1;
|
vector<shared_ptr<TemplateType>> c1;
|
||||||
vector<shared_ptr<ValType>> c2;
|
vector<shared_ptr<ValType>> c2;
|
||||||
map<string, shared_ptr<ValType>> vars;
|
map<int, shared_ptr<ValType>> vars;
|
||||||
map<string, shared_ptr<Struct>> structs;
|
map<int, shared_ptr<Struct>> structs;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ValType {
|
struct ValType {
|
||||||
|
@ -30,11 +30,12 @@ enum class TokenType {
|
|||||||
};
|
};
|
||||||
|
|
||||||
extern std::string token_mp[];
|
extern std::string token_mp[];
|
||||||
|
extern std::vector<std::string> id_mp;
|
||||||
|
|
||||||
struct Token {
|
struct Token {
|
||||||
int line;
|
int line;
|
||||||
TokenType type;
|
TokenType type;
|
||||||
std::string s;
|
int s;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif
|
#endif
|
24
src/scan.cpp
24
src/scan.cpp
@ -1,4 +1,5 @@
|
|||||||
#include "scan.h"
|
#include "scan.h"
|
||||||
|
#include <map>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
@ -14,9 +15,11 @@ vector<Token> scan(string s) {
|
|||||||
pt++;
|
pt++;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
map<string, int> mp;
|
||||||
for (skipSpace(); pt < s.size(); skipSpace()) {
|
for (skipSpace(); pt < s.size(); skipSpace()) {
|
||||||
TokenType type;
|
TokenType type;
|
||||||
string t;
|
string t;
|
||||||
|
int id = 0;
|
||||||
if (s[pt] == ',') {
|
if (s[pt] == ',') {
|
||||||
type = TokenType::COMMA, pt++;
|
type = TokenType::COMMA, pt++;
|
||||||
} else if (s[pt] == ';') {
|
} else if (s[pt] == ';') {
|
||||||
@ -56,27 +59,32 @@ vector<Token> scan(string s) {
|
|||||||
}
|
}
|
||||||
t = s.substr(pt, r - pt);
|
t = s.substr(pt, r - pt);
|
||||||
if (t == "struct") {
|
if (t == "struct") {
|
||||||
type = TokenType::STRUCT, t.clear();
|
type = TokenType::STRUCT;
|
||||||
} else if (t == "Fn") {
|
} else if (t == "Fn") {
|
||||||
type = TokenType::FN, t.clear();
|
type = TokenType::FN;
|
||||||
} else if (t == "return") {
|
} else if (t == "return") {
|
||||||
type = TokenType::RETURN, t.clear();
|
type = TokenType::RETURN;
|
||||||
} else if (t == "typeof") {
|
} else if (t == "typeof") {
|
||||||
type = TokenType::TYPEOF, t.clear();
|
type = TokenType::TYPEOF;
|
||||||
} else if (t == "private") {
|
} else if (t == "private") {
|
||||||
type = TokenType::PRIVATE, t.clear();
|
type = TokenType::PRIVATE;
|
||||||
} else if (t == "admit") {
|
} else if (t == "admit") {
|
||||||
type = TokenType::ADMIT, t.clear();
|
type = TokenType::ADMIT;
|
||||||
} else if (t == "delete") {
|
} else if (t == "delete") {
|
||||||
type = TokenType::DELETE, t.clear();
|
type = TokenType::DELETE;
|
||||||
} else {
|
} else {
|
||||||
type = TokenType::ID;
|
type = TokenType::ID;
|
||||||
|
if(mp.find(t) == mp.end()) {
|
||||||
|
mp[t] = id_mp.size();
|
||||||
|
id_mp.push_back(t);
|
||||||
|
}
|
||||||
|
id = mp[t];
|
||||||
}
|
}
|
||||||
pt = r;
|
pt = r;
|
||||||
} else {
|
} else {
|
||||||
printf("error on line %d", line), exit(1);
|
printf("error on line %d", line), exit(1);
|
||||||
}
|
}
|
||||||
tokens.push_back({line, type, t});
|
tokens.push_back({line, type, id});
|
||||||
}
|
}
|
||||||
return tokens;
|
return tokens;
|
||||||
}
|
}
|
@ -1,7 +1,9 @@
|
|||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
std::string token_mp[] = {",",
|
using namespace std;
|
||||||
|
|
||||||
|
string token_mp[] = {",",
|
||||||
";",
|
";",
|
||||||
"{",
|
"{",
|
||||||
"}",
|
"}",
|
||||||
@ -22,3 +24,5 @@ std::string token_mp[] = {",",
|
|||||||
"admit",
|
"admit",
|
||||||
"delete",
|
"delete",
|
||||||
"ID"};
|
"ID"};
|
||||||
|
|
||||||
|
vector<string> id_mp{""};
|
40
src/work.cpp
40
src/work.cpp
@ -47,7 +47,7 @@ struct DefVar : Def {
|
|||||||
shared_ptr<ValType> def_var;
|
shared_ptr<ValType> def_var;
|
||||||
};
|
};
|
||||||
|
|
||||||
map<string, shared_ptr<Def>> ndefs;
|
map<int, shared_ptr<Def>> ndefs;
|
||||||
|
|
||||||
bool preview(vector<TokenType> v) {
|
bool preview(vector<TokenType> v) {
|
||||||
if (pt + v.size() > tokens.size()) {
|
if (pt + v.size() > tokens.size()) {
|
||||||
@ -63,7 +63,7 @@ bool preview(vector<TokenType> v) {
|
|||||||
bool preview(TokenType t) {
|
bool preview(TokenType t) {
|
||||||
return preview(vector<TokenType>{t});
|
return preview(vector<TokenType>{t});
|
||||||
}
|
}
|
||||||
string jump(TokenType t) {
|
int jump(TokenType t) {
|
||||||
if (preview(t)) {
|
if (preview(t)) {
|
||||||
return tokens[pt++].s;
|
return tokens[pt++].s;
|
||||||
}
|
}
|
||||||
@ -81,10 +81,10 @@ vector<shared_ptr<ValType>> createVals();
|
|||||||
shared_ptr<ValType> _createType(shared_ptr<Struct>);
|
shared_ptr<ValType> _createType(shared_ptr<Struct>);
|
||||||
shared_ptr<ValType> createType();
|
shared_ptr<ValType> createType();
|
||||||
vector<shared_ptr<ValType>> createTypes();
|
vector<shared_ptr<ValType>> createTypes();
|
||||||
vector<pair<string, shared_ptr<TemplateType>>> createTems();
|
vector<pair<int, shared_ptr<TemplateType>>> createTems();
|
||||||
vector<pair<string, shared_ptr<ValType>>> createPars(map<string, shared_ptr<ValType>>* = nullptr);
|
vector<pair<int, shared_ptr<ValType>>> createPars(map<int, shared_ptr<ValType>>* = nullptr);
|
||||||
pair<string, shared_ptr<Struct>> createStruct();
|
pair<int, shared_ptr<Struct>> createStruct();
|
||||||
pair<string, shared_ptr<ValType>> createVar();
|
pair<int, shared_ptr<ValType>> createVar();
|
||||||
|
|
||||||
shared_ptr<ValType> _createVal(shared_ptr<StructType> ft) {
|
shared_ptr<ValType> _createVal(shared_ptr<StructType> ft) {
|
||||||
auto str = ft->str;
|
auto str = ft->str;
|
||||||
@ -137,7 +137,7 @@ shared_ptr<ValType> createVal() {
|
|||||||
if (preview(TokenType::FN)) {
|
if (preview(TokenType::FN)) {
|
||||||
pt++;
|
pt++;
|
||||||
auto tt = make_shared<FunctionType>();
|
auto tt = make_shared<FunctionType>();
|
||||||
vector<pair<string, shared_ptr<TemplateType>>> tems;
|
vector<pair<int, shared_ptr<TemplateType>>> tems;
|
||||||
if (preview(TokenType::LT)) {
|
if (preview(TokenType::LT)) {
|
||||||
tems = createTems();
|
tems = createTems();
|
||||||
}
|
}
|
||||||
@ -152,7 +152,7 @@ shared_ptr<ValType> createVal() {
|
|||||||
tt->c3 = createType();
|
tt->c3 = createType();
|
||||||
|
|
||||||
jump(TokenType::LB);
|
jump(TokenType::LB);
|
||||||
vector<string> defs;
|
vector<int> defs;
|
||||||
while (!preview(TokenType::RETURN)) {
|
while (!preview(TokenType::RETURN)) {
|
||||||
if (preview({TokenType::STRUCT, TokenType::ID})) {
|
if (preview({TokenType::STRUCT, TokenType::ID})) {
|
||||||
defs.push_back(createStruct().first);
|
defs.push_back(createStruct().first);
|
||||||
@ -370,8 +370,8 @@ vector<shared_ptr<ValType>> createTypes() {
|
|||||||
return types;
|
return types;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<pair<string, shared_ptr<TemplateType>>> createTems() {
|
vector<pair<int, shared_ptr<TemplateType>>> createTems() {
|
||||||
vector<pair<string, shared_ptr<TemplateType>>> tems;
|
vector<pair<int, shared_ptr<TemplateType>>> tems;
|
||||||
jump(TokenType::LT);
|
jump(TokenType::LT);
|
||||||
if (preview(TokenType::ID)) {
|
if (preview(TokenType::ID)) {
|
||||||
auto single = [&]() {
|
auto single = [&]() {
|
||||||
@ -391,8 +391,8 @@ vector<pair<string, shared_ptr<TemplateType>>> createTems() {
|
|||||||
return tems;
|
return tems;
|
||||||
}
|
}
|
||||||
|
|
||||||
vector<pair<string, shared_ptr<ValType>>> createPars(map<string, shared_ptr<ValType>>* vars) {
|
vector<pair<int, shared_ptr<ValType>>> createPars(map<int, shared_ptr<ValType>>* vars) {
|
||||||
vector<pair<string, shared_ptr<ValType>>> pars;
|
vector<pair<int, shared_ptr<ValType>>> pars;
|
||||||
jump(TokenType::LP);
|
jump(TokenType::LP);
|
||||||
if (!preview(TokenType::RP)) {
|
if (!preview(TokenType::RP)) {
|
||||||
auto single = [&]() {
|
auto single = [&]() {
|
||||||
@ -418,11 +418,11 @@ vector<pair<string, shared_ptr<ValType>>> createPars(map<string, shared_ptr<ValT
|
|||||||
return pars;
|
return pars;
|
||||||
}
|
}
|
||||||
|
|
||||||
pair<string, shared_ptr<Struct>> createStruct() {
|
pair<int, shared_ptr<Struct>> createStruct() {
|
||||||
jump(TokenType::STRUCT);
|
jump(TokenType::STRUCT);
|
||||||
string s;
|
int s = 0;
|
||||||
vector<pair<string, shared_ptr<TemplateType>>> tems;
|
vector<pair<int, shared_ptr<TemplateType>>> tems;
|
||||||
vector<pair<string, shared_ptr<ValType>>> pars;
|
vector<pair<int, shared_ptr<ValType>>> pars;
|
||||||
auto t = make_shared<Struct>();
|
auto t = make_shared<Struct>();
|
||||||
|
|
||||||
bool constructor = 1;
|
bool constructor = 1;
|
||||||
@ -451,7 +451,7 @@ pair<string, shared_ptr<Struct>> createStruct() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
jump(TokenType::LB);
|
jump(TokenType::LB);
|
||||||
vector<string> defs;
|
vector<int> defs;
|
||||||
vector<shared_ptr<Struct>> subs;
|
vector<shared_ptr<Struct>> subs;
|
||||||
while (!preview(TokenType::RB)) {
|
while (!preview(TokenType::RB)) {
|
||||||
bool pub = 1;
|
bool pub = 1;
|
||||||
@ -489,7 +489,7 @@ pair<string, shared_ptr<Struct>> createStruct() {
|
|||||||
ndefs.erase(ss);
|
ndefs.erase(ss);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!s.empty()) {
|
if (s) {
|
||||||
auto tt = make_shared<DefStruct>();
|
auto tt = make_shared<DefStruct>();
|
||||||
tt->def_struct = t;
|
tt->def_struct = t;
|
||||||
ndefs[s] = static_pointer_cast<Def>(tt);
|
ndefs[s] = static_pointer_cast<Def>(tt);
|
||||||
@ -497,8 +497,8 @@ pair<string, shared_ptr<Struct>> createStruct() {
|
|||||||
return {s, t};
|
return {s, t};
|
||||||
}
|
}
|
||||||
|
|
||||||
pair<string, shared_ptr<ValType>> createVar() {
|
pair<int, shared_ptr<ValType>> createVar() {
|
||||||
string s;
|
int s;
|
||||||
shared_ptr<ValType> t;
|
shared_ptr<ValType> t;
|
||||||
if (preview(TokenType::STRUCT)) {
|
if (preview(TokenType::STRUCT)) {
|
||||||
auto pr = createStruct();
|
auto pr = createStruct();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user