-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathextension.c
141 lines (129 loc) · 5.69 KB
/
extension.c
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
/*
* Description: Extension module of the Chaos Programming Language's source
*
* Copyright (c) 2019-2021 Chaos Language Development Authority <[email protected]>
*
* License: GNU General Public License v3.0
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>
*
* Authors: M. Mert Yildiran <[email protected]>
*/
#include "extension.h"
void initKaosApi() {
kaos.defineFunction = defineFunction;
kaos.getVariableBool = getVariableBool;
kaos.getVariableInt = getVariableInt;
kaos.getVariableFloat = getVariableFloat;
kaos.getVariableString = getVariableString;
kaos.getVariableBoolByTypeCasting = getVariableBoolByTypeCasting;
kaos.getVariableIntByTypeCasting = getVariableIntByTypeCasting;
kaos.getVariableFloatByTypeCasting = getVariableFloatByTypeCasting;
kaos.getVariableStringByTypeCasting = getVariableStringByTypeCasting;
kaos.getListLength = getListLength;
kaos.getListElementBool = getListElementBool;
kaos.getListElementInt = getListElementInt;
kaos.getListElementFloat = getListElementFloat;
kaos.getListElementString = getListElementString;
kaos.getListElementBoolByTypeCasting = getListElementBoolByTypeCasting;
kaos.getListElementIntByTypeCasting = getListElementIntByTypeCasting;
kaos.getListElementFloatByTypeCasting = getListElementFloatByTypeCasting;
kaos.getListElementStringByTypeCasting = getListElementStringByTypeCasting;
kaos.copyListElement = copyListElement;
kaos.getListElementType = getListElementType;
kaos.getListElementValueType = getListElementValueType;
kaos.getDictLength = getDictLength;
kaos.getDictKeyByIndex = getDictKeyByIndex;
kaos.getDictElementBool = getDictElementBool;
kaos.getDictElementInt = getDictElementInt;
kaos.getDictElementFloat = getDictElementFloat;
kaos.getDictElementString = getDictElementString;
kaos.getDictElementBoolByTypeCasting = getDictElementBoolByTypeCasting;
kaos.getDictElementIntByTypeCasting = getDictElementIntByTypeCasting;
kaos.getDictElementFloatByTypeCasting = getDictElementFloatByTypeCasting;
kaos.getDictElementStringByTypeCasting = getDictElementStringByTypeCasting;
kaos.copyDictElement = copyDictElement;
kaos.getDictElementType = getDictElementType;
kaos.getDictElementValueType = getDictElementValueType;
kaos.dumpVariableToString = dumpVariableToString;
kaos.returnVariableBool = returnVariableBool;
kaos.returnVariableInt = returnVariableInt;
kaos.returnVariableFloat = returnVariableFloat;
kaos.returnVariableString = returnVariableString;
kaos.createVariableBool = createVariableBool;
kaos.createVariableInt = createVariableInt;
kaos.createVariableFloat = createVariableFloat;
kaos.createVariableString = createVariableString;
kaos.startBuildingList = startBuildingList;
kaos.returnList = returnList;
kaos.startBuildingDict = startBuildingDict;
kaos.returnDict = returnDict;
kaos.returnComplex = returnComplex;
kaos.finishList = finishList;
kaos.finishDict = finishDict;
kaos.finishComplex = finishComplex;
kaos.getListType = getListType;
kaos.getDictType = getDictType;
kaos.getValueType = getValueType;
kaos.getRole = getRole;
kaos.raiseError = raiseError;
kaos.parseJson = parseJson;
}
void callRegisterInDynamicLibrary(char* dynamic_library_path) {
dynamic_library dylib = getFunctionFromDynamicLibrary(dynamic_library_path, __KAOS_EXTENSION_REGISTER_FUNCTION__);
dylib.func(kaos);
#ifndef CHAOS_COMPILER
if (is_interactive)
phase = PROGRAM;
#endif
}
void callFunctionFromDynamicLibrary(_Function* function, cpu* c) {
char* function_name = "";
function_name = strcat_ext(function_name, __KAOS_EXTENSION_FUNCTION_PREFIX__);
function_name = strcat_ext(function_name, function->name);
dynamic_library dylib = getFunctionFromDynamicLibrary(function->module_context, function_name);
startFunctionScope(function);
populateCallParametersDynamicLibrary(function, c);
dylib.func();
handleFunctionReturnDynamicLibrary(function, c);
removeSymbolsByScope(getCurrentScope());
endFunction();
free(function_name);
}
void populateCallParametersDynamicLibrary(_Function* function, cpu* c) {
}
void handleFunctionReturnDynamicLibrary(_Function* function, cpu* c) {
}
dynamic_library getFunctionFromDynamicLibrary(char* dynamic_library_path, char* function_name) {
dynamic_library dylib;
dylib.handle = OPENLIB(dynamic_library_path);
if (dylib.handle == NULL) {
#if !defined(_WIN32) && !defined(_WIN64) && !defined(__CYGWIN__)
fprintf(stderr, "Unable to open lib: %s\n", dlerror());
#endif
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
dylib.func = LIBFUNC(dylib.handle, function_name);
#pragma GCC diagnostic pop
if (dylib.func == NULL) {
fprintf(stderr, "Unable to get symbol\n");
}
return dylib;
}
void returnVariable(Symbol* symbol) {
scope_override = function_call_stack.arr[function_call_stack.size - 1]->parent_scope;
function_call_stack.arr[function_call_stack.size - 1]->function->symbol = symbol;
scope_override = NULL;
// removeSymbol(symbol);
}