自作言語nullptrのインタープリターです。
修订版 | a2bd179d2a8663cbde84481ec3087194853b5978 (tree) |
---|---|
时间 | 2015-08-07 23:27:16 |
作者 | Diverge <diverge.vosystems@gmai...> |
Commiter | Diverge |
演算機作成中
一部機能追加
@@ -0,0 +1,26 @@ | ||
1 | +using moduleの実装について | |
2 | + | |
3 | +モジュールはC/C++が呼び出せるライブラリ形式(動的読み込みが可能なものに限る)で実装する。 | |
4 | +中には必ず | |
5 | +open(void) | |
6 | +close(void) | |
7 | +run関数群(後述) | |
8 | +を入れる。 | |
9 | + | |
10 | + | |
11 | +run関数群 | |
12 | +実行される関数は必ずしも引数が一つづつとは限らないので、ポインタを使い返り値の型の違いで関数を分ける。 | |
13 | +幸いなことに、nullptrには整数型が複数個ないため実装の数を減らすことができる。 | |
14 | +bool runRB(std::string, Int*, String*, bool*); | |
15 | +Int runRI(std::string, Int*, String*, bool*); | |
16 | +String runRS(std::string, Int*, String*, bool*); | |
17 | +最初の引数std::stringは機能の名前になる | |
18 | + | |
19 | +モジュールファイルには各機能に必要な引数の個数と返り値の型を書いておく。 | |
20 | +bool func(Int 2, String 0, bool 0); | |
21 | +上記のような機能定義の場合、呼び出される関数はrunRBとなり | |
22 | +runRB("func", Int*, nullptr, nullptr); | |
23 | +となる。 | |
24 | + | |
25 | +同時に読み込むDLLは一つまでとする。(同じ名前の関数ができてしまうため。) | |
26 | +読み込んだら初めにopen、終わったらcloseを呼びリンクを解く | |
\ No newline at end of file |
@@ -59,5 +59,27 @@ void Nullptr::Keyboard(std::string line) | ||
59 | 59 | |
60 | 60 | void Nullptr::EScreen(std::string line) |
61 | 61 | { |
62 | + vector<string> str=Split(line,"<<<"); | |
62 | 63 | |
64 | + for(size_t C=1; C<str.size(); C++){ | |
65 | + if(regex_match(str[C],regex("\".+\""))){ | |
66 | + cerr<<Replace(str[C],"\"",""); | |
67 | + }else if(str[C]=="endl"){ | |
68 | + cerr<<endl; | |
69 | + }else if(str[C]=="flush"){ | |
70 | + cerr<<flush; | |
71 | + }else if(regex_match(str[C],regex("\\d+"))){ | |
72 | + cerr<<stoi(str[C]); | |
73 | + }else{ | |
74 | + BasicTypes::Int ull; | |
75 | + if(GetInt(str[C],ull)){ | |
76 | + cerr<<BasicTypes::IntManager(to_string(0),ull)(ull,'d'); | |
77 | + }else{ | |
78 | + string s; | |
79 | + if(GetStr(str[C],s)){ | |
80 | + cerr<<s; | |
81 | + } | |
82 | + } | |
83 | + } | |
84 | + } | |
63 | 85 | } |
\ No newline at end of file |
@@ -58,5 +58,123 @@ bool Nullptr::GetBool(string judge) | ||
58 | 58 | |
59 | 59 | } |
60 | 60 | } |
61 | -TrueEnd: | |
61 | +} | |
62 | + | |
63 | +vector<string> Nullptr::Lex(string formula) | |
64 | +{ | |
65 | + vector<string> form; | |
66 | + formula=Replace(formula," ",""); | |
67 | + smatch result; | |
68 | + | |
69 | + for(size_t C=0; C<formula.size(); C++){ | |
70 | + if(regex_search(formula,result,regex("\\d+"))){ | |
71 | + size_t diff=result.end()-result.begin(); | |
72 | + string num(result.begin(),result.end()); | |
73 | + form.push_back(num); | |
74 | + C+=diff; | |
75 | + }else{ | |
76 | + switch(formula[C]){ | |
77 | + case '+': | |
78 | + case '-': | |
79 | + if(C+1<formula.size()){ | |
80 | + if(formula[C+1]==formula[C]){ | |
81 | + form.push_back({formula[C++],formula[C],'\0'}); | |
82 | + break; | |
83 | + } | |
84 | + } | |
85 | + case '*': | |
86 | + case '/': | |
87 | + case '%': | |
88 | + form.push_back({formula[C],'\0'}); | |
89 | + break; | |
90 | + | |
91 | + default: | |
92 | + char* varname=new char[formula.size()]; | |
93 | + varname[0]=formula[C]; | |
94 | + for(size_t varpoint=0; varpoint<formula.size(); varpoint++){ | |
95 | + if(isalnum(formula[C+varpoint])){ | |
96 | + varname[varpoint]=formula[C+varpoint]; | |
97 | + }else{ | |
98 | + C+=varpoint; | |
99 | + break; | |
100 | + } | |
101 | + } | |
102 | + form.push_back(varname); | |
103 | + break; | |
104 | + } | |
105 | + } | |
106 | + | |
107 | + } | |
108 | +} | |
109 | + | |
110 | +vector<std::string> Nullptr::ReplaceVar2Num(vector<string> formula) | |
111 | +{ | |
112 | + BasicTypes::Int intb; | |
113 | + vector<string> lex; | |
114 | + | |
115 | + for(size_t C=0; C<formula.size(); C++){ | |
116 | + if(isalpha(formula[C][0])){ | |
117 | + GetInt(formula[C],intb); | |
118 | + lex.push_back(BasicTypes::IntManager()(intb,'d')); | |
119 | + }else{ | |
120 | + lex.push_back(formula[C]); | |
121 | + } | |
122 | + } | |
123 | + return lex; | |
124 | +} | |
125 | + | |
126 | +BasicTypes::Int Nullptr::Calc(vector<string> lex) | |
127 | +{ | |
128 | + BasicTypes::Int intd; | |
129 | +} | |
130 | + | |
131 | +std::vector<std::string> Nullptr::ProcBrackets(std::vector<std::string> lex) | |
132 | +{ | |
133 | + vector<string>::iterator open,close; | |
134 | + vector<string> lexed; | |
135 | + | |
136 | + for(size_t C=0; C<lex.size(); C++){ | |
137 | + if(lex[C]=="("){ | |
138 | + open=lex.begin()+C; | |
139 | + }else if(lex[C]==")"){ | |
140 | + close=lex.begin()+C; | |
141 | + lexed.push_back(CalcBracket(string(open,close))); | |
142 | + }else{ | |
143 | + lexed.push_back(lex[C]); | |
144 | + } | |
145 | + } | |
146 | +} | |
147 | + | |
148 | +std::string Nullptr::CalcBracket(std::string inBracket) | |
149 | +{ | |
150 | + vector<BasicTypes::Int> num; | |
151 | + bool minus=false; | |
152 | + for(size_t C=0; C<inBracket.size(); C++){ | |
153 | + switch(inBracket[C]){ | |
154 | + case '+': | |
155 | + | |
156 | + break; | |
157 | + case '-': | |
158 | + | |
159 | + break; | |
160 | + case '*': | |
161 | + | |
162 | + break; | |
163 | + case '/': | |
164 | + | |
165 | + break; | |
166 | + case '%': | |
167 | + | |
168 | + break; | |
169 | + | |
170 | + default: | |
171 | + char* n=new char[inBracket.size()-C]; | |
172 | + for(size_t c=C; c<inBracket.size(); c++){ | |
173 | + if(!isdigit(inBracket[c]))break; | |
174 | + n[c-C]=inBracket[c]; | |
175 | + } | |
176 | + num.push_back({!minus,stoull(n)}); | |
177 | + break; | |
178 | + } | |
179 | + } | |
62 | 180 | } |
\ No newline at end of file |
@@ -16,16 +16,7 @@ void IntManager::operator=(Int data) | ||
16 | 16 | |
17 | 17 | Int IntManager::operator+(Int i) |
18 | 18 | { |
19 | - Int tmp; | |
20 | - | |
21 | - if(i.positive==this->m_data.positive){ | |
22 | - tmp.data=i.data + this->m_data.data; | |
23 | - tmp.positive=i.positive; | |
24 | - }else{ | |
25 | - tmp.data=(i.data > this->m_data.data ? +1 : -1)*(i.data - this->m_data.data); | |
26 | - tmp.positive=(i.data > this->m_data.data ? true : false); | |
27 | - } | |
28 | - return tmp; | |
19 | + return m_data+i; | |
29 | 20 | } |
30 | 21 | |
31 | 22 | std::string IntManager::operator()(Int i, char type) |
@@ -56,36 +47,22 @@ std::string IntManager::operator()(Int i, char type) | ||
56 | 47 | |
57 | 48 | Int IntManager::operator-(Int i) |
58 | 49 | { |
59 | - Int tmp; | |
60 | - tmp=this->operator+({i.data,!i.positive}); | |
61 | - return tmp; | |
50 | + return m_data-i; | |
62 | 51 | } |
63 | 52 | |
64 | 53 | Int IntManager::operator*(Int i) |
65 | 54 | { |
66 | - Int tmp; | |
67 | - | |
68 | - tmp.data=i.data*this->m_data.data; | |
69 | - tmp.positive=(i.positive==false || this->m_data.positive==false ? false : true); | |
70 | - return tmp; | |
55 | + return m_data*i; | |
71 | 56 | } |
72 | 57 | |
73 | 58 | Int IntManager::operator/(Int i) |
74 | 59 | { |
75 | - Int tmp; | |
76 | - | |
77 | - tmp.data=this->m_data.data/i.data; | |
78 | - tmp.positive=(i.positive==false || this->m_data.positive==false ? false : true); | |
79 | - return tmp; | |
60 | + return m_data/i; | |
80 | 61 | } |
81 | 62 | |
82 | 63 | Int IntManager::operator%(Int i) |
83 | 64 | { |
84 | - Int tmp; | |
85 | - | |
86 | - tmp.data=this->m_data.data%i.data; | |
87 | - tmp.positive=(i.positive==false || this->m_data.positive==false ? false : true); | |
88 | - return tmp; | |
65 | + return m_data%i; | |
89 | 66 | } |
90 | 67 | |
91 | 68 | Int IntManager::operator()(void) |
@@ -95,7 +72,7 @@ Int IntManager::operator()(void) | ||
95 | 72 | |
96 | 73 | std::string IntManager::operator()(char type) |
97 | 74 | { |
98 | - string str=IntManager("tmp",this->m_data)(this->m_data,type); | |
75 | + string str=IntManager()(this->m_data,type); | |
99 | 76 | return str; |
100 | 77 | } |
101 | 78 |
@@ -137,4 +114,53 @@ Int IntManager::GetValue(void) | ||
137 | 114 | std::string IntManager::GetName(void) |
138 | 115 | { |
139 | 116 | return m_name; |
140 | -} | |
\ No newline at end of file | ||
117 | +} | |
118 | + | |
119 | + | |
120 | +Int VOSystemsNullptr::BasicTypes::operator+(Int p1,Int p2) | |
121 | +{ | |
122 | + Int tmp; | |
123 | + | |
124 | + if(p2.positive==p1.positive){ | |
125 | + tmp.data=p2.data + p1.data; | |
126 | + tmp.positive=p2.positive; | |
127 | + }else{ | |
128 | + tmp.data=(p2.data > p1.data ? +1 : -1)*(p2.data - p1.data); | |
129 | + tmp.positive=(p2.data > p1.data ? true : false); | |
130 | + } | |
131 | + return tmp; | |
132 | +} | |
133 | + | |
134 | +Int VOSystemsNullptr::BasicTypes::operator-(Int p1, Int p2) | |
135 | +{ | |
136 | + Int tmp={p2.data,!p2.positive}; | |
137 | + tmp=p1+tmp; | |
138 | + return tmp; | |
139 | +} | |
140 | + | |
141 | +Int VOSystemsNullptr::BasicTypes::operator*(Int p1, Int p2) | |
142 | +{ | |
143 | + Int tmp; | |
144 | + | |
145 | + tmp.data=p1.data*p2.data; | |
146 | + tmp.positive=(p1.positive==false || p2.positive==false ? false : true); | |
147 | + return tmp; | |
148 | +} | |
149 | + | |
150 | +Int VOSystemsNullptr::BasicTypes::operator/(Int p1, Int p2) | |
151 | +{ | |
152 | + Int tmp; | |
153 | + | |
154 | + tmp.data=p1.data/p2.data; | |
155 | + tmp.positive=(p1.positive==false || p2.positive==false ? false : true); | |
156 | + return tmp; | |
157 | +} | |
158 | + | |
159 | +Int VOSystemsNullptr::BasicTypes::operator%(Int p1, Int p2) | |
160 | +{ | |
161 | + Int tmp; | |
162 | + | |
163 | + tmp.data=p1.data%p2.data; | |
164 | + tmp.positive=(p1.positive==false || p2.positive==false ? false : true); | |
165 | + return tmp; | |
166 | +} |
@@ -28,7 +28,7 @@ namespace VOSystemsNullptr | ||
28 | 28 | |
29 | 29 | public: |
30 | 30 | IntManager(std::string name, Int data) :m_name(name),m_data(data) {}; |
31 | - | |
31 | + IntManager(void){}; | |
32 | 32 | void operator=(Int data); |
33 | 33 | |
34 | 34 | BasicTypes::Int operator+(BasicTypes::Int i); |
@@ -50,6 +50,12 @@ namespace VOSystemsNullptr | ||
50 | 50 | std::string GetName(void); |
51 | 51 | }; |
52 | 52 | |
53 | + BasicTypes::Int operator+(BasicTypes::Int,BasicTypes::Int); | |
54 | + BasicTypes::Int operator-(BasicTypes::Int,BasicTypes::Int); | |
55 | + BasicTypes::Int operator*(BasicTypes::Int,BasicTypes::Int); | |
56 | + BasicTypes::Int operator/(BasicTypes::Int,BasicTypes::Int); | |
57 | + BasicTypes::Int operator%(BasicTypes::Int,BasicTypes::Int); | |
58 | + | |
53 | 59 | typedef struct _String{ |
54 | 60 | std::string name; |
55 | 61 | std::string data; |
@@ -57,6 +57,11 @@ namespace VOSystemsNullptr | ||
57 | 57 | bool Goto(std::string line); |
58 | 58 | bool GetBool(std::string judge); |
59 | 59 | |
60 | + std::vector<std::string> Lex(std::string formula); | |
61 | + std::vector<std::string> ReplaceVar2Num(std::vector<std::string> formula); | |
62 | + BasicTypes::Int Calc(std::vector<std::string> lex); | |
63 | + std::vector<std::string> ProcBrackets(std::vector<std::string> lex); | |
64 | + std::string CalcBracket(std::string inBracket); | |
60 | 65 | /*バージョン*/ |
61 | 66 | unsigned int |
62 | 67 | m_major=0, |