OLD | NEW |
---|---|
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "tools/gn/functions.h" | 5 #include "tools/gn/functions.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <iostream> | 8 #include <iostream> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/environment.h" | 11 #include "base/environment.h" |
12 #include "base/strings/string_util.h" | 12 #include "base/strings/string_util.h" |
13 #include "tools/gn/config.h" | 13 #include "tools/gn/config.h" |
14 #include "tools/gn/config_values_generator.h" | 14 #include "tools/gn/config_values_generator.h" |
15 #include "tools/gn/err.h" | 15 #include "tools/gn/err.h" |
16 #include "tools/gn/input_file.h" | 16 #include "tools/gn/input_file.h" |
17 #include "tools/gn/parse_node_value_adapter.h" | |
17 #include "tools/gn/parse_tree.h" | 18 #include "tools/gn/parse_tree.h" |
18 #include "tools/gn/pool.h" | 19 #include "tools/gn/pool.h" |
19 #include "tools/gn/scheduler.h" | 20 #include "tools/gn/scheduler.h" |
20 #include "tools/gn/scope.h" | 21 #include "tools/gn/scope.h" |
21 #include "tools/gn/settings.h" | 22 #include "tools/gn/settings.h" |
22 #include "tools/gn/template.h" | 23 #include "tools/gn/template.h" |
23 #include "tools/gn/token.h" | 24 #include "tools/gn/token.h" |
24 #include "tools/gn/value.h" | 25 #include "tools/gn/value.h" |
25 #include "tools/gn/value_extractors.h" | 26 #include "tools/gn/value_extractors.h" |
26 #include "tools/gn/variables.h" | 27 #include "tools/gn/variables.h" |
(...skipping 777 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
804 const BuildSettings::PrintCallback& cb = | 805 const BuildSettings::PrintCallback& cb = |
805 scope->settings()->build_settings()->print_callback(); | 806 scope->settings()->build_settings()->print_callback(); |
806 if (cb.is_null()) | 807 if (cb.is_null()) |
807 printf("%s", output.c_str()); | 808 printf("%s", output.c_str()); |
808 else | 809 else |
809 cb.Run(output); | 810 cb.Run(output); |
810 | 811 |
811 return Value(); | 812 return Value(); |
812 } | 813 } |
813 | 814 |
815 // split_list ------------------------------------------------------------------ | |
816 | |
817 const char kSplitList[] = "split_list"; | |
818 const char kSplitList_HelpShort[] = | |
819 "split_list: Splits a list into N different sub-lists."; | |
820 const char kSplitList_Help[] = | |
821 "split_list: Splits a list into N different sub-lists.\n" | |
822 "\n" | |
823 " result = split_list(input, n)\n" | |
824 "\n" | |
825 " Given a list and a number N, splits the list into N sub-lists of\n" | |
826 " approximately equal size. The return value is a list of the sub-lists.\n" | |
827 " The result will always be a list of size N. If N is greater than the\n" | |
828 " number of elements in the input, it will be padded with empty lists.\n" | |
829 "\n" | |
830 " The expected use is to divide source files into smaller uniform\n" | |
831 " chunks.\n" | |
832 "\n" | |
833 "Example\n" | |
834 "\n" | |
835 " The code:\n" | |
836 " mylist = [1, 2, 3, 4, 5, 6]\n" | |
837 " print(split_list(mylist, 3))\n" | |
838 "\n" | |
839 " Will print:\n" | |
840 " [[1, 2], [3, 4], [5, 6]\n"; | |
841 Value RunSplitList(Scope* scope, | |
842 const FunctionCallNode* function, | |
843 const ListNode* args_list, | |
844 Err* err) { | |
845 const auto& args_vector = args_list->contents(); | |
846 if (args_vector.size() != 2) { | |
847 *err = Err(function, "Wrong number of arguments to split_list().", | |
848 "Expecting exactly two."); | |
849 return Value(); | |
850 } | |
851 | |
852 ParseNodeValueAdapter list_adapter; | |
853 if (!list_adapter.InitForType(scope, args_vector[0].get(), Value::LIST, err)) | |
854 return Value(); | |
855 const std::vector<Value>& input = list_adapter.get().list_value(); | |
856 | |
857 ParseNodeValueAdapter count_adapter; | |
858 if (!count_adapter.InitForType(scope, args_vector[1].get(), Value::INTEGER, | |
859 err)) | |
860 return Value(); | |
861 int64_t count = count_adapter.get().int_value(); | |
862 if (count <= 0) { | |
863 *err = Err(function, "Negative result size."); | |
scottmg
2016/06/27 22:06:01
<= isn't quite negative, maybe "Should be greater
| |
864 return Value(); | |
865 } | |
866 | |
867 Value result(function, Value::LIST); | |
868 result.list_value().resize(count); | |
869 | |
870 int64_t items_per_result = | |
871 static_cast<int64_t>(input.size() + count - 1) / count; | |
872 for (int64_t i = 0; i < count; i++) { | |
873 result.list_value()[i] = Value(function, Value::LIST); | |
874 | |
875 // Assign all remainders to the last one in the bucket. | |
876 auto begin_add = input.begin() + i * items_per_result; | |
877 auto end_add = (i == count - 1) ? input.end() | |
878 : begin_add + items_per_result; | |
scottmg
2016/06/27 22:06:01
This doesn't seem quite right. e.g. if there's 6 i
| |
879 result.list_value()[i].list_value().assign(begin_add, end_add); | |
880 } | |
881 | |
882 return result; | |
883 } | |
884 | |
814 // ----------------------------------------------------------------------------- | 885 // ----------------------------------------------------------------------------- |
815 | 886 |
816 FunctionInfo::FunctionInfo() | 887 FunctionInfo::FunctionInfo() |
817 : self_evaluating_args_runner(nullptr), | 888 : self_evaluating_args_runner(nullptr), |
818 generic_block_runner(nullptr), | 889 generic_block_runner(nullptr), |
819 executed_block_runner(nullptr), | 890 executed_block_runner(nullptr), |
820 no_block_runner(nullptr), | 891 no_block_runner(nullptr), |
821 help_short(nullptr), | 892 help_short(nullptr), |
822 help(nullptr), | 893 help(nullptr), |
823 is_target(false) { | 894 is_target(false) { |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
916 INSERT_FUNCTION(GetTargetOutputs, false) | 987 INSERT_FUNCTION(GetTargetOutputs, false) |
917 INSERT_FUNCTION(Import, false) | 988 INSERT_FUNCTION(Import, false) |
918 INSERT_FUNCTION(Pool, false) | 989 INSERT_FUNCTION(Pool, false) |
919 INSERT_FUNCTION(Print, false) | 990 INSERT_FUNCTION(Print, false) |
920 INSERT_FUNCTION(ProcessFileTemplate, false) | 991 INSERT_FUNCTION(ProcessFileTemplate, false) |
921 INSERT_FUNCTION(ReadFile, false) | 992 INSERT_FUNCTION(ReadFile, false) |
922 INSERT_FUNCTION(RebasePath, false) | 993 INSERT_FUNCTION(RebasePath, false) |
923 INSERT_FUNCTION(SetDefaults, false) | 994 INSERT_FUNCTION(SetDefaults, false) |
924 INSERT_FUNCTION(SetDefaultToolchain, false) | 995 INSERT_FUNCTION(SetDefaultToolchain, false) |
925 INSERT_FUNCTION(SetSourcesAssignmentFilter, false) | 996 INSERT_FUNCTION(SetSourcesAssignmentFilter, false) |
997 INSERT_FUNCTION(SplitList, false) | |
926 INSERT_FUNCTION(Template, false) | 998 INSERT_FUNCTION(Template, false) |
927 INSERT_FUNCTION(Tool, false) | 999 INSERT_FUNCTION(Tool, false) |
928 INSERT_FUNCTION(Toolchain, false) | 1000 INSERT_FUNCTION(Toolchain, false) |
929 INSERT_FUNCTION(ToolchainArgs, false) | 1001 INSERT_FUNCTION(ToolchainArgs, false) |
930 INSERT_FUNCTION(WriteFile, false) | 1002 INSERT_FUNCTION(WriteFile, false) |
931 | 1003 |
932 #undef INSERT_FUNCTION | 1004 #undef INSERT_FUNCTION |
933 } | 1005 } |
934 }; | 1006 }; |
935 const FunctionInfoInitializer function_info; | 1007 const FunctionInfoInitializer function_info; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1011 } | 1083 } |
1012 | 1084 |
1013 // Otherwise it's a no-block function. | 1085 // Otherwise it's a no-block function. |
1014 if (!VerifyNoBlockForFunctionCall(function, block, err)) | 1086 if (!VerifyNoBlockForFunctionCall(function, block, err)) |
1015 return Value(); | 1087 return Value(); |
1016 return found_function->second.no_block_runner(scope, function, | 1088 return found_function->second.no_block_runner(scope, function, |
1017 args.list_value(), err); | 1089 args.list_value(), err); |
1018 } | 1090 } |
1019 | 1091 |
1020 } // namespace functions | 1092 } // namespace functions |
OLD | NEW |