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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
#![allow(clippy::missing_safety_doc)]

use libc::*;
use std::ffi::CString;
use std::slice;

// types

pub enum CSymbolTable {}
pub enum CExpression {}
pub enum CParser {}
pub enum CppString {}

// simple types used for communications with C++

#[repr(C)]
pub struct Pair<T, U>(pub T, pub U);

pub type CStrList = Pair<size_t, *const *const c_char>;

impl CStrList {
    pub unsafe fn get_slice(&self) -> &[*const c_char] {
        slice::from_raw_parts(self.1, self.0 as usize)
    }
}

#[repr(C)]
pub struct CParseError {
    pub is_err: bool,
    pub mode: c_int,
    pub token_type: *const c_char,
    pub token_value: *const c_char,
    pub diagnostic: *const c_char,
    pub error_line: *const c_char,
    pub line_no: size_t,
    pub column_no: size_t,
}

// for deallocating CString from C
#[no_mangle]
pub unsafe extern "C" fn free_rust_cstring(s: *mut c_char) {
    let _ = CString::from_raw(s);
}

// functions without polymorphism
extern "C" {

    // these methods depend on a specific precision

    pub fn symbol_table_new() -> *mut CSymbolTable;
    pub fn symbol_table_add_variable(
        t: *mut CSymbolTable,
        variable_name: *const c_char,
        value: *const c_double,
        is_constant: bool,
    ) -> bool;
    pub fn symbol_table_add_constant(
        t: *mut CSymbolTable,
        variable_name: *const c_char,
        value: c_double,
    ) -> bool;
    pub fn symbol_table_create_variable(
        t: *mut CSymbolTable,
        variable_name: *const c_char,
        value: c_double,
    ) -> bool;
    pub fn symbol_table_add_stringvar(
        t: *mut CSymbolTable,
        variable_name: *const c_char,
        string: *mut CppString,
        is_const: bool,
    ) -> bool;
    pub fn symbol_table_create_stringvar(
        t: *mut CSymbolTable,
        variable_name: *const c_char,
        string: *const c_char,
    ) -> bool;
    pub fn symbol_table_add_vector(
        t: *mut CSymbolTable,
        variable_name: *const c_char,
        ptr: *const c_double,
        len: size_t,
    ) -> bool;
    pub fn symbol_table_remove_variable(t: *mut CSymbolTable, name: *const c_char) -> bool;
    pub fn symbol_table_remove_stringvar(t: *mut CSymbolTable, name: *const c_char) -> bool;
    pub fn symbol_table_remove_vector(t: *mut CSymbolTable, name: *const c_char) -> bool;
    pub fn symbol_table_clear_variables(t: *mut CSymbolTable);
    pub fn symbol_table_clear_strings(t: *mut CSymbolTable);
    pub fn symbol_table_clear_vectors(t: *mut CSymbolTable);
    pub fn symbol_table_clear_local_constants(t: *mut CSymbolTable);
    pub fn symbol_table_clear_functions(t: *mut CSymbolTable);
    pub fn symbol_table_variable_ref(
        t: *mut CSymbolTable,
        variable_name: *const c_char,
    ) -> *mut c_double;
    pub fn symbol_table_stringvar_ref(
        t: *mut CSymbolTable,
        variable_name: *const c_char,
    ) -> *mut CppString;
    pub fn symbol_table_vector_ptr(
        t: *mut CSymbolTable,
        variable_name: *const c_char,
    ) -> *const c_double;
    pub fn symbol_table_set_string(
        t: *mut CSymbolTable,
        ptr: *const CppString,
        string: *const c_char,
    );
    pub fn symbol_table_variable_count(t: *mut CSymbolTable) -> size_t;
    pub fn symbol_table_stringvar_count(t: *mut CSymbolTable) -> size_t;
    pub fn symbol_table_vector_count(t: *mut CSymbolTable) -> size_t;
    pub fn symbol_table_function_count(t: *mut CSymbolTable) -> size_t;
    pub fn symbol_table_add_pi(t: *mut CSymbolTable) -> bool;
    pub fn symbol_table_add_epsilon(t: *mut CSymbolTable) -> bool;
    pub fn symbol_table_add_infinity(t: *mut CSymbolTable) -> bool;
    pub fn symbol_table_get_variable_list(t: *mut CSymbolTable) -> *mut CStrList;
    pub fn symbol_table_get_stringvar_list(t: *mut CSymbolTable) -> *mut CStrList; //StringPtrList;
    pub fn symbol_table_get_vector_list(t: *mut CSymbolTable) -> *mut CStrList;
    pub fn symbol_table_valid(t: *mut CSymbolTable) -> bool;
    pub fn symbol_table_symbol_exists(t: *mut CSymbolTable, name: *const c_char) -> bool;
    pub fn symbol_table_load_from(t: *mut CSymbolTable, other: *const CSymbolTable);
    pub fn symbol_table_add_constants(t: *mut CSymbolTable) -> bool;
    pub fn symbol_table_is_constant_node(t: *mut CSymbolTable, name: *const c_char) -> bool;
    pub fn symbol_table_is_constant_string(t: *mut CSymbolTable, name: *const c_char) -> bool;
    pub fn symbol_table_destroy(t: *mut CSymbolTable);

    // // blocked by #5668
    // macro_rules! func_declare {
    //     ($add_name:ident, $free_name:ident, $($ty:ty),*) => {
    //         pub fn $add_name(t: *mut CSymbolTable, name: *const c_char,
    //             cb: extern fn (*mut c_void, $($ty),*) -> c_double,
    //             user_data: *mut c_void) -> Pair<bool, *mut c_void>;
    //         pub fn $free_name(c_func: *mut c_void);
    //     }
    // }

    pub fn symbol_table_add_func1(
        t: *mut CSymbolTable,
        name: *const c_char,
        cb: extern "C" fn(*mut c_void, c_double) -> c_double,
        user_data: *mut c_void,
    ) -> Pair<bool, *mut c_void>;
    pub fn symbol_table_free_func1(c_func: *mut c_void);

    pub fn symbol_table_add_func2(
        t: *mut CSymbolTable,
        name: *const c_char,
        cb: extern "C" fn(*mut c_void, c_double, c_double) -> c_double,
        user_data: *mut c_void,
    ) -> Pair<bool, *mut c_void>;
    pub fn symbol_table_free_func2(c_func: *mut c_void);

    pub fn symbol_table_add_func3(
        t: *mut CSymbolTable,
        name: *const c_char,
        cb: extern "C" fn(*mut c_void, c_double, c_double, c_double) -> c_double,
        user_data: *mut c_void,
    ) -> Pair<bool, *mut c_void>;
    pub fn symbol_table_free_func3(c_func: *mut c_void);

    pub fn symbol_table_add_func4(
        t: *mut CSymbolTable,
        name: *const c_char,
        cb: extern "C" fn(*mut c_void, c_double, c_double, c_double, c_double) -> c_double,
        user_data: *mut c_void,
    ) -> Pair<bool, *mut c_void>;
    pub fn symbol_table_free_func4(c_func: *mut c_void);

    pub fn symbol_table_add_func5(
        t: *mut CSymbolTable,
        name: *const c_char,
        cb: extern "C" fn(
            *mut c_void,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
        ) -> c_double,
        user_data: *mut c_void,
    ) -> Pair<bool, *mut c_void>;
    pub fn symbol_table_free_func5(c_func: *mut c_void);

    pub fn symbol_table_add_func6(
        t: *mut CSymbolTable,
        name: *const c_char,
        cb: extern "C" fn(
            *mut c_void,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
        ) -> c_double,
        user_data: *mut c_void,
    ) -> Pair<bool, *mut c_void>;
    pub fn symbol_table_free_func6(c_func: *mut c_void);

    pub fn symbol_table_add_func7(
        t: *mut CSymbolTable,
        name: *const c_char,
        cb: extern "C" fn(
            *mut c_void,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
        ) -> c_double,
        user_data: *mut c_void,
    ) -> Pair<bool, *mut c_void>;
    pub fn symbol_table_free_func7(c_func: *mut c_void);

    pub fn symbol_table_add_func8(
        t: *mut CSymbolTable,
        name: *const c_char,
        cb: extern "C" fn(
            *mut c_void,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
        ) -> c_double,
        user_data: *mut c_void,
    ) -> Pair<bool, *mut c_void>;
    pub fn symbol_table_free_func8(c_func: *mut c_void);

    pub fn symbol_table_add_func9(
        t: *mut CSymbolTable,
        name: *const c_char,
        cb: extern "C" fn(
            *mut c_void,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
        ) -> c_double,
        user_data: *mut c_void,
    ) -> Pair<bool, *mut c_void>;
    pub fn symbol_table_free_func9(c_func: *mut c_void);

    pub fn symbol_table_add_func10(
        t: *mut CSymbolTable,
        name: *const c_char,
        cb: extern "C" fn(
            *mut c_void,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
            c_double,
        ) -> c_double,
        user_data: *mut c_void,
    ) -> Pair<bool, *mut c_void>;
    pub fn symbol_table_free_func10(c_func: *mut c_void);

    // Expression
    pub fn expression_new() -> *mut CExpression;
    pub fn expression_register_symbol_table(e: *mut CExpression, t: *const CSymbolTable);
    pub fn expression_value(e: *mut CExpression) -> c_double;
    pub fn expression_destroy(e: *mut CExpression);

    pub fn parser_new() -> *mut CParser;
    pub fn parser_destroy(p: *mut CParser);
    pub fn parser_compile(p: *mut CParser, s: *const c_char, e: *const CExpression) -> bool;
    pub fn parser_compile_resolve(
        p: *mut CParser,
        s: *const c_char,
        e: *const CExpression,
        cb: extern "C" fn(*const c_char, *mut c_void) -> *const c_char,
        fn_pointer: *mut c_void,
    ) -> bool;
    pub fn parser_error(p: *mut CParser) -> *const CParseError;
    pub fn parser_error_free(p: *const CParseError);

    pub fn string_array_free(l: *mut CStrList);

    pub fn cpp_string_create(s: *const c_char, len: size_t) -> *mut CppString;
    pub fn cpp_string_set(s: *mut CppString, replacement: *const c_char, len: size_t);
    pub fn cpp_string_get(s: *const CppString) -> *const c_char;
    pub fn cpp_string_free(s: *mut CppString);

}