コード例 #1
0
        Value print(FunctionArguments arguments, SymbolTable symbol_table)
        {
            string sep = arguments.get_string_argument(null, "sep", " ", symbol_table);

            // string end = arguments.get_string_argument(null, "end", "\n", symbol_table);
            string end = arguments.get_string_argument(null, "end", Environment.NewLine, symbol_table);

            var list_arg = arguments.get_list_arguments(0, symbol_table);

            var sb = new StringBuilder();

            for (int i = 0; i < list_arg.Count; i++)
            {
                sb.Append(list_arg[i].ToString());

                if (i < list_arg.Count - 1)
                {
                    sb.Append(sep);
                }
            }

            sb.Append(end);

            Console.Write(sb.ToString());

            if (text_writer != null)
            {
                text_writer.Write(sb.ToString());
            }

            return(NoneValue.NONE);
        }
コード例 #2
0
        Value min(FunctionArguments arguments, SymbolTable symbol_table)
        {
            arguments.check_num_args_minimum(1);

            var list_arg = arguments.get_list_arguments(0, symbol_table);

            // A single string is a special case
            if (list_arg.Count == 1 && list_arg[0].Type == ValueType.String)
            {
                // return the smallest character of this string
                string str = ((StringValue)list_arg[0]).value;

                if (str.Length == 0)
                {
                    throw new Exception("min() is called on an empty sequence.");
                }

                char smallest_char = str[0];
                for (int i = 1; i < str.Length; i++)
                {
                    if (str[i] < smallest_char)
                    {
                        smallest_char = str[i];
                    }
                }

                return(new StringValue(smallest_char.ToString()));
            }

            // Standard case
            Value smallest = null;

            for (int i = 0; i < list_arg.Count; i++)
            {
                if (list_arg[i].Type == ValueType.List)
                {
                    // Note: This is NOT standard Python behavior
                    smallest = find_smallest_in_list((ListValue)list_arg[i], smallest);
                }

                else
                {
                    if (smallest == null)
                    {
                        smallest = list_arg[i];
                    }
                    else
                    {
                        // possible alternative:
                        // var True = new DynamicValue(true);
                        // if (list_arg[i].operate(OperatorType.Less, smallest).Equals(True))
                        if (ListValue.compare_function(list_arg[i], smallest) < 0)
                        {
                            smallest = list_arg[i];
                        }
                    }
                }
            }

            if (smallest == null)
            {
                throw new Exception("min() is called on an empty sequence.");
            }

            return(ListValue.shallow_copy(smallest));
        }