compile: add program arguments passed to C compiler; gcc's -l, -L, -I and -c
[nit.git] / src / compiling / compiling_base.nit
index 4debc4f..a1f2f2d 100644 (file)
 # Common things for NIT compilation and C generation
 package compiling_base
 
-import syntax
+import mmloader
 private import utils
 import primitive_info
+import program
 
 redef class ToolContext
-       readable writable var _global: Bool = false
        readable writable var _compdir: nullable String = null
        readable writable var _clibdir: nullable String = null
        readable writable var _bindir: nullable String = null
        readable writable var _output_file: nullable String = null
        readable writable var _boost: Bool = false
        readable writable var _no_cc: Bool = false
+       readable writable var _cc_link: Bool = false
+       readable writable var _cc_libs: Array[String] = new Array[String]
+       readable writable var _cc_lib_paths: Array[String] = new Array[String]
+       readable writable var _cc_include_paths: Array[String] = new Array[String]
        readable writable var _ext_prefix: String = ""
 end
 
@@ -37,25 +41,43 @@ 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, ";")
                end
        end
 
+       # C outputs written outside the current C function.
+       readable writable var _out_contexts: Array[CContext] = new Array[CContext]
+
        # Return a unique new number for the instance
        fun new_number: Int
        do
@@ -83,8 +105,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
@@ -96,14 +117,14 @@ class CompilerVisitor
        # The current indent lever
        readable writable var _indent_level: Int = 0
 
-       # The ToolContext info
-       readable var _tc: ToolContext
+       # The program we are compiling
+       readable var _program: Program
 
        # Create a new CompilerVisitor based on a module
-       init(module: MMModule, tc: ToolContext)
+       init(module: MMModule, p: Program)
        do
                _module = module
-               _tc = tc
+               _program = p
        end
 end