1
1
Fork 0
mirror of https://github.com/azur1s/bobbylisp.git synced 2024-10-16 02:37:40 -05:00

refactor: Nil, more helpful help

This commit is contained in:
Natapat Samutpong 2022-02-10 03:34:37 +07:00
parent 9e8c9e72dd
commit 1859a9c4bb
4 changed files with 27 additions and 9 deletions

2
Cargo.lock generated
View file

@ -3,5 +3,5 @@
version = 3 version = 3
[[package]] [[package]]
name = "vy" name = "vyc"
version = "0.1.0" version = "0.1.0"

View file

@ -1,5 +1,5 @@
[package] [package]
name = "vy" name = "vyc"
version = "0.1.0" version = "0.1.0"
edition = "2021" edition = "2021"

View file

@ -3,13 +3,24 @@ use std::{process::exit, fs::read_to_string};
pub mod parser; pub mod parser;
const EXECUTABLE_NAME: &str = env!("CARGO_PKG_NAME"); const EXECUTABLE_NAME: &str = env!("CARGO_PKG_NAME");
const HELP_MESSAGE: &str = "\
-h, --help
Print this help message and exit.
-v, --version
Print version information and exit.
-c FILE, --compile FILE
Compile the given file and exit.\
";
fn main() { fn main() {
let args = std::env::args().collect::<Vec<String>>(); let args = std::env::args().collect::<Vec<String>>();
let mut args_index: usize = 0; let mut args_index: usize = 0;
match args.len() { match args.len() {
// No argument provided // No argument provided
1 => { display_help(1); }, 1 => {
println!("No argument provided.");
display_help(1);
},
_ => { _ => {
while args.len() > args_index { while args.len() > args_index {
let arg: &str = &args[args_index]; let arg: &str = &args[args_index];
@ -29,6 +40,7 @@ fn main() {
println!("{:?}", node); println!("{:?}", node);
} }
} else { } else {
println!("No file provided.");
display_help(1); display_help(1);
} }
} }
@ -40,6 +52,7 @@ fn main() {
} }
fn display_help(exit_code: i32) { fn display_help(exit_code: i32) {
println!("Usage: {} <file>", EXECUTABLE_NAME); println!("Usage: {} [OPTIONS]", EXECUTABLE_NAME);
println!("{}", HELP_MESSAGE);
exit(exit_code); exit(exit_code);
} }

View file

@ -10,6 +10,10 @@ pub struct List {
pub value: Option<Rc<RefCell<Cons>>>, pub value: Option<Rc<RefCell<Cons>>>,
} }
impl List {
pub const NIL: List = List { value: None };
}
/// Cons -> (car, [cdr]) /// Cons -> (car, [cdr])
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Cons { pub struct Cons {
@ -25,11 +29,12 @@ pub enum Value {
// Numbers // Numbers
Int(i64), Float(f64), Int(i64), Float(f64),
String(String), Symbol(String), String(String), Symbol(String),
List(List), List(List),
}
Nil, impl Value {
pub const NIL: Value = Value::List(List::NIL);
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -261,7 +266,7 @@ fn read<'a>(
if let Tree::List { vec, quote } = &finished { if let Tree::List { vec, quote } = &finished {
if vec.is_empty() { if vec.is_empty() {
finished = Tree::Atom { finished = Tree::Atom {
atom: Value::Nil, atom: Value::NIL,
quote: *quote, quote: *quote,
}; };
} }
@ -300,7 +305,7 @@ fn read_atom(token: &str) -> Value {
match lower.as_str() { match lower.as_str() {
"true" => Value::True, "true" => Value::True,
"false" => Value::False, "false" => Value::False,
"nil" => Value::Nil, "nil" => Value::NIL,
_ => { _ => {
// Parse number // Parse number
if let Ok(int) = token.parse::<i64>() { Value::Int(int) } if let Ok(int) = token.parse::<i64>() { Value::Int(int) }