Эх сурвалжийг харах

fix interpreter's handling of optional else of if statement (#323)

Jeremy G. Siek 5 жил өмнө
parent
commit
5457916abb

+ 1 - 3
executable_semantics/BUILD

@@ -81,9 +81,7 @@ EXAMPLES = [
     "fun6_fail_type",
     "funptr1",
     "if1",
-    # (Temporarily disabled pending
-    # https://github.com/carbon-language/carbon-lang/issues/311)
-    # "if2",
+    "if2",
     "if3",
     "match_int_default",
     "match_int",

+ 3 - 1
executable_semantics/interpreter/interpreter.cpp

@@ -1177,12 +1177,14 @@ void HandleValue() {
             // -> { { then_stmt :: C, E, F } :: S, H}
             frame->todo.Pop(2);
             frame->todo.Push(MakeStmtAct(stmt->u.if_stmt.then_stmt));
-          } else {
+          } else if (stmt->u.if_stmt.else_stmt) {
             //    { {false :: if ([]) then_stmt else else_stmt :: C, E, F} ::
             //      S, H}
             // -> { { else_stmt :: C, E, F } :: S, H}
             frame->todo.Pop(2);
             frame->todo.Push(MakeStmtAct(stmt->u.if_stmt.else_stmt));
+          } else {
+            frame->todo.Pop(2);
           }
           break;
         case StatementKind::While:

+ 20 - 2
executable_semantics/testdata/if2.golden

@@ -136,8 +136,26 @@ env: main: fun<main>,
 else
 <1>(false,) --->
 {
-stack: main{<-1> :: return 0;<-1>} :: top{}
+stack: main{return 0;<-1>} :: top{}
 heap: fun<main>, 
 env: main: fun<main>, 
 }
---- step stmt  --->
+--- step stmt return 0; --->
+{
+stack: main{0<-1> :: return 0;<0>} :: top{}
+heap: fun<main>, 
+env: main: fun<main>, 
+}
+--- step exp 0 --->
+{
+stack: main{0<-1> :: return 0;<0>} :: top{}
+heap: fun<main>, 
+env: main: fun<main>, 
+}
+--- handle value 0 with return 0;<1>(0,) --->
+{
+stack: top{0<-1>}
+heap: fun<main>, 
+env: main: fun<main>, 
+}
+result: 0