Commit ddc2bb22 authored by Matthew Dawson's avatar Matthew Dawson
Browse files

Variables really should be emitted in declared order.

Since variable subtypes can depend upon previously declared variables, emit
the variables in the same order as defined.  While some languages may not care,
it is better to be safe then sorry.

Also, since variables are now always emitted in defined order, remove some
excess try .. catch blocks, which were attempting to handle the unknown order.
parent 46d66936
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ import ca.mcmaster.cas.tabularexpressiontoolbox.parsers.VariableParserParser.Rea
import ca.mcmaster.cas.tabularexpressiontoolbox.parsers.VariableParserParser.VarContext;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

@@ -159,7 +160,7 @@ final public class VariableParser {
            m_variableCollection = new VariableCollection(m_variables, m_usedEnumerationTypes);
        }

        private Map<String, Variable>  m_variables = new HashMap<String, Variable>();
        private Map<String, Variable>  m_variables = new LinkedHashMap<String, Variable>();
        private Set<EnumerationVariableType> m_usedEnumerationTypes = new HashSet<EnumerationVariableType>();
        public VariableCollection m_variableCollection;

+2 −12
Original line number Diff line number Diff line
@@ -99,20 +99,10 @@ public class VariableParserTest {
    public void testMultiVariableOutput() {
        VariableParser variableParser = new VariableParser();
        VariableCollection variableCollection = variableParser.parseVariables("a,b");
        try {
            assertEquals("a:REAL;\nb:REAL\n;", generator.GenerateVariablesDeclaration(variableCollection));
        } catch(AssertionError e) {
            //Depending upon maps sort order, either a or be may be first.  Either is correct, so if the first test fails, retry with order reversed.
            assertEquals("b:REAL;\na:REAL;\n", generator.GenerateVariablesDeclaration(variableCollection));
        }
        assertEquals("a:REAL;\nb:REAL;\n", generator.GenerateVariablesDeclaration(variableCollection));

        variableCollection = variableParser.parseVariables("a:fixedt(1,16,9),b");
        try {
            assertEquals("a:BITVECTOR(16);\nb:REAL\n;", generator.GenerateVariablesDeclaration(variableCollection));
        } catch(AssertionError e) {
            //Depending upon maps sort order, either a or be may be first.  Either is correct, so if the first test fails, retry with order reversed.
            assertEquals("b:REAL;\na:BITVECTOR(16);\n", generator.GenerateVariablesDeclaration(variableCollection));
        }
        assertEquals("a:BITVECTOR(16);\nb:REAL;\n", generator.GenerateVariablesDeclaration(variableCollection));
    }
    @Test
    public void testSubtypeTypeExtraction() {