compile: optimize strings in add_instr
authorJean Privat <jean@pryen.org>
Sun, 26 Jul 2009 00:32:10 +0000 (20:32 -0400)
committerJean Privat <jean@pryen.org>
Sun, 26 Jul 2009 00:32:10 +0000 (20:32 -0400)
Signed-off-by: Jean Privat <jean@pryen.org>

src/compiling/compiling.nit
src/compiling/compiling_base.nit

index d89a2d6..883e388 100644 (file)
@@ -120,14 +120,17 @@ redef class MMModule
                f.write("#ifndef {name}_sep\n")
                f.write("#define {name}_sep\n")
                for m in mhe.direct_greaters do f.write("#include \"{m.name}._sep.h\"\n")
-               f.write(v.ctx.decls.join("\n"))
-               f.write("\n#endif\n")
+               for s in v.ctx.decls do
+                       f.write(s)
+               end
+               f.write("#endif\n")
                f.close
                var f = new OFStream.open("{tc.compdir}/{name}._sep.c")
                f.write("/* This C file is generated by NIT to compile module {name}. */\n")
                f.write("#include \"{name}._sep.h\"\n")
-               f.write(v.ctx.instrs.join("\n"))
-               f.write("\n")
+               for s in v.ctx.instrs do
+                       f.write(s)
+               end
                f.close
        end
 end
index f92e00b..0e3d6f0 100644 (file)
@@ -37,30 +37,37 @@ end
 # Note also that this class is unefficient and poorly designed thus requires love.
 class CompilerVisitor
        # Add a line in the current declaration block
-       fun add_decl(s: String)
+       fun add_decl(s: String...)
        do
-               if _indent_level >= 8 then
-                       _ctx.decls.add("\t\t" + s)
-               else
-                       _ctx.decls.add("  " * _indent_level + s)
-               end
+               add_line_to(_ctx.decls, s)
        end
 
        # Add a line in the current instr block
-       fun add_instr(s: String)
+       fun add_instr(s: String...)
+       do
+               add_line_to(_ctx.instrs, s)
+       end
+
+       fun add_line_to(a: Array[String], s: Array[String])
        do
                if _indent_level >= 8 then
-                       _ctx.instrs.add("\t\t" + s)
+                       a.add("\t\t")
                else
-                       _ctx.instrs.add("  " * _indent_level + s)
+                       for i in [0.._indent_level[ do
+                               a.add("  ")
+                       end
+               end
+               for i in s do
+                       a.add(i)
                end
+               a.add("\n")
        end
 
        # Add a assignment between a variable and an expression
        fun add_assignment(v: String, s: String)
        do
                if v != s then
-                       add_instr("{v} = {s};")
+                       add_instr(v, " = ", s, ";")
                end
        end
 
@@ -94,8 +101,7 @@ class CompilerVisitor
                var out = new Array[String]
                out.append(_ctx.decls)
                out.append(_ctx.instrs)
-               out.add("")
-               return out.join("\n")
+               return out.to_s
        end
 
        # The processed module