Ejemplo n.º 1
0
        public string Emit()
        {
            StringBuilder sb = new StringBuilder();

            StackSegment stack = new StackSegment();

            sb.AppendLine(stack.Generate());

            if (mDataSegment.Count > 0)
            {
                sb.AppendLine(DataSegmentElement.GetDataSegmentKeyword());
                foreach (DataSegmentElement data in mDataSegment)
                {
                    sb.AppendLine(data.Generate());
                }
            }
            sb.AppendLine(CodeSegmentElement.GetCodeSegmentKeyword());

            if (mCodeSegment.ContainsKey("init"))
            {
                sb.AppendLine("init:");
                foreach (CodeSegmentElement elm in mCodeSegment["init"])
                {
                    sb.AppendLine(elm.Generate());
                }
                mCodeSegment.Remove("init");
            }
            if (mCodeSegment.ContainsKey("main"))
            {
                sb.AppendLine("start:");
                sb.AppendLine(Commands.GetCommand(Command.call, "main"));
                sb.AppendLine("teminate:");
                sb.AppendLine(Commands.GetCommand(Command.exit));
                sb.AppendLine(Commands.GetCommand(Command.nop));
            }
            foreach (string label in mCodeSegment.Keys)
            {
                foreach (CodeSegmentElement elm in mCodeSegment[label])
                {
                    sb.AppendLine(elm.Generate());
                }
            }
            return(sb.ToString());
        }
Ejemplo n.º 2
0
        public override void CaseAVariableDefinition(AVariableDefinition node)
        {
            // create the variable in the data segment
            DataSegmentElement elm = new DataSegmentElement(node.GetName().Text, TypeEnvironment.GetVxcTypeOf(node.GetTypeSpecifier().GetType()));

            mOutputFile.mDataSegment.Add(elm);

            // create initializer expression in code segment.
            PExpression pexp = node.GetInit();

            if (pexp != null)
            {
                long value = TypeEnvironment.GetValueOf(pexp);
                VariableInitializerExpression exp = new VariableInitializerExpression(value, elm.Type, elm.Identifier);
                mOutputFile.AddTemplate("init", exp);
            }

            base.CaseAVariableDefinition(node);
        }