Skip to content

Commit 08f278d

Browse files
author
mulab
committed
[mU] 将CInterface功能移植到0.x
1 parent 8a2737b commit 08f278d

File tree

30 files changed

+494
-208
lines changed

30 files changed

+494
-208
lines changed

Include/mU/Interface.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
#include "Kernel.h"
3+
4+
namespace mU {
5+
typedef const wchar* wcs;
6+
API string cstr(wcs);
7+
inline string cstr(const wstring& x) {
8+
return cstr(x.c_str());
9+
}
10+
API string cstr(Var);
11+
API string cpath(const char*);
12+
API void* cload(const char*);
13+
API void* cnoload(const char*);
14+
API void cunload(void*);
15+
API void* csym(void*, const char*);
16+
API bool cinstall(const char*);
17+
API bool cuninstall(const char*);
18+
API string cname(Var);
19+
API void* cfunc(void*, Var);
20+
}

Include/mU/Kernel.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ API std::map<Var,def_t> DownValues;
8787
API std::map<Var,def_t> SubValues;
8888
API std::map<Var,map_t> Properties;
8989
API std::map<Var,attr_t> Attributes;
90-
API stdext::hash_map<Var,CProc> Externals;
91-
API stdext::hash_map<Var,COper> Functionals;
90+
API stdext::hash_map<Var,CProc> CProcs;
91+
API stdext::hash_map<Var,COper> COpers;
9292
API var Eval(Var);
9393
API void Set(Var,Var);
9494
API void Unset(Var);

Include/mU/Parser.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,12 @@ class parser
6161
DLL static stdext::hash_map<wstring,size_t> s_postfix_symbol, s_prefix_symbol, s_infix_symbol;
6262
DLL static std::set<token_t> s_end;
6363
DLL static void init();
64-
parser();
65-
parser(wistream&);
66-
void start(wistream&);
67-
void parse();
68-
var result();
69-
void clear();
64+
DLL parser();
65+
DLL parser(wistream&);
66+
DLL void start(wistream&);
67+
DLL void parse();
68+
DLL var result();
69+
DLL void clear();
7070

7171
size_t lineno;
7272
size_t column;

Include/mU/Var.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ typedef var_t* Var;
3333
typedef unsigned long uint;
3434
typedef signed long sint;
3535
typedef wchar_t wchar;
36+
#define __W(x) L ## x
37+
#define _W(x) __W(x)
3638
using std::string;using std::wstring;using std::istream;using std::wistream;
3739
using std::ostream;using std::wostream;using std::iostream;using std::wiostream;
3840
using std::istringstream;using std::wistringstream;
@@ -81,16 +83,17 @@ struct hash<std::wstring>
8183
namespace stdext = __gnu_cxx;
8284
#endif
8385
#ifdef _WIN32
84-
#ifdef MU_EXPORTS
86+
#ifdef KERNEL_EXPORTS
8587
#define DLL __declspec(dllexport)
8688
#else
8789
#define DLL __declspec(dllimport)
88-
#pragma comment(lib,"mU")
90+
#pragma comment(lib,"Kernel")
8991
#endif
9092
#else
9193
#define DLL
9294
#endif
9395
#define API DLL extern
96+
#define CAPI extern "C" DLL
9497

