Skip to content

Commit

Permalink
set_program
Browse files Browse the repository at this point in the history
  • Loading branch information
mikebenfield committed Dec 5, 2024
1 parent d0c2d52 commit dd3dce8
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 10 deletions.
19 changes: 15 additions & 4 deletions interpreter/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ pub struct Cursor<'a> {
pub block_height: u32,

pub really_async: bool,

pub program: Option<Symbol>,
}

impl<'a> Cursor<'a> {
Expand All @@ -282,9 +284,18 @@ impl<'a> Cursor<'a> {
signer,
block_height,
really_async,
program: None,
}
}

pub fn set_program(&mut self, program: &str) {
self.program = Some(Symbol::intern(program));
}

pub fn current_program(&self) -> Option<Symbol> {
self.contexts.current_program().or(self.program)
}

pub fn increment_step(&mut self) {
let Some(Frame { step, .. }) = self.frames.last_mut() else {
panic!("frame expected");
Expand Down Expand Up @@ -741,9 +752,9 @@ impl<'a> Cursor<'a> {
let len = self.values.len();
let (program, name) = match &*call.function {
Expression::Identifier(id) => {
let program = call.program.unwrap_or_else(|| {
self.contexts.current_program().expect("there should be a current program")
});
let program = call
.program
.unwrap_or_else(|| self.current_program().expect("there should be a current program"));
(program, id.name)
}
Expression::Locator(locator) => (locator.program.name.name, locator.name),
Expand Down Expand Up @@ -818,7 +829,7 @@ impl<'a> Cursor<'a> {
}

// And now put them into an IndexMap in the correct order.
let program = self.contexts.current_program().expect("there should be a current program");
let program = self.current_program().expect("there should be a current program");
let id = GlobalId { program, name: struct_.name.name };
let struct_type = self.structs.get(&id).expect_tc(struct_.span())?;
let contents = struct_type
Expand Down
18 changes: 13 additions & 5 deletions interpreter/src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,22 @@ impl Interpreter {
/// Returns true if any watchpoints changed.
pub fn update_watchpoints(&mut self) -> Result<bool> {
let mut changed = false;
let safe_cursor = self.cursor.clone();

for i in 0..self.watchpoints.len() {
let code = self.watchpoints[i].code.clone();
if let Some(ret) = self.action(InterpreterAction::LeoInterpretOver(code))? {
let new_value = Some(ret.to_string());
if self.watchpoints[i].last_result != new_value {
changed = true;
self.watchpoints[i].last_result = new_value;
let new_value = match self.action(InterpreterAction::LeoInterpretOver(code)) {
Ok(None) => None,
Ok(Some(ret)) => Some(ret.to_string()),
Err(LeoError::InterpreterHalt(halt)) => {
self.cursor = safe_cursor.clone();
Some(halt.to_string())
}
Err(e) => return Err(e),
};
if self.watchpoints[i].last_result != new_value {
changed = true;
self.watchpoints[i].last_result = new_value;
}
}
Ok(changed)
Expand Down
13 changes: 12 additions & 1 deletion interpreter/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ You can set a breakpoint with
When executing Aleo VM code, you can print the value of a register like this:
#print 2
You may also use one letter abbreviations for these commands, such as #i.
Some of the commands may be run with one letter abbreviations, such as #i.
Note that this interpreter is not line oriented as in many common debuggers;
rather it is oriented around expressions and statements.
Expand All @@ -83,6 +83,13 @@ If there are futures available to be executed, they will be listed by
numerical index, and you may run them using `#future` (or `#f`); for instance
#future 0
The interpreter begins in a global context, not in any Leo program. You can set
the current program with
#set_program program_name
This allows you to refer to structs and other items in the indicated program.
Input history is available - use the up and down arrow keys.
";

Expand Down Expand Up @@ -182,6 +189,10 @@ pub fn interpret(
}
}
("#w" | "#watch", rest) => InterpreterAction::Watch(rest.to_string()),
("#set_program", rest) => {
interpreter.cursor.set_program(rest);
continue;
}
("", rest) => InterpreterAction::LeoInterpretOver(rest.to_string()),
_ => {
println!("Failed to parse command {user_input}");
Expand Down

0 comments on commit dd3dce8

Please sign in to comment.