Esempio n. 1
0
 public LuaStructDeclarationSyntax(LuaIdentifierNameSyntax name)
 {
     UpdateIdentifiers(name, LuaIdentifierNameSyntax.Namespace, LuaIdentifierNameSyntax.Struct, LuaIdentifierNameSyntax.Namespace);
 }
Esempio n. 2
0
 public LuaKeyValueTableItemSyntax(LuaIdentifierNameSyntax key, LuaExpressionSyntax value) : this(new LuaTableLiteralKeySyntax(key), value)
 {
 }
Esempio n. 3
0
 public LuaClassDeclarationSyntax(LuaIdentifierNameSyntax name)
 {
     UpdateIdentifiers(name, LuaIdentifierNameSyntax.Namespace, LuaIdentifierNameSyntax.Class, LuaIdentifierNameSyntax.Namespace);
 }
 public LuaPropertyOrEventIdentifierNameSyntax(bool isProperty, bool isGetOrAdd, LuaIdentifierNameSyntax name) : base(string.Empty)
 {
     IsProperty = isProperty;
     IsGetOrAdd = isGetOrAdd;
     Name       = name;
 }
 private void CheckLocalSymbolName(ISymbol symbol, ref LuaIdentifierNameSyntax name) {
   var newName = localReservedNames_.GetOrDefault(symbol);
   if (newName != null) {
     name = newName;
   }
 }
