• R/O
  • HTTP
  • SSH
  • HTTPS

提交

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

自作言語nullptrのインタープリターです。


Commit MetaInfo

修订版70230ce6af7d452211f3f5497d7e76899a1025ef (tree)
时间2015-08-12 14:09:04
作者Diverge <diverge.vosystems@gmai...>
CommiterDiverge

Log Message

Module Loader created
begin to create Interpreter

更改概述

差异

Binary files a/nullptr.sdf and b/nullptr.sdf differ
--- a/nullptr/Interpreter.h
+++ b/nullptr/Interpreter.h
@@ -15,7 +15,7 @@ namespace VOSystemsNullptr
1515 {
1616 namespace Core
1717 {
18- class Interpreter
18+ class Interpreter : System::StringModule
1919 {
2020 private:
2121 std::vector<BasicTypes::IntManager> m_int;//Int変数リスト
@@ -35,10 +35,7 @@ namespace VOSystemsNullptr
3535
3636 std::string m_stdout,m_stdin,m_stderr;//標準入出力系の名前
3737
38- std::vector<std::string> Split(const std::string &str, const std::string &delim);//文字列分割
39- std::string Replace(std::string src, std::string before, std::string after);//文字列置き換え
40- bool Find(const std::string& str, const std::string& find);//文字列検索
41-
38+
4239 void Screen(std::string line);//ベーシックモジュール->標準出力
4340 void Keyboard(std::string line);//ベーシックモジュール->標準入力
4441 void EScreen(std::string line);//ベーシックモジュール標準エラー出力
--- /dev/null
+++ b/nullptr/Module.cpp
@@ -0,0 +1,109 @@
1+/*
2+nullptr interpreter
3+(C) 2015 VOSystems.
4+*/
5+
6+#include"nullptr.h"
7+#include"Interpreter.h"
8+#include"Module.h"
9+#include<regex>
10+#include<thread>
11+#include<future>
12+
13+using namespace std;
14+using namespace VOSystemsNullptr::Core;
15+
16+inline VOSystemsNullptr::System::ModuleFunction SearchDLL(VOSystemsNullptr::System::Module module, string name);
17+
18+bool Module::Connect(std::string name)
19+{
20+ m_mf=DllSearch(name);
21+ string dllname=m_mf.dllname;
22+
23+ if(m_CurrentConnectModuleName==dllname){
24+ return true;
25+ }else if(!m_CurrentConnectModuleName.empty()){
26+ DisConnect();
27+ }
28+
29+ m_module=LoadLibrary((dllname+".dll").c_str());
30+ if(m_module==NULL){
31+ m_CurrentConnectModuleName="";
32+ return false;
33+ }else{
34+ m_CurrentConnectModuleName=dllname;
35+
36+ return true;
37+ }
38+}
39+
40+bool Module::Run(std::string name, BasicTypes::Int* paramInt, BasicTypes::String* paramString)
41+{
42+ if(!Connect(name))return false;
43+
44+ switch(m_mf.mode){
45+ case System::NotFound:
46+ case System::ReturnBool:
47+ default:
48+ return false;
49+ break;
50+ case System::ReturnInt:
51+ Core::IntFunc ifunc=(Core::IntFunc)GetProcAddress(m_module,"runRI");
52+ m_int=(*ifunc)(name,paramInt,paramString);
53+
54+ break;
55+ case System::ReturnString:
56+ Core::StrFunc sfunc=(Core::StrFunc)GetProcAddress(m_module,"runRS");
57+ m_str=(*sfunc)(name,paramInt,paramString);
58+ break;
59+ }
60+ return true;
61+}
62+
63+bool Module::DisConnect(void)
64+{
65+ if(m_module==NULL)return true;
66+
67+ if(FreeLibrary(m_module)){
68+ m_module=NULL;
69+ return true;
70+ }else{
71+ return false;
72+ }
73+}
74+
75+VOSystemsNullptr::System::ModuleFunction Module::DllSearch(std::string name)
76+{
77+ this->m_modules.size();
78+ vector<thread> th(this->m_modules.size());
79+ vector<future<System::ModuleFunction>> fu(this->m_modules.size());
80+
81+ for(size_t C=0; C<this->m_modules.size(); C++){
82+ System::Module mod=m_modules[C];
83+ fu[C]=async([mod,name](string){SearchDLL(mod,name);});//th[C]=thread([mod,name](){SearchDLL(mod,name);});
84+ }
85+
86+ vector<System::ModuleFunction> result(this->m_modules.size());
87+ for(size_t C=0; C<this->m_modules.size(); C++){
88+ result[C]=fu[C].get();
89+ if(!result[C].mode==System::NotFound){
90+ return result[C];
91+ }
92+ }
93+
94+ VOSystemsNullptr::System::ModuleFunction mf;
95+ mf.mode=VOSystemsNullptr::System::NotFound;
96+ return mf;
97+}
98+
99+inline VOSystemsNullptr::System::ModuleFunction SearchDLL(VOSystemsNullptr::System::Module module, string name)
100+{
101+ for(size_t C=0; C<module.size(); C++){
102+ if(module[C].name==name){
103+ return module[C];
104+ }
105+ }
106+ VOSystemsNullptr::System::ModuleFunction mf;
107+ mf.mode=VOSystemsNullptr::System::NotFound;
108+ return mf;
109+}
\ No newline at end of file
--- a/nullptr/Module.h
+++ b/nullptr/Module.h
@@ -1,6 +1,7 @@
11 #pragma once
22
33 #include<string>
4+#include<Windows.h>
45
56 #include"nullptr.h"
67 #include"Preprocessor.h"
@@ -10,17 +11,32 @@ namespace VOSystemsNullptr
1011 {
1112 namespace Core
1213 {
14+ typedef BasicTypes::String (*StrFunc)(std::string, BasicTypes::Int*,BasicTypes::String*);
15+ typedef BasicTypes::Int (*IntFunc)(std::string, BasicTypes::Int*,BasicTypes::String*);
16+
1317 class Module
1418 {
1519 private:
1620 std::vector<System::Module> m_modules;
17- System::Module m_CurrentConnectModule;
21+ std::string m_CurrentConnectModuleName;
22+ HMODULE m_module;
23+ System::ModuleFunction m_mf;
24+ BasicTypes::Int m_int;
25+ BasicTypes::String m_str;
1826
27+ VOSystemsNullptr::System::ModuleFunction DllSearch(std::string name);
1928 public:
20- bool Load(std::string name);
2129 bool Connect(std::string name);
22- bool Run(std::string name, BasicTypes::Int* paramInt, BasicTypes::String paramString);
23- bool DisConnect(std::string name);
30+ bool Run(std::string name, BasicTypes::Int* paramInt, BasicTypes::String* paramString);
31+ bool DisConnect(void);
32+ template<typename _Ty> _Ty GetResult(void)
33+ {
34+ if(typeid(_Ty)==typeid(BasicTypes::Int)){
35+ return m_int;
36+ }else if(typeid(_Ty)==typeid(BasicTypes::String)){
37+ return m_str;
38+ }
39+ }
2440 };
2541 }
2642 }
\ No newline at end of file
--- a/nullptr/ModuleLoader.cpp
+++ /dev/null
@@ -1,81 +0,0 @@
1-/*
2-nullptr interpreter
3-(C) 2015 VOSystems.
4-*/
5-
6-#include"nullptr.h"
7-#include"Interpreter.h"
8-#include"Module.h"
9-#include<regex>
10-
11-using namespace std;
12-using namespace VOSystemsNullptr::Core;
13-
14-bool Module::Load(std::string name)
15-{
16- string src=m_src;
17- string work=m_workline;
18-
19- System::Module module;
20- module.name=name;
21- string ident[3]={"Int","String","bool"};
22- m_src=name+".nullptrmodule";
23- do{
24- LineLoad();
25- if(regex_match(m_workline,regex(".+ .+(.+)"))){
26- System::ModuleFunction mf;
27- string::iterator namestart,nameend;
28-
29- m_workline=Replace(m_workline," ","");
30-
31- if(regex_match(m_workline,regex("bool .+(.+)"))){
32- mf.mode=System::ReturnBool;
33- namestart=m_workline.begin()+5;
34-
35- }else if(regex_match(m_workline,regex("Int .+(.+)"))){
36- mf.mode=System::ReturnInt;
37- namestart=m_workline.begin()+4;
38-
39- }else if(regex_match(m_workline,regex("String .+(.+)"))){
40- mf.mode=System::ReturnString;
41- namestart=m_workline.begin()+7;
42-
43- }
44- nameend=m_workline.begin()+m_workline.find_first_of('(');
45- mf.name=string(namestart,nameend);
46-
47- size_t offset=0;
48- unsigned char count[3]={0};
49- for(size_t C=0; C<3; C++){
50- do{
51- offset=m_workline.find(ident[C],offset+1);
52- count[C]++;
53- }while(offset==string::npos);
54- }
55- mf.ArguInt=count[0];
56- mf.ArguString=count[1];
57- mf.ArguBool=count[2];
58-
59- module.modules.push_back(mf);
60- }
61- }while(!m_workline.empty());
62- m_modules.push_back(module);
63-
64- m_src=src;
65- m_workline=work;
66-}
67-
68-bool Module::Connect(std::string name)
69-{
70-
71-}
72-
73-bool Module::Run(std::string name, BasicTypes::Int* paramInt, BasicTypes::String paramString)
74-{
75-
76-}
77-
78-bool Module::DisConnect(std::string name)
79-{
80-
81-}
--- /dev/null
+++ b/nullptr/Preprocessor.cpp
@@ -0,0 +1,176 @@
1+/*
2+
3+*/
4+
5+#include<direct.h>
6+#include<fstream>
7+
8+#include"Preprocessor.h"
9+
10+using namespace VOSystemsNullptr::Core;
11+using namespace std;
12+
13+Preprocessor::Preprocessor(string source) :fope(source,"\n") {}
14+
15+void Preprocessor::Extract(void)
16+{
17+ _mkdir("NullptrTmp");
18+ ofstream fout("./NullptrTmp/Extracted.nullptr");
19+ string line;
20+ do{
21+ line=fope();
22+
23+ if (Find(line, "#include")) {
24+ string str(line.begin()+line.find_first_of('\"')+1, line.begin()+line.find_last_of('\"'));
25+
26+ ifstream ufile(str);
27+
28+ if(!ufile)goto ErrorNext;
29+
30+ ufile.seekg(0,ios_base::end);
31+ size_t len=ufile.tellg();
32+ ufile.seekg(0,ios_base::beg);
33+
34+ char* ch=new char[len];
35+
36+ ufile.read(ch,len);
37+
38+ fout<<ch<<endl;
39+
40+ delete ch;
41+
42+ }else if (regex_match(line, regex("#pragma comment(lib,\".+\""))) {
43+ string str(line.begin()+line.find_first_of('\"')+1, line.begin()+line.find_last_of('\"'));
44+
45+ ifstream ufile(str+".nullptrmodule");
46+ if(!ufile)goto ErrorNext;
47+ ufile.close();
48+
49+ m_modulefiles.push_back(str+".nullptrmodule");
50+ }else{
51+ fout<<line<<";"<<endl;
52+ }
53+ ErrorNext:;
54+ }while(!line.empty());
55+
56+ fope = "./NullptrTmp/Extracted.nullptr";
57+}
58+
59+void Preprocessor::Trim(void)
60+{
61+ ofstream fout("./NullptrTmp/Prerocessed.nullptr");
62+ string line;
63+ bool lcomment=false;
64+ do{
65+ line=fope();
66+
67+ if(Find(line,"//")){
68+ bool first=false;
69+
70+ for(size_t C=0; C<line.size(); C++){
71+ if(line[C]=='/' && !first){
72+ first=true;
73+
74+ }else if(line[C]=='/'){
75+ break;
76+
77+ }else if(first && line[C]!='/'){
78+ fout<<"/"<<line[C];
79+ first=false;
80+
81+ }else{
82+ fout<<line[C];
83+
84+ }
85+ }
86+ }else if(Find(line,"/*")){
87+ bool first=false;
88+
89+ for(size_t C=0; C<line.size(); C++){
90+ if(line[C]=='/' && !first){
91+ first=true;
92+
93+ }else if(line[C]=='*'){
94+ break;
95+
96+ }else if(first && line[C]!='*'){
97+ fout<<"/"<<line[C];
98+ first=false;
99+
100+ }else{
101+ fout<<line[C];
102+
103+ }
104+ }
105+
106+ lcomment=true;
107+
108+ }else if(Find(line,"*/")){
109+ for(size_t C=0; C<line.size()-1; C++){
110+ if(line[C]=='*' && line[C]=='/'){
111+ fout<<string(line.begin()+C,line.end());
112+ break;
113+ }
114+ }
115+ lcomment=false;
116+
117+ }else{
118+ if(!lcomment){
119+ fout<<line;
120+ }
121+ }
122+ }while(!line.empty());
123+}
124+
125+void Preprocessor::Load(System::Module& module)
126+{
127+ regex func_regex(R"(.+ .+(Int \d+\s*,\s*String \d+\s*,\s*bool \d+\s*);*)");
128+ regex bool_regex(R"(bool .+(Int \d+\s*,\s*String \d+\s*,\s*bool \d+\s*);*)");
129+ regex int_regex(R"(Int .+(Int \d+\s*,\s*String \d+\s*,\s*bool \d+\s*);*)");
130+ regex str_regex(R"(String .+(Int \d+\s*,\s*String \d+\s*,\s*bool \d+\s*);*)");
131+
132+ string line;
133+
134+ for(size_t C=0; C<m_modulefiles.size(); C++){
135+ System::FileOperator fop(m_modulefiles[C],";");
136+ do{
137+ line=fop();
138+ if(regex_match(line,func_regex)){
139+ System::ModuleFunction mf;
140+ mf.dllname=m_modulefiles[C];
141+ string::iterator start;
142+
143+ if(regex_match(line,bool_regex)){
144+ mf.mode=System::ReturnBool;
145+ start=line.begin()+5;
146+
147+ }else if(regex_match(line,bool_regex)){
148+ mf.mode=System::ReturnInt;
149+ start=line.begin()+4;
150+
151+ }else if(regex_match(line,bool_regex)){
152+ mf.mode=System::ReturnString;
153+ start=line.begin()+7;
154+
155+ }else{
156+ mf.mode=System::NotFound;
157+ start=line.begin()+4;
158+
159+ }
160+ char name[32]={0};
161+ for(size_t C=0; *start!='(' || C<32; start++ ,C++)name[C]=*start;
162+ mf.name=name;
163+
164+ size_t whrint=line.find_last_of("Int")+4;
165+ size_t whrstr=line.find_last_of("String")+7;
166+ size_t whrbool=line.find_last_of("bool")+5;
167+
168+ mf.ArguBool=stoul(string(line.begin()+whrbool, line.begin()+line.find_last_of(")")));
169+ mf.ArguInt=stoul(string(line.begin()+whrint, line.begin()+whrstr-7));
170+ mf.ArguString=stoul(string(line.begin()+whrstr, line.begin()+whrbool-4));
171+
172+ module.push_back(mf);
173+ }
174+ }while(!line.empty());
175+ }
176+}
\ No newline at end of file
--- a/nullptr/Preprocessor.h
+++ b/nullptr/Preprocessor.h
@@ -7,12 +7,17 @@ namespace VOSystemsNullptr
77 {
88 namespace Core
99 {
10- class Preprocessor
10+ class Preprocessor : System::StringModule
1111 {
1212 private:
13-
13+ std::string m_src;
14+ System::FileOperator fope;
15+ std::vector<std::string> m_modulefiles;
1416 public:
15-
17+ Preprocessor(std::string source);
18+ void Extract(void);
19+ void Trim(void);
20+ void Load(System::Module& module);
1621 };
1722 }
1823 }
\ No newline at end of file
--- a/nullptr/nullptr.cpp
+++ b/nullptr/nullptr.cpp
@@ -15,7 +15,7 @@
1515 using namespace VOSystemsNullptr;
1616 using namespace std;
1717
18-vector<string> Nullptr::Split(const string &str, const string &delim)
18+vector<string> System::StringModule::Split(const string &str, const string &delim)
1919 {
2020 vector<string> res;
2121 size_t current = 0, found, delimlen = delim.size();
@@ -27,7 +27,7 @@ vector<string> Nullptr::Split(const string &str, const string &delim)
2727 return res;
2828 }
2929
30-std::string Nullptr::Replace(std::string src, std::string before, std::string after)
30+std::string System::StringModule::Replace(std::string src, std::string before, std::string after)
3131 {
3232 std::string::size_type Pos(src.find(before));
3333
@@ -39,7 +39,7 @@ std::string Nullptr::Replace(std::string src, std::string before, std::string af
3939 return src;
4040 }
4141
42-bool Nullptr::Find(const string& str, const string& find)
42+bool System::StringModule::Find(const string& str, const string& find)
4343 {
4444 return(str.find(find) != string::npos);
4545 }
@@ -56,73 +56,7 @@ Nullptr::~Nullptr(void)
5656 _rmdir("NullptrTmp");
5757 }
5858
59-void Nullptr::Extract(void)
60-{
61- _mkdir("NullptrTmp");
62- ofstream fout("./NullptrTmp/Extracted.nullptr");
63-
64- do{
65- LineLoad();
66-
67- if (Find(m_workline, "using file")) {
68- string str(m_workline.begin()+m_workline.find_first_of('\"')+1, m_workline.begin()+m_workline.find_last_of('\"'));
69-
70- ifstream ufile(str);
71-
72- if(!ufile)goto ErrorNext;
73-
74- ufile.seekg(0,ios_base::end);
75- size_t len=ufile.tellg();
76- ufile.seekg(0,ios_base::beg);
77-
78- char* ch=new char[len];
79-
80- ufile.read(ch,len);
81-
82- fout<<ch<<endl;
83-
84- delete ch;
8559
86- }else if (Find(m_workline, "using module")) {
87- string str(m_workline.begin()+m_workline.find_first_of('\"')+1, m_workline.begin()+m_workline.find_last_of('\"'));
88-
89- ifstream ufile(str+".nullptrmodule");
90-
91- if(!ufile)goto ErrorNext;
92-
93- string id;
94- do{
95- ufile>>id;
96- id=Replace(id,"\n","");
97- id=Replace(id,"\r","");
98- id=Replace(id,"\t","");
99- id=Replace(id," ","");
100- id=Replace(id,";","");
101- vector<string> ids=Split(id,"->");
102- if(id.size()>1){
103- if(ids[0]=="stdin"){
104- m_stdin=ids[1];
105- }else if(ids[0]=="stdout"){
106- m_stdout=ids[1];
107- }else if(ids[0]=="stderr"){
108- m_stderr=ids[1];
109- }
110- }
111- }while(!ufile.eof());
112-
113- }else{
114- fout<<m_workline<<";"<<endl;
115- }
116-ErrorNext:;
117- }while(!m_workline.empty());
118-
119- m_src = "./NullptrTmp/Extracted.nullptr";
120-}
121-
122-void Nullptr::Trim(void)
123-{
124-
125-}
12660
12761 void Nullptr::Work(void)
12862 {
--- a/nullptr/nullptr.h
+++ b/nullptr/nullptr.h
@@ -10,12 +10,18 @@
1010 #include<regex>
1111 #include<fstream>
1212
13+#include"Interpreter.h"
14+#include"Module.h"
15+#include"Preprocessor.h"
16+
1317 namespace VOSystemsNullptr
1418 {
1519 class Nullptr
1620 {
1721 private:
18-
22+ Core::Interpreter m_int;
23+ Core::Module m_module;
24+ Core::Preprocessor m_pre;
1925 public:
2026
2127 };
@@ -91,25 +97,34 @@ namespace VOSystemsNullptr
9197 };
9298
9399 struct ModuleFunction{
94- std::string name;
100+ std::string name,dllname;
95101 unsigned char mode;
96102 unsigned char ArguInt,ArguBool,ArguString;
97103 };
98104
99- typedef struct _Module{
100- std::string name;
101- std::vector<struct ModuleFunction> modules;
102- }Module;
105+ typedef std::vector<struct ModuleFunction> Module;
103106
104- class FileComponent
107+ class FileOperator
105108 {
106109 private:
107- size_t m_p_loaded;
108- protected:
110+ size_t m_eip;
109111 std::string m_src;
112+ std::string m_separator;
113+
110114 public:
111- virtual std::string Load(void);
115+ FileOperator(std::string source, std::string separator) :m_src(source), m_separator(separator), m_eip(0) {};
112116
117+ std::string operator()(void);
118+ std::string operator()(size_t startpoint);
119+ std::string operator=(std::string source);
120+ };
121+
122+ class StringModule
123+ {
124+ public:
125+ std::vector<std::string> Split(const std::string &str, const std::string &delim);//文字列分割
126+ std::string Replace(std::string src, std::string before, std::string after);//文字列置き換え
127+ bool Find(const std::string& str, const std::string& find);//文字列検索
113128 };
114129 }
115130 #define loop(count) for(size_t LoopCounter=0; LoopCounter<count; LoopCounter++)
--- a/nullptr/nullptr.vcxproj
+++ b/nullptr/nullptr.vcxproj
@@ -128,9 +128,10 @@
128128 <ClCompile Include="ControlManager.cpp" />
129129 <ClCompile Include="int.cpp" />
130130 <ClCompile Include="ManageVar.cpp" />
131- <ClCompile Include="ModuleLoader.cpp" />
131+ <ClCompile Include="Module.cpp" />
132132 <ClCompile Include="nullptr.cpp" />
133133 <ClCompile Include="open.cpp" />
134+ <ClCompile Include="Preprocessor.cpp" />
134135 </ItemGroup>
135136 <ItemGroup>
136137 <None Include="iostream.nullptrmodule" />
--- a/nullptr/nullptr.vcxproj.filters
+++ b/nullptr/nullptr.vcxproj.filters
@@ -47,7 +47,10 @@
4747 <ClCompile Include="ControlManager.cpp">
4848 <Filter>ソース ファイル</Filter>
4949 </ClCompile>
50- <ClCompile Include="ModuleLoader.cpp">
50+ <ClCompile Include="Preprocessor.cpp">
51+ <Filter>ソース ファイル</Filter>
52+ </ClCompile>
53+ <ClCompile Include="Module.cpp">
5154 <Filter>ソース ファイル</Filter>
5255 </ClCompile>
5356 </ItemGroup>