Skip to content

Commit

Permalink
Add comments to tasm code (#152)
Browse files Browse the repository at this point in the history
This change crudely removes `//` comments from source code. Anticipating
a better parser, we want to move forward by being able to comment our
code.
  • Loading branch information
sshine authored Dec 8, 2022
1 parent c06f0cd commit cdbcf43
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
1 change: 1 addition & 0 deletions triton-vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ rand_core = "0.6.4"
rand_distr = "0.4.3"
rand_pcg = "0.3.1"
rayon = "1.5"
regex = "1.7"
ring = "0.16.20"
rusty-leveldb = "1.0"
serde = { version = "1.0", features = ["derive"] }
Expand Down
12 changes: 9 additions & 3 deletions triton-vm/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use std::vec;
use anyhow::{bail, Result};
use itertools::Itertools;
use num_traits::One;
use regex::Regex;
use strum::{EnumCount, IntoEnumIterator};
use strum_macros::{Display as DisplayMacro, EnumCount as EnumCountMacro, EnumIter};
use twenty_first::shared_math::b_field_element::BFieldElement;
Expand Down Expand Up @@ -393,7 +394,9 @@ fn convert_labels_helper(
}
}

pub fn parse(code: &str) -> Result<Vec<LabelledInstruction>> {
pub fn parse(code_with_comments: &str) -> Result<Vec<LabelledInstruction>> {
let remove_comments = Regex::new(r"//.*?(?:\n|$)").expect("a regex that matches comments");
let code = remove_comments.replace_all(code_with_comments, "");
let mut tokens = code.split_whitespace();
let mut instructions = vec![];

Expand Down Expand Up @@ -1069,20 +1072,23 @@ pub mod sample_programs {
";

pub const FIB_SHOOTOUT: &str = "
// Initialize stack: _ 0 1 i
push 0
push 1
divine
call fib-loop
write_io
write_io
write_io // After loop, this is 0
write_io // After loop, this is fib(i)
halt
fib-loop:
dup0 skiz call fib-step
dup0 skiz recurse
return
// Before: _ a b i
// After: _ b (a+b) (i-1)
fib-step:
push -1
add
Expand Down

0 comments on commit cdbcf43

Please sign in to comment.