lots of changes
[cdsspec-compiler.git] / src / edu / uci / eecs / specCompiler / codeGenerator / CodeGenerator.java
index 40f06429ee058759ab708364ab97fb791b149d12..3dbb595947a07018de3faa3eb1f8bd84a06cc5ce 100644 (file)
@@ -9,7 +9,6 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.Iterator;
 
-import edu.uci.eecs.specCompiler.specExtraction.ActionSubConstruct.DefineVar;
 import edu.uci.eecs.specCompiler.specExtraction.CPDefineCheckConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.CPDefineConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.ConditionalInterface;
@@ -17,8 +16,10 @@ import edu.uci.eecs.specCompiler.specExtraction.Construct;
 import edu.uci.eecs.specCompiler.specExtraction.GlobalConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.IDExtractor;
 import edu.uci.eecs.specCompiler.specExtraction.InterfaceConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.InterfaceDefineConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.PotentialCPDefineConstruct;
 import edu.uci.eecs.specCompiler.specExtraction.SequentialDefineSubConstruct;
+import edu.uci.eecs.specCompiler.specExtraction.SourceFileInfo;
 import edu.uci.eecs.specCompiler.specExtraction.SpecExtractor;
 
 /**
@@ -36,7 +37,7 @@ public class CodeGenerator {
 
        private File[] srcFiles;
 
-       private HashMap<File, ArrayList<String>> contents;
+       private HashMap<File, SourceFileInfo> srcFilesInfo;
 
        private HashMap<File, ArrayList<CodeAddition>> codeAdditions;
 
@@ -47,13 +48,13 @@ public class CodeGenerator {
                _extractor = new SpecExtractor();
                _extractor.extract(srcFiles);
                
-               this.contents = _extractor.contents;
+               this.srcFilesInfo = _extractor.srcFilesInfo;
                
                this.globalContent = null;
                this.codeAdditions = new HashMap<File, ArrayList<CodeAddition>>();
 
 
-               _semantics = new SemanticsChecker(_extractor.constructs);
+               _semantics = new SemanticsChecker(_extractor.getConstructs());
                try {
                        _semantics.check();
                        System.out.println(_semantics);
@@ -62,16 +63,6 @@ public class CodeGenerator {
                }
        }
 
-       private ArrayList<String> readSrcFile(File f) throws IOException {
-               BufferedReader bf = new BufferedReader(new FileReader(f));
-               ArrayList<String> content = new ArrayList<String>();
-               String curLine;
-               while ((curLine = bf.readLine()) != null) {
-                       content.add(curLine);
-               }
-               return content;
-       }
-
        /**
         * <p>
         * Generate all the global code, including the "@DefineVar" in each
@@ -88,16 +79,16 @@ public class CodeGenerator {
        // Mainly rename and wrap the interface
        private void interface2Code(InterfaceConstruct construct)
                        throws InterfaceWrongFormatException {
-               int lineNum = construct.begin + 1;
+               int lineNum = construct.beginLineNum;
+               String funcName = "";
 
                // Rename the interface name
-               File file = inst.file;
-               String funcDecl = inst.interfaceDeclBody;
+               
                // Rename the function declaration
-               String funcName = renameInterface(inst);
+               
                // Also rename the function definition if it's separated from the
                // declaration
-               SpecConstruct definition = _semantics.interfaceName2DefineConstruct
+               InterfaceDefineConstruct definition = (InterfaceDefineConstruct) _semantics.interfaceName2DefineConstruct
                                .get(construct.name);
                if (definition != null) {
                        String funcDefintionName = renameInterface(definition);
@@ -106,20 +97,20 @@ public class CodeGenerator {
 
                // Generate new wrapper
                ArrayList<String> newCode = CodeVariables.generateInterfaceWrapper(
-                               _semantics, inst);
+                               _semantics, construct);
                // Add it to the codeAdditions
                CodeAddition addition = new CodeAddition(lineNum, newCode);
-               if (!codeAdditions.containsKey(inst.file)) {
-                       codeAdditions.put(inst.file, new ArrayList<CodeAddition>());
+               if (!codeAdditions.containsKey(construct.file)) {
+                       codeAdditions.put(construct.file, new ArrayList<CodeAddition>());
                }
-               codeAdditions.get(inst.file).add(addition);
+               codeAdditions.get(construct.file).add(addition);
        }
 
        // Returns the function name that has been renamed and replace the old line
-       private String renameInterface(Construct inst)
+       private String renameInterface(Construct construct)
                        throws InterfaceWrongFormatException {
-               String funcDecl = inst.interfaceDeclBody;
-               ArrayList<String> content = contents.get(inst.file);
+               String funcDecl = "";
+               ArrayList<String> content = srcFilesInfo.get(construct.file).content;
 
                // Depending on "(" to find the function name, so it doesn't matter if
                // there's any template
@@ -139,7 +130,7 @@ public class CodeGenerator {
 
                int lineNumOfID = idExtractor.lineNumOfID();
                // Be careful: lineNum - 1 -> index of content array
-               content.set(inst.endLineNum + lineNumOfID, newLine);
+               content.set(construct.beginLineNum - 1, newLine);
                return funcName;
        }