From 1859a9c4bb5f342bee460acf147f3066de1c2505 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Thu, 10 Feb 2022 03:34:37 +0700 Subject: [PATCH] refactor: Nil, more helpful help --- Cargo.lock | 2 +- Cargo.toml | 2 +- src/main.rs | 17 +++++++++++++++-- src/parser.rs | 15 ++++++++++----- 4 files changed, 27 insertions(+), 9 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8deab14..53b4b76 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3,5 +3,5 @@ version = 3 [[package]] -name = "vy" +name = "vyc" version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml index 35b327f..261bc01 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "vy" +name = "vyc" version = "0.1.0" edition = "2021" diff --git a/src/main.rs b/src/main.rs index cfe5c26..c65e2da 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,13 +3,24 @@ use std::{process::exit, fs::read_to_string}; pub mod parser; 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() { let args = std::env::args().collect::>(); let mut args_index: usize = 0; match args.len() { // No argument provided - 1 => { display_help(1); }, + 1 => { + println!("No argument provided."); + display_help(1); + }, _ => { while args.len() > args_index { let arg: &str = &args[args_index]; @@ -29,6 +40,7 @@ fn main() { println!("{:?}", node); } } else { + println!("No file provided."); display_help(1); } } @@ -40,6 +52,7 @@ fn main() { } fn display_help(exit_code: i32) { - println!("Usage: {} ", EXECUTABLE_NAME); + println!("Usage: {} [OPTIONS]", EXECUTABLE_NAME); + println!("{}", HELP_MESSAGE); exit(exit_code); } \ No newline at end of file diff --git a/src/parser.rs b/src/parser.rs index 1fddcab..471d4c7 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -10,6 +10,10 @@ pub struct List { pub value: Option>>, } +impl List { + pub const NIL: List = List { value: None }; +} + /// Cons -> (car, [cdr]) #[derive(Debug, Clone)] pub struct Cons { @@ -25,11 +29,12 @@ pub enum Value { // Numbers Int(i64), Float(f64), - String(String), Symbol(String), - + String(String), Symbol(String), List(List), +} - Nil, +impl Value { + pub const NIL: Value = Value::List(List::NIL); } #[derive(Debug, Clone)] @@ -261,7 +266,7 @@ fn read<'a>( if let Tree::List { vec, quote } = &finished { if vec.is_empty() { finished = Tree::Atom { - atom: Value::Nil, + atom: Value::NIL, quote: *quote, }; } @@ -300,7 +305,7 @@ fn read_atom(token: &str) -> Value { match lower.as_str() { "true" => Value::True, "false" => Value::False, - "nil" => Value::Nil, + "nil" => Value::NIL, _ => { // Parse number if let Ok(int) = token.parse::() { Value::Int(int) }