Skip to content
Open
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
20 changes: 20 additions & 0 deletions src/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,26 @@ impl Formatter {

result.push_str("fi");

result
}
Node::Function { name, list, has_keyword } => {
let mut result = self.indent();
if *has_keyword {
result.push_str("function ");
}
result.push_str(name);
result.push_str("() {");
result.push('\n');

self.indent_level += 1;
result.push_str(&self.format(list));
self.indent_level -= 1;

result.push('\n');
result.push_str(&self.indent());

result.push('}');

result
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ impl Interpreter {

Ok(0)
}
&Node::IfStatement { .. } | &Node::ElifBranch { .. } | &Node::ElseBranch { .. } => {
&Node::IfStatement { .. } | &Node::ElifBranch { .. } | &Node::ElseBranch { .. } | &Node::Function { .. } => {
todo!()
}
}
Expand Down
71 changes: 68 additions & 3 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ pub enum Node {
ElseBranch {
consequence: Box<Node>,
},
Function {
name: String,
list: Box<Node>,
has_keyword: bool,
},
}

/// Redirection types
Expand Down Expand Up @@ -107,6 +112,10 @@ impl Parser {
if self.peek_token.kind == TokenKind::Assignment {
return Some(self.parse_assignment());
}
// Check for LParen (function without keyword)
if self.peek_token.kind == TokenKind::LParen {
return Some(self.parse_function(false));
}

// Regular command
Some(self.parse_command())
Expand All @@ -121,6 +130,10 @@ impl Parser {
Some(Node::Comment(comment))
}
TokenKind::ExtGlob(_) => Some(self.parse_extglob()),
TokenKind::Function => {
self.next_token();
Some(self.parse_function(true))
}
_ => None,
}
}
Expand Down Expand Up @@ -221,6 +234,27 @@ impl Parser {
}
}

fn parse_function(&mut self, has_keyword: bool) -> Node {
let name = self.current_token.value.clone();

if self.peek_token.kind == TokenKind::LParen {
self.next_token(); // Skip LParen
self.next_token(); // Skip RParen
}

self.next_token(); // Skip LBrace

let list = self.parse_until_token_kind(TokenKind::RBrace);

self.next_token(); // Skip RBrace

Node::Function {
name,
list: Box::new(list),
has_keyword,
}
}

// method to parse statements until a specific token kind is encountered
fn parse_until_token_kind(&mut self, stop_at: TokenKind) -> Node {
let mut statements = Vec::new();
Expand Down Expand Up @@ -1389,9 +1423,40 @@ function hello() {
hello World
"#;

// This would require additional parsing logic not present in the current code
// Just verify it doesn't panic
let _result = parse_test(input);
let result = parse_test(input);
match result {
Node::List {
statements,
operators,
} => {
assert_eq!(statements.len(), 2);
assert_eq!(operators.len(), 2);

match &statements[0] {
Node::Function {
name,
list,
has_keyword,
} => {
assert_eq!(name, "hello");
assert_eq!(*has_keyword, true);

match &**list {
Node::List {
statements,
operators,
} => {
assert_eq!(statements.len(), 1);
assert_eq!(operators.len(), 0);
}
_ => panic!("Expected List node inside function"),
}
}
_ => panic!("Expected Function node"),
}
}
_ => panic!("Expected List node"),
}
}

#[test]
Expand Down