Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 96d6074

Browse files
committed
if support and more borrows
1 parent 0e9a634 commit 96d6074

File tree

12 files changed

+108
-74
lines changed

12 files changed

+108
-74
lines changed

‎benchmarker.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def benchmark_rs(test):
6868
"""
6969

7070
if __name__ == '__main__':
71-
test_file = "test_while_add.py"
71+
test_file = "test_simple_if_for.py"
7272

7373
rusty_time = benchmark_rusty(test_file)
7474
rs_time = benchmark_rs(test_file)

‎flamegraph.svg‎

Lines changed: 2 additions & 2 deletions
Loading[フレーム]

‎src/builtins/function_utils.rs‎

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub fn call_function(func: PyObject, args: &[PyObject], arena: &mut PyArena) ->
1010
match func {
1111
PyObject::Internal(inner) => {
1212
match inner {
13-
PyInternalObject::InternalFunction(func) => eval_internal_func(func, args, arena),
13+
PyInternalObject::InternalFunction(func) => eval_internal_func(&func, args, arena),
1414
PyInternalObject::InternalClass(pyclass) => eval_obj_init(pyclass, args, arena)
1515
}
1616
}
@@ -24,7 +24,7 @@ pub fn call_function(func: PyObject, args: &[PyObject], arena: &mut PyArena) ->
2424
}
2525
}
2626

27-
pub fn call_function_1_arg_min(func: PyObject, first_arg: &PyObject, args: &[PyObject], arena: &mut PyArena) -> FuncReturnType {
27+
pub fn call_function_1_arg_min(func: &PyObject, first_arg: &PyObject, args: &[PyObject], arena: &mut PyArena) -> FuncReturnType {
2828
match func {
2929
PyObject::Internal(inner) => {
3030
match inner {
@@ -42,7 +42,7 @@ pub fn call_function_1_arg_min(func: PyObject, first_arg: &PyObject, args: &[PyO
4242
}
4343
}
4444

45-
pub(crate) fn eval_internal_func(func: Rc<PyInternalFunction>, args: &[PyObject], arena: &mut PyArena) -> FuncReturnType {
45+
pub(crate) fn eval_internal_func(func: &Rc<PyInternalFunction>, args: &[PyObject], arena: &mut PyArena) -> FuncReturnType {
4646
match (func.deref(), args.len()) {
4747
(PyInternalFunction::NewFunc(func), n) => {
4848
func(arena, expect_class(&args[0]), &args[1..n]) // TODO find a way to not clone the class
@@ -66,7 +66,7 @@ pub(crate) fn eval_internal_func(func: Rc<PyInternalFunction>, args: &[PyObject]
6666
}
6767
}
6868

69-
pub(crate) fn eval_internal_func_1_arg_min(func: Rc<PyInternalFunction>, first_arg: &PyObject, args: &[PyObject], arena: &mut PyArena) -> FuncReturnType {
69+
pub(crate) fn eval_internal_func_1_arg_min(func: &Rc<PyInternalFunction>, first_arg: &PyObject, args: &[PyObject], arena: &mut PyArena) -> FuncReturnType {
7070
match (func.deref(), args.len()) {
7171
(PyInternalFunction::NewFunc(func), _n) => {
7272
func(arena, expect_class(first_arg), args)
@@ -101,8 +101,8 @@ pub(crate) fn eval_obj_init(pyclass: Rc<PyClass>, args: &[PyObject], arena: &mut
101101
panic!("{:?} has no __init__ method", pyclass)
102102
}
103103

104-
let new_func = new_func.unwrap();
105-
let init_func = init_func.unwrap();
104+
let refnew_func = new_func.unwrap();
105+
let refinit_func = init_func.unwrap();
106106

107107
let new_object = call_function_1_arg_min(new_func, &PyObject::new_internal_class(pyclass), &args, arena)?;
108108

@@ -112,8 +112,8 @@ pub(crate) fn eval_obj_init(pyclass: Rc<PyClass>, args: &[PyObject], arena: &mut
112112
}
113113

114114
pub(crate) fn init_internal_class(pyclass: Rc<PyClass>, args: &[PyObject], arena: &mut PyArena) -> FuncReturnType {
115-
let new_func = pyclass.get_magic_method_internal(&PyMagicMethod::New).unwrap();
116-
let init_func = pyclass.get_magic_method_internal(&PyMagicMethod::Init).unwrap();
115+
let refnew_func = pyclass.get_magic_method_internal(&PyMagicMethod::New).unwrap();
116+
let refinit_func = pyclass.get_magic_method_internal(&PyMagicMethod::Init).unwrap();
117117

118118
let new_object = eval_internal_func_1_arg_min(new_func, &PyObject::new_internal_class(pyclass), &args, arena)?;
119119

‎src/builtins/functions/compare.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub fn compare_op(left: &PyObject, right: &PyObject, comp: &Comparitor, arena: &
2323
fn left_hand_compare_op(op: &PyMagicMethod, left: &PyObject, right: &PyObject, arena: &mut PyArena) -> FuncReturnType {
2424
let left_compare_func = left.get_magic_method(op, arena);
2525

26-
if let Some(left_compare_func) = left_compare_func {
26+
if let Some(refleft_compare_func) = left_compare_func {
2727
let left_compare = call_function_1_arg_min(left_compare_func, left, &[right.clone()], arena);
2828

2929
return match left_compare {
@@ -42,7 +42,7 @@ fn left_hand_compare_op(op: &PyMagicMethod, left: &PyObject, right: &PyObject, a
4242

4343
fn right_hand_compare_op(op: &PyMagicMethod, left: &PyObject, right: &PyObject, arena: &mut PyArena) -> FuncReturnType {
4444
let right_op = flip_to_right_hand_op(op);
45-
let right_compare_func = right.get_magic_method(right_op, arena);
45+
let refright_compare_func = right.get_magic_method(right_op, arena);
4646

4747
if let Some(right_compare_func) = right_compare_func {
4848
let right_compare = call_function_1_arg_min(right_compare_func, right, &[left.clone()], arena);

‎src/builtins/functions/math_op.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::builtins::structure::pyobject::{FuncReturnType, PyObject};
44
use crate::pyarena::PyArena;
55

66
pub fn math_op(left: PyObject, right: PyObject, py_magic_method: PyMagicMethod, arena: &mut PyArena) -> FuncReturnType {
7-
let left_math_func = left.get_magic_method(&py_magic_method, arena);
7+
let refleft_math_func = left.get_magic_method(&py_magic_method, arena);
88

99
if let Some(left_math_func) = left_math_func {
1010
let math_result = call_function_1_arg_min(left_math_func, &left, &[right.clone()], arena);
@@ -26,7 +26,7 @@ pub fn math_op(left: PyObject, right: PyObject, py_magic_method: PyMagicMethod,
2626
fn right_hand_math_op(left: PyObject, right: PyObject, mut py_magic_method: PyMagicMethod, arena: &mut PyArena) -> FuncReturnType {
2727
py_magic_method.make_right_handed();
2828

29-
let right_math_func = right.get_magic_method(&py_magic_method, arena);
29+
let refright_math_func = right.get_magic_method(&py_magic_method, arena);
3030

3131
if let Some(right_math_fun) = right_math_func {
3232
return call_function_1_arg_min(right_math_fun, &right, &[left], arena);

‎src/builtins/types/pybool.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub fn expect_bool(pyobj: &PyObject, arena: &mut PyArena) -> Result<bool, PyExce
2222

2323

2424
fn convert_mutable_to_bool(pyobj: &PyObject, mutable_obj: &PyMutableObject, arena: &mut PyArena ) -> Result<bool, PyException> {
25-
let bool_func = mutable_obj.get_magic_method(&PyMagicMethod::Bool, arena);
25+
let refbool_func = mutable_obj.get_magic_method(&PyMagicMethod::Bool, arena);
2626

2727
if let Some(bool_func) = bool_func {
2828
let func_result = call_function_1_arg_min(bool_func, pyobj, &[], arena)?;

‎src/builtins/types/pyfloat.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ pub fn convert_mutable_to_float(pyobj: &PyObject, mutable_obj: &PyMutableObject,
3434
let float_func = mutable_obj.get_magic_method(&PyMagicMethod::Float, arena);
3535

3636
if let Some(float_func) = float_func {
37-
let func_result = call_function_1_arg_min(float_func, pyobj, &[], arena)?;
37+
let func_result = call_function_1_arg_min(&float_func, pyobj, &[], arena)?;
3838

3939
let float_result = expect_float(&func_result, arena);
4040

‎src/builtins/types/pyint.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub fn convert_mutable_to_int(pyobj: &PyObject, mutable_obj: &PyMutableObject, a
3333
let int_func = mutable_obj.get_magic_method(&PyMagicMethod::Int, arena);
3434

3535
if let Some(int_func) = int_func {
36-
let func_result = call_function_1_arg_min(int_func, pyobj, &[], arena)?;
36+
let func_result = call_function_1_arg_min(&int_func, pyobj, &[], arena)?;
3737

3838
let int_result = expect_int(&func_result, arena);
3939

‎src/builtins/types/str.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn py_str_tmp(obj: &PyObject, arena: &mut PyArena) -> FuncReturnType {
1212

1313
let str_fn = str_fn.unwrap();
1414

15-
let str_rtn = call_function_1_arg_min(str_fn, obj, &[], arena);
15+
let str_rtn = call_function_1_arg_min(&str_fn, obj, &[], arena);
1616

1717
str_rtn // TODO assert str_rtn is a string
1818
}

‎src/evaluator.rs‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use crate::builtins::structure::magic_methods::PyMagicMethod::{Add, Mul, Pow, Su
66
use crate::builtins::structure::pyexception::PyException;
77
use crate::builtins::structure::pyobject::{EmptyFuncReturnType, FuncReturnType, PyInternalObject, PyIteratorFlag, PyObject};
88
use crate::builtins::types::pybool::{convert_pyobj_to_bool};
9-
use crate::builtins::types::pyint::expect_int;
109
use crate::parser::*;
1110
use crate::pyarena::PyArena;
1211

@@ -54,7 +53,7 @@ fn eval_fun_call(func: &Box<Expr>, args: &[Expr], arena: &mut PyArena) -> FuncRe
5453
// todo!()
5554
// }
5655
PyInternalObject::InternalFunction(func) => {
57-
eval_internal_func(func.clone(), &evaluated_args[..], arena)
56+
eval_internal_func(func, &evaluated_args[..], arena)
5857
}
5958
PyInternalObject::InternalClass(pyclass) => {
6059
eval_obj_init(pyclass.clone(), &evaluated_args[..], arena)
@@ -213,15 +212,35 @@ fn eval_while(condition: &Expr, code: &CodeBlock, arena: &mut PyArena) -> CodeBl
213212
Ok(None)
214213
}
215214

215+
fn eval_if(cond: &Expr, if_code: &CodeBlock, elif_cond_code: &Vec<(Expr, CodeBlock)>, else_code: &Option<CodeBlock>, arena: &mut PyArena) -> CodeBlockReturn {
216+
if convert_pyobj_to_bool(&eval_expr(cond, arena)?, arena)? {
217+
return eval_code_block(if_code, arena);
218+
}
219+
220+
for (elif_cond, elif_code) in elif_cond_code {
221+
if convert_pyobj_to_bool(&eval_expr(elif_cond, arena)?, arena)? {
222+
return eval_code_block(elif_code, arena);
223+
}
224+
}
225+
226+
if let Some(else_code) = else_code {
227+
return eval_code_block(else_code, arena);
228+
}
229+
230+
Ok(None)
231+
}
232+
216233
fn eval_code_block(code: &CodeBlock, arena: &mut PyArena) -> CodeBlockReturn {
217234
for statement in code.statements.iter() {
218235
let mut rtn_val: Option<PyObject> = None;
219236
match statement {
220237
Statement::Expr(expr) => { eval_expr(expr, arena)?; },
221238
Statement::Defn(define) => eval_defn(define, arena)?,
239+
Statement::If(cond, if_code, elif, else_code) => rtn_val = eval_if(cond, if_code, elif, else_code, arena)?,
222240
Statement::For(iter_var, iter_exp, code) => rtn_val = eval_for(iter_var, iter_exp, code, arena)?,
223241
Statement::While(condition, code) => rtn_val = eval_while(condition, code, arena)?,
224242
Statement::Return(rtn_expr) => rtn_val = Some(eval_expr(rtn_expr, arena)?),
243+
Statement::Assert(expr1, expr2) => todo!(),
225244
Statement::Continue => rtn_val = Some(PyObject::continue_()),
226245
Statement::Break => rtn_val = Some(PyObject::break_()),
227246
};

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /