Skip to content

Commit

Permalink
add a demo flag on the load command
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpaulino committed Oct 26, 2023
1 parent 370a995 commit 5043171
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 22 deletions.
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,

Check warning on line 116 in src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/mod.rs#L116

Added line #L116 was not covered by tests
}

#[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,

Check warning on line 158 in src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/mod.rs#L158

Added line #L158 was not covered by tests
}

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,

Check warning on line 176 in src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/mod.rs#L176

Added line #L176 was not covered by tests
}
}
}
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)?;

Check warning on line 315 in src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/mod.rs#L315

Added line #L315 was not covered by tests
}
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)?;

Check warning on line 368 in src/cli/mod.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/mod.rs#L368

Added line #L368 was not covered by tests
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
52 changes: 38 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,52 @@ 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())
)
}

Check warning on line 444 in src/cli/repl.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L437-L444

Added lines #L437 - L444 were not covered by tests

fn handle_form<'a>(&mut self, input: parser::Span<'a>, demo: bool) -> Result<parser::Span<'a>> {
let (syntax_start, 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()?;

Check warning on line 459 in src/cli/repl.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L450-L459

Added lines #L450 - L459 were not covered by tests
// wait for ENTER to be pressed
std::io::stdin().read_line(&mut String::new())?;

Check warning on line 461 in src/cli/repl.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L461

Added line #L461 was not covered by tests
}
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");

Check warning on line 474 in src/cli/repl.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L474

Added line #L474 was not covered by tests
} 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 +513,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()) {

Check warning on line 516 in src/cli/repl.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L516

Added line #L516 was not covered by tests
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)) => {

Check warning on line 520 in src/cli/repl.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/repl.rs#L520

Added line #L520 was not covered by tests
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,

Check warning on line 83 in src/parser/position.rs

View check run for this annotation

Codecov / codecov/patch

src/parser/position.rs#L83

Added line #L83 was not covered by tests
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, _)

Check warning on line 48 in src/syntax.rs

View check run for this annotation

Codecov / codecov/patch

src/syntax.rs#L43-L48

Added lines #L43 - L48 were not covered by tests
| 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);
}
}

0 comments on commit 5043171

Please sign in to comment.