9598
namespace mU {
9699
//////////////////////////////////////

Kernel/Interface.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <mU/Interface.h>
2+
#ifdef _WIN32
3+
#define WIN32_LEAN_AND_MEAN
4+
#include <Windows.h>
5+
#include <ShellAPI.h>
6+
#else
7+
#include <dlfcn.h>
8+
#endif
9+
10+
namespace mU {
11+
string cstr(wcs x) {
12+
return string(x, x + wcslen(x)).c_str();
13+
}
14+
string cstr(Var x) {
15+
return cstr(CStr(x).c_str());
16+
}
17+
string cpath(const char* x) {
18+
#ifdef _WIN32
19+
#ifdef _MSC_VER
20+
return string(x) + string(".dll");
21+
#else
22+
return string("lib") + string(x) + string(".dll");
23+
#endif
24+
#else
25+
return string("lib") + string(x) + string(".so");
26+
#endif
27+
}
28+
void* cload(const char* x) {
29+
#ifdef _WIN32
30+
return LoadLibraryA(cpath(x).c_str());
31+
#else
32+
string s = cpath(x);
33+
return dlopen(s.c_str(), RTLD_LAZY);
34+
#endif
35+
}
36+
void* cnoload(const char* x) {
37+
#ifdef _WIN32
38+
return GetModuleHandleA(cpath(x).c_str());
39+
#else
40+
string s = cpath(x);
41+
return dlopen(s.c_str(), RTLD_LAZY | RTLD_NOLOAD);
42+
#endif
43+
}
44+
void cunload(void* x) {
45+
#ifdef _WIN32
46+
FreeLibrary(reinterpret_cast<HMODULE>(x));
47+
#else
48+
dlclose(x);
49+
#endif
50+
}
51+
void* csym(void* m, const char* x) {
52+
return
53+
#ifdef _WIN32
54+
(void*)GetProcAddress(reinterpret_cast<HMODULE>(m), x)
55+
#else
56+
dlsym(m, x)
57+
#endif
58+
;
59+
}
60+
bool cinstall(const char* x) {
61+
typedef void(*Ptr)();
62+
void* m = cload(x);
63+
if (!m)
64+
return false;
65+
void* ptr = csym(m, "Install");
66+
if (ptr)
67+
reinterpret_cast<Ptr>(ptr)();
68+
return true;
69+
}
70+
bool cuninstall(const char* x) {
71+
typedef void(*Ptr)();
72+
void* m = cnoload(x);
73+
if (!m)
74+
return false;
75+
void* ptr = csym(m, "Uninstall");
76+
if (ptr)
77+
reinterpret_cast<Ptr>(ptr)();
78+
cunload(m);
79+
return true;
80+
}
81+
string cname(Var x) {
82+
wstring s = ContextName[Context(x)];
83+
std::replace(s.begin(), s.end(), _W('`'),_W('_'));
84+
s += Name(x);
85+
return cstr(s.c_str());
86+
}
87+
void* cfunc(void* m, Var x) {
88+
string s = cname(x);
89+
return csym(m, s.c_str());
90+
}
91+
}
92+
93+
using namespace mU;
94+
95+
CAPI var System_CInstall(Var x) {
96+
return cinstall(cstr(At(x,0)).c_str()) ? True : False;
97+
}
98+
CAPI var System_CUninstall(Var x) {
99+
return cuninstall(cstr(At(x,0)).c_str()) ? True : False;
100+
}
101+
CAPI var System_CProc(Var x) {
102+
void* m = cnoload(cstr(At(x,0)).c_str());
103+
if (!m)
104+
return False;
105+
for (size_t i = 1; i < Size(x); ++i) {
106+
CProc ptr = (CProc)cfunc(m, At(x,i));
107+
if (!ptr) {
108+
wcerr << _W("CProc ") << Name(At(x,i)) << _W(" not found!") << std::endl;
109+
return False;
110+
}
111+
CProcs[At(x,i)] = ptr;
112+
}
113+
return True;
114+
}

Kernel/Kernel.u

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
(* Begin["System`"] *)
2+
(* CInstall["kernel"] *)
3+
4+
CProc["kernel",Box,CInstall,CUninstall]
5+
6+
(* End[] *)

Kernel/box.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,3 +445,11 @@ else if(h == tag_##x)\
445445
}
446446
//////////////////////////////////////
447447
}
448+
449+
using namespace mU;
450+
451+
CAPI var System_Box(Var x) {
452+
wostringstream t;
453+
BoxPrint(Pretty(At(x,0)),t);
454+
return Str(t.str());
455+
}

