nitj: implement AIFExpr
authorAlexandre Terrasa <alexandre@moz-code.org>
Fri, 10 Jul 2015 06:02:17 +0000 (02:02 -0400)
committerAlexandre Terrasa <alexandre@moz-code.org>
Tue, 21 Jul 2015 21:23:22 +0000 (17:23 -0400)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

src/compiler/java_compiler.nit

index bafc98b..2077fa5 100644 (file)
@@ -640,6 +640,11 @@ class JavaCompilerVisitor
                return res
        end
 
+       # Alias for `self.expr(nexpr, self.bool_type)`
+       fun expr_bool(nexpr: AExpr): RuntimeVariable do
+               return expr(nexpr, compiler.mainmodule.bool_type)
+       end
+
        # Correctly assign a left and a right value
        # Boxing and unboxing is performed if required
        fun assign(left, right: RuntimeVariable) do
@@ -1919,6 +1924,28 @@ redef class AReturnExpr
        end
 end
 
+redef class AIfExpr
+       redef fun stmt(v) do
+               var cond = v.expr_bool(self.n_expr)
+               v.add("if ({cond})\{")
+               v.stmt(self.n_then)
+               v.add("\} else \{")
+               v.stmt(self.n_else)
+               v.add("\}")
+       end
+
+       redef fun expr(v) do
+               var res = v.new_var(self.mtype.as(not null))
+               var cond = v.expr_bool(self.n_expr)
+               v.add("if ({cond})\{")
+               v.assign(res, v.expr(self.n_then.as(not null), null))
+               v.add("\} else \{")
+               v.assign(res, v.expr(self.n_else.as(not null), null))
+               v.add("\}")
+               return res
+       end
+end
+
 redef class AVardeclExpr
        redef fun stmt(v) do
                var variable = self.variable.as(not null)