Skip to content

Commit ab394a7

Browse files
committed
Initial commit
1 parent 15319b4 commit ab394a7

File tree

11 files changed

+617
-0
lines changed

11 files changed

+617
-0
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.vs
2+
client-MicrosoftVS
3+
x64-Debug

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# CMakeList.txt : CMake project for CppAsScript, include source and define
2+
# project specific logic here.
3+
#
4+
cmake_minimum_required (VERSION 3.8)
5+
set(CMAKE_CXX_STANDARD 17)
6+
7+
project ("CppAsScript")
8+
9+
# Add source to this project's executable.
10+
add_executable (CppAsScript "CppAsScript.cpp" "CppAsScript.h" "maker/_win.cpp" "maker/CLangScripter.cpp" "api_test/export.cpp")
11+
12+
# TODO: Add tests and install targets if needed.

CppAsScript.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// CppAsScript.cpp : Defines the entry point for the application.
2+
//
3+
4+
#include "maker/CLangScripter.h"
5+
#include <filesystem>
6+
7+
int main()
8+
{
9+
CLangProject PR;
10+
std::filesystem::path cur = std::filesystem::current_path().parent_path().parent_path().parent_path();
11+
12+
std::filesystem::path test = cur;
13+
test.append("test/test.cpp");
14+
15+
std::filesystem::path inc = cur;
16+
inc.append("api_test/");
17+
18+
PR.addFile(test.string()).addIncludeFolder(inc.string()).debug();
19+
20+
auto f = PR.bind<void()>("main");
21+
if (f)f();
22+
23+
auto f1 = PR.bind<void(int)>("test123");
24+
if (f1)f1(23);
25+
26+
return 0;
27+
}

CppAsScript.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// CppAsScript.h : Include file for standard system include files,
2+
// or project specific include files.
3+
4+
#pragma once
5+
6+
#include <iostream>
7+
8+
// TODO: Reference additional headers your program requires here.

api_test/export.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <cstdio>
2+
3+
#define APICALL __declspec(dllexport)
4+
5+
#include "export.h"
6+
7+
APICALL void test(const char* res) {
8+
printf("%s", res);
9+
}
10+
11+
void testClass::testfn(const char* x) {
12+
printf("%s", x);
13+
}

api_test/export.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
APICALL void test(const char* res);
2+
class APICALL testClass {
3+
public:
4+
void testfn(const char* x);
5+
};

maker/CLangScripter.cpp

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
// CLangScripter.cpp : Defines the entry point for the application.
2+
//
3+
4+
#include "CLangScripter.h"
5+
6+
#include "md5.h"
7+
8+
static std::string _replace(const std::string& str, const std::string& sub, const std::string& mod) {
9+
std::string tmp(str);
10+
auto fnd = tmp.find(sub);
11+
if (fnd != -1)tmp.replace(fnd, sub.length(), mod);
12+
return tmp;
13+
}
14+
15+
void CLangProject::_add(const std::string& opt) {
16+
_remove(opt);
17+
_options += " " + opt;
18+
_replace(_options, " ", " ");
19+
}
20+
21+
void CLangProject::_remove(const std::string& opt) {
22+
_replace(_options, opt, "");
23+
_replace(_options, " ", " ");
24+
}
25+
26+
CLangProject::CLangProject() {
27+
builder = CLangBuilder::create();
28+
ref = nullptr;
29+
_options = "/std:c++latest /LD";
30+
}
31+
32+
CLangProject::~CLangProject() {
33+
if (builder)delete(builder);
34+
builder = nullptr;
35+
if (ref)delete(ref);
36+
ref = nullptr;
37+
}
38+
39+
bool CLangProject::valid() {
40+
return ref != nullptr;
41+
}
42+
43+
std::string& CLangProject::compileLog() {
44+
return log;
45+
}
46+
47+
std::string& CLangProject::module() {
48+
return modulePath;
49+
}
50+
51+
std::vector<std::string>& CLangProject::filesList() {
52+
return files;
53+
}
54+
55+
std::string& CLangProject::options() {
56+
return _options;
57+
}
58+
59+
std::string& CLangProject::includes() {
60+
return _includes;
61+
}
62+
63+
CLangProject& CLangProject::addFile(const std::string& path) {
64+
filesList().push_back(path);
65+
return *this;
66+
}
67+
68+
CLangProject& CLangProject::addIncludeFolder(const std::string& path) {
69+
includes() += " /I " + path;
70+
return *this;
71+
}
72+
73+
CLangProject& CLangProject::addSource(const char* cpp_text) {
74+
std::string fn = "temp_" + md5::hash(cpp_text) + ".cpp";
75+
std::string res = builder->pathInHeap(fn);
76+
std::ofstream f(res);
77+
if (f.is_open()) {
78+
f << cpp_text;
79+
f.close();
80+
}
81+
filesList().push_back(res);
82+
return *this;
83+
}
84+
85+
CLangProject& CLangProject::speedOptimization() {
86+
_add("/Ot");
87+
return *this;
88+
}
89+
90+
CLangProject& CLangProject::sizeOptimization() {
91+
_add("/Os");
92+
return *this;
93+
}
94+
95+
CLangProject& CLangProject::debug() {
96+
_remove("/LD");
97+
_add("/LDd");
98+
return *this;
99+
}
100+
101+
CLangProject& CLangProject::release() {
102+
_remove("/LDd");
103+
_add("/LD");
104+
return *this;
105+
}
106+
107+
void CLangProject::recompileIfNeed() {
108+
if(builder) {
109+
builder->compile(*this);
110+
if (ref)delete(ref);
111+
ref = LibReference::create();
112+
ref->loadModule(module().c_str());
113+
}
114+
}
115+

