Skip to content
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

Add a demo flag on the load command #806

Merged
merged 1 commit into from
Oct 31, 2023
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
14 changes: 10 additions & 4 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@ struct LoadArgs {
/// Path to circom directory
#[clap(long, value_parser)]
circom_dir: Option<Utf8PathBuf>,

/// Flag to load the file in demo mode
#[arg(long)]
demo: bool,
}

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -149,6 +153,9 @@ struct LoadCli {

#[clap(long, value_parser)]
circom_dir: Option<Utf8PathBuf>,

#[arg(long)]
demo: bool,
}

impl LoadArgs {
Expand All @@ -166,6 +173,7 @@ impl LoadArgs {
proofs_dir: self.proofs_dir,
commits_dir: self.commits_dir,
circom_dir: self.circom_dir,
demo: self.demo,
}
}
}
Expand Down Expand Up @@ -304,7 +312,7 @@ impl ReplCli {
( $rc: expr, $limit: expr, $field: path, $backend: expr ) => {{
let mut repl = new_repl!(self, $rc, $limit, $field, $backend);
if let Some(lurk_file) = &self.load {
repl.load_file(lurk_file)?;
repl.load_file(lurk_file, false)?;
}
repl.start()
}};
Expand Down Expand Up @@ -344,7 +352,6 @@ impl ReplCli {
backend.validate_field(field)?;
match field {
LanguageField::Pallas => repl!(rc, limit, pallas::Scalar, backend.clone()),
// LanguageField::Vesta => repl!(rc, limit, vesta::Scalar, backend),
LanguageField::Vesta => todo!(),
LanguageField::BLS12_381 => todo!(),
LanguageField::BN256 => todo!(),
Expand All @@ -358,7 +365,7 @@ impl LoadCli {
macro_rules! load {
( $rc: expr, $limit: expr, $field: path, $backend: expr ) => {{
let mut repl = new_repl!(self, $rc, $limit, $field, $backend);
repl.load_file(&self.lurk_file)?;
repl.load_file(&self.lurk_file, self.demo)?;
if self.prove {
repl.prove_last_frames()?;
}
Expand Down Expand Up @@ -400,7 +407,6 @@ impl LoadCli {
backend.validate_field(field)?;
match field {
LanguageField::Pallas => load!(rc, limit, pallas::Scalar, backend.clone()),
// LanguageField::Vesta => load!(rc, limit, vesta::Scalar, backend),
LanguageField::Vesta => todo!(),
LanguageField::BLS12_381 => todo!(),
LanguageField::BN256 => todo!(),
Expand Down
54 changes: 40 additions & 14 deletions src/cli/repl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustyline::{
Config, Editor,
};
use rustyline_derive::{Completer, Helper, Highlighter, Hinter};
use std::{cell::RefCell, collections::HashMap, fs::read_to_string, rc::Rc, sync::Arc};
use std::{cell::RefCell, collections::HashMap, fs::read_to_string, io::Write, rc::Rc, sync::Arc};
use tracing::info;

use crate::{
Expand Down Expand Up @@ -433,23 +433,54 @@ impl Repl<F> {
Ok(())
}

fn handle_form<'a>(&mut self, input: parser::Span<'a>) -> Result<parser::Span<'a>> {
let (input, ptr, is_meta) = self.store.read_maybe_meta(self.state.clone(), &input)?;
#[inline]
fn input_marker(&self) -> String {
format!(
"{}> ",
self.state
.borrow()
.fmt_to_string(self.state.borrow().get_current_package_name())
)
}

fn handle_form<'a>(&mut self, input: parser::Span<'a>, demo: bool) -> Result<parser::Span<'a>> {
let (syntax_start, mut new_input, ptr, is_meta) =
self.store.read_maybe_meta(self.state.clone(), &input)?;
if demo {
let potential_commentaries = &input[..syntax_start];
let actual_syntax = &input[syntax_start..new_input.location_offset()];
let input_marker = &self.input_marker();
if actual_syntax.contains('\n') {
// print the expression on a new line to avoid messing with the user's formatting
print!("{potential_commentaries}{input_marker}\n{actual_syntax}");
} else {
print!("{potential_commentaries}{input_marker}{actual_syntax}");
}
std::io::stdout().flush()?;
// wait for ENTER to be pressed
std::io::stdin().read_line(&mut String::new())?;
// ENTER already prints a new line so we can remove it from the start of incoming input
new_input = parser::Span::new(new_input.trim_start_matches('\n'));
}
if is_meta {
self.handle_meta(ptr)?;
} else {
self.handle_non_meta(ptr)?;
}
Ok(input)
Ok(new_input)
}

pub fn load_file(&mut self, file_path: &Utf8Path) -> Result<()> {
pub fn load_file(&mut self, file_path: &Utf8Path, demo: bool) -> Result<()> {
let input = read_to_string(file_path)?;
println!("Loading {file_path}");
if demo {
println!("Loading {file_path} in demo mode");
} else {
println!("Loading {file_path}");
}

let mut input = parser::Span::new(&input);
loop {
match self.handle_form(input) {
match self.handle_form(input, demo) {
Ok(new_input) => input = new_input,
Err(e) => {
if let Some(parser::Error::NoInput) = e.downcast_ref::<parser::Error>() {
Expand Down Expand Up @@ -484,16 +515,11 @@ impl Repl<F> {
}

loop {
match editor.readline(&format!(
"{}> ",
self.state
.borrow()
.fmt_to_string(self.state.borrow().get_current_package_name())
)) {
match editor.readline(&self.input_marker()) {
Ok(line) => {
editor.save_history(history_path)?;
match self.store.read_maybe_meta(self.state.clone(), &line) {
Ok((_, expr_ptr, is_meta)) => {
Ok((.., expr_ptr, is_meta)) => {
if is_meta {
if let Err(e) = self.handle_meta(expr_ptr) {
println!("!Error: {e}");
Expand Down
2 changes: 1 addition & 1 deletion src/cli/repl/meta_cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl MetaCmd<F> {
match repl.store.fetch_string(&first) {
Some(path) => {
let joined = repl.pwd_path.join(Utf8Path::new(&path));
repl.load_file(&joined)?
repl.load_file(&joined, false)?
}
_ => bail!("Argument of `load` must be a string."),
}
Expand Down
10 changes: 8 additions & 2 deletions src/lem/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,17 @@ impl<F: LurkField> Store<F> {
&self,
state: Rc<RefCell<State>>,
input: &'a str,
) -> Result<(Span<'a>, Ptr<F>, bool), Error> {
) -> Result<(usize, Span<'a>, Ptr<F>, bool), Error> {
match preceded(syntax::parse_space, syntax::parse_maybe_meta(state, false))
.parse(input.into())
{
Ok((i, Some((is_meta, x)))) => Ok((i, self.intern_syntax(x), is_meta)),
Ok((i, Some((is_meta, x)))) => {
let from_offset = x
.get_pos()
.get_from_offset()
.expect("Parsed syntax should have its Pos set");
Ok((from_offset, i, self.intern_syntax(x), is_meta))
}
Ok((_, None)) => Err(Error::NoInput),
Err(e) => Err(Error::Syntax(format!("{}", e))),
}
Expand Down
8 changes: 8 additions & 0 deletions src/parser/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,12 @@ impl Pos {
upto_column: upto.get_utf8_column(),
}
}

/// Retrieves the `from_offset` attribute, if present
pub fn get_from_offset(&self) -> Option<usize> {
match self {
Self::No => None,
Self::Pos { from_offset, .. } => Some(*from_offset),
}
}
}
16 changes: 16 additions & 0 deletions src/syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ pub enum Syntax<F: LurkField> {
Improper(Pos, Vec<Syntax<F>>, Box<Syntax<F>>),
}

impl<F: LurkField> Syntax<F> {
/// Retrieves the `Pos` attribute
pub fn get_pos(&self) -> &Pos {
match self {
Self::Num(pos, _)
| Self::UInt(pos, _)
| Self::Symbol(pos, _)
| Self::String(pos, _)
| Self::Char(pos, _)
| Self::Quote(pos, _)
| Self::List(pos, _)
| Self::Improper(pos, ..) => pos,
}
}
}

#[cfg(not(target_arch = "wasm32"))]
impl<Fr: LurkField> Arbitrary for Syntax<Fr> {
type Parameters = ();
Expand Down
2 changes: 1 addition & 1 deletion tests/lurk-lib-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ fn lurk_cli_tests() {
let joined_new = Utf8PathBuf::from_path_buf(joined.clone()).unwrap();

repl::<S1, ReplState<S1, Coproc<S1>>, _, Coproc<S1>>(Some(joined), Lang::new()).unwrap();
let _ = repl_new.load_file(&joined_new);
let _ = repl_new.load_file(&joined_new, false);
}
}
Loading