Skip to content

Commit

Permalink
Adding cargo standards
Browse files Browse the repository at this point in the history
  • Loading branch information
justinfargnoli committed Feb 10, 2020
1 parent 7848cbf commit 1305184
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 39 deletions.
34 changes: 18 additions & 16 deletions src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,13 @@ pub enum AST {
body: Box<AST>,
},
ArecC {
func_name: String,
arg_name: String,
arg_type: Box<Type>,
func_name: String,
arg_name: String,
arg_type: Box<Type>,
ret_type: Box<Type>,
body: Box<AST>,
func_use: Box<AST>,
}
},
}

impl AST {
Expand Down Expand Up @@ -135,8 +135,8 @@ impl AST {

Box::new(AST::AfdC {
arg_name: string_ast,
arg_type: arg_type,
ret_type: ret_type,
arg_type,
ret_type,
body: ast_body,
})
}
Expand All @@ -149,12 +149,13 @@ impl AST {
Box::new(AST::AeqC(ast1, ast2))
}
Token::TRecC => {
assert_eq!(Token::ParenLeft, token_stream.next().unwrap());
// 1st parameter
assert_eq!(Token::ParenLeft, token_stream.next().unwrap());
// 1st parameter
assert_eq!(Token::Quote, token_stream.next().unwrap());
let rec_func_name;
match token_stream.next().unwrap() {
Token::ID(val) => { //Token::ID, not to be confused with idC
Token::ID(val) => {
//Token::ID, not to be confused with idC
rec_func_name = val;
}
_ => panic!("String not found!"),
Expand All @@ -165,7 +166,8 @@ impl AST {
assert_eq!(Token::Quote, token_stream.next().unwrap());
let rec_arg_name;
match token_stream.next().unwrap() {
Token::ID(val) => { //Token::ID, not to be confused with idC
Token::ID(val) => {
//Token::ID, not to be confused with idC
rec_arg_name = val;
}
_ => panic!("String not found!"),
Expand All @@ -185,12 +187,12 @@ impl AST {
let rec_func_use_ast = AST::build(token_stream);
assert_eq!(Token::ParenRight, token_stream.next().unwrap());
Box::new(AST::ArecC {
func_name: rec_func_name,
arg_name: rec_arg_name,
arg_type: rec_arg_type,
ret_type: rec_ret_type,
body: rec_body_ast,
func_use: rec_func_use_ast,
func_name: rec_func_name,
arg_name: rec_arg_name,
arg_type: rec_arg_type,
ret_type: rec_ret_type,
body: rec_body_ast,
func_use: rec_func_use_ast,
})
}
_ => panic!("Parsing error"), ////TODO: THIS should never happen
Expand Down
6 changes: 3 additions & 3 deletions src/tokenize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl TokenStream {
pub fn build_test(token_stream: VecDeque<Token>, curr_index: usize) -> TokenStream {
TokenStream {
stream: token_stream,
curr_index: curr_index,
curr_index,
}
}

Expand All @@ -59,7 +59,7 @@ impl TokenStream {
char_stream.pop_front().unwrap();
}
loop {
if char_stream.front().unwrap().to_digit(10).is_some() {
if char_stream.front().unwrap().is_digit(10) {
num_str.push(char_stream.pop_front().unwrap());
} else if !num_str.is_empty() {
let mut num = num_str.parse::<i32>().unwrap();
Expand Down Expand Up @@ -175,7 +175,7 @@ impl TokenStream {
assert_eq!(char_stream.pop_front().unwrap(), 'C');
tokens.push_back(Token::TRecC);
}
' ' | '\t' |'\n' | '\r' => continue,
' ' | '\t' | '\n' | '\r' => continue,
_ => panic!("Your input wasn't able to be converted into a token stream."),
}
}
Expand Down
39 changes: 22 additions & 17 deletions src/type_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,29 @@ pub fn tc(ast: Box<AST>, tenv: &mut HashMap<String, Type>) -> Type {
}
}
AST::ArecC {
func_name,
arg_name,
arg_type,
ret_type,
body,
func_use,
func_name,
arg_name,
arg_type,
ret_type,
body,
func_use,
} => {
tenv.insert(func_name.clone(), Type::FunT{arg: arg_type.clone(), ret: ret_type.clone()});
tenv.insert(arg_name.clone(), *arg_type.clone());
if *ret_type == tc(body, tenv) {
tc(func_use, tenv);
tenv.remove(&func_name);
tenv.remove(&arg_name);
*ret_type
}
else {
panic!("Return type of recursive function does not match return type of the body!");
}
tenv.insert(
func_name.clone(),
Type::FunT {
arg: arg_type.clone(),
ret: ret_type.clone(),
},
);
tenv.insert(arg_name.clone(), *arg_type);
if *ret_type == tc(body, tenv) {
tc(func_use, tenv);
tenv.remove(&func_name);
tenv.remove(&arg_name);
*ret_type
} else {
panic!("Return type of recursive function does not match return type of the body!");
}
}
AST::AfdC {
arg_name,
Expand Down
3 changes: 0 additions & 3 deletions tests/integration_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,3 @@ fn type_check_input_rec_c_factorial() {
Type::NumT
);
}



0 comments on commit 1305184

Please sign in to comment.