Skip to content
Snippets Groups Projects
Commit 835ca150 authored by Hunter Goldstein's avatar Hunter Goldstein
Browse files

case statements are now implicitly ended

parent 47085708
No related branches found
No related tags found
No related merge requests found
for i in [1 2 3 4 5 6] for i in [1 2 3 4 5 6]
match $((i % 2)) match $((i % 2))
case 0; echo "Even!"; end case 0; echo "Even!"
case 1; echo "Odd!" ; end case 1; echo "Odd!"
end end
end end
...@@ -10,8 +10,8 @@ let out2 = "/foo/bar/baz.quxx: application/quxx-archive"; ...@@ -10,8 +10,8 @@ let out2 = "/foo/bar/baz.quxx: application/quxx-archive";
fn analyze output fn analyze output
match @split(output)[1] match @split(output)[1]
case application/x-gzip; echo "Use tar -xzf"; end case application/x-gzip; echo "Use tar -xzf"
case _ ; echo "Unknown file type"; end case _ ; echo "Unknown file type"
end end
end end
...@@ -20,8 +20,8 @@ analyze $out2 ...@@ -20,8 +20,8 @@ analyze $out2
fn wildcard input fn wildcard input
match $input match $input
case _; echo "WILDCARD!"; end case _; echo "WILDCARD!"
case huh; echo "U N R E A C H A B L E"; end case huh; echo "U N R E A C H A B L E"
end end
end end
...@@ -30,11 +30,11 @@ wildcard "huh" ...@@ -30,11 +30,11 @@ wildcard "huh"
fn report usage fn report usage
match $usage match $usage
case @(seq 0 25); echo "Plenty of space my guy"; end case @(seq 0 25); echo "Plenty of space my guy"
case @(seq 26 50); echo "Almost half full (or half empty)"; end case @(seq 26 50); echo "Almost half full (or half empty)"
case @(seq 51 75); echo "Getting close to full :O"; end case @(seq 51 75); echo "Getting close to full :O"
case @(seq 76 99); echo "Time for spring cleaning, almost full!"; end case @(seq 76 99); echo "Time for spring cleaning, almost full!"
case _; echo "How did you even do this..."; end case _; echo "How did you even do this..."
end end
end end
...@@ -47,10 +47,8 @@ fn animated filetype ...@@ -47,10 +47,8 @@ fn animated filetype
match $filetype match $filetype
case ["image/jpeg" "image/png"] case ["image/jpeg" "image/png"]
echo "Static :(" echo "Static :("
end
case "image/gif" case "image/gif"
echo "Animated :D" echo "Animated :D"
end
end end
end end
......
...@@ -133,32 +133,32 @@ pub struct Function { ...@@ -133,32 +133,32 @@ pub struct Function {
pub statements: Vec<Statement> pub statements: Vec<Statement>
} }
macro_rules! add_to_case {
($cases:expr, $statement:expr) => {
match $cases.last_mut() {
// XXX: When does this actually happen? What syntax error is this???
None => return Err(["ion: syntax error: encountered ",
$statement.short(),
" outside of `case ... end` block"].concat()),
Some(ref mut case) => case.statements.push($statement),
}
}
}
pub fn collect_cases<I>(iterator: &mut I, cases: &mut Vec<Case>, level: &mut usize) -> Result<(), String> pub fn collect_cases<I>(iterator: &mut I, cases: &mut Vec<Case>, level: &mut usize) -> Result<(), String>
where I : Iterator<Item=Statement> where I : Iterator<Item=Statement>
{ {
macro_rules! add_to_case {
($statement:expr) => {
match cases.last_mut() {
// XXX: When does this actually happen? What syntax error is this???
None => return Err(["ion: syntax error: encountered ",
$statement.short(),
" outside of `case ...` block"].concat()),
Some(ref mut case) => case.statements.push($statement),
}
}
}
while let Some(statement) = iterator.next() { while let Some(statement) = iterator.next() {
match statement { match statement {
Statement::Case(case) => { Statement::Case(case) => {
*level += 1; if *level == 1 {
if *level == 2 { // If the level is 1, then we are at a top-level case
// When the control flow level equals two, this means we are inside the // statement for this match block and should push this case
// body of the match statement and should treat this as the new case of _this_
// match. Otherwise we will just add it to the current case.
cases.push(case); cases.push(case);
} else { } else {
add_to_case!(cases, Statement::Case(case)); // This is just part of the current case block
add_to_case!(Statement::Case(case));
} }
}, },
Statement::End => { Statement::End => {
...@@ -172,15 +172,8 @@ pub fn collect_cases<I>(iterator: &mut I, cases: &mut Vec<Case>, level: &mut usi ...@@ -172,15 +172,8 @@ pub fn collect_cases<I>(iterator: &mut I, cases: &mut Vec<Case>, level: &mut usi
Statement::If { .. } | Statement::If { .. } |
Statement::Match { .. } | Statement::Match { .. } |
Statement::Function { .. } => { Statement::Function { .. } => {
if *level < 2 { *level += 1;
// If the level is less than two, then this statement has appeared outside add_to_case!(statement);
// of a block delimited by a case...end pair
return Err(["ion: syntax error: expected end or case, got ", statement.short()].concat());
} else {
// Otherwise it means we've hit a case statement for some other match construct
*level += 1;
add_to_case!(cases, statement);
}
}, },
Statement::Default | Statement::Default |
Statement::Else | Statement::Else |
...@@ -192,7 +185,7 @@ pub fn collect_cases<I>(iterator: &mut I, cases: &mut Vec<Case>, level: &mut usi ...@@ -192,7 +185,7 @@ pub fn collect_cases<I>(iterator: &mut I, cases: &mut Vec<Case>, level: &mut usi
Statement::Pipeline(_) | Statement::Pipeline(_) |
Statement::Break => { Statement::Break => {
// This is the default case with all of the other statements explicitly listed // This is the default case with all of the other statements explicitly listed
add_to_case!(cases, statement); add_to_case!(statement);
}, },
} }
} }
...@@ -208,7 +201,7 @@ pub fn collect_loops <I: Iterator<Item = Statement>> ( ...@@ -208,7 +201,7 @@ pub fn collect_loops <I: Iterator<Item = Statement>> (
while let Some(statement) = iterator.next() { while let Some(statement) = iterator.next() {
match statement { match statement {
Statement::While{..} | Statement::For{..} | Statement::If{..} | Statement::While{..} | Statement::For{..} | Statement::If{..} |
Statement::Function{..} | Statement::Match{..} | Statement::Case{..} => *level += 1, Statement::Function{..} | Statement::Match{..} => *level += 1,
Statement::End if *level == 1 => { *level = 0; break }, Statement::End if *level == 1 => { *level = 0; break },
Statement::End => *level -= 1, Statement::End => *level -= 1,
_ => (), _ => (),
...@@ -226,7 +219,7 @@ pub fn collect_if<I>(iterator: &mut I, success: &mut Vec<Statement>, else_if: &m ...@@ -226,7 +219,7 @@ pub fn collect_if<I>(iterator: &mut I, success: &mut Vec<Statement>, else_if: &m
while let Some(statement) = iterator.next() { while let Some(statement) = iterator.next() {
match statement { match statement {
Statement::While{..} | Statement::For{..} | Statement::If{..} | Statement::While{..} | Statement::For{..} | Statement::If{..} |
Statement::Function{..} | Statement::Match{..} | Statement::Case{..} => *level += 1, Statement::Function{..} | Statement::Match{..} => *level += 1,
Statement::ElseIf(ref elseif) if *level == 1 => { Statement::ElseIf(ref elseif) if *level == 1 => {
if current_block == 1 { if current_block == 1 {
return Err("ion: syntax error: else block already given"); return Err("ion: syntax error: else block already given");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment