Example #1
0
        /// <summary>
        /// Parses a for command
        /// </summary>
        private ICommandNode ParseForCommand()
        {
            Debugger.Write("Parsing For Command");
            Position startPosition = CurrentToken.Position;

            Accept(TokenType.For);

            IdentifierNode identifier = ParseIdentifier();

            // Declare I as a new variable of type integer with an undefined value
            VarDeclarationNode declaration = new VarDeclarationNode(identifier,
                                                                    new TypeDenoterNode(
                                                                        new IdentifierNode(
                                                                            new Token(TokenType.Identifier, "Integer", startPosition))), startPosition);

            // Evaluate E1
            Accept(TokenType.Becomes);
            IExpressionNode becomesExpression = ParseExpression();

            // Assign I to the value of E1
            AssignCommandNode assign = new AssignCommandNode(identifier, becomesExpression);

            // Evaluate E2
            Accept(TokenType.To);
            IExpressionNode toExpression = ParseExpression();

            Accept(TokenType.Do);

            // Execute command
            ICommandNode command = ParseSingleCommand();

            Accept(TokenType.Next);

            return(new ForCommandNode(declaration, assign, toExpression, command, startPosition));
        }
        public void visitVarDeclaration(VarDeclarationNode node)
        {
            IdentifierNode identifier   = (IdentifierNode)node.getChildren()[0];
            string         variableName = identifier.getVariableName();

            if (this.forLoopControlVariables.Count > 0)
            {
                throw new SemanticException("Declaring variables inside for loop is not allowed.");
            }
            if (variableAlreadyDeclared(variableName))
            {
                throw new SemanticException("Variable '" + variableName + "' already declared.");
            }
            TypeNode typeNode = (TypeNode)node.getChildren()[1];

            MiniPLTokenType type = (MiniPLTokenType)typeNode.getValue();

            if (type == MiniPLTokenType.TYPE_IDENTIFIER_INTEGER)
            {
                this.symbolTable.addVariable(variableName, 0);
            }
            else if (type == MiniPLTokenType.TYPE_IDENTIFIER_STRING)
            {
                this.symbolTable.addVariable(variableName, "");
            }
            else if (type == MiniPLTokenType.TYPE_IDENTIFIER_BOOL)
            {
                this.symbolTable.addVariable(variableName, false);
            }
            else
            {
                throw new Exception("Unknown type usage in semantic analyzer.");
            }
        }
Example #3
0
        /// <inheritdoc />
        public Node Visit(VarDeclarationParseNode vdpn)
        {
            Node val  = null;
            Node type = null;

            if (vdpn.Value != null)
            {
                val = vdpn.Value.Visit(this);
            }
            if (vdpn.Type != null)
            {
                type = vdpn.Type.Visit(this);
            }
            var ret = new VarDeclarationNode(vdpn.Token, vdpn,
                                             val, type);

            addAnnotations(vdpn.Annotations, ret.Annotations);
            if (vdpn.Annotations != null &&
                vdpn.Annotations.HasAnnotation("public"))
            {
                ret.Readable = true;
                ret.Writable = true;
            }
            else
            {
                ret.Readable = (vdpn.Annotations != null &&
                                vdpn.Annotations.HasAnnotation("readable"));
                ret.Writable = (vdpn.Annotations != null &&
                                vdpn.Annotations.HasAnnotation("writable"));
            }
            return(ret);
        }
        public string VisitVarDeclaration(VarDeclarationNode declaration)
        {
            var symbol = new VariableSymbol(declaration.VarNode.VariableName,
                                            CurrentScope.LookupSymbol(declaration.TypeNode.TypeValue, true));

            CurrentScope.Define(symbol);
            return($"{AddSpaces()}var {declaration.VarNode.VariableName}{CurrentScope.ScopeLevel} : {declaration.TypeNode.TypeValue.ToUpper()};\r\n");
        }
 /// <summary>
 /// Creates a new for node
 /// </summary>
 /// <param name="varDeclaration"></param>
 /// <param name="assignCommand"></param>
 /// <param name="toExpression"></param>
 /// <param name="command"></param>
 /// <param name="position"></param>
 public ForCommandNode(VarDeclarationNode varDeclaration, AssignCommandNode assignCommand, IExpressionNode toExpression, ICommandNode command, Position position)
 {
     VarDeclaration = varDeclaration;
     AssignCommand  = assignCommand;
     ToExpression   = toExpression;
     Command        = command;
     Position       = position;
 }
        public void visitVarDeclaration(VarDeclarationNode node)
        {
            IdentifierNode  identifier   = (IdentifierNode)node.getChildren()[0];
            string          variableName = identifier.getVariableName();
            TypeNode        typeNode     = (TypeNode)node.getChildren()[1];
            MiniPLTokenType type         = (MiniPLTokenType)typeNode.getValue();

            typeCheck(typeNode, type);
        }
