-
Notifications
You must be signed in to change notification settings - Fork 45
/
metta.rs
245 lines (205 loc) · 8.69 KB
/
metta.rs
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
use hyperon::Atom;
use hyperon::metta::text::*;
use hyperon::metta::interpreter;
use hyperon::metta::interpreter::InterpretedAtom;
use hyperon::common::plan::StepResult;
use hyperon::metta::runner::Metta;
use crate::util::*;
use crate::atom::*;
use crate::space::*;
use std::os::raw::*;
use regex::Regex;
use std::path::PathBuf;
// Tokenizer
pub type tokenizer_t = SharedApi<Tokenizer>;
#[no_mangle]
pub extern "C" fn tokenizer_new() -> *mut tokenizer_t {
tokenizer_t::new(Tokenizer::new())
}
#[no_mangle]
pub extern "C" fn tokenizer_free(tokenizer: *mut tokenizer_t) {
tokenizer_t::drop(tokenizer)
}
type atom_constr_t = extern "C" fn(*const c_char, *mut c_void) -> *mut atom_t;
#[repr(C)]
pub struct droppable_t {
ptr: *mut c_void,
free: Option<extern "C" fn(ptr: *mut c_void)>,
}
impl Drop for droppable_t {
fn drop(&mut self) {
let free = (*self).free;
if let Some(free) = free {
free(self.ptr);
}
}
}
#[no_mangle]
pub unsafe extern "C" fn tokenizer_register_token(tokenizer: *mut tokenizer_t,
regex: *const c_char, constr: atom_constr_t, context: droppable_t) {
let regex = Regex::new(cstr_as_str(regex)).unwrap();
(*tokenizer).borrow_mut().register_token(regex, move |token| {
let catom = Box::from_raw(constr(str_as_cstr(token).as_ptr(), context.ptr));
catom.atom
});
}
#[no_mangle]
pub extern "C" fn tokenizer_clone(tokenizer: *const tokenizer_t) -> *mut tokenizer_t {
let copy = unsafe { (*tokenizer).borrow().clone() };
tokenizer_t::new(copy)
}
// SExprParser
pub type sexpr_parser_t<'a> = SharedApi<SExprParser<'a>>;
#[no_mangle]
pub extern "C" fn sexpr_parser_new<'a>(text: *const c_char) -> *mut sexpr_parser_t<'a> {
sexpr_parser_t::new(SExprParser::new(cstr_as_str(text)))
}
#[no_mangle]
pub extern "C" fn sexpr_parser_free(parser: *mut sexpr_parser_t) {
sexpr_parser_t::drop(parser)
}
#[no_mangle]
pub unsafe extern "C" fn sexpr_parser_parse(parser: *mut sexpr_parser_t,
tokenizer: *const tokenizer_t) -> *mut atom_t {
(*parser).borrow_mut().parse(&(*tokenizer).borrow())
.unwrap()
.map_or(std::ptr::null_mut(), |atom| { atom_into_ptr(atom) })
}
#[no_mangle] pub extern "C" fn ATOM_TYPE_UNDEFINED() -> *mut atom_t { atom_into_ptr(hyperon::metta::ATOM_TYPE_UNDEFINED) }
#[no_mangle] pub extern "C" fn ATOM_TYPE_TYPE() -> *mut atom_t { atom_into_ptr(hyperon::metta::ATOM_TYPE_TYPE) }
#[no_mangle] pub extern "C" fn ATOM_TYPE_ATOM() -> *mut atom_t { atom_into_ptr(hyperon::metta::ATOM_TYPE_ATOM) }
#[no_mangle] pub extern "C" fn ATOM_TYPE_SYMBOL() -> *mut atom_t { atom_into_ptr(hyperon::metta::ATOM_TYPE_SYMBOL) }
#[no_mangle] pub extern "C" fn ATOM_TYPE_VARIABLE() -> *mut atom_t { atom_into_ptr(hyperon::metta::ATOM_TYPE_VARIABLE) }
#[no_mangle] pub extern "C" fn ATOM_TYPE_EXPRESSION() -> *mut atom_t { atom_into_ptr(hyperon::metta::ATOM_TYPE_EXPRESSION) }
#[no_mangle] pub extern "C" fn ATOM_TYPE_GROUNDED() -> *mut atom_t { atom_into_ptr(hyperon::metta::ATOM_TYPE_GROUNDED) }
#[no_mangle]
pub unsafe extern "C" fn check_type(space: *const grounding_space_t, atom: *const atom_t, typ: *const atom_t) -> bool {
hyperon::metta::types::check_type((*space).borrow().deref(), &(*atom).atom, &(*typ).atom)
}
#[no_mangle]
pub unsafe extern "C" fn check_type_v2(space: *const space_t, atom: *const atom_t, typ: *const atom_t) -> bool {
hyperon::metta::types::check_type((*space).borrow().deref().as_space(), &(*atom).atom, &(*typ).atom)
}
#[no_mangle]
pub unsafe extern "C" fn validate_atom(space: *const grounding_space_t, atom: *const atom_t) -> bool {
hyperon::metta::types::validate_atom((*space).borrow().deref(), &(*atom).atom)
}
#[no_mangle]
pub unsafe extern "C" fn validate_atom_v2(space: *const space_t, atom: *const atom_t) -> bool {
hyperon::metta::types::validate_atom((*space).borrow().deref().as_space(), &(*atom).atom)
}
#[no_mangle]
pub extern "C" fn get_atom_types(space: *const grounding_space_t, atom: *const atom_t,
callback: c_atoms_callback_t, context: *mut c_void) {
let space = unsafe{ &(*space).borrow() };
let atom = unsafe{ &(*atom).atom };
let types = hyperon::metta::types::get_atom_types(space.deref(), atom);
return_atoms(&types, callback, context);
}
#[no_mangle]
pub extern "C" fn get_atom_types_v2(space: *const space_t, atom: *const atom_t,
callback: c_atoms_callback_t, context: *mut c_void) {
let space = unsafe{ &(*space).borrow() };
let atom = unsafe{ &(*atom).atom };
let types = hyperon::metta::types::get_atom_types(space.deref().as_space(), atom);
return_atoms(&types, callback, context);
}
// MeTTa interpreter API
pub struct step_result_t<'a> {
result: StepResult<'a, Vec<InterpretedAtom>, (Atom, Atom)>,
}
#[no_mangle]
pub extern "C" fn interpret_init<'a>(space: *mut grounding_space_t, expr: *const atom_t) -> *mut step_result_t<'a> {
let space = unsafe{ &(*space) };
let expr = unsafe{ &(*expr) };
let step = interpreter::interpret_init(space.shared(), &expr.atom);
Box::into_raw(Box::new(step_result_t{ result: step }))
}
#[no_mangle]
pub extern "C" fn interpret_init_v2<'a>(space: *mut space_t, expr: *const atom_t) -> *mut step_result_t<'a> {
let space = unsafe{ &(*space) };
let expr = unsafe{ &(*expr) };
let step = interpreter::interpret_init(space.shared(), &expr.atom);
Box::into_raw(Box::new(step_result_t{ result: step }))
}
#[no_mangle]
pub extern "C" fn interpret_step(step: *mut step_result_t) -> *mut step_result_t {
let step = unsafe { Box::from_raw(step) };
let next = interpreter::interpret_step(step.result);
Box::into_raw(Box::new(step_result_t{ result: next }))
}
#[no_mangle]
pub extern "C" fn step_has_next(step: *const step_result_t) -> bool {
unsafe{ (*step).result.has_next() }
}
#[no_mangle]
pub extern "C" fn step_get_result(step: *mut step_result_t,
callback: c_atoms_callback_t, context: *mut c_void) {
let step = unsafe{ Box::from_raw(step) };
match step.result {
StepResult::Return(mut res) => {
let res = res.drain(0..).map(|res| res.into_tuple().0).collect();
return_atoms(&res, callback, context);
},
StepResult::Error(_) => return_atoms(&vec![], callback, context),
_ => panic!("Not expected step result: {:?}", step.result),
}
}
#[no_mangle]
pub extern "C" fn step_to_str(step: *const step_result_t, callback: c_str_callback_t, context: *mut c_void) {
let result = unsafe{ &(*step).result };
callback(str_as_cstr(format!("{:?}", result).as_str()).as_ptr(), context);
}
pub type metta_t = SharedApi<Metta>;
#[no_mangle]
pub extern "C" fn metta_new(space: *mut grounding_space_t, tokenizer: *mut tokenizer_t, cwd: *const c_char) -> *mut metta_t {
let space = unsafe{ &mut *space }.shared();
let tokenizer = unsafe{ &mut *tokenizer }.shared();
metta_t::new(Metta::from_space_cwd(space, tokenizer, PathBuf::from(cstr_as_str(cwd))))
}
#[no_mangle]
pub extern "C" fn metta_clone(metta: *mut metta_t) -> *mut metta_t {
let metta = unsafe{ &(*metta) };
metta_t::from_shared(metta.shared())
}
#[no_mangle]
pub extern "C" fn metta_free(metta: *mut metta_t) {
metta_t::drop(metta);
}
#[no_mangle]
pub extern "C" fn metta_space(metta: *mut metta_t) -> *mut grounding_space_t {
let space = unsafe{ &*metta }.borrow().space();
grounding_space_t::from_shared(space)
}
#[no_mangle]
pub extern "C" fn metta_tokenizer(metta: *mut metta_t) -> *mut tokenizer_t {
let tokenizer = unsafe{ &*metta }.borrow().tokenizer();
tokenizer_t::from_shared(tokenizer)
}
#[no_mangle]
pub extern "C" fn metta_run(metta: *mut metta_t, parser: *mut sexpr_parser_t,
output: c_atoms_callback_t, out_context: *mut c_void) {
let metta = unsafe{ &*metta }.borrow();
let mut parser = unsafe{ &mut *parser }.borrow_mut();
let results = metta.run(&mut parser);
// TODO: return erorrs properly after step_get_result() is changed to return errors.
for result in results.expect("Returning errors from C API is not implemented yet") {
return_atoms(&result, output, out_context);
}
}
#[no_mangle]
pub extern "C" fn metta_evaluate_atom(metta: *mut metta_t, atom: *mut atom_t,
output: c_atoms_callback_t, out_context: *mut c_void) {
let metta = unsafe{ &*metta }.borrow();
let atom = ptr_into_atom(atom);
let result = metta.evaluate_atom(atom)
.expect("Returning errors from C API is not implemented yet");
return_atoms(&result, output, out_context);
}
#[no_mangle]
pub extern "C" fn metta_load_module(metta: *mut metta_t, name: *const c_char) {
let metta = unsafe{ &*metta }.borrow();
// TODO: return erorrs properly
metta.load_module(PathBuf::from(cstr_as_str(name)))
.expect("Returning errors from C API is not implemented yet");
}