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