Browse Source

Revert "Change function declaration syntax"

This reverts commit b21c781a64.
github-actions
Garrit Franke 3 years ago
parent
commit
4d5ef86243
  1. 5
      examples/hello_world.sb
  2. 12
      src/lexer/mod.rs
  3. 62
      src/lexer/tests.rs
  4. 5
      src/parser/mod.rs
  5. 48
      src/parser/tests.rs

5
examples/hello_world.sb

@ -1,4 +1,3 @@
main :: () { fn main(n) {
return "Hello World" n * 2;
} }

12
src/lexer/mod.rs

@ -53,8 +53,6 @@ pub enum TokenKind {
Slash, Slash,
/// ":" /// ":"
Colon, Colon,
/// "::"
DoubleColon,
/// ";" /// ";"
SemiColon, SemiColon,
/// "!" /// "!"
@ -107,6 +105,7 @@ pub enum Keyword {
If, If,
Else, Else,
Return, Return,
Function,
Boolean, Boolean,
Unknown, Unknown,
} }
@ -185,13 +184,7 @@ impl Cursor<'_> {
} }
_ => Assign, _ => Assign,
}, },
':' => match self.first() { ':' => Colon,
':' => {
self.bump();
DoubleColon
}
_ => Colon,
},
';' => SemiColon, ';' => SemiColon,
',' => Comma, ',' => Comma,
'<' => match self.first() { '<' => match self.first() {
@ -290,6 +283,7 @@ impl Cursor<'_> {
match original { match original {
c if c == "if" => Keyword::If, c if c == "if" => Keyword::If,
c if c == "else" => Keyword::Else, c if c == "else" => Keyword::Else,
c if c == "fn" => Keyword::Function,
c if c == "true" || c == "false" => Keyword::Boolean, c if c == "true" || c == "false" => Keyword::Boolean,
c if c == "let" => Keyword::Let, c if c == "let" => Keyword::Let,
c if c == "return" => Keyword::Return, c if c == "return" => Keyword::Return,

62
src/lexer/tests.rs

@ -160,46 +160,18 @@ mod tests {
#[test] #[test]
fn test_functions() { fn test_functions() {
let mut tokens = tokenize("fib :: () {}").into_iter(); let mut tokens = tokenize("fn fib() {}").into_iter();
assert_eq!(
tokens.nth(0).unwrap(),
Token {
len: 3,
kind: TokenKind::Identifier("fib".into()),
raw: "fib".to_owned(),
pos: Position {
raw: 2,
line: 1,
offset: 2
}
}
);
assert_eq!(
tokens.nth(0).unwrap(),
Token {
len: 1,
kind: TokenKind::Whitespace,
raw: " ".to_owned(),
pos: Position {
raw: 3,
line: 1,
offset: 3
}
}
);
assert_eq!( assert_eq!(
tokens.nth(0).unwrap(), tokens.nth(0).unwrap(),
Token { Token {
len: 2, len: 2,
kind: TokenKind::DoubleColon, kind: TokenKind::Keyword(Keyword::Function),
raw: "::".to_owned(), raw: "fn".to_owned(),
pos: Position { pos: Position {
raw: 5, raw: 1,
line: 1, line: 1,
offset: 5 offset: 1
} }
} }
); );
@ -209,7 +181,7 @@ mod tests {
fn test_comments() { fn test_comments() {
let mut tokens = tokenize( let mut tokens = tokenize(
"// foo "// foo
fib :: () {} fn fib() {}
", ",
) )
.into_iter() .into_iter()
@ -233,30 +205,16 @@ fib :: () {}
} }
); );
assert_eq!(
tokens.nth(0).unwrap(),
Token {
len: 3,
kind: TokenKind::Identifier("fib".into()),
raw: "fib".to_owned(),
pos: Position {
raw: 9,
line: 2,
offset: 3
}
}
);
assert_eq!( assert_eq!(
tokens.nth(0).unwrap(), tokens.nth(0).unwrap(),
Token { Token {
len: 2, len: 2,
kind: TokenKind::DoubleColon, kind: TokenKind::Keyword(Keyword::Function),
raw: "::".to_owned(), raw: "fn".to_owned(),
pos: Position { pos: Position {
raw: 12, raw: 8,
line: 2, line: 2,
offset: 6 offset: 2
} }
} }
); );

5
src/parser/mod.rs

@ -48,8 +48,6 @@ impl Parser {
self.peeked.pop() self.peeked.pop()
}; };
dbg!(&item);
self.current = item.to_owned(); self.current = item.to_owned();
item item
} }
@ -151,8 +149,9 @@ impl Parser {
} }
fn parse_function(&mut self) -> Result<Function, String> { fn parse_function(&mut self) -> Result<Function, String> {
self.match_keyword(Keyword::Function)?;
let name = self.match_identifier()?; let name = self.match_identifier()?;
self.match_token(TokenKind::DoubleColon)?;
self.match_token(TokenKind::BraceOpen)?; self.match_token(TokenKind::BraceOpen)?;
let arguments: Vec<Variable> = match self.peek() { let arguments: Vec<Variable> = match self.peek() {

48
src/parser/tests.rs

@ -3,7 +3,7 @@ use crate::parser::*;
#[test] #[test]
fn test_parse_empty_function() { fn test_parse_empty_function() {
let raw = "main :: () {}"; let raw = "fn main() {}";
let tokens = tokenize(raw); let tokens = tokenize(raw);
let tree = parse(tokens, Some(raw.to_string())); let tree = parse(tokens, Some(raw.to_string()));
assert!(tree.is_ok()) assert!(tree.is_ok())
@ -12,7 +12,7 @@ fn test_parse_empty_function() {
#[test] #[test]
fn test_parse_function_with_return() { fn test_parse_function_with_return() {
let raw = " let raw = "
main :: () { fn main() {
return 1 return 1
} }
"; ";
@ -24,7 +24,7 @@ fn test_parse_function_with_return() {
#[test] #[test]
fn test_parse_redundant_semicolon() { fn test_parse_redundant_semicolon() {
let raw = " let raw = "
main :: () { fn main() {
return 1; return 1;
} }
"; ";
@ -46,12 +46,12 @@ fn test_parse_no_function_context() {
#[test] #[test]
fn test_parse_multiple_functions() { fn test_parse_multiple_functions() {
let raw = " let raw = "
foo :: () { fn foo() {
let x = 2 let x = 2
return x return x
} }
bar :: () { fn bar() {
let y = 5 let y = 5
return y return y
} }
@ -64,7 +64,7 @@ fn test_parse_multiple_functions() {
#[test] #[test]
fn test_parse_variable_declaration() { fn test_parse_variable_declaration() {
let raw = " let raw = "
main :: () { fn main() {
let x = 1 let x = 1
return x return x
} }
@ -77,7 +77,7 @@ fn test_parse_variable_declaration() {
#[test] #[test]
fn test_parse_function_with_args() { fn test_parse_function_with_args() {
let raw = " let raw = "
main :: (foo) { fn main(foo) {
return foo return foo
} }
"; ";
@ -89,11 +89,11 @@ fn test_parse_function_with_args() {
#[test] #[test]
fn test_parse_function_call() { fn test_parse_function_call() {
let raw = " let raw = "
main :: (foo) { fn main(foo) {
foo() foo()
} }
foo :: () { fn foo() {
foo(2) foo(2)
} }
"; ";
@ -105,11 +105,11 @@ fn test_parse_function_call() {
#[test] #[test]
fn test_parse_return_function_call() { fn test_parse_return_function_call() {
let raw = " let raw = "
main :: () { fn main() {
return fib(2) return fib(2)
} }
fib :: () { fn fib() {
return fib(2) return fib(2)
} }
"; ";
@ -121,11 +121,11 @@ fn test_parse_return_function_call() {
#[test] #[test]
fn test_parse_function_call_multiple_arguments() { fn test_parse_function_call_multiple_arguments() {
let raw = " let raw = "
main :: () { fn main() {
fib(1, 2, 3) fib(1, 2, 3)
} }
fib :: () { fn fib() {
return 2 return 2
} }
"; ";
@ -137,11 +137,11 @@ fn test_parse_function_call_multiple_arguments() {
#[test] #[test]
fn test_parse_nexted_function_call() { fn test_parse_nexted_function_call() {
let raw = " let raw = "
main :: () { fn main() {
fib(fib(2), 2) fib(fib(2), 2)
} }
fib :: (n) { fn fib(n) {
return 2 return 2
} }
"; ";
@ -153,7 +153,7 @@ fn test_parse_nexted_function_call() {
#[test] #[test]
fn test_parse_basic_ops() { fn test_parse_basic_ops() {
let raw = " let raw = "
main :: () { fn main() {
return 2 * 5 return 2 * 5
} }
"; ";
@ -165,7 +165,7 @@ fn test_parse_basic_ops() {
#[test] #[test]
fn test_parse_compound_ops() { fn test_parse_compound_ops() {
let raw = " let raw = "
main :: () { fn main() {
2 * 5 / 3 2 * 5 / 3
} }
"; ";
@ -177,7 +177,7 @@ fn test_parse_compound_ops() {
#[test] #[test]
fn test_parse_compound_ops_with_function_call() { fn test_parse_compound_ops_with_function_call() {
let raw = " let raw = "
main :: () { fn main() {
return 2 * fib(1) / 3 return 2 * fib(1) / 3
} }
"; ";
@ -189,7 +189,7 @@ fn test_parse_compound_ops_with_function_call() {
#[test] #[test]
fn test_parse_compound_ops_with_strings() { fn test_parse_compound_ops_with_strings() {
let raw = " let raw = "
main :: () { fn main() {
return 2 * \"Hello\" return 2 * \"Hello\"
} }
"; ";
@ -201,7 +201,7 @@ fn test_parse_compound_ops_with_strings() {
#[test] #[test]
fn test_parse_compound_ops_with_identifier() { fn test_parse_compound_ops_with_identifier() {
let raw = " let raw = "
main :: (n) { fn main(n) {
return 2 * n return 2 * n
} }
"; ";
@ -214,7 +214,7 @@ fn test_parse_compound_ops_with_identifier() {
#[ignore] #[ignore]
fn test_parse_compound_ops_with_identifier_first() { fn test_parse_compound_ops_with_identifier_first() {
let raw = " let raw = "
main :: (n) { fn main(n) {
return n * 2 return n * 2
} }
"; ";
@ -226,7 +226,7 @@ fn test_parse_compound_ops_with_identifier_first() {
#[test] #[test]
fn test_parse_compound_ops_return() { fn test_parse_compound_ops_return() {
let raw = " let raw = "
main :: (n) { fn main(n) {
return 2 * n return 2 * n
} }
"; ";
@ -238,7 +238,7 @@ fn test_parse_compound_ops_return() {
#[test] #[test]
fn test_parse_basic_conditional() { fn test_parse_basic_conditional() {
let raw = " let raw = "
main :: (n) { fn main(n) {
if n { if n {
return n return n
} }
@ -252,7 +252,7 @@ fn test_parse_basic_conditional() {
#[test] #[test]
fn test_parse_basic_conditional_with_multiple_statements() { fn test_parse_basic_conditional_with_multiple_statements() {
let raw = " let raw = "
main :: (n) { fn main(n) {
if n { if n {
let x = 2 * n let x = 2 * n
return x return x

Loading…
Cancel
Save