PistonDevelopers / dyon

A rusty dynamically typed scripting language
Apache License 2.0
1.78k stars 55 forks source link

Execute single expressions #241

Open bvssvni opened 8 years ago

bvssvni commented 8 years ago

This was requested on reddit https://www.reddit.com/r/rust_gamedev/comments/4lk6la/dyon_gets_adhoc_types/d3owjsq

Izzeri commented 8 years ago

What I had in mind was something akin to stack based implementations of other scripting languages, where you can execute expressions one at a time. Something like:

dyon::execute("let a = 10");
let result = dyon::execute("10 + a");

I don't know if this makes sense for Dyon, but it would be really nice for doing stuff like damage formulas, e.g.:

pub struct Battler {
    pub hp: i32,
    pub dmg: i32,
    pub def: i32,
}

dyon_obj!{ Battler { hp, dmg, def } }

let hero = Battler { hp: 10, atk: 10, def: 4 }
let enemy = Battler { hp: 5, atk: 3, def: 2 }
dyon::set_var("attacker", hero);
dyon::set_var("defender", enemy);

enemy.hp -= dyon::execute("attacker.atk - defender.def").ok();
bvssvni commented 8 years ago

I am unsure about the best approach for this. Some lifetime/type checking is required, but this runs currently on whole modules. The run-time is also designed to run whole programs.