Esempio n. 6
0
 internal void Render(LuaIdentifierNameSyntax node)
 {
     Write(node.ValueText);
 }
 public QueryIdentifier(SyntaxToken identifier, LuaIdentifierNameSyntax name)
 {
     Identifier = identifier;
     name_      = name;
 }
        public LuaSyntaxNode BuildQueryBody(QueryBodySyntax node, LuaExpressionSyntax fromClauseExpression, LuaIdentifierNameSyntax rangeVariable)
        {
            LuaExpressionSyntax collection = fromClauseExpression;

            foreach (var clause in node.Clauses)
            {
                switch (clause.Kind())
                {
                case SyntaxKind.WhereClause: {
                    collection = BuildQueryWhere(collection, (WhereClauseSyntax)clause, rangeVariable);
                    break;
                }

                case SyntaxKind.OrderByClause: {
                    collection = BuildQueryOrderBy(collection, (OrderByClauseSyntax)clause, rangeVariable);
                    break;
                }

                default: {
                    throw new NotSupportedException();
                }
                }
            }
            if (node.SelectOrGroup.IsKind(SyntaxKind.SelectClause))
            {
                var selectClause = (SelectClauseSyntax)node.SelectOrGroup;
                collection = BuildQuerySelect(collection, selectClause, rangeVariable);
            }
            else
            {
                var groupClause = (GroupClauseSyntax)node.SelectOrGroup;
                collection = BuildGroupClause(collection, groupClause, rangeVariable);
            }
            return(collection);
        }
        private LuaExpressionSyntax BuildQueryWhere(LuaExpressionSyntax collection, WhereClauseSyntax node, LuaIdentifierNameSyntax rangeVariable)
        {
            var condition     = (LuaExpressionSyntax)node.Condition.Accept(this);
            var whereFunction = new LuaFunctionExpressionSyntax();

            whereFunction.AddParameter(rangeVariable);
            whereFunction.AddStatement(new LuaReturnStatementSyntax(condition));
            return(new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.LinqWhere, collection, whereFunction));
        }
        private LuaExpressionSyntax BuildQuerySelect(LuaExpressionSyntax collection, SelectClauseSyntax node, LuaIdentifierNameSyntax rangeVariable)
        {
            var expression = (LuaExpressionSyntax)node.Expression.Accept(this);

            if (node.Expression.IsKind(SyntaxKind.IdentifierName))
            {
                var identifierName = expression as LuaIdentifierNameSyntax;
                if (identifierName != null && identifierName.ValueText == rangeVariable.ValueText)
                {
                    return(collection);
                }
            }
            var type           = semanticModel_.GetTypeInfo(node.Expression).Type;
            var typeExpression = GetTypeName(type);

            return(new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.LinqSelect, collection, expression, typeExpression));
        }
        private LuaExpressionSyntax BuildGroupClause(LuaExpressionSyntax collection, GroupClauseSyntax node, LuaIdentifierNameSyntax rangeVariable)
        {
            var type         = semanticModel_.GetTypeInfo(node.ByExpression).Type;
            var typeName     = GetTypeName(type);
            var byExpression = (LuaExpressionSyntax)node.ByExpression.Accept(this);
            var keySelector  = new LuaFunctionExpressionSyntax();

            keySelector.AddParameter(rangeVariable);
            keySelector.AddStatement(new LuaReturnStatementSyntax(byExpression));
            return(new LuaInvocationExpressionSyntax(LuaIdentifierNameSyntax.LinqGroupBy, collection, keySelector, typeName));
        }
 private LuaExpressionSyntax BuildQueryOrderBy(LuaExpressionSyntax collection, OrderByClauseSyntax node, LuaIdentifierNameSyntax rangeVariable)
 {
     foreach (var ordering in node.Orderings)
     {
         bool isDescending = ordering.AscendingOrDescendingKeyword.IsKind(SyntaxKind.DescendingKeyword);
         if (ordering == node.Orderings.First())
         {
             var methodName = isDescending ? LuaIdentifierNameSyntax.LinqOrderByDescending : LuaIdentifierNameSyntax.LinqOrderBy;
             collection = BuildOrdering(methodName, collection, ordering, rangeVariable);
         }
         else
         {
             var methodName = isDescending ? LuaIdentifierNameSyntax.LinqThenByDescending : LuaIdentifierNameSyntax.LinqThenBy;
             collection = BuildOrdering(methodName, collection, ordering, rangeVariable);
         }
     }
     return(collection);
 }
 public LuaStringLiteralExpressionSyntax(LuaIdentifierNameSyntax identifier)
 {
     Identifier = identifier;
 }
    private LuaNumericalForStatementSyntax GetNumericalForStatement(ForStatementSyntax node) {
      if (node.Declaration == null || node.Declaration.Variables.Count > 1) {
        goto Fail;
      }

      if (node.Condition == null) {
        goto Fail;
      }

      if (node.Incrementors.Count != 1) {
        goto Fail;
      }

      var variable = node.Declaration.Variables.First();
      if (variable.Initializer == null) {
        goto Fail;
      }

      var conditionKind = node.Condition.Kind();
      if (conditionKind < SyntaxKind.NotEqualsExpression || conditionKind > SyntaxKind.GreaterThanOrEqualExpression) {
        goto Fail;
      }

      var condition = (BinaryExpressionSyntax)node.Condition;
      if (!IsNumericalForVariableMatch(condition.Left, variable.Identifier)) {
        goto Fail;
      }

      var limitConst = semanticModel_.GetConstantValue(condition.Right);
      if (limitConst.HasValue) {
        if (!(limitConst.Value is int)) {
          goto Fail;
        }
      } else {
        bool isReadOnly = false;
        var symbol = semanticModel_.GetSymbolInfo(condition.Right).Symbol;
        if (symbol != null) {
          if (symbol.Kind == SymbolKind.Field) {
            isReadOnly = ((IFieldSymbol)symbol).IsReadOnly;
          } else if (symbol.Kind == SymbolKind.Property) {
            var propertySymbol = (IPropertySymbol)symbol;
            isReadOnly = propertySymbol.IsReadOnly && IsPropertyField(propertySymbol);
          }
        } 
        if (!isReadOnly) {
          goto Fail;
        }
      }

      bool hasNoEqual;
      bool isPlus;
      var incrementor = node.Incrementors.First();
      switch (incrementor.Kind()) {
        case SyntaxKind.PreIncrementExpression:
        case SyntaxKind.PreDecrementExpression:
          var prefixUnaryExpression = (PrefixUnaryExpressionSyntax)incrementor;
          if (!IsNumericalForVariableMatch(prefixUnaryExpression.Operand, variable.Identifier)) {
            goto Fail;
          }
          if (incrementor.IsKind(SyntaxKind.PreIncrementExpression)) {
            if (!IsNumericalForLess(conditionKind, out hasNoEqual)) {
              goto Fail;
            }
            isPlus = true;
          } else {
            if (!IsNumericalForGreater(conditionKind, out hasNoEqual)) {
              goto Fail;
            }
            isPlus = false;
          }
          break;
        case SyntaxKind.PostIncrementExpression:
        case SyntaxKind.PostDecrementExpression:
          var postfixUnaryExpression = (PostfixUnaryExpressionSyntax)incrementor;
          if (!IsNumericalForVariableMatch(postfixUnaryExpression.Operand, variable.Identifier)) {
            goto Fail;
          }
          if (incrementor.IsKind(SyntaxKind.PostIncrementExpression)) {
            if (!IsNumericalForLess(conditionKind, out hasNoEqual)) {
              goto Fail;
            }
            isPlus = true;
          } else {
            if (!IsNumericalForGreater(conditionKind, out hasNoEqual)) {
              goto Fail;
            }
            isPlus = false;
          }
          break;
        default:
          goto Fail;
      }

      LuaIdentifierNameSyntax identifier = new LuaIdentifierNameSyntax(variable.Identifier.ValueText);
      CheckLocalVariableName(ref identifier, variable);

      var startExpression = (LuaExpressionSyntax)variable.Initializer.Value.Accept(this);
      LuaExpressionSyntax limitExpression;
      LuaExpressionSyntax stepExpression = null;
      if (hasNoEqual) {
        if (limitConst.Value != null) {
          int limit = (int)limitConst.Value;
          if (isPlus) {
            --limit;
          } else {
            ++limit;
            stepExpression = new LuaPrefixUnaryExpressionSyntax(LuaIdentifierNameSyntax.One, LuaSyntaxNode.Tokens.Sub);
          }
          limitExpression = new LuaIdentifierLiteralExpressionSyntax(limit.ToString());
        } else {
          limitExpression = (LuaExpressionSyntax)condition.Right.Accept(this);
          if (isPlus) {
            limitExpression = new LuaBinaryExpressionSyntax(limitExpression, LuaSyntaxNode.Tokens.Sub, LuaIdentifierNameSyntax.One);
          } else {
            limitExpression = new LuaBinaryExpressionSyntax(limitExpression, LuaSyntaxNode.Tokens.Plus, LuaIdentifierNameSyntax.One);
            stepExpression = new LuaPrefixUnaryExpressionSyntax(LuaIdentifierNameSyntax.One, LuaSyntaxNode.Tokens.Sub);
          }
        }
      } else {
        limitExpression = (LuaExpressionSyntax)condition.Right.Accept(this);
        if (!isPlus) {
          stepExpression = new LuaPrefixUnaryExpressionSyntax(LuaIdentifierNameSyntax.One, LuaSyntaxNode.Tokens.Sub);
        }
      }
    
      var numericalForStatement = new LuaNumericalForStatementSyntax(identifier, startExpression, limitExpression, stepExpression);
      VisitLoopBody(node.Statement, numericalForStatement.Body);
      return numericalForStatement;

    Fail:
      return null;
    }
Esempio n. 15
0
 public LuaInterfaceDeclarationSyntax(LuaIdentifierNameSyntax name)
 {
     UpdateIdentifiers(name, LuaIdentifierNameSyntax.Namespace, LuaIdentifierNameSyntax.Interface);
 }
        private LuaExpressionSyntax BuildOrdering(LuaIdentifierNameSyntax methodName, LuaExpressionSyntax collection, OrderingSyntax node, LuaIdentifierNameSyntax rangeVariable)
        {
            var type        = semanticModel_.GetTypeInfo(node.Expression).Type;
            var typeName    = GetTypeName(type);
            var expression  = (LuaExpressionSyntax)node.Expression.Accept(this);
            var keySelector = new LuaFunctionExpressionSyntax();

            keySelector.AddParameter(rangeVariable);
            keySelector.AddStatement(new LuaReturnStatementSyntax(expression));
            return(new LuaInvocationExpressionSyntax(methodName, collection, keySelector, LuaIdentifierNameSyntax.Nil, typeName));
        }
Esempio n. 17
0
 public LuaEnumDeclarationSyntax(string fullName, LuaIdentifierNameSyntax name, LuaCompilationUnitSyntax compilationUnit)
 {
     FullName        = fullName;
     CompilationUnit = compilationUnit;
     UpdateIdentifiers(name, LuaIdentifierNameSyntax.Namespace, LuaIdentifierNameSyntax.Enum);
 }
Esempio n. 18
0
        private LuaExpressionSyntax CreateQueryAnonymousType(LuaIdentifierNameSyntax key1, LuaExpressionSyntax value1, LuaIdentifierNameSyntax key2, LuaExpressionSyntax value2)
        {
            LuaTableExpression table = new LuaTableExpression();

            table.Add(key1, value1);
            table.Add(key2, value2);
            return(table);
        }
        private LuaExpressionSyntax CreateQueryAnonymousType(LuaIdentifierNameSyntax key1, LuaExpressionSyntax value1, LuaIdentifierNameSyntax key2, LuaExpressionSyntax value2)
        {
            LuaTableInitializerExpression table = new LuaTableInitializerExpression();

            table.Items.Add(new LuaKeyValueTableItemSyntax(new LuaTableLiteralKeySyntax(key1), value1));
            table.Items.Add(new LuaKeyValueTableItemSyntax(new LuaTableLiteralKeySyntax(key2), value2));
            return(table);
        }
        public override LuaSyntaxNode VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
        {
            IMethodSymbol ctorSymbol = semanticModel_.GetDeclaredSymbol(node);

            methodInfos_.Push(new MethodInfo(ctorSymbol));

            LuaConstructorAdapterExpressionSyntax function = new LuaConstructorAdapterExpressionSyntax();

            PushFunction(function);
            bool isStatic = node.Modifiers.IsStatic();

            function.AddParameter(LuaIdentifierNameSyntax.This);
            var parameterList = (LuaParameterListSyntax)node.ParameterList.Accept(this);

            function.ParameterList.Parameters.AddRange(parameterList.Parameters);

            if (node.Initializer != null)
            {
                var symbol      = (IMethodSymbol)semanticModel_.GetSymbolInfo(node.Initializer).Symbol;
                int ctroCounter = GetConstructorIndex(symbol);
                LuaInvocationExpressionSyntax otherCtorInvoke;
                if (node.Initializer.IsKind(SyntaxKind.ThisConstructorInitializer))
                {
                    Contract.Assert(ctroCounter != 0);
                    LuaIdentifierNameSyntax thisCtor = new LuaIdentifierNameSyntax(LuaSyntaxNode.SpecailWord(LuaSyntaxNode.Tokens.Ctor + ctroCounter));
                    otherCtorInvoke           = new LuaInvocationExpressionSyntax(thisCtor);
                    function.IsInvokeThisCtor = true;
                }
                else
                {
                    otherCtorInvoke = BuildCallBaseConstructor(symbol.ReceiverType, ctroCounter);
                }
                otherCtorInvoke.AddArgument(LuaIdentifierNameSyntax.This);
                var arguments = BuildArgumentList(symbol, symbol.Parameters, node.Initializer.ArgumentList);
                otherCtorInvoke.AddArguments(arguments);
                function.AddStatement(otherCtorInvoke);
            }
            else if (!isStatic)
            {
                var baseCtorInvoke = BuildCallBaseConstructor(ctorSymbol.ContainingType);
                if (baseCtorInvoke != null)
                {
                    function.AddStatement(baseCtorInvoke);
                }
            }

            LuaBlockSyntax block = (LuaBlockSyntax)node.Body.Accept(this);

            function.AddStatements(block.Statements);
            PopFunction();
            if (isStatic)
            {
                CurType.SetStaticCtor(function);
            }
            else
            {
                CurType.AddCtor(function, node.ParameterList.Parameters.Count == 0);
            }

            methodInfos_.Pop();
            return(function);
        }
 public LuaPropertyOrEventIdentifierNameSyntax(bool isProperty, LuaIdentifierNameSyntax name) : this(isProperty, true, name)
 {
 }
Esempio n. 22
0
 public LuaTableLiteralKeySyntax(LuaIdentifierNameSyntax identifier)
 {
     Identifier = identifier;
 }
Esempio n. 23
0
 public void AddParameter(LuaIdentifierNameSyntax parameter)
 {
     ParameterList.Parameters.Add(parameter);
 }
 private void AddLocalVariableMapping(LuaIdentifierNameSyntax name, SyntaxNode node) {
   ISymbol symbol = semanticModel_.GetDeclaredSymbol(node);
   Contract.Assert(symbol != null);
   localReservedNames_.Add(symbol, name);
 }