maker/CLangScripter.h

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
// CLangScripter.h : Include file for standard system include files,
2+
// or project specific include files.
3+
#pragma once
4+
5+
#include <vector>
6+
#include <iostream>
7+
#include <stdexcept>
8+
#include <cstdio>
9+
#include <functional>
10+
11+
class LibReference {
12+
public:
13+
virtual ~LibReference() {};
14+
virtual bool valid() const = 0;
15+
virtual bool loadModule(const char* path) = 0;
16+
virtual void unLoadModule() = 0;
17+
virtual void* raw_ptr(const char* functionName) = 0;
18+
static LibReference* create();
19+
};
20+
21+
class CLangProject;
22+
23+
class CLangBuilder {
24+
friend class CLangProject;
25+
public:
26+
27+
virtual ~CLangBuilder() {};
28+
virtual const char* getModuleExtension() const = 0;
29+
virtual void compile(CLangProject& project) = 0;
30+
virtual std::string pathInHeap(const std::string& relative) = 0;
31+
32+
static CLangBuilder* create();
33+
};
34+
35+
class CLangProject {
36+
protected:
37+
std::vector<std::string> files;
38+
std::string modulePath;
39+
std::string log;
40+
std::string _options;
41+
std::string _includes;
42+
LibReference* ref;
43+
CLangBuilder* builder;
44+
void _add(const std::string& opt);
45+
void _remove(const std::string& opt);
46+
public:
47+
CLangProject();
48+
virtual ~CLangProject();
49+
bool valid();
50+
CLangProject& addFile(const std::string& path);
51+
CLangProject& addIncludeFolder(const std::string& path);
52+
53+
CLangProject& addSource(const char* cpp_text);
54+
CLangProject& speedOptimization();
55+
CLangProject& sizeOptimization();
56+
CLangProject& debug();
57+
CLangProject& release();
58+
59+
void recompileIfNeed();
60+
61+
template <class F>
62+
std::function<F> bind(const char* functionName);
63+
64+
std::string& compileLog();
65+
std::string& module();
66+
std::vector<std::string>& filesList();
67+
std::string& options();
68+
std::string& includes();
69+
};
70+
71+
template <class F>
72+
std::function<F> CLangProject::bind(const char* functionName) {
73+
std::function<F> f1 = nullptr;
74+
if (!ref) {
75+
recompileIfNeed();
76+
}
77+
if (ref) {
78+
f1 = static_cast<F*>(ref->raw_ptr(functionName));
79+
}
80+
return f1;
81+
}

0 commit comments

Comments
 (0)