compile: New function is_valid_variable v0.2.1
authorJean Privat <jean@pryen.org>
Tue, 9 Jun 2009 16:10:47 +0000 (12:10 -0400)
committerJean Privat <jean@pryen.org>
Wed, 10 Jun 2009 14:04:55 +0000 (10:04 -0400)
Used to improve ensure_var and compile_expr.

Signed-off-by: Jean Privat <jean@pryen.org>

src/compiling/compiling_methods.nit

index 3e20f95..6ec048a 100644 (file)
@@ -38,7 +38,7 @@ redef class CompilerVisitor
                var i = cfc._variable_index
                var s = n.compile_expr(self)
                cfc._variable_index = i
-               if s[0] == ' ' then
+               if s[0] == ' ' or cfc.is_valid_variable(s) then
                        return s
                end
                var v = cfc.get_var("Result for expr {n.locate}")
@@ -49,7 +49,8 @@ redef class CompilerVisitor
        # Ensure that a c expression is a var
        meth ensure_var(s: String, comment: String): String
        do
-               if s.substring(0,3) == "variable" then
+               if cfc.is_valid_variable(s) then
+                       add_instr("/* Ensure var {s}: {comment}*/")
                        return s
                end
                var v = cfc.get_var(null)
@@ -235,6 +236,15 @@ class CFunctionContext
                end
        end
 
+       # Is s a valid variable
+       protected meth is_valid_variable(s: String): Bool
+       do
+               for i in [0.._variable_index[ do
+                       if s == variable(i) then return true
+               end
+               return false
+       end
+
        # Mark the variable available
        meth free_var(v: String)
        do