Skip to content

Break continue fix #500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ fn _run_string(vm: &mut VirtualMachine, source: &str, source_path: String) -> Py
)
.map_err(|err| {
let syntax_error = vm.context().exceptions.syntax_error.clone();
vm.new_exception(syntax_error, err.description().to_string())
vm.new_exception(syntax_error, err.to_string())
})?;
// trace!("Code object: {:?}", code_obj.borrow());
let builtins = vm.get_builtin_scope();
Expand Down
6 changes: 3 additions & 3 deletions vm/src/builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ fn builtin_compile(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {

compile::compile(&source, &mode, filename, vm.ctx.code_type()).map_err(|err| {
let syntax_error = vm.context().exceptions.syntax_error.clone();
vm.new_exception(syntax_error, err.description().to_string())
vm.new_exception(syntax_error, err.to_string())
})
}

Expand Down Expand Up @@ -206,7 +206,7 @@ fn builtin_eval(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
compile::compile(&source, &mode, "<string>".to_string(), vm.ctx.code_type()).map_err(
|err| {
let syntax_error = vm.context().exceptions.syntax_error.clone();
vm.new_exception(syntax_error, err.description().to_string())
vm.new_exception(syntax_error, err.to_string())
},
)?
} else {
Expand Down Expand Up @@ -241,7 +241,7 @@ fn builtin_exec(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
compile::compile(&source, &mode, "<string>".to_string(), vm.ctx.code_type()).map_err(
|err| {
let syntax_error = vm.context().exceptions.syntax_error.clone();
vm.new_exception(syntax_error, err.description().to_string())
vm.new_exception(syntax_error, err.to_string())
},
)?
} else if objtype::isinstance(source, &vm.ctx.code_type()) {
Expand Down
15 changes: 15 additions & 0 deletions vm/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ struct Compiler {
nxt_label: usize,
source_path: Option<String>,
current_source_location: ast::Location,
in_loop: bool,
}

/// Compile a given sourcecode into a bytecode object.
Expand Down Expand Up @@ -73,6 +74,7 @@ impl Compiler {
nxt_label: 0,
source_path: None,
current_source_location: ast::Location::default(),
in_loop: false,
}
}

Expand Down Expand Up @@ -229,7 +231,10 @@ impl Compiler {
self.set_label(start_label);

self.compile_test(test, None, Some(else_label), EvalContext::Statement)?;

self.in_loop = true;
self.compile_statements(body)?;
self.in_loop = false;
self.emit(Instruction::Jump {
target: start_label,
});
Expand Down Expand Up @@ -290,7 +295,10 @@ impl Compiler {
self.compile_store(target)?;

// Body of loop:
self.in_loop = true;
self.compile_statements(body)?;
self.in_loop = false;

self.emit(Instruction::Jump {
target: start_label,
});
Expand Down Expand Up @@ -563,9 +571,15 @@ impl Compiler {
self.set_label(end_label);
}
ast::Statement::Break => {
if !self.in_loop {
return Err(CompileError::SyntaxErr(String::from("break")));
}
self.emit(Instruction::Break);
}
ast::Statement::Continue => {
if !self.in_loop {
return Err(CompileError::SyntaxErr(String::from("continue")));
}
self.emit(Instruction::Continue);
}
ast::Statement::Return { value } => {
Expand Down Expand Up @@ -648,6 +662,7 @@ impl Compiler {
name: &str,
args: &ast::Parameters,
) -> Result<bytecode::FunctionOpArg, CompileError> {
self.in_loop = false;
let have_kwargs = !args.defaults.is_empty();
if have_kwargs {
// Construct a tuple:
Expand Down
3 changes: 3 additions & 0 deletions vm/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub enum CompileError {
Parse(ParseError),
// Multiple `*` detected
StarArgs,
// Catches errors undetectable by the parser
SyntaxErr(String),
}

impl fmt::Display for CompileError {
Expand All @@ -25,6 +27,7 @@ impl fmt::Display for CompileError {
CompileError::ExpectExpr => write!(f, "Expecting expression, got statement"),
CompileError::Parse(err) => err.fmt(f),
CompileError::StarArgs => write!(f, "Two starred expressions in assignment"),
CompileError::SyntaxErr(expr) => write!(f, "Syntax Error: '{}' ouside loop", expr),
}
}
}
Expand Down