Inheritance: VariableReferance
        public void Visit(TableVariableReference variable)
        {
            IScopeData codeType;
            Scope scope = Scope.Current;

            if (!Scope.Current.IsRegistered(variable.Id)) //If variable doesn't exist we need to register Sematic error
            {
                Errors.Add(new UnknownVariableReferenceException(new Semantic.LineInfo(variable.Line.Line, variable.Line.CharacterPosition), variable.Id));
                codeType = new ScopeData<TableDescriptor> { Type = new TableDescriptor(null), CodeDomReference = new CodeTypeReference("Table", new CodeTypeReference(variable.Id)) };
            }
            else if (!Scope.Current.IsTableRegistered(variable.Id)) //has to be a table
            {
                Errors.Add(new NotTableVariableException(new Semantic.LineInfo(variable.Line.Line, variable.Line.CharacterPosition), variable.Id));
                codeType = new ScopeData<TableDescriptor> { Type = new TableDescriptor(null), CodeDomReference = new CodeTypeReference("Table", new CodeTypeReference(variable.Id)) };
            }
            else
            {
                scope = Scope.FindScope(variable.Id);
                codeType = scope.GetScope(variable.Id);
            }

            _codeStack.Peek().Scope = codeType;
            _codeStack.Peek().CodeExpression = new CodeVariableReferenceExpression("_" + scope.ScopeIdentifier + "." + variable.Id);
        }
Exemplo n.º 2
0
        public void Visit(TableVariableReference variable, CommonTree tree)
        {
            Parent(tree).Children.Add(variable);
            SetLine(variable, tree);

            variable.Id = tree.Text;
        }
        public void Visit(UpdateStatement statement)
        {
            using (Scope.PushSelect())
            {
                CodeMemberMethod method = new CodeMemberMethod();
                method.Name = "Update_" + Guid.NewGuid().ToString("N");
                method.Attributes = MemberAttributes.Private;
                GenerateCallStatement(method.Statements, statement.Line.Line);

                _mainType.Type.Members.Add(method);

                FromStatement fromStatement = null;
                if (statement.From == null) //alias must be a table ref
                {
                    var tableReference = new TableVariableReference() { Id = statement.Alias.Id, Line = statement.Alias.Line };
                    fromStatement = new FromStatement() { Line = tableReference.Line };
                    fromStatement.Children.Add(tableReference);

                }
                else
                    fromStatement = statement.From;

                var fromDomArg = VisitChild(fromStatement);
                var rowType = fromDomArg.Scope.CodeDomReference.TypeArguments[0];

                var type = Scope.Current.FindTypeWithAlias(statement.Alias.Id);
                if (type != null && type == typeof(FileTable<>)) //filetables cannot be updated only inserted into
                    Errors.Add(new FileTableImmutable(new Semantic.LineInfo(statement.Line.Line, statement.Line.CharacterPosition)));

                method.Statements.Add(new CodeVariableDeclarationStatement(fromDomArg.Scope.CodeDomReference,
                   "fromTable",
                   fromDomArg.CodeExpression));

                if (statement.Where != null)
                {
                    var domWhereArgs = VisitChild(statement.Where, new CodeDomArg() { Scope = fromDomArg.Scope });
                    method.Statements.Add(new CodeAssignStatement(new CodeVariableReferenceExpression("fromTable"), domWhereArgs.CodeExpression));
                }

                //outside iterator
                method.Statements.Add(new CodeVariableDeclarationStatement(new CodeTypeReference("IEnumerator", rowType),
                    "x",
                new CodeMethodInvokeExpression(
                        new CodeMethodReferenceExpression(new CodeTypeReferenceExpression("fromTable"), "GetEnumerator",
                        null))));

                var loop = new CodeIterationStatement();
                loop.InitStatement = new CodeSnippetStatement();
                loop.IncrementStatement = new CodeSnippetStatement();
                loop.TestExpression = new CodeMethodInvokeExpression(
                    new CodeMethodReferenceExpression(new CodeTypeReferenceExpression("x"), "MoveNext",
                    null));

                loop.Statements.Add(new CodeVariableDeclarationStatement(rowType,
                    "row",
                    new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("x"), "Current")));

                foreach (var a in statement.SetArgs.AssignStatements)
                {
                    var assignmentArg = VisitChild(a);
                    loop.Statements.AddRange(assignmentArg.ParentStatements);
                }

                method.Statements.Add(loop);

                var methodcall = new CodeMethodInvokeExpression(
                        new CodeMethodReferenceExpression(null, method.Name));
                _codeStack.Peek().CodeExpression = methodcall;
                _codeStack.Peek().ParentStatements.Add(methodcall);
            }
        }