Index: sasmodels/py2c.py
===================================================================
--- sasmodels/py2c.py	(revision 2badeca29cc1a284f655f99079641fba3e5e5822)
+++ sasmodels/py2c.py	(revision 937afef5eb4bbb05b74a63683302424b0954bc2e)
@@ -45,8 +45,8 @@
 12/07/2017, OE: Translation of integer division, '\\' in python, implemented in translate_integer_divide, called from visit_BinOp
 12/07/2017, OE: C variable definition handled in 'define_C_Vars'
+              : Python integer division, '//', translated to C in 'translate_integer_divide'
 """
 import ast
 import sys
-import os
 from ast import NodeVisitor
 
@@ -89,5 +89,5 @@
 
 #def to_source(node, indent_with=' ' * 4, add_line_information=False):
-def to_source(node, func_name, global_vectors):
+def to_source(node, func_name):
     """This function can convert a node tree back into python sourcecode.
     This is useful for debugging purposes, especially if you're dealing with
@@ -106,12 +106,6 @@
     of the nodes are added to the output.  This can be used to spot wrong line
     number information of statement nodes.
-
-    global_vectors arguments is a list of strings, holding the names of global
-    variables already declared as arrays
     """
-#    print(str(node))
-#    return
     generator = SourceGenerator(' ' * 4, False)
-    generator.global_vectors = global_vectors
 #    generator.required_functions = func_name
     generator.visit(node)
@@ -119,40 +113,4 @@
 #    return ''.join(generator.result)
     return ''.join(generator.c_proc)
-
-EXTREN_DOUBLE = "extern double "
-
-def get_c_array_value(val):
-    val_str = str(val).strip()
-    val_str = val_str.replace('[','')
-    val_str = val_str.replace(']','')
-    val_str = val_str.strip()
-    double_blank = "  " in val_str
-    while (double_blank):
-        val_str = val_str.replace('  ',' ')
-        double_blank = "  " in val_str
-    val_str = val_str.replace(' ',',')
-    return (val_str)
-
-def write_include_const (snippets, constants):
-    c_globals=[]
-    snippets.append("#include <math.h>\n")
-    const_names = constants.keys()
-    for name in const_names:
-        c_globals.append(name)
-        val = constants[name]
-        var_dcl = "double "# + " = "
-        if (hasattr(val, "__len__")): # the value is an array
-            val_str = get_c_array_value(val)
-            var_dcl += str(name) + "[] = {" + str(val_str) + "}"
-        else:
-            var_dcl += str(name) + " = " + str(val)
-        var_dcl += ";\n"
-        snippets.append(var_dcl)
-            
-    return (c_globals)
-#    for c_var in constants:
-#        sTmp = EXTREN_DOUBLE + str(c_var) + ";"
-#        c_externs.append (EXTREN_DOUBLE + c_var + ";")
-#    snippets.append (c_externs)
 
 def isevaluable(s):
@@ -197,5 +155,4 @@
         self.is_sequence = False
         self.visited_args = False
-        self.global_vectors = []
 
     def write_python(self, x):
@@ -321,5 +278,11 @@
                     self.C_Vars.append (target.id)
 
+    def add_semi_colon (self):
+        semi_pos = self.current_statement.find(';')
+        if (semi_pos < 0):
+            self.write_c(';')
+
     def visit_Assign(self, node):
+        self.add_current_line ()
         for idx, target in enumerate(node.targets): # multi assign, as in 'a = b = c = 7'
             if idx:
@@ -334,5 +297,6 @@
         self.visited_args = False
         self.visit(node.value)
-        self.write_c(';')
+        self.add_semi_colon ()
+#        self.write_c(';')
         self.add_current_line ()
         for n,item in enumerate (self.Tuples):
@@ -340,8 +304,8 @@
             self.write_c(' = ')
             self.visit(item)
-            self.write_c(';')
+            self.add_semi_colon ()
             self.add_current_line ()
         if ((self.is_sequence) and (not self.visited_args)):
-            for target  in node.targets:
+            for target in node.targets:
                 if (hasattr (target, 'id')):
                     if ((target.id in self.C_Vars) and (target.id not in self.C_DclPointers)):
@@ -359,5 +323,6 @@
         self.write_c(' ' + BINOP_SYMBOLS[type(node.op)] + '= ')
         self.visit(node.value)
-        self.write_c(';')
+        self.add_semi_colon ()
+#        self.write_c(';')
         self.add_current_line ()
 
@@ -405,4 +370,14 @@
     def insert_C_Vars (self, start_var):
         fLine = False
+        start_var = self.write_C_Pointers (start_var)
+        if (len(self.C_IntVars) > 0):
+            for var in self.C_IntVars:
+                if (var in self.C_Vars):
+                    self.C_Vars.remove(var)
+            s = self.listToDeclare(self.C_IntVars)
+            self.c_proc.insert (start_var, "    int " + s + ";\n")
+            fLine = True
+            start_var += 1
+            
         if (len(self.C_Vars) > 0):
             s = self.listToDeclare(self.C_Vars)
@@ -410,9 +385,9 @@
             fLine = True
             start_var += 1
-        if (len(self.C_IntVars) > 0):
-            s = self.listToDeclare(self.C_IntVars)
-            self.c_proc.insert (start_var, "    int " + s + ";\n")
-            fLine = True
-            start_var += 1
+#        if (len(self.C_IntVars) > 0):
+#            s = self.listToDeclare(self.C_IntVars)
+#            self.c_proc.insert (start_var, "    int " + s + ";\n")
+#            fLine = True
+#            start_var += 1
         if (len (self.C_Vectors) > 0):
             s = self.listToDeclare(self.C_Vectors)
@@ -422,20 +397,9 @@
                 self.c_proc.insert (start_var, c_dcl + "\n")
                 start_var += 1
-        if (len (self.C_Pointers) > 0):
-            vars = ""
-            for n in range(len(self.C_Pointers)):
-                if (len(vars) > 0):
-                    vars += ", "
-                if (self.C_Pointers[n] not in self.arguments):
-                    vars += "*" + self.C_Pointers[n]
-            if (len(vars) > 0):
-                c_dcl = "    double " + vars + ";"
-                self.c_proc.insert (start_var, c_dcl + "\n")
-                start_var += 1
         self.C_Vars.clear()
         self.C_IntVars.clear()
         self.C_Vectors.clear()
         self.C_Pointers.clear()
-        self.C_DclPointers.clear()
+        self.C_DclPointers
         if (fLine == True):
             self.c_proc.insert (start_var, "\n")
@@ -450,9 +414,9 @@
             self.c_proc.insert (start_var + 1, "\n")
 
-#    def writeInclude (self):
-#        if (self.MathIncludeed == False):
-#            self.add_c_line("#include <math.h>\n")
-#            self.add_c_line("static double pi = 3.14159265359;\n")
-#            self.MathIncludeed = True
+    def writeInclude (self):
+        if (self.MathIncludeed == False):
+            self.add_c_line("#include <math.h>\n")
+            self.add_c_line("static double pi = 3.14159265359;\n")
+            self.MathIncludeed = True
 
     def ListToString (self, strings):
@@ -493,5 +457,5 @@
         self.name = node.name
 #        if self.name not in self.required_functions[0]:
-#            return
+#           return
         print("Parsing '" + self.name + "'")
         args_str = ""
@@ -499,5 +463,5 @@
         self.visit(node.args)
 # for C
-#        self.writeInclude()
+        self.writeInclude()
         self.getMethodSignature ()
 # for C
@@ -583,21 +547,60 @@
         return (line_number)
 
+    def GetNodeAsString (self, node):
+        res = ''
+        if (hasattr(node,'n')):
+            res = str(node.n)
+        elif (hasattr(node,'id')):
+            res = node.id
+        return (res)
+
+    def GetForRange(self, node):
+        stop = ""
+        start = '0'
+        step = '1'
+        for_args = []
+        temp_statement = self.current_statement
+        self.current_statement = ''
+        for arg in node.iter.args:
+            self.visit(arg)
+            for_args.append(self.current_statement)
+            self.current_statement = ''
+        self.current_statement = temp_statement
+        if (len(for_args) == 1):
+            stop = for_args[0]
+        elif (len(for_args) == 2):
+            start = for_args[0]
+            stop = for_args[1]
+        elif (len(for_args) == 3):
+            start = for_args[0]
+            stop = for_args[1]
+            start = for_args[2]
+        else:
+            raise("Ilegal for loop parameters")
+        return (start, stop, step)
+
     def visit_For(self, node):
-        if (len(self.current_statement) > 0):
-            self.add_c_line(self.current_statement)
-            self.current_statement = ''
+# node: for iterator is stored in node.target.
+# Iterator name is in node.target.id.
+        self.add_current_line()
+#        if (len(self.current_statement) > 0):
+#            self.add_c_line(self.current_statement)
+#            self.current_statement = ''
         fForDone = False
+        self.current_statement = ''
         if (hasattr(node.iter,'func')):
             if (hasattr (node.iter.func,'id')):
                 if (node.iter.func.id == 'range'):
-                    if ('n' not in self.C_IntVars):
-                        self.C_IntVars.append ('n')
-                    self.write_c ("for (n=0 ; n < len(")
-                    for arg in node.iter.args:
-                        self.visit(arg)
-                    self.write_c (") ; n++) {")
+                    self.visit(node.target)
+                    iterator = self.current_statement
+                    self.current_statement = ''
+                    if (iterator not in self.C_IntVars):
+                        self.C_IntVars.append (iterator)
+                    start, stop, step = self.GetForRange(node)
+                    self.write_c ("for (" + iterator + "=" + str(start) + \
+                                  " ; " + iterator + " < " + str(stop) + \
+                                  " ; " + iterator + " += " + str(step) + ") {")
                     self.body_or_else(node)
                     self.write_c ("}")
-                    self.add_current_line () # just for breaking point. to be deleted. 
                     fForDone = True
         if (fForDone == False):
@@ -771,5 +774,5 @@
                 self.write_c('**')
                 self.visit(node.kwargs)
-        self.write_c(')')
+        self.write_c(');')
 
     def visit_Name(self, node):
@@ -784,10 +787,9 @@
             name = node.id
 #       add variable to C_Vars if it ins't there yet, not an argument and not a number
-        if (name not in self.global_vectors):
-            if ((name not in self.C_Functions) and (name not in self.C_Vars) and (name not in self.C_IntVars) and (name not in self.arguments) and (name.isnumeric () == False)):
-                if (self.InSubscript):
-                    self.C_IntVars.append (node.id)
-                else:
-                    self.C_Vars.append (node.id)
+        if ((name not in self.C_Functions) and (name not in self.C_Vars) and (name not in self.C_IntVars) and (name not in self.arguments) and (name.isnumeric () == False)):
+            if (self.InSubscript):
+                self.C_IntVars.append (node.id)
+            else:
+                self.C_Vars.append (node.id)
 
     def visit_Str(self, node):
@@ -969,8 +971,10 @@
         def visit(self, node):
             self.write_python(left)
+            self.write_c(left)
             self.visit(node.elt)
             for comprehension in node.generators:
                 self.visit(comprehension)
-            self.write_python(right)
+            self.write_c(right)
+#            self.write_python(right)
         return visit
 
@@ -1016,5 +1020,6 @@
         self.write_c(' for ')
         self.visit(node.target)
-        self.write_python(' in ')
+        self.write_C(' in ')
+#        self.write_python(' in ')
         self.visit(node.iter)
         if node.ifs:
@@ -1077,36 +1082,5 @@
         print(tree_source)
 
-
-def write_consts (constants):
-    f = False
-    if (constants is not None):
-        f = True
-
-def translate(functions, constants):
-    # type: (List[Tuple[str, str, int]], Dict[str, Any]) -> str
-#    print ("Size of functions is: " + str(len(functions)))
-    if (os.path.exists ("xlate.c")):
-        os.remove("xlate.c")
-    snippets = []
-    global_vectors = write_include_const (snippets, constants)
-    const_keys = constants.keys ()
-    for source, filename, offset in functions:
-        tree = ast.parse(source)
-        snippet = to_source(tree, filename, global_vectors)#, offset)
-        snippets.append(snippet)
-    if (constants is not None):
-        write_consts (constants)
-    c_text = "\n".join(snippets)
-    try:
-        translated = open ("xlate.c", "a+")
-        translated.write (c_text)
-        translated.close()
-    except Exception as excp:
-        strErr = "Error:\n" + str(excp.args)
-        print(strErr)
-    return (c_text)
-
-def translate_from_file (functions, constants=0):
-    print ("Size of functions is: " + len(functions))
+def translate (functions, constants=0):
     sniplets = []
     fname = functions[1]
@@ -1162,3 +1136,2 @@
         print ("Error:\n" + str(excp.args))
     print("...Done")
-#            "program": "${workspaceRoot}/ -m sascomp.generate",
