Skip to content
Browse files

elif in Parser with test

  • Loading branch information...
1 parent b4c0c7f commit f5abd8cc508d24e2f5a7688baedcaccf99720a38 @ajlopez committed Nov 20, 2012
Showing with 43 additions and 6 deletions.
  1. +27 −0 Src/PythonSharp.Tests/Compiler/ParserTest.cs
  2. +15 −5 Src/PythonSharp/Compiler/Parser.cs
  3. +1 −1 Src/Todo.txt
View
27 Src/PythonSharp.Tests/Compiler/ParserTest.cs
@@ -1765,6 +1765,33 @@ public void CompileContinueCommand()
Assert.IsNull(parser.CompileCommand());
}
+ [TestMethod]
+ public void CompileIfCommandWithElifCommand()
+ {
+ Parser parser = new Parser("if a:\r\n print(a)\r\nelif b:\r\n print(a)\r\n print(b)");
+
+ ICommand cmd = parser.CompileCommand();
+
+ Assert.IsNotNull(cmd);
+ Assert.IsInstanceOfType(cmd, typeof(IfCommand));
+
+ IfCommand ifcmd = (IfCommand)cmd;
+
+ Assert.IsNotNull(ifcmd.Condition);
+ Assert.IsInstanceOfType(ifcmd.Condition, typeof(NameExpression));
+ Assert.IsNotNull(ifcmd.ThenCommand);
+ Assert.IsNotNull(ifcmd.ElseCommand);
+ Assert.IsInstanceOfType(ifcmd.ElseCommand, typeof(IfCommand));
+
+ IfCommand elifcmd = (IfCommand)ifcmd.ElseCommand;
+
+ Assert.IsNotNull(elifcmd.ThenCommand);
+ Assert.IsInstanceOfType(elifcmd.ThenCommand, typeof(CompositeCommand));
+ Assert.IsNull(elifcmd.ElseCommand);
+
+ Assert.IsNull(parser.CompileCommand());
+ }
+
private static object CompileAndEvaluateExpression(string text)
{
Machine machine = new Machine();
View
20 Src/PythonSharp/Compiler/Parser.cs
@@ -507,14 +507,24 @@ private ICommand CompileIfCommand()
int indent = this.lexer.NextIndent();
- if (indent == this.indent && this.TryCompile(TokenType.Name, "else"))
+ if (indent == this.indent)
{
- ICommand elsecommand = this.CompileSuite();
+ if (this.TryCompile(TokenType.Name, "else"))
+ {
+ ICommand elsecommand = this.CompileSuite();
+
+ return new IfCommand(condition, thencommand, elsecommand);
+ }
+
+ if (this.TryCompile(TokenType.Name, "elif"))
+ {
+ ICommand elsecommand = this.CompileIfCommand();
- return new IfCommand(condition, thencommand, elsecommand);
+ return new IfCommand(condition, thencommand, elsecommand);
+ }
}
- else
- this.lexer.PushIndent(indent);
+
+ this.lexer.PushIndent(indent);
return new IfCommand(condition, thencommand);
}
View
2 Src/Todo.txt
@@ -41,7 +41,7 @@ doc() (review name)
+ 4/5 should return 0.8
= Refactor if/else parse implementation
-elif in parser
++ elif in parser
+ doc string in module
+ doc string in def

0 comments on commit f5abd8c

Please sign in to comment.
Something went wrong with that request. Please try again.