Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: using cargo fmt for formatting #7

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ jobs:
run: cargo build --verbose
- name: Run tests
run: cargo test --verbose
- name: Run Cargo Fmt
run: cargo fmt --check
13 changes: 5 additions & 8 deletions src/arg_paresr.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@

use std::env;

pub fn arg_parser() -> InputType{
pub fn arg_parser() -> InputType {
let args: Vec<String> = env::args().collect();

match args.get(1) {
None => InputType::Repl,
Some(param) => match param.as_str() {
"--help" | "-h" => InputType::Help,
"--version" | "-v"=> InputType::Version,
file_name => {
InputType::ExecuteFile(file_name.to_string())
}
"--version" | "-v" => InputType::Version,
file_name => InputType::ExecuteFile(file_name.to_string()),
},
}
}

pub enum InputType{
pub enum InputType {
ExecuteFile(String),
Help,
Repl,
Version
Version,
}
5 changes: 2 additions & 3 deletions src/common/chunk.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

use super::param::Param;

#[derive(Debug, PartialEq, Clone)]
pub enum Chunk{
pub enum Chunk {
Params(Vec<Param>),
Function(String),
}
}
36 changes: 16 additions & 20 deletions src/common/data_type.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@


#[derive(PartialEq, Debug, Clone)]
pub enum DataType {
String(String),
Int(i32),
Float(f64),
Bool(bool),
List(Vec<DataType>)
List(Vec<DataType>),
}

impl ToString for DataType {
Expand All @@ -17,18 +15,17 @@ impl ToString for DataType {
Self::Float(f) => f.to_string(),
Self::Bool(b) => b.to_string(),
Self::List(b) => {
let a = b.iter().map(|x|x.to_string());
let a = b.iter().map(|x| x.to_string());
let a: Vec<String> = a.collect();
let i = a.join(" ");
format!("[{}]",i)
},
format!("[{}]", i)
}
}
}
}

impl DataType {

pub fn type_name(&self) -> String{
pub fn type_name(&self) -> String {
match self {
DataType::String(_) => "string".to_string(),
DataType::Int(_) => "int".to_string(),
Expand All @@ -39,26 +36,25 @@ impl DataType {
}
}


#[cfg(test)]
mod tests{
mod tests {
use super::*;

#[test]
fn data_type_equal_test(){
assert!(
DataType::String("abcd".to_string()) == DataType::String("abcd".to_string())
);
assert!(
DataType::String("abce".to_string()) != DataType::String("abcd".to_string())
);
fn data_type_equal_test() {
assert!(DataType::String("abcd".to_string()) == DataType::String("abcd".to_string()));
assert!(DataType::String("abce".to_string()) != DataType::String("abcd".to_string()));
}

#[test]
fn list_to_string(){
fn list_to_string() {
assert_eq!(
DataType::List(vec![DataType::Int(1), DataType::String("hello".to_string())]).to_string(),
DataType::List(vec![
DataType::Int(1),
DataType::String("hello".to_string())
])
.to_string(),
"[1 hello]"
);
}
}
}
79 changes: 56 additions & 23 deletions src/common/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum ErrorType {
}

impl ErrorType {
pub fn to_string(&self) -> &str{
pub fn to_string(&self) -> &str {
match *self {
ErrorType::Syntax => "syntax",
ErrorType::StaticAnalyzer => "static analyzer",
Expand All @@ -22,60 +22,93 @@ impl ErrorType {
}

#[derive(PartialEq, Debug, Clone)]
pub struct ChapError{
pub struct ChapError {
line_number: u32,
msg: Option<String>,
pub err_type: ErrorType
pub err_type: ErrorType,
}

pub type Result<T> = core::result::Result<T, ChapError>;


impl ChapError {

pub fn syntax_with_msg(line_number: u32, msg: String) -> Self {
Self { line_number, msg: Some(msg), err_type: ErrorType::Syntax }
Self {
line_number,
msg: Some(msg),
err_type: ErrorType::Syntax,
}
}
pub fn static_analyzer_with_msg(line_number: u32, msg: String) -> Self {
Self { line_number, msg: Some(msg), err_type: ErrorType::StaticAnalyzer }
Self {
line_number,
msg: Some(msg),
err_type: ErrorType::StaticAnalyzer,
}
}
pub fn runtime_with_msg(line_number: u32, msg: String) -> Self {
Self { line_number, msg: Some(msg), err_type: ErrorType::Runtime }
Self {
line_number,
msg: Some(msg),
err_type: ErrorType::Runtime,
}
}

pub fn syntax(line_number: u32) -> Self {
Self { line_number, msg: None, err_type: ErrorType::Syntax }
Self {
line_number,
msg: None,
err_type: ErrorType::Syntax,
}
}
pub fn static_analyzer(line_number: u32) -> Self {
Self { line_number, msg: None, err_type: ErrorType::StaticAnalyzer }
Self {
line_number,
msg: None,
err_type: ErrorType::StaticAnalyzer,
}
}
pub fn runtime(line_number: u32) -> Self {
Self { line_number, msg: None, err_type: ErrorType::Runtime }
Self {
line_number,
msg: None,
err_type: ErrorType::Runtime,
}
}
pub fn no_more_line() -> Self{
Self { line_number: 0, msg: None, err_type: ErrorType::NothingToExecute }
pub fn no_more_line() -> Self {
Self {
line_number: 0,
msg: None,
err_type: ErrorType::NothingToExecute,
}
}
pub fn stop() -> Self{
Self { line_number: 0, msg: None, err_type: ErrorType::Stop }
pub fn stop() -> Self {
Self {
line_number: 0,
msg: None,
err_type: ErrorType::Stop,
}
}

pub fn exit_with_error(&self){
pub fn exit_with_error(&self) {
println!("{}", self.error_message());
panic!();
}

// on the REPL mode we should not shut the whole interperter down
pub fn show_warning(&self){
// on the REPL mode we should not shut the whole interperter down
pub fn show_warning(&self) {
println!("{}", self.error_message());
}

pub fn error_message(&self) -> String{
pub fn error_message(&self) -> String {
let mut result = String::new();
result.push_str(&format!("\t{} Error on line: {}", self.err_type.to_string(), self.line_number));
if let Some(msg) = &self.msg{
result.push_str(&format!("\terror message: {} ",msg));
result.push_str(&format!(
"\t{} Error on line: {}",
self.err_type.to_string(),
self.line_number
));
if let Some(msg) = &self.msg {
result.push_str(&format!("\terror message: {} ", msg));
}
result
}

}
26 changes: 12 additions & 14 deletions src/common/executable.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@

use crate::runtime::Runtime;
use crate::runtime::builtin_function::closure_gen;
use crate::common::errors::Result;
use super::param::Param;
use crate::common::errors::Result;
use crate::runtime::builtin_function::closure_gen;
use crate::runtime::Runtime;

pub type BuiltinFunction = fn(&mut Runtime, &ExecutableLine) -> Result<()>;

#[derive(PartialEq, Debug, Clone)]
pub struct ExecutableLine{
pub struct ExecutableLine {
pub line_number: u32, // for error throw information
pub function_name: String,
pub params: Vec<Param>,
Expand All @@ -16,24 +15,23 @@ pub struct ExecutableLine{
}

impl ExecutableLine {

pub fn new(
line_number: u32,
function_name: String,
params: Vec<Param>,
output_var: Option<String>
) -> Self{
Self {
output_var: Option<String>,
) -> Self {
Self {
line_number,
function_name,
params,
output_var,
closure: |_,_|{Ok(())},
output_var,
closure: |_, _| Ok(()),
}
}

pub fn bind_closure(&mut self) -> Result<()>{
self.closure = closure_gen(self)?;
pub fn bind_closure(&mut self) -> Result<()> {
self.closure = closure_gen(self)?;
Ok(())
}
}
}
5 changes: 2 additions & 3 deletions src/common/help.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

pub fn show_help(){
pub fn show_help() {
println!("┌───────────────────────────────────────────────┐");
println!("│ Chap Language │");
println!("│ │");
Expand All @@ -16,4 +15,4 @@ pub fn show_help(){
println!("│ -v, --version │");
println!("│ │");
println!("└───────────────────────────────────────────────┘");
}
}
12 changes: 5 additions & 7 deletions src/common/line_of_code.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@


#[derive(Debug)]
pub struct LineOfCode{
pub struct LineOfCode {
pub line_number: u32,
pub code: String
pub code: String,
}

impl LineOfCode {
pub fn new(line_number: u32, code: String) -> Self{
Self{line_number, code}
pub fn new(line_number: u32, code: String) -> Self {
Self { line_number, code }
}
}
}
9 changes: 4 additions & 5 deletions src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

pub mod line_of_code;
pub mod chunk;
pub mod param;
pub mod data_type;
pub mod executable;
pub mod errors;
pub mod executable;
pub mod help;
pub mod line_of_code;
pub mod param;
pub mod splitter;
pub mod version;
pub mod splitter;
3 changes: 1 addition & 2 deletions src/common/param.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@

use super::data_type::DataType;

#[derive(PartialEq, Debug, Clone)]
pub enum Param {
Tag(String),
Variable(String),
Value(DataType),
}
}
Loading
Loading