Ejemplo n.º 1
0
        public override void ExitUnaryExpr(GoParser.UnaryExprContext context)
        {
            // unaryExpr
            //     : primaryExpr
            //     | ('+' | '-' | '!' | '^' | '*' | '&' | '<-') expression

            if (PrimaryExpressions.TryGetValue(context.primaryExpr(), out string primaryExpression))
            {
                UnaryExpressions[context] = primaryExpression;
            }
            else if (context.expression() != null)
            {
                string unaryOP = context.children[0].GetText();

                if (unaryOP.Equals("^", StringComparison.Ordinal))
                {
                    unaryOP = "~";
                }
                else if (unaryOP.Equals("<-", StringComparison.Ordinal))
                {
                    // TODO: Handle channel value access (update when channel class is created):
                    unaryOP = null;
                    UnaryExpressions[context] = $"{Expressions[context.expression()]}.Receive()";
                }
                else if (unaryOP.Equals("&", StringComparison.Ordinal))
                {
                    // TODO: Handle pointer acquisition context - may need to augment pre-scan metadata for heap allocation notation
                    unaryOP = null;
                    UnaryExpressions[context] = $"ref {Expressions[context.expression()]}";
                }
                else if (unaryOP.Equals("*", StringComparison.Ordinal))
                {
                    // TODO: Handle pointer dereference context - if this is a ref variable, Deref call is unnecessary
                    unaryOP = null;
                    UnaryExpressions[context] = $"{Expressions[context.expression()]}.Deref";
                }

                if ((object)unaryOP != null)
                {
                    UnaryExpressions[context] = $"{unaryOP}{Expressions[context.expression()]}";
                }
            }
            else if (!UnaryExpressions.ContainsKey(context))
            {
                AddWarning(context, $"Unexpected unary expression \"{context.GetText()}\"");
            }
        }