Struct dyon::Module

source ·
pub struct Module { /* private fields */ }
Expand description

Stores functions for a Dyon module.

Implementations§

source§

impl Module

source

pub fn empty() -> Module

Creates a new empty module.

source

pub fn import_ext_prelude(&mut self, other: &Module)

Import external prelude from other module.

source

pub fn import(&mut self, other: &Module)

Import external prelude and loaded functions from module.

Examples found in repository?
examples/dyon.rs (line 150)
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
fn main() {
    let mut file: Option<String> = None;
    let mut imports: Vec<Module> = vec![];

    let mut module = Module::new();
    let mut ctx = String::new();

    println!("=== Dyon 0.49 ===");
    println!("Type `help` for more information.");
    loop {
        if let Some(x) = file.as_ref() {
            print!("({}) ", x);
        }
        print!("> ");
        let _ = std::io::stdout().flush();
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).expect("Expected input");

        let command = input.trim().to_string();

        if command == "\\" {
            input = String::new();
            let mut empty_lines = 0;
            loop {
                let mut line = String::new();
                std::io::stdin().read_line(&mut line).expect("Expected input");
                if line.trim() == "" {
                    empty_lines += 1;
                    if empty_lines >= 2 {break}
                } else {
                    if empty_lines > 0 {input.push_str("\n")}
                    empty_lines = 0;
                    input.push_str(&line);
                }
            }
        }

        match &*command {
            "bye" => break,
            "ctx" => {
                println!("{}", ctx);
                continue;
            }
            "clear" => {
                ctx = String::new();
                module = Module::new();
                file = None;
                continue;
            }
            "help" => {
                println!("{}", include_str!("../assets/repl/help.txt"));
                continue;
            }
            "save" => {
                if let Some(x) = file.as_ref() {
                    if std::fs::write(x, &ctx).is_err() {
                        println!("Saving failed.");
                    }
                } else {
                    println!("Could not save.\nUse `save \"<file>\"`.");
                }
                continue;
            }
            _ if command.starts_with("save ") => {
                let new_file = if let Some(x) = json_str(&command[5..]) {x}
                               else {
                                    println!("Saving failed.");
                                    continue;
                               };
                if std::fs::write(&new_file, &ctx).is_err() {
                    println!("Saving failed.");
                    continue;
                }
                file = Some(new_file);
                continue;
            }
            _ if command.starts_with("load ") => {
                println!("Loading...");

                let new_file = if let Some(x) = json_str(&command[5..]) {x}
                           else {
                                println!("Loading failed.");
                                continue;
                           };
                println!("  {}", new_file);

                ctx = match std::fs::read_to_string(&new_file) {
                    Ok(x) => x,
                    Err(_) => {
                        println!("Could not load {}", new_file);
                        continue;
                    }
                };
                file = Some(new_file);
                if reload(&ctx, &mut module, &imports) {
                    continue;
                }

                println!("Done!");
                continue;
            }
            _ if command.starts_with("import ") => {
                println!("Importing...");

                let file = if let Some(x) = json_str(&command[7..]) {x}
                           else {
                                println!("Import failed.");
                                continue;
                           };
                println!("  {}", file);
                let mut m = Module::new();
                if error(load(&file, &mut m)) {
                    continue;
                }
                imports.push(m);

                if reload(&ctx, &mut module, &imports) {
                    continue;
                }

                println!("Done!");
                continue;
            }
            _ if command.starts_with("call ") => {
                let f = if let Some(x) = json_str(&command[5..]) {x}
                        else {
                            println!("Could not call function.");
                            continue;
                        };
                input = format!("{{\n{}()\nreturn\n}}\n", f);
            }
            "" => {
                // Print separator for readability.
                print!("\n------------------------------------<o=o");
                println!("o=o>------------------------------------\n");
                continue;
            }
            _ => {}
        }

        let mut sub_module = Module::new();
        sub_module.import(&module);
        let res = load_str("dyonrepl", Arc::new(input.clone()), &mut sub_module); 
        if res.is_err() {
            // Try evaluation expression.
            let code = format!("fn main() {{println({})}}", input);
            if error(run_str_with_module("dyonrepl", Arc::new(code), &module)) {
                error(res);
            }
        } else {
            println!("{}", input);

            ctx.push_str(&input);
            if reload(&ctx, &mut module, &imports) {
                continue;
            }
        }
    }
}

fn reload(ctx: &str, module: &mut Module, imports: &[Module]) -> bool {
    let mut new_module = Module::new();
    for m in imports {new_module.import(m)};
    if !error(load_str("dyonrep", Arc::new(ctx.into()), &mut new_module)) {
        *module = new_module;
        false
    } else {true}
}