Kernel/eval.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ var Eval(Var x)
126126
}
127127
{
128128
stdext::hash_map<Var,CProc>::const_iterator
129-
iter = Externals.find(h);
130-
if(iter != Externals.end())
129+
iter = CProcs.find(h);
130+
if(iter != CProcs.end())
131131
{
132132
var t = iter->second(r);
133133
if(t) return t;
@@ -158,8 +158,8 @@ var Eval(Var x)
158158
}
159159
{
160160
stdext::hash_map<Var,COper>::const_iterator
161-
iter = Functionals.find(Head(h));
162-
if(iter != Functionals.end())
161+
iter = COpers.find(Head(h));
162+
if(iter != COpers.end())
163163
{
164164
var t = iter->second(Body(h),r);
165165
if(t) return t;

Kernel/evalf.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ var Evalf(Var x)
3535
iter = Attributes.find(x);
3636
if(iter != Attributes.end() &&
3737
iter->second.count(Constant))
38-
return Externals[x](0);
38+
return CProcs[x](0);
3939
}
4040
break;
4141
case TYPE(vec):

Kernel/initialize.cpp

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <mU/String.h>
33
#include <mU/Pattern.h>
44
#include <mU/Kernel.h>
5+
#include <mU/Interface.h>
56

67
namespace mU {
78
namespace {
@@ -614,12 +615,6 @@ Wrap(Full)
614615
FullPrint(At(x,0),t);
615616
return Str(t.str());
616617
}
617-
Wrap(Box)
618-
{
619-
wostringstream t;
620-
BoxPrint(Pretty(At(x,0)),t);
621-
return Str(t.str());
622-
}
623618
Wrap(ToString)
624619
{
625620
wostringstream t;
@@ -866,8 +861,8 @@ std::map<Var,def_t> DownValues;
866861
std::map<Var,def_t> SubValues;
867862
std::map<Var,map_t> Properties;
868863
std::map<Var,attr_t> Attributes;
869-
stdext::hash_map<Var,CProc> Externals;
870-
stdext::hash_map<Var,COper> Functionals;
864+
stdext::hash_map<Var,CProc> CProcs;
865+
stdext::hash_map<Var,COper> COpers;
871866
var
872867
Global, System, Null, True, False, Nil,
873868
Constant, Flat, HoldAll, HoldAllComplete, HoldFirst,
@@ -932,10 +927,10 @@ void Initialize()
932927
T(Exponent)T(Deg)T(CoefficientList)T(FromCoefficientList)T(Graphics2D)T(Graphics3D)
933928
T(Options)T(StringLength)T(StringInsert)T(StringTake)T(StringDrop)
934929
#undef T
935-
#define T(x) Externals[TAG(x)] = x;
930+
#define T(x) CProcs[TAG(x)] = x;
936931
T(Plus)T(Times)T(Dot)
937932
#undef T
938-
#define T(x) Externals[TAG(x)] = WRAP(x);
933+
#define T(x) CProcs[TAG(x)] = WRAP(x);
939934
T(Timing)T(And)T(Or)T(Not)T(If)T(For)T(While)T(Flatten)T(FlattenAll)
940935
T(SameQ)T(Less)T(Equal)T(Greater)T(UnsameQ)T(GreaterEqual)T(Unequal)T(LessEqual)
941936
T(FreeQ)T(MatchQ)T(MemberQ)T(With)T(Block)T(Module)T(ReplaceRepeated)
@@ -946,7 +941,7 @@ void Initialize()
946941
T(Contexts)T(ContextPath)T(Apply)T(Map)T(Unset)T(Full)T(ToString)T(ToExpression)
947942
T(Exit)T(Quit)T(Set)T(SetDelayed)T(Hold)T(Run)T(Task)T(Kill)T(Array)T(Table)T(Do)
948943
T(N)T(IntegerPart)T(Floor)T(Ceiling)T(Round)T(Expand)T(Variables)T(Coefficient)
949-
T(Exponent)T(Deg)T(CoefficientList)T(FromCoefficientList)T(Box)T(StringLength)
944+
T(Exponent)T(Deg)T(CoefficientList)T(FromCoefficientList)T(StringLength)
950945
T(Set)T(Part)T(Property)T(SetDelayed)T(CompoundExpression)T(Power)T(Mod)
951946
T(StringInsert)T(StringTake)T(StringDrop)
952947
#undef T
@@ -964,7 +959,7 @@ void Initialize()
964959
T(Unevaluated,HoldAll)T(Function,HoldAll)T(Context,HoldFirst)T(Property,HoldAll)
965960
T(RuleDelayed,HoldRest)T(Unset,HoldFirst)T(Task,HoldAll)T(Table,HoldAll)T(Do,HoldAll)
966961
#undef T
967-
#define T(x) Functionals[TAG(x)] = WRAP(x);
962+
#define T(x) COpers[TAG(x)] = WRAP(x);
968963
T(Function)
969964
#undef T
970965
ContextPath.push(std::list<Var>());
@@ -975,3 +970,15 @@ void Initialize()
975970
}
976971
}
977972
DLLMAIN(mU::Initialize)
973+
974+
using namespace mU;
975+
#define SYS(x) Sym(_W(#x),System)
976+
CAPI void Install() {
977+
CProcs[SYS(CProc)] = (CProc)cfunc(cnoload("kernel"), SYS(CProc));
978+
Begin(System);
979+
ParseFile(Path() + _W("Kernel/Kernel.u"));
980+
End();
981+
}
982+
CAPI void Uninstall() {
983+
wcout << "#Kernel# Uninstall Successfully!" << std::endl;
984+
}

0 commit comments

Comments
 (0)