示例#1
0
        /// <summary>
        /// Generates a standard error checking sequence.
        /// E.g. if err != nil return err
        /// </summary>
        /// <param name="errorVar">The name of the error variable.</param>
        /// <param name="onError">How the error should be handled.</param>
        /// <returns>The root node of this error checking AST.</returns>
        public static Node Generate(string errorVar, OnError onError)
        {
            if (string.IsNullOrWhiteSpace(errorVar))
            {
                throw new ArgumentException(nameof(errorVar));
            }

            Node handler = null;

            switch (onError)
            {
            case OnError.Panic:
                handler = FunctionCall.Generate("panic", new[]
                {
                    new FuncCallParam(new Identifier(errorVar), TypeModifier.ByValue)
                });
                break;

            case OnError.ReturnError:
                var ret = new Return();
                ret.AddChild(new Identifier(errorVar));
                handler = ret;
                break;

            default:
                throw new NotImplementedException($"OnError state {onError} NYI");
            }

            var node = IfBlock.Generate(BinaryOpSequence.Generate(BinaryOperatorType.NotEqualTo, new Identifier(errorVar), new Nil()), new[]
            {
                handler
            });

            return(node);
        }
示例#2
0
        /// <summary>
        /// Generates a standard error checking sequence.
        /// E.g. if err != nil return var1, var2, varN
        /// </summary>
        /// <param name="errorVar">The name of the error variable.</param>
        /// <param name="toReturn">
        /// List of variables to return.  All variables in
        /// this list will be returned in the specified order.
        /// </param>
        /// <returns>The root node of this error checking AST.</returns>
        public static Node Generate(string errorVar, IReadOnlyList <Node> toReturn)
        {
            var ret = new Return();

            ret.AddChild(DelimitedSequence.Generate(UnaryDelimiterType.Comma, toReturn));

            var node = IfBlock.Generate(BinaryOpSequence.Generate(BinaryOperatorType.NotEqualTo, new Identifier(errorVar), new Nil()), new[]
            {
                ret
            });

            return(node);
        }
示例#3
0
        /// <summary>
        /// Generates a return statement with optional list of variables.
        /// </summary>
        /// <param name="varNames">Optional list of variables to be returned.  Pass null if there are no variables to return.</param>
        /// <returns>Root node in this return statement AST.</returns>
        public static Node Generate(IReadOnlyList <string> varNames)
        {
            if (varNames != null && varNames.Count == 0)
            {
                throw new ArgumentException("pass null for no arguments to return");
            }

            var ret = new Return();

            if (varNames != null)
            {
                Node[] returnVars = new Node[varNames.Count];
                for (int i = 0; i < returnVars.Length; ++i)
                {
                    returnVars[i] = new Identifier(varNames[i]);
                }

                ret.AddChild(DelimitedSequence.Generate(UnaryDelimiterType.Comma, returnVars));
            }

            return(ret);
        }