-
Notifications
You must be signed in to change notification settings - Fork 0
/
library.cpp
173 lines (118 loc) · 3.85 KB
/
library.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include "library.h"
#include <cmath>
#include <iostream>
#include <w32api/vadefs.h>
#include <cstring>
#include <unordered_map>
UniversalModuleInterface *moduleInterface;
const char *FLOAT = "FLOAT";
const char *FOUT = "F.";
const char *FADD = "F+";
const char *FSUB = "F-";
const char *FMUL = "F*";
const char *FDIV = "F/";
const char *FSQRT = "FSQRT";
const char *FTOI = "FTOI";
const char *ITOF = "ITOF";
std::unordered_map<std::string, size_t> commands = {
{FLOAT, 0},
{FOUT, 1},
{FADD, 2},
{FSUB, 3},
{FMUL, 4},
{FDIV, 5},
{FSQRT, 6},
{FTOI, 7},
{ITOF, 8}
};
/*
* std::vector<std::string> f_init(UniversalModuleInterface *universalModuleInterface)
*
* This method is called at initialization of module
* Instance of UniversalModuleInterface is received as parameter
* Function must return std::vector<std::string> of all words, that your module introduces
*
* */
std::vector<std::string> __declspec(dllexport) __stdcall f_init(UniversalModuleInterface *universalModuleInterface) {
moduleInterface = universalModuleInterface;
std::vector<std::string> words = {FLOAT, FOUT, FADD, FSUB, FMUL, FDIV, FSQRT, FTOI, ITOF};
return words;
}
/*
* void f_delete()
*
* This method is called before deleting module
*
* */
void __declspec(dllexport) __stdcall f_delete() {
delete moduleInterface;
}
/*
* void f_execWord(char *word)
*
* This method is called each time some word from this module needs to be executed
* Name of word that is required to be executed is received as char* parameter
*
* */
void __declspec(dllexport) __stdcall f_execWord(char *word) {
iWORD *tmp1;
iWORD *tmp2;
float f;
const char *str;
auto got = commands.find(word);
if(got._M_cur) {
switch (got->second) {
case 0:
str = moduleInterface->getInputString();
f = std::strtof(str, nullptr);
moduleInterface->setStack((iWORD*)(&f));
break;
case 1:
tmp1 = moduleInterface->getStack();
printf("%f", *(float*)(tmp1));
break;
case 2:
tmp2 = moduleInterface->getStack();
tmp1 = moduleInterface->getStack();
f = *(float *)(tmp1) + *(float *)(tmp2);
moduleInterface->setStack((iWORD*)(&f));
break;
case 3:
tmp2 = moduleInterface->getStack();
tmp1 = moduleInterface->getStack();
f = *(float *)(tmp1) - *(float *)(tmp2);
moduleInterface->setStack((iWORD*)(&f));
break;
case 4:
tmp2 = moduleInterface->getStack();
tmp1 = moduleInterface->getStack();
f = *(float *)(tmp1) * *(float *)(tmp2);
moduleInterface->setStack((iWORD*)(&f));
break;
case 5:
tmp2 = moduleInterface->getStack();
tmp1 = moduleInterface->getStack();
f = *(float *)(tmp1) / *(float *)(tmp2);
moduleInterface->setStack((iWORD*)(&f));
break;
case 6:
tmp1 = moduleInterface->getStack();
f = std::sqrt(*(float *)(tmp1));
moduleInterface->setStack((iWORD*)(&f));
break;
case 7:
tmp1 = moduleInterface->getStack();
f = *(float *)(tmp1);
*tmp1 = (iWORD)f;
moduleInterface->setStack(tmp1);
break;
case 8:
tmp1 = moduleInterface->getStack();
f = (float)(*tmp1);
moduleInterface->setStack((iWORD*)(&f));
break;
default:
break;
}
}
}