public void Visit(ClassNode node)
        {
            string cclass;

            cclass = VariableManager.CurrentClass = node.TypeClass.Text;
            Code.Add(new InheritCodeLine(node.TypeClass.Text, Scope.GetType(node.TypeClass.Text).Parent.Text));

            //VirtualTable.DefineClass(VariableManager.CurrentClass);
            int self = VariableManager.VariableCounter = 0;

            VariableManager.IncrementVariableCounter();
            VariableManager.PushVariableCounter();

            List <AttributeNode> attributes = new List <AttributeNode>();
            List <MethodNode>    methods    = new List <MethodNode>();

            foreach (var f in node.FeatureNodes)
            {
                if (f is AttributeNode)
                {
                    attributes.Add((AttributeNode)f);
                }
                else
                {
                    methods.Add((MethodNode)f);
                }
            }


            foreach (var method in methods)
            {
                method.Accept(this);
            }


            //begin constructor function

            Code.Add(new LabelCodeLine(VariableManager.CurrentClass, "constructor"));
            Code.Add(new ParamCodeLine(self));

            //calling first the parent constructor method
            if (VariableManager.CurrentClass != "Object")
            {
                Code.Add(new PushParamCodeLine(self));
                LabelCodeLine label = new LabelCodeLine(node.TypeInherit.Text, "constructor");
                Code.Add(new CallLabelCodeLine(label));
                Code.Add(new PopParamCodeLine(1));
            }


            foreach (var method in methods)
            {
                (string, string)label = ClassManager.GetDefinition(node.TypeClass.Text, method.Id.Text);
                Code.Add(new CommentCodeLine("set method: " + label.Item1 + "." + label.Item2));
                Code.Add(new AssignmentLabelToMemoryCodeLine(self, new LabelCodeLine(label.Item1, label.Item2), ClassManager.GetOffset(node.TypeClass.Text, method.Id.Text)));
            }


            foreach (var attr in attributes)
            {
                VariableManager.PushVariableCounter();
                attr.Accept(this);
                VariableManager.PopVariableCounter();
                Code.Add(new CommentCodeLine("set attribute: " + attr.Formal.Id.Text));
                Code.Add(new AssignmentVariableToMemoryCodeLine(self, VariableManager.PeekVariableCounter(), ClassManager.GetOffset(node.TypeClass.Text, attr.Formal.Id.Text)));
            }


            Code.Add(new CommentCodeLine("set class name: " + node.TypeClass.Text));
            Code.Add(new AssignmentStringToMemoryCodeLine(0, node.TypeClass.Text, 0));
            Code.Add(new CommentCodeLine("set class size: " + ClassManager.GetSizeClass(node.TypeClass.Text) + " words"));
            Code.Add(new AssignmentConstantToMemoryCodeLine(0, ClassManager.GetSizeClass(node.TypeClass.Text), 1));
            Code.Add(new CommentCodeLine("set class generation label"));
            Code.Add(new AssignmentLabelToMemoryCodeLine(0, new LabelCodeLine("_class", node.TypeClass.Text), 2));

            Code.Add(new ReturnCodeLine(-1));

            VariableManager.PopVariableCounter();
        }
コード例 #2
0
 public ConditionalJumpCodeLine(int conditional_var, LabelCodeLine label)
 {
     Label          = label;
     ConditionalVar = conditional_var;
 }
コード例 #3
0
 public CallLabelCodeLine(LabelCodeLine method, int result_variable = -1)
 {
     Method = method;
     Result = result_variable;
 }
コード例 #4
0
 public GotoJumpCodeLine(LabelCodeLine label)
 {
     Label = label;
 }