M Cargo.lock +1 -1
@@ 16,7 16,7 @@ checksum = "baf1de4339761588bc0619e3cbc0
[[package]]
name = "elements-lang"
-version = "0.2.0"
+version = "0.2.1"
dependencies = [
"rand",
]
M src/lexer.rs +4 -2
@@ 187,7 187,7 @@ fn match_token(token: String, prev_paren
}
/// Given a string, tokenize it into a vector of tokens
-pub fn tokenize(s: String) -> Vec<Token> {
+pub fn tokenize(s: String, is_debug: bool) -> Vec<Token> {
// split the string into a vector of strings based on whitespace
let separated: Vec<String> = s
.replace("(", " ( ")
@@ 198,7 198,9 @@ pub fn tokenize(s: String) -> Vec<Token>
.map(String::from)
.collect();
- println!("{:?}", separated);
+ if is_debug {
+ println!("{:?}", separated);
+ }
// match the tokens
let mut tokens: Vec<Token> = Vec::new();
M src/main.rs +21 -5
@@ 21,10 21,18 @@ fn main() {
std::process::exit(1);
}
- // check if label is enabled
+ // check if label and debug is enabled
let mut is_label = false;
+ let mut is_debug = false;
if args.len() > 2 && args[2] == "--label" {
is_label = true;
+ } else if args.len() > 2 && args[2] == "--debug" {
+ is_debug = true;
+ }
+ if args.len() > 3 && args[3] == "--label" {
+ is_label = true;
+ } else if args.len() > 3 && args[3] == "--debug" {
+ is_debug = true;
}
// see if file exists
@@ 38,7 46,7 @@ fn main() {
let contents = std::fs::read_to_string(filename).expect("Failed to read file");
// tokenize string
- let tokens: Vec<Token> = tokenize(contents);
+ let tokens: Vec<Token> = tokenize(contents, is_debug);
// evaluate tokens
let values: Vec<Value> = match evaluate(tokens) {
@@ 48,11 56,19 @@ fn main() {
std::process::exit(1);
}
};
- println!("{:?}", values);
+
+ // if debug is enabled, print the values
+ if is_debug {
+ println!("{:?}", values);
+ }
// render values to svg
- let svg = render(values, is_label).expect("Failed to render");
- println!("{}", svg);
+ let svg = render(values, is_label, is_debug).expect("Failed to render");
+
+ // if debug is enabled, print the svg elements
+ if is_debug {
+ println!("{}", svg);
+ }
// write svg to file
let filename = "out.svg";
M src/renderer.rs +10 -6
@@ 254,7 254,6 @@ pub struct SvgLine {
impl Render for SvgLine {
impl_as_any!(SvgLine);
fn render(&self) -> String {
- println!("reached");
format!(
"\t<line x1=\"{}\" y1=\"{}\" x2=\"{}\" y2=\"{}\" stroke=\"black\" stroke-width=\"0.02\"/>\n",
self.start.x, self.start.y, self.end.x, self.end.y
@@ 447,13 446,18 @@ fn label(svg: &mut Svg) {
}
}
-pub fn render(values: Vec<Value>, is_label: bool) -> Result<String, String> {
+pub fn render(values: Vec<Value>, is_label: bool, is_debug: bool) -> Result<String, String> {
let mut elements: Vec<Box<dyn Render>> = Vec::new();
- for value in values {
- println!("{:?}", value);
- let svg_elements: Vec<Box<dyn Render>> = value.to_svg();
- elements.extend(svg_elements);
+
+ // print values if debug is enabled
+ if is_debug {
+ for value in values {
+ println!("{:?}", value);
+ let svg_elements: Vec<Box<dyn Render>> = value.to_svg();
+ elements.extend(svg_elements);
+ }
}
+
let mut svg = Svg { elements };
if is_label {
label(&mut svg);