Skip to content

Commit

Permalink
Cli init (#5)
Browse files Browse the repository at this point in the history
* starting work on CLI

* added clap

* simple octahedron sphere

* tidy up pre merge
  • Loading branch information
coillteoir authored Jun 7, 2024
1 parent 65f3f1e commit 9d3cc68
Show file tree
Hide file tree
Showing 14 changed files with 542 additions and 222 deletions.
230 changes: 230 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ name = "kodama"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
clap = {version = "4.5.6",features = ["derive"]}

77 changes: 77 additions & 0 deletions src/compiler.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
use crate::shape::{cone, cube, cuboid, sphere, Point};

#[derive(Debug, Clone)]
struct Token {}

fn validate_braces(source: &str) -> Result<(), String> {
let braces = String::from(source)
.chars()
.filter(|c| (*c == '{' || *c == '}'))
.collect::<Vec<char>>();

if braces.len() % 2 != 0 {
return Err(String::from("invalid amount of braces"));
}
let mut stack = Vec::<char>::new();
for brace in braces.into_iter() {
if brace == '{' {
stack.push(brace);
}

if brace == '}' {
stack.pop();
}
}
if !stack.is_empty() {
return Err(String::from("mismatched braces"));
}
Ok(())
}

fn parser(data: &str) -> Result<String, String> {
let _ = validate_braces(data);
Ok(String::from("yay"))
}

pub fn compile(data: &str) -> String {
let mut result = String::new();
let _ = parser(data);
let lines = data.split('\n');
for line in lines {
let tokens: Vec<&str> = line.split(' ').collect();
match *tokens.first().unwrap() {
"cube" => result.push_str(
&cube(
Point::new(0.0, 0.0, 0.0),
tokens[1].parse::<f32>().expect("invalid value given"),
)
.unwrap(),
),
"cuboid" => result.push_str(
&cuboid(
Point::new(0.0, 0.0, 0.0),
tokens[1].parse::<f32>().expect("non numeric value given"),
tokens[2].parse::<f32>().expect("non numeric value given"),
tokens[3].parse::<f32>().expect("non numeric value given"),
)
.unwrap(),
),
"cone" => result.push_str(&cone(
Point::new(0.0, 0.0, 0.0),
0,
tokens[1].parse::<f32>().expect("non numeric value given"),
)),
"sphere" => result.push_str(
&sphere(
Point::new(0.0, 0.0, 0.0),
tokens[1].parse::<f32>().expect("non numeric value given"),
10,
)
.unwrap(),
),
"#" => todo!(),
&_ => eprintln!("{} not supported", tokens[0]),
}
}
result
}
Loading

0 comments on commit 9d3cc68

Please sign in to comment.