Add a few more conversion methods to Int, Float, Str
Signed-off-by: Alek Ratzloff <alekratz@gmail.com>
This commit is contained in:
@@ -221,6 +221,32 @@ impl StrInst {
|
|||||||
StrInst::create(format!("'{}'", escaped)).into()
|
StrInst::create(format!("'{}'", escaped)).into()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn to_int(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
|
||||||
|
let parsed: Result<i64, _> =
|
||||||
|
with_obj_downcast(vm.frame_stack()[0].clone(), |str_inst: &StrInst| {
|
||||||
|
str_inst.str_value().parse()
|
||||||
|
});
|
||||||
|
match parsed {
|
||||||
|
Ok(int) => IntInst::create(int).into(),
|
||||||
|
// TODO StrInst::to_int - throw an exception when we fail to parse an integer
|
||||||
|
// BLOCKED-ON - exceptions
|
||||||
|
Err(e) => todo!("error parsing string to an integer: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn to_float(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
|
||||||
|
let parsed: Result<f64, _> =
|
||||||
|
with_obj_downcast(vm.frame_stack()[0].clone(), |str_inst: &StrInst| {
|
||||||
|
str_inst.str_value().parse()
|
||||||
|
});
|
||||||
|
match parsed {
|
||||||
|
Ok(float) => FloatInst::create(float).into(),
|
||||||
|
// TODO StrInst::to_int - throw an exception when we fail to parse an integer
|
||||||
|
// BLOCKED-ON - exceptions
|
||||||
|
Err(e) => todo!("error parsing string to a float: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub(crate) fn len(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
|
pub(crate) fn len(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
|
||||||
let len = with_obj_downcast(vm.frame_stack()[0].clone(), |str_inst: &StrInst| {
|
let len = with_obj_downcast(vm.frame_stack()[0].clone(), |str_inst: &StrInst| {
|
||||||
str_inst.str_value().len() as i64
|
str_inst.str_value().len() as i64
|
||||||
@@ -318,6 +344,15 @@ macro_rules! int_bin_op_logical {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl IntInst {
|
impl IntInst {
|
||||||
|
pub(crate) fn to_int(_vm: &mut Vm, _state: FunctionState) -> FunctionResult {
|
||||||
|
FunctionResult::Return
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn to_float(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
|
||||||
|
let int_value = with_obj_downcast(vm.frame_stack()[0].clone(), IntInst::int_value);
|
||||||
|
FloatInst::create(int_value as f64).into()
|
||||||
|
}
|
||||||
|
|
||||||
int_bin_op_math!(add, +);
|
int_bin_op_math!(add, +);
|
||||||
int_bin_op_math!(sub, -);
|
int_bin_op_math!(sub, -);
|
||||||
|
|
||||||
@@ -428,6 +463,15 @@ macro_rules! float_bin_op_logical {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl FloatInst {
|
impl FloatInst {
|
||||||
|
pub(crate) fn to_int(vm: &mut Vm, _state: FunctionState) -> FunctionResult {
|
||||||
|
let float_value = with_obj_downcast(vm.frame_stack()[0].clone(), FloatInst::float_value);
|
||||||
|
IntInst::create(float_value as i64).into()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub(crate) fn to_float(_vm: &mut Vm, _state: FunctionState) -> FunctionResult {
|
||||||
|
FunctionResult::Return
|
||||||
|
}
|
||||||
|
|
||||||
float_bin_op_math!(add, +);
|
float_bin_op_math!(add, +);
|
||||||
float_bin_op_math!(sub, -);
|
float_bin_op_math!(sub, -);
|
||||||
float_bin_op_math!(mul, *);
|
float_bin_op_math!(mul, *);
|
||||||
|
|||||||
19
src/obj.rs
19
src/obj.rs
@@ -118,11 +118,14 @@ pub fn init_types() {
|
|||||||
base_type: Type,
|
base_type: Type,
|
||||||
// type definitions
|
// type definitions
|
||||||
Type {
|
Type {
|
||||||
// Method conversion
|
// Conversion methods
|
||||||
to_str => BuiltinFunctionInst::create("to_str", BaseObjInst::to_str, 1),
|
to_str => BuiltinFunctionInst::create("to_str", BaseObjInst::to_str, 1),
|
||||||
to_repr => BuiltinFunctionInst::create("to_repr", BaseObjInst::to_repr, 1),
|
to_repr => BuiltinFunctionInst::create("to_repr", BaseObjInst::to_repr, 1),
|
||||||
to_bool => BuiltinFunctionInst::create("to_bool", BaseObjInst::to_bool, 1),
|
to_bool => BuiltinFunctionInst::create("to_bool", BaseObjInst::to_bool, 1),
|
||||||
|
to_int => BuiltinFunctionInst::create("to_int", BaseObjInst::not_implemented_un, 1),
|
||||||
|
to_float => BuiltinFunctionInst::create("to_float", BaseObjInst::not_implemented_un, 1),
|
||||||
len => BuiltinFunctionInst::create("len", BaseObjInst::not_implemented_un, 1),
|
len => BuiltinFunctionInst::create("len", BaseObjInst::not_implemented_un, 1),
|
||||||
|
|
||||||
// Operators
|
// Operators
|
||||||
__add__ => BuiltinFunctionInst::create("__add__", BaseObjInst::not_implemented_bin, 2),
|
__add__ => BuiltinFunctionInst::create("__add__", BaseObjInst::not_implemented_bin, 2),
|
||||||
__sub__ => BuiltinFunctionInst::create("__sub__", BaseObjInst::not_implemented_bin, 2),
|
__sub__ => BuiltinFunctionInst::create("__sub__", BaseObjInst::not_implemented_bin, 2),
|
||||||
@@ -142,15 +145,23 @@ pub fn init_types() {
|
|||||||
},
|
},
|
||||||
Obj { },
|
Obj { },
|
||||||
Str {
|
Str {
|
||||||
|
// Conversion methods
|
||||||
to_str => BuiltinFunctionInst::create("to_str", StrInst::to_str, 1),
|
to_str => BuiltinFunctionInst::create("to_str", StrInst::to_str, 1),
|
||||||
to_repr => BuiltinFunctionInst::create("to_repr", StrInst::to_repr, 1),
|
to_repr => BuiltinFunctionInst::create("to_repr", StrInst::to_repr, 1),
|
||||||
|
to_int => BuiltinFunctionInst::create("to_int", StrInst::to_int, 1),
|
||||||
|
to_float => BuiltinFunctionInst::create("to_float", StrInst::to_float, 1),
|
||||||
len => BuiltinFunctionInst::create("len", StrInst::len, 1),
|
len => BuiltinFunctionInst::create("len", StrInst::len, 1),
|
||||||
|
|
||||||
// Operators
|
// Operators
|
||||||
__add__ => BuiltinFunctionInst::create("__add__", StrInst::add, 2),
|
__add__ => BuiltinFunctionInst::create("__add__", StrInst::add, 2),
|
||||||
__mul__ => BuiltinFunctionInst::create("__mul__", StrInst::mul, 2),
|
__mul__ => BuiltinFunctionInst::create("__mul__", StrInst::mul, 2),
|
||||||
// .lower, .upper, .slice, etc
|
// .lower, .upper, .slice, etc
|
||||||
},
|
},
|
||||||
Int {
|
Int {
|
||||||
|
// Conversion methods
|
||||||
|
to_int => BuiltinFunctionInst::create("to_int", IntInst::to_int, 1),
|
||||||
|
to_float => BuiltinFunctionInst::create("to_float", IntInst::to_float, 1),
|
||||||
|
|
||||||
// Operators
|
// Operators
|
||||||
__add__ => BuiltinFunctionInst::create("__add__", IntInst::add, 2),
|
__add__ => BuiltinFunctionInst::create("__add__", IntInst::add, 2),
|
||||||
__sub__ => BuiltinFunctionInst::create("__sub__", IntInst::sub, 2),
|
__sub__ => BuiltinFunctionInst::create("__sub__", IntInst::sub, 2),
|
||||||
@@ -165,11 +176,15 @@ pub fn init_types() {
|
|||||||
__neg__ => BuiltinFunctionInst::create("__neg__", IntInst::neg, 1),
|
__neg__ => BuiltinFunctionInst::create("__neg__", IntInst::neg, 1),
|
||||||
},
|
},
|
||||||
Float {
|
Float {
|
||||||
|
// Conversion methods
|
||||||
|
to_int => BuiltinFunctionInst::create("to_int", FloatInst::to_int, 1),
|
||||||
|
to_float => BuiltinFunctionInst::create("to_float", FloatInst::to_float, 1),
|
||||||
|
|
||||||
|
// Operators
|
||||||
__add__ => BuiltinFunctionInst::create("__add__", FloatInst::add, 2),
|
__add__ => BuiltinFunctionInst::create("__add__", FloatInst::add, 2),
|
||||||
__sub__ => BuiltinFunctionInst::create("__sub__", FloatInst::sub, 2),
|
__sub__ => BuiltinFunctionInst::create("__sub__", FloatInst::sub, 2),
|
||||||
__mul__ => BuiltinFunctionInst::create("__mul__", FloatInst::mul, 2),
|
__mul__ => BuiltinFunctionInst::create("__mul__", FloatInst::mul, 2),
|
||||||
__div__ => BuiltinFunctionInst::create("__div__", FloatInst::div, 2),
|
__div__ => BuiltinFunctionInst::create("__div__", FloatInst::div, 2),
|
||||||
|
|
||||||
__gt__ => BuiltinFunctionInst::create("__gt__", FloatInst::gt, 2),
|
__gt__ => BuiltinFunctionInst::create("__gt__", FloatInst::gt, 2),
|
||||||
__ge__ => BuiltinFunctionInst::create("__ge__", FloatInst::ge, 2),
|
__ge__ => BuiltinFunctionInst::create("__ge__", FloatInst::ge, 2),
|
||||||
__lt__ => BuiltinFunctionInst::create("__lt__", FloatInst::lt, 2),
|
__lt__ => BuiltinFunctionInst::create("__lt__", FloatInst::lt, 2),
|
||||||
|
|||||||
Reference in New Issue
Block a user