Example #7
0
        /// <summary>
        /// Generates code for a var declaration node
        /// </summary>
        /// <param name="varDeclaration">The node to generate code for</param>
        private void GenerateCodeForVarDeclaration(VarDeclarationNode varDeclaration)
        {
            Debugger.Write("Generating code for Var Declaration");
            byte  variableSize     = varDeclaration.EntityType.Size;
            short currentScopeSize = scopes.GetLocalScopeSize();

            code.AddInstruction(OpCode.PUSH, 0, 0, variableSize);
            varDeclaration.RuntimeEntity = new RuntimeVariable(currentScopeSize, variableSize);
            scopes.AddToLocalScope(variableSize);
        }
        public override string VisitVarDeclaration(VarDeclarationNode varDeclaration)
        {
            var typeValue = varDeclaration.TypeNode.TypeValue.ToUpper();


            typeValue = this.typesConverter.GetTypeName(typeValue);
            if (varDeclaration.Annotations.ContainsKey("Global"))
            {
                return($"{AddSpaces()}static {typeValue} {varDeclaration.VarNode.VariableName};");
            }
            return($"{AddSpaces()}{typeValue} {varDeclaration.VarNode.VariableName}");
        }
Example #9
0
        public XmlElement Visit(VarDeclarationNode n)
        {
            var el = makeNode(n, "var-declaration");

            addProperty(el, "name", n.Name);
            addProperty(el, "value", n.Value);
            addProperty(el, "annotations", n.Annotations);
            addProperty(el, "type", n.Type);
            addProperty(el, "readable", n.Readable);
            addProperty(el, "writable", n.Writable);
            return(el);
        }
        protected override List <AssemblyElement> VisitVarDeclaration(VarDeclarationNode node)
        {
            List <AssemblyElement> instructions = new List <AssemblyElement>();

            if (node.RightSideNode != null)
            {
                instructions.AddRange(Visit(node.RightSideNode));
                instructions.Add(new PushVar(node.Symbol));
                instructions.Add(GetAssignInstruction(node.Symbol));
            }

            return(instructions);
        }
        /// <summary>
        /// Carries out identification on a var declaration node
        /// </summary>
        /// <param name="varDeclaration">The node to perform identification on</param>
        private void PerformIdentificationOnVarDeclaration(VarDeclarationNode varDeclaration)
        {
            Token token   = varDeclaration.Identifier.IdentifierToken;
            bool  success = SymbolTable.Enter(token.Spelling, varDeclaration);

            if (!success)
            {
                Reporter.ReportError($"{token.Position} -> Var declaration with {token.Spelling} " +
                                     $"already exists in the current scope");
            }

            PerformIdentification(varDeclaration.TypeDenoter);
        }
        /// <summary>
        /// Carries out identification on a var declaration node
        /// </summary>
        /// <param name="varDeclaration">The node to perform identification on</param>
        private void PerformIdentificationOntypeDenoterDeclaration(VarDeclarationNode varDeclaration)
        {
            Token token   = varDeclaration.Identifier.IdentifierToken;
            bool  success = SymbolTable.Enter(token.Spelling, varDeclaration);

            if (success)
            {
                PerformIdentification(varDeclaration.TypeDenoter);
            }
            else
            {
                IsItDirty();
                Reporter.IdentifierErrorPositions.Add($"Error has occured here: {token.Position}, var already declared");
            }
        }
        protected override void VisitVarDeclaration(VarDeclarationNode node)
        {
            if (node.RightSideNode != null)
            {
                ASTNode ancestorNode = node.GetFirstSignificantAncestorNode();
                switch (ancestorNode)
                {
                case PrototypeDefinitionNode _:
                case InstanceDefinitionNode _:
                case FunctionDefinitionNode _:
                    break;

                default:
                    node.Annotations.Add(new VarAssignmentNotAllowedHereError(node.Location));
                    break;
                }
            }
            //base.VisitVarDeclaration(node);
        }
