Пример #1
0
        public void CheckSemantic(LocalOrFieldInit node, IScope scope = null)
        {
            if (!scope.IsDefinedSymbol(node.Name, out var symbol))
            {
                Logger.LogError(node.Line, node.CharPositionInLine,
                                $"The type '{scope}' does contain a definition for '{node.Name}'");
                return;
            }
            if (node.Initialization == null)
            {
                node.ComputedType = symbol.Type;
                return;
            }

            CheckSemantic(node.Initialization, scope);

            if ((CoolType)node.Initialization.ComputedType <= (CoolType)symbol.Type)
            {
                node.ComputedType = node.Initialization.ComputedType;
            }
            else
            {
                Logger.LogError(node.Initialization.Line, node.Initialization.CharPositionInLine,
                                $"Cannot implicitly convert type '{node.Initialization.ComputedType}' to '{symbol.Type}'");
            }
        }
Пример #2
0
        public void CheckSemantic(LocalOrFieldInit node, IScope scope = null)
        {
            if (node.Name == "self")
            {
                Logger.LogError(node.Line, node.CharPositionInLine,
                                $"It is illegal to have attributes named self");
                return;
            }

            if (!TypeTable.IsDefinedType(node.TypeObj, out var type))
            {
                Logger.LogError(node.Line, node.CharPositionInLine,
                                $"The type '{node.TypeObj}' could not be found");
                return;
            }

            var cls = scope as IType;

            if (cls.IsDefinedSymbol(node.Name, out var symbol) &&
                node.TypeObj != symbol.Type.Name)
            {
                Logger.LogError(node.Line, node.CharPositionInLine,
                                $"The type '{cls}' already contains a definition for '{node.Name}'");
                return;
            }

            node.Symbol = new Symbol(node.Name, type, true);
            if (!cls.DefineSymbol(node.Symbol))
            {
                Logger.LogError(node.Line, node.CharPositionInLine,
                                $"The type '{cls}' already contains a definition for '{node.Name}'");
            }
        }
Пример #3
0
        public void GenerateCode(LocalOrFieldInit node, ICIL_CodeGenerator codeGenerator)
        {
            node.Symbol.Holder = codeGenerator.DefineVariable();

            // add Local Variable
            codeGenerator.AddLocalVariable(
                new CIL_LocalVariable((Variable)node.Symbol.Holder));

            if (node.Initialization == null)
            {
                switch (node.TypeObj)
                {
                    #region Int, Bool

                case "Int":
                case "Bool":

                    codeGenerator.AddInstruction(
                        new Allocate((Variable)node.Symbol.Holder, node.TypeObj));

                    var byDefault = codeGenerator.DefineVariable();

                    // add Local Variable
                    codeGenerator.AddLocalVariable(
                        new CIL_LocalVariable(byDefault));

                    codeGenerator.AddInstruction(
                        new Assign(byDefault, 0));

                    codeGenerator.AddInstruction(
                        new SetAttr((Variable)node.Symbol.Holder,
                                    $"{node.TypeObj}_value",
                                    byDefault));

                    break;

                    #endregion

                    #region String

                case "String":

                    var empty = CIL_Factory.Data.Single(data =>
                                                        data.Name == "empty");

                    var str = codeGenerator.DefineVariable();

                    // add Local Variable
                    codeGenerator.AddLocalVariable(
                        new CIL_LocalVariable(str));

                    codeGenerator.AddInstruction(
                        new Load(str, empty));

                    codeGenerator.AddInstruction(
                        new Allocate((Variable)node.Symbol.Holder, node.TypeObj));

                    codeGenerator.AddInstruction(
                        new SetAttr((Variable)node.Symbol.Holder,
                                    $"{node.TypeObj}_addrStr",
                                    str));
                    break;

                    #endregion

                    #region Others

                default:

                    codeGenerator.AddInstruction(
                        new Assign((Variable)node.Symbol.Holder, -1));

                    break;

                    #endregion
                }
            }
            else
            {
                GenerateCode(node.Initialization, codeGenerator);

                codeGenerator.AddInstruction(
                    new Assign((Variable)node.Symbol.Holder, node.Initialization.Holder));
            }
        }