fn run_str_with_module(
    source: &str,
    d: Arc<String>,
    module: &Module,
) -> Result<(), String> {
    let mut m = Module::new();
    m.import(module);
    load_str(source, d, &mut m)?;
    let mut runtime = Runtime::new();
    runtime.run(&Arc::new(m))?;
    Ok(())
}
source

pub fn new() -> Module

Creates a new module with standard library.

Examples found in repository?
examples/functions.rs (line 21)
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
fn load_module() -> Option<dyon::Module> {
    use dyon::Type::*;
    use dyon::{error, load, Dfn, Module};

    let mut module = Module::new();
    module.add_str("say_hello", say_hello, Dfn::nl(vec![], Void));
    module.add_str("homer", homer, Dfn::nl(vec![], Any));
    module.add_str("age", age, Dfn::nl(vec![Any], Any));
    module.add_str("mr", mr, Dfn::nl(vec![Str; 2], Str));
    module.add_str("origo", origo, Dfn::nl(vec![], Object));
    module.add_str("id", id, Dfn::nl(vec![], Mat4));

    // Register custom Rust object with an ad-hoc type.
    let ty_custom_object = AdHoc(Arc::new("CustomObject".into()), Box::new(Any));
    module.add_str(
        "custom_object",
        custom_object,
        Dfn::nl(vec![], ty_custom_object.clone()),
    );
    module.add_str(
        "print_custom_object",
        print_custom_object,
        Dfn::nl(vec![ty_custom_object.clone()], Void),
    );
    if error(load("source/functions/loader.dyon", &mut module)) {
        None
    } else {
        Some(module)
    }
}
More examples
Hide additional examples
examples/call.rs (line 8)
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
fn main() {
    let mut module = Module::new();

    // Add functions to read `a` and `b` from `RustArgs`.
    module.add(
        Arc::new("a_of".into()),
        a_of,
        Dfn::nl(vec![Type::Any], Type::F64),
    );
    module.add(
        Arc::new("b_of".into()),
        b_of,
        Dfn::nl(vec![Type::Any], Type::F64),
    );

    error(load_str(
        "main.dyon",
        Arc::new(
            r#"
        fn add_args(a: f64, b: f64) {
            println("add_args:")
            println(link {a" + "b" = "a + b})
        }

        fn add_obj(obj: {}) {
            println("add_obj:")
            println(link {obj.a" + "obj.b" = "obj.a + obj.b})
        }

        fn add_rust(obj: any) {
            println("add_rust")
            a := a_of(obj)
            b := b_of(obj)
            println(link {a" + "b" = "a + b})
        }

        add(a, b) = a + b

        create_vec(a, b) = (a, b)

        id(obj) = clone(obj)
    "#
            .into(),
        ),
        &mut module,
    ));
    let ref module = Arc::new(module);

    let a = 20.0;
    let b = 30.0;

    // Call with multiple arguments.
    let call = Call::new("add_args").arg(a).arg(b);
    error(call.run(&mut Runtime::new(), module));

    // Call with object.
    let call = Call::new("add_obj").arg(Args { a, b });
    error(call.run(&mut Runtime::new(), module));

    // Call with rust object.
    let call = Call::new("add_rust").rust(RustArgs { a, b });
    error(call.run(&mut Runtime::new(), module));

    // Call function with return value.
    let call = Call::new("add").arg(a).arg(b);
    match call.run_ret::<f64>(&mut Runtime::new(), module) {
        Ok(answer) => {
            println!("{}", answer);
        }
        Err(err) => {
            error(Err(err));
        }
    }

    // Call function that returns vec4.
    let call = Call::new("create_vec").arg(a).arg(b);
    match call.run_vec4::<[f64; 2]>(&mut Runtime::new(), module) {
        Ok(answer) => {
            println!("{:?}", answer);
        }
        Err(err) => {
            error(Err(err));
        }
    }

    // Call function that returns Rust object.
    let call = Call::new("id").rust(RustArgs { a, b });
    match call.run_ret::<RustObject>(&mut Runtime::new(), module) {
        Ok(answer) => {
            println!("{:?}", answer.lock().unwrap().downcast_ref::<RustArgs>());
        }
        Err(err) => {
            error(Err(err));
        }
    }
}
examples/dyon.rs (line 13)
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
fn main() {
    let mut file: Option<String> = None;
    let mut imports: Vec<Module> = vec![];

    let mut module = Module::new();
    let mut ctx = String::new();

    println!("=== Dyon 0.49 ===");
    println!("Type `help` for more information.");
    loop {
        if let Some(x) = file.as_ref() {
            print!("({}) ", x);
        }
        print!("> ");
        let _ = std::io::stdout().flush();
        let mut input = String::new();
        std::io::stdin().read_line(&mut input).expect("Expected input");

        let command = input.trim().to_string();

        if command == "\\" {
            input = String::new();
            let mut empty_lines = 0;
            loop {
                let mut line = String::new();
                std::io::stdin().read_line(&mut line).expect("Expected input");
                if line.trim() == "" {
                    empty_lines += 1;
                    if empty_lines >= 2 {break}
                } else {
                    if empty_lines > 0 {input.push_str("\n")}
                    empty_lines = 0;
                    input.push_str(&line);
                }
            }
        }

        match &*command {
            "bye" => break,
            "ctx" => {
                println!("{}", ctx);
                continue;
            }
            "clear" => {
                ctx = String::new();
                module = Module::new();
                file = None;
                continue;
            }
            "help" => {
                println!("{}", include_str!("../assets/repl/help.txt"));
                continue;
            }
            "save" => {
                if let Some(x) = file.as_ref() {
                    if std::fs::write(x, &ctx).is_err() {
                        println!("Saving failed.");
                    }
                } else {
                    println!("Could not save.\nUse `save \"<file>\"`.");
                }
                continue;
            }
            _ if command.starts_with("save ") => {
                let new_file = if let Some(x) = json_str(&command[5..]) {x}
                               else {
                                    println!("Saving failed.");
                                    continue;
                               };
                if std::fs::write(&new_file, &ctx).is_err() {
                    println!("Saving failed.");
                    continue;
                }
                file = Some(new_file);
                continue;
            }
            _ if command.starts_with("load ") => {
                println!("Loading...");

                let new_file = if let Some(x) = json_str(&command[5..]) {x}
                           else {
                                println!("Loading failed.");
                                continue;
                           };
                println!("  {}", new_file);

                ctx = match std::fs::read_to_string(&new_file) {
                    Ok(x) => x,
                    Err(_) => {
                        println!("Could not load {}", new_file);
                        continue;
                    }
                };
                file = Some(new_file);
                if reload(&ctx, &mut module, &imports) {
                    continue;
                }

                println!("Done!");
                continue;
            }
            _ if command.starts_with("import ") => {
                println!("Importing...");

                let file = if let Some(x) = json_str(&command[7..]) {x}
                           else {
                                println!("Import failed.");
                                continue;
                           };
                println!("  {}", file);
                let mut m = Module::new();
                if error(load(&file, &mut m)) {
                    continue;
                }
                imports.push(m);

                if reload(&ctx, &mut module, &imports) {
                    continue;
                }

                println!("Done!");
                continue;
            }
            _ if command.starts_with("call ") => {
                let f = if let Some(x) = json_str(&command[5..]) {x}
                        else {
                            println!("Could not call function.");
                            continue;
                        };
                input = format!("{{\n{}()\nreturn\n}}\n", f);
            }
            "" => {
                // Print separator for readability.
                print!("\n------------------------------------<o=o");
                println!("o=o>------------------------------------\n");
                continue;
            }
            _ => {}
        }

        let mut sub_module = Module::new();
        sub_module.import(&module);
        let res = load_str("dyonrepl", Arc::new(input.clone()), &mut sub_module); 
        if res.is_err() {
            // Try evaluation expression.
            let code = format!("fn main() {{println({})}}", input);
            if error(run_str_with_module("dyonrepl", Arc::new(code), &module)) {
                error(res);
            }
        } else {
            println!("{}", input);

            ctx.push_str(&input);
            if reload(&ctx, &mut module, &imports) {
                continue;
            }
        }
    }
}