Example #14
0
        public override dynamic Visit(VarDeclarationNode node)
        {
            foreach (var idNode in node.Ids)
            {
                var id = idNode.Accept(this);
                if (idNode.Type is ArrayTypeNode at)
                {
                    var size = at.Size.Accept(this);

                    if (size != null && size != PrimitiveType.Integer)
                    {
                        Context.ErrorService.Add(
                            ErrorType.Unknown,
                            idNode.Token,
                            $"array {id} size must be empty or an integer expression"
                            );
                    }
                    //throw new Exception($"array {id} size must be empty or an integer expression");
                }
            }

            return(null);
        }
Example #15
0
        public override dynamic Visit(VarDeclarationNode node)
        {
            CurrentBlock.AddStatement(node);

            return(null);
        }
Example #16
0
 /// <summary>
 /// Carries out identification on a var declaration node
 /// </summary>
 /// <param name="varDeclaration">The node to perform identification on</param>
 private void PerformIdentificationOnVarDeclaration(VarDeclarationNode varDeclaration)
 {
     PerformIdentification(varDeclaration.TypeDenoter);
     Token token   = varDeclaration.Identifier.IdentifierToken;
     bool  success = SymbolTable.Enter(token.Spelling, varDeclaration);
 }
Example #17
0
 public virtual T VisitVarDeclaration(VarDeclarationNode varDeclaration)
 {
     throw new NotImplementedException();
 }
Example #18
0
 public override dynamic Visit(VarDeclarationNode node)
 {
     return(null);
 }
 public void visitVarDeclaration(VarDeclarationNode node)
 {
     updateValue(node);
 }
Example #20
0
 public abstract dynamic Visit(VarDeclarationNode node);
Example #21
0
        public CodeBlock CreateStatement(VarDeclarationNode node)
        {
            var output = new CodeBlock();

            foreach (var id in node.Ids)
            {
                var t = NextTemporary;
                var v = new Variable
                {
                    Name  = id.Token.Content,
                    Scope = CurrentScope
                };

                AddTemporary(v, t);

                if (node.Type is SimpleTypeNode)
                {
                    output.Add(CodeLine.Of($"{VarSignature(node, t.Representation)};", id.Token.Content,
                                           id.Token.Line));
                }
                else
                {
                    HasArrays = true;
                    var at = (ArrayTypeNode)node.Type;

                    var size    = -1;
                    var type    = _types[at.SubType];
                    var sizeStr = "";

                    switch (at.Size)
                    {
                    case NoOpNode _:
                        t.Dynamic = true;
                        t.Size    = 0;
                        sizeStr   = "0";
                        break;

                    case ValueNode vn:
                        t.Size  = vn.Value;
                        sizeStr = $"{t.Size}";
                        break;

                    default:
                    {
                        var(sizeTemp, sizeCode) = ComputeTemporary(at.Size);
                        output.Add(sizeCode);
                        sizeStr = sizeTemp.Representation;
                        break;
                    }
                    }
                    // TODO: skip this if value
                    _arrayDefinitions.Add($"typedef _array({type}, {at.SubType});");
                    output.Add(CodeLine.Of($"_array_{at.SubType} {t.Representation};"));
                    output.Add(CodeLine.Of($"_initArray({t.Representation}, {sizeStr});",
                                           $"{id}: array[{(at.Size is NoOpNode ? "" : at.Size.ToString())}] of {at.Type.Token.Content}",
                                           node.Token.Line));
                    AddFreeable(t);
                }
            }

            return(output);
        }
 public string VisitVarDeclaration(VarDeclarationNode varDeclaration)
 {
     throw new NotImplementedException();
 }
Example #23
0
 public AddressNode VisitVarDeclaration(VarDeclarationNode varDeclaration)
 {
     throw new System.NotImplementedException();
 }