Exemplo n.º 1
0
 public override INode VisitPosfixOperationExpression(PosfixOperationExpression posfixOperationExpression)
 {
     return(new PosfixOperationExpression(posfixOperationExpression.Context, posfixOperationExpression.Children.Select(Visit))
     {
         Type = posfixOperationExpression.Type
     });
 }
Exemplo n.º 2
0
        public override void ExitPosfixOperationExpression(PosfixOperationExpression posfixOperationExpression)
        {
            if (!(posfixOperationExpression.Parent is ExpressionStatement || posfixOperationExpression.Parent is ForStatement forStatement && forStatement.NextExpressions.Contains(posfixOperationExpression)))
            {
                Errors.Add(new CompilationError(posfixOperationExpression.Context, "The postfix operator can only be used directly in a statement"));
            }
            var assignmentExpression = new AssignmentExpression(
                posfixOperationExpression.Context,
                new INode[]
            {
                AstCloner.Clone(posfixOperationExpression.Base),
                new AssignmentOperator(posfixOperationExpression.Context, "="),
                new BinaryExpression(
                    posfixOperationExpression.Context,
                    new INode[]
                {
                    AstCloner.Clone(posfixOperationExpression.Base),
                    new Token(posfixOperationExpression.Operation.Context, posfixOperationExpression.Operation.Text.Substring(0, 1)),
                    new NumberLiteral(posfixOperationExpression.Context, "1")
                }),
            });

            posfixOperationExpression.ReplaceWith(assignmentExpression);
            Visit(assignmentExpression);
        }
Exemplo n.º 3
0
        public override void ExitPosfixOperationExpression(PosfixOperationExpression posfixOperationExpression)
        {
            var baseType = posfixOperationExpression.Base.Type;
            var op       = posfixOperationExpression.Operation.Text;

            if (!(baseType is NumberType))
            {
                Errors.Add(new CompilationError(posfixOperationExpression.Context, $"{op} can only be applied to a number. Found {baseType}."));
            }
            posfixOperationExpression.Type = new NumberType(posfixOperationExpression.Context);
        }
Exemplo n.º 4
0
        public override IEnumerable <INode> VisitPrimaryExpression(TypescriptParser.PrimaryExpressionContext context)
        {
            var @base      = (IExpression)Visit(context.primaryExpressionStart()).SingleOrDefault();
            var operations = context.bracketExpression()
                             .Concat <IParseTree>(context.memberAccess())
                             .Concat(context.methodInvocation())
                             .Concat(context.OP_INC())
                             .Concat(context.OP_DEC())
                             .OrderBy(x => x.GetTokens().First().Line)
                             .ThenBy(x => x.GetTokens().First().Column);

            foreach (var operation in operations)
            {
                if (operation is TypescriptParser.BracketExpressionContext bracketExpression)
                {
                    @base = new ArrayIndexExpression(bracketExpression, @base.Yield().Concat(Visit(bracketExpression)));
                }
                else if (operation is TypescriptParser.MemberAccessContext memberAccess)
                {
                    @base = new MemberExpression(memberAccess, memberAccess.identifier().GetText(), @base.Yield());
                }
                else if (operation is TypescriptParser.MethodInvocationContext methodInvocation)
                {
                    var genericTypeArguments = Visit((@base.Context as TypescriptParser.MemberAccessContext)?.typeArgumentList()).Cast <ITypeNode>();
                    var arguments            = Visit(methodInvocation.argumentList()).Cast <IExpression>();

                    @base = new MethodInvocationExpression(
                        methodInvocation,
                        @base.Yield().Concat <INode>(genericTypeArguments).Concat(arguments));
                }
                else if (operation is ITerminalNode terminalNode)
                {
                    @base = new PosfixOperationExpression(terminalNode, new INode[] { @base, new Token(terminalNode) });
                }
                else
                {
                    throw new Exception("Unexpected operation: " + operation);
                }
            }

            yield return(@base);
        }
Exemplo n.º 5
0
 public virtual void ExitPosfixOperationExpression(PosfixOperationExpression posfixOperationExpression)
 {
 }
Exemplo n.º 6
0
 public virtual T VisitPosfixOperationExpression(PosfixOperationExpression posfixOperationExpression)
 {
     return(VisitChildren(posfixOperationExpression));
 }