fn reload(ctx: &str, module: &mut Module, imports: &[Module]) -> bool {
    let mut new_module = Module::new();
    for m in imports {new_module.import(m)};
    if !error(load_str("dyonrep", Arc::new(ctx.into()), &mut new_module)) {
        *module = new_module;
        false
    } else {true}
}

fn run_str_with_module(
    source: &str,
    d: Arc<String>,
    module: &Module,
) -> Result<(), String> {
    let mut m = Module::new();
    m.import(module);
    load_str(source, d, &mut m)?;
    let mut runtime = Runtime::new();
    runtime.run(&Arc::new(m))?;
    Ok(())
}
source

pub fn ns(&mut self, ns: &str)

Sets namespace for following added functions.

source

pub fn no_ns(&mut self)

Sets no namespace.

source

pub fn find_function(&self, name: &Arc<String>, relative: usize) -> FnIndex

Find function relative another function index.

source

pub fn add<T>( &mut self, name: Arc<String>, f: fn(_: &mut Runtime) -> T, prelude_function: Dfn )where fn(_: &mut Runtime) -> T: Into<FnExt>,

Adds a new external prelude function.

Examples found in repository?
examples/call.rs (lines 11-15)
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
fn main() {
    let mut module = Module::new();

    // Add functions to read `a` and `b` from `RustArgs`.
    module.add(
        Arc::new("a_of".into()),
        a_of,
        Dfn::nl(vec![Type::Any], Type::F64),
    );
    module.add(
        Arc::new("b_of".into()),
        b_of,
        Dfn::nl(vec![Type::Any], Type::F64),
    );

    error(load_str(
        "main.dyon",
        Arc::new(
            r#"
        fn add_args(a: f64, b: f64) {
            println("add_args:")
            println(link {a" + "b" = "a + b})
        }

        fn add_obj(obj: {}) {
            println("add_obj:")
            println(link {obj.a" + "obj.b" = "obj.a + obj.b})
        }

        fn add_rust(obj: any) {
            println("add_rust")
            a := a_of(obj)
            b := b_of(obj)
            println(link {a" + "b" = "a + b})
        }

        add(a, b) = a + b

        create_vec(a, b) = (a, b)

        id(obj) = clone(obj)
    "#
            .into(),
        ),
        &mut module,
    ));
    let ref module = Arc::new(module);

    let a = 20.0;
    let b = 30.0;

    // Call with multiple arguments.
    let call = Call::new("add_args").arg(a).arg(b);
    error(call.run(&mut Runtime::new(), module));

    // Call with object.
    let call = Call::new("add_obj").arg(Args { a, b });
    error(call.run(&mut Runtime::new(), module));

    // Call with rust object.
    let call = Call::new("add_rust").rust(RustArgs { a, b });
    error(call.run(&mut Runtime::new(), module));

    // Call function with return value.
    let call = Call::new("add").arg(a).arg(b);
    match call.run_ret::<f64>(&mut Runtime::new(), module) {
        Ok(answer) => {
            println!("{}", answer);
        }
        Err(err) => {
            error(Err(err));
        }
    }

    // Call function that returns vec4.
    let call = Call::new("create_vec").arg(a).arg(b);
    match call.run_vec4::<[f64; 2]>(&mut Runtime::new(), module) {
        Ok(answer) => {
            println!("{:?}", answer);
        }
        Err(err) => {
            error(Err(err));
        }
    }

    // Call function that returns Rust object.
    let call = Call::new("id").rust(RustArgs { a, b });
    match call.run_ret::<RustObject>(&mut Runtime::new(), module) {
        Ok(answer) => {
            println!("{:?}", answer.lock().unwrap().downcast_ref::<RustArgs>());
        }
        Err(err) => {
            error(Err(err));
        }
    }
}
source

pub fn add_str<T>( &mut self, name: &str, f: fn(_: &mut Runtime) -> T, prelude_function: Dfn )where fn(_: &mut Runtime) -> T: Into<FnExt>,

Adds a new external prelude function.

Examples found in repository?
examples/functions.rs (line 22)
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
fn load_module() -> Option<dyon::Module> {
    use dyon::Type::*;
    use dyon::{error, load, Dfn, Module};

    let mut module = Module::new();
    module.add_str("say_hello", say_hello, Dfn::nl(vec![], Void));
    module.add_str("homer", homer, Dfn::nl(vec![], Any));
    module.add_str("age", age, Dfn::nl(vec![Any], Any));
    module.add_str("mr", mr, Dfn::nl(vec![Str; 2], Str));
    module.add_str("origo", origo, Dfn::nl(vec![], Object));
    module.add_str("id", id, Dfn::nl(vec![], Mat4));

    // Register custom Rust object with an ad-hoc type.
    let ty_custom_object = AdHoc(Arc::new("CustomObject".into()), Box::new(Any));
    module.add_str(
        "custom_object",
        custom_object,
        Dfn::nl(vec![], ty_custom_object.clone()),
    );
    module.add_str(
        "print_custom_object",
        print_custom_object,
        Dfn::nl(vec![ty_custom_object.clone()], Void),
    );
    if error(load("source/functions/loader.dyon", &mut module)) {
        None
    } else {
        Some(module)
    }
}
source

pub fn add_binop( &mut self, name: Arc<String>, f: fn(_: &Variable, _: &Variable) -> Result<Variable, String>, prelude_function: Dfn )

Adds a new external prelude binary operator.

source

pub fn add_unop( &mut self, name: Arc<String>, f: fn(_: &Variable) -> Result<Variable, String>, prelude_function: Dfn )

Adds a new external prelude unary operator.

source

pub fn add_unop_str( &mut self, name: &str, f: fn(_: &Variable) -> Result<Variable, String>, prelude_function: Dfn )

Adds a new external prelude unary operator.

Trait Implementations§

source§

impl Clone for Module

source§

fn clone(&self) -> Module

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Default for Module

source§

fn default() -> Module

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Module

§

impl Send for Module

§

impl Sync for Module

§

impl Unpin for Module

§

impl !UnwindSafe for Module

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Err = <U as TryFrom<T>>::Err

§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Err>