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
use std::ffi::{CString};
use std::env::args;
use std::ptr;
use std::sync;
use ll::*;
use interpreter::Interpreter;
use object::{Object, TclObject};
static INIT_TCL: sync::Once = sync::ONCE_INIT;
pub struct TclEnvironment;
pub fn init() -> TclEnvironment {
let ptr = match args().next() {
Some(path) => {
CString::new(path.as_bytes()).unwrap().as_ptr()
},
None => ptr::null()
};
INIT_TCL.call_once(|| {
unsafe { Tcl_FindExecutable(ptr) };
});
TclEnvironment
}
impl TclEnvironment {
pub fn interpreter(&self) -> Result<Interpreter, &str> {
Interpreter::new(self)
}
pub fn new_object<V: TclObject>(&self, val: V) -> Object {
Object::new(self, val)
}
}
#[derive(Debug)]
pub enum TclResult {
Ok,
Error(String),
Return,
Break,
Continue
}
impl TclResult {
pub fn from_ll(result: i32, interpreter: &Interpreter) -> TclResult {
match result {
TCL_OK => TclResult::Ok,
TCL_ERROR => TclResult::Error(interpreter.string_result().to_string()),
TCL_RETURN => TclResult::Return,
TCL_BREAK => TclResult::Break,
TCL_CONTINUE => TclResult::Continue,
_ => TclResult::Error("Unknown result".to_string())
}
}
pub fn is_ok(&self) -> bool {
match *self {
TclResult::Ok => true,
_ => false
}
}
}