Changeset 13512
- Timestamp:
- Sep 17, 2019 6:00:48 PM (3 months ago)
- Location:
- trunk
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk
- Property svn:mergeinfo changed
/branches/dev-mj-5835 (added) merged: 13497-13498,13502-13503,13506
- Property svn:mergeinfo changed
-
trunk/CHANGELOG.txt
r13404 r13512 1 1 ================= Unreleased ================== 2 # Fixed ; Minor ; Compiler; #5835 3 Format string argument to the String function is now useable. 4 2 5 # Change ; Minor ; Compiler; #5830 3 6 ParseHandler now have a target for Subscript. -
trunk/Compiler/ModelicaFlatTree/src/jastadd/ConstantEvaluation/ConstantEvaluation.jrag
r13431 r13512 2792 2792 return CValue.UNKNOWN; 2793 2793 } 2794 2794 2795 CValue cval = getValue().ceval(evaluator); 2795 if (getValue().type().isReal()) { 2796 boolean isReal = getValue().type().isReal(); 2797 if (isReal) { 2796 2798 cval = cval.convertReal(); 2797 2799 } 2798 2800 Object value = cval.objectValue(); 2799 String format = formatString(evaluator); 2800 return new CValueString(String.format((Locale) null, format, value)); 2801 final String format = formatString(evaluator); 2802 char formatChar = format.charAt(format.length() - 1); 2803 2804 // Modelica allows Integer to Real conversion for formatting but not the other direction 2805 boolean mustBeInteger = formatChar == 'd' || formatChar == 'i' || formatChar == 'o' || 2806 formatChar == 'x' || formatChar == 'X' || formatChar == 'u' || formatChar == 'c'; 2807 if (mustBeInteger && isReal) { 2808 throw new ConstantEvaluationException(cval, "format the resulting value. The format '"+ format + "' requires value of Integer type but Real value is provided. "); 2809 } 2810 2811 if (getValue().type().isInteger()) { 2812 // Java formatter do not convert types 2813 final boolean convertToFloat = formatChar == 'e' || formatChar == 'E' || 2814 formatChar == 'f' || formatChar == 'g' || formatChar == 'G'; 2815 if (convertToFloat) { 2816 return new CValueString(String.format((Locale) null, format, cval.realValue())); 2817 } 2818 if (formatChar == 'u') { 2819 String formatCorrect = format.substring(0, format.length()-1) + "s"; 2820 return new CValueString(String.format((Locale) null, formatCorrect, Integer.toUnsignedLong(cval.intValue()))); 2821 } 2822 2823 if (formatChar == 'i') { 2824 String formatCorrect = format.substring(0, format.length()-1) + "d"; 2825 return new CValueString(String.format((Locale) null, formatCorrect, cval.intValue())); 2826 } 2827 } 2828 2829 try { 2830 return new CValueString(String.format((Locale) null, format, value)); 2831 } catch (java.util.IllegalFormatException e) { 2832 throw new ConstantEvaluationException(cval, 2833 "format the resulting value. " + format + " is not a supported valid format string"); 2834 } 2801 2835 } 2802 2836 eq FGetInstanceName.cevalCalc(VariableEvaluator evaluator) = new CValueString(calcInstanceName()); -
trunk/Compiler/ModelicaFlatTree/test/modelica/EvaluationTests.mo
r13431 r13512 4722 4722 end StringConvertWithParam2; 4723 4723 4724 model StringRealformatSpecifier_f 4725 constant Real x = 1.23456789; 4726 constant String s = String(x, format = "1.3f"); 4727 4728 annotation(__JModelica(UnitTesting(tests={ 4729 FlatteningTestCase( 4730 name="StringRealformatSpecifier_f", 4731 description="String() operator, Real, format using f specifier", 4732 flatModel=" 4733 fclass EvaluationTests.StringConvert.StringRealformatSpecifier_f 4734 constant Real x = 1.23456789; 4735 constant String s = \"1.235\"; 4736 end EvaluationTests.StringConvert.StringRealformatSpecifier_f; 4737 ")}))); 4738 end StringRealformatSpecifier_f; 4739 4740 model StringIntegerformatSpecifier_f 4741 constant Integer x = 1234; 4742 constant String s = String(x, format = "1.3f"); 4743 4744 annotation(__JModelica(UnitTesting(tests={ 4745 FlatteningTestCase( 4746 name="StringIntegerformatSpecifier_f", 4747 description="String() operator, Integer, format using f specifier", 4748 flatModel=" 4749 fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_f 4750 constant Integer x = 1234; 4751 constant String s = \"1234.000\"; 4752 end EvaluationTests.StringConvert.StringIntegerformatSpecifier_f; 4753 ")}))); 4754 end StringIntegerformatSpecifier_f; 4755 4756 model StringRealformatSpecifier_e 4757 constant Real x = 1.23456789; 4758 constant String s = String(x, format = "1.3e"); 4759 4760 annotation(__JModelica(UnitTesting(tests={ 4761 FlatteningTestCase( 4762 name="StringRealformatSpecifier_e", 4763 description="String() operator, Real, format using e specifier", 4764 flatModel=" 4765 fclass EvaluationTests.StringConvert.StringRealformatSpecifier_e 4766 constant Real x = 1.23456789; 4767 constant String s = \"1.235e+00\"; 4768 end EvaluationTests.StringConvert.StringRealformatSpecifier_e; 4769 ")}))); 4770 end StringRealformatSpecifier_e; 4771 4772 model StringIntegerformatSpecifier_e 4773 constant Integer x = 1234; 4774 constant String s = String(x, format = "1.3e"); 4775 4776 annotation(__JModelica(UnitTesting(tests={ 4777 FlatteningTestCase( 4778 name="StringIntegerformatSpecifier_e", 4779 description="String() operator, Integer, format using e specifier", 4780 flatModel=" 4781 fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_e 4782 constant Integer x = 1234; 4783 constant String s = \"1.234e+03\"; 4784 end EvaluationTests.StringConvert.StringIntegerformatSpecifier_e; 4785 ")}))); 4786 end StringIntegerformatSpecifier_e; 4787 4788 model StringRealformatSpecifier_E 4789 constant Real x = 1.23456789; 4790 constant String s = String(x, format = "1.3E"); 4791 4792 annotation(__JModelica(UnitTesting(tests={ 4793 FlatteningTestCase( 4794 name="StringRealformatSpecifier_E", 4795 description="String() operator, Real, format using E specifier", 4796 flatModel=" 4797 fclass EvaluationTests.StringConvert.StringRealformatSpecifier_E 4798 constant Real x = 1.23456789; 4799 constant String s = \"1.235E+00\"; 4800 end EvaluationTests.StringConvert.StringRealformatSpecifier_E; 4801 ")}))); 4802 end StringRealformatSpecifier_E; 4803 4804 model StringIntegerformatSpecifier_E 4805 constant Integer x = 1234; 4806 constant String s = String(x, format = "1.3E"); 4807 4808 annotation(__JModelica(UnitTesting(tests={ 4809 FlatteningTestCase( 4810 name="StringIntegerformatSpecifier_E", 4811 description="String() operator, Integer, format using E specifier", 4812 flatModel=" 4813 fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_E 4814 constant Integer x = 1234; 4815 constant String s = \"1.234E+03\"; 4816 end EvaluationTests.StringConvert.StringIntegerformatSpecifier_E; 4817 ")}))); 4818 end StringIntegerformatSpecifier_E; 4819 4820 model StringRealformatSpecifier_g 4821 constant Real x = 1.23456789; 4822 constant String s = String(x, format = "1.3g"); 4823 4824 annotation(__JModelica(UnitTesting(tests={ 4825 FlatteningTestCase( 4826 name="StringRealformatSpecifier_g", 4827 description="String() operator, Real, format using g specifier", 4828 flatModel=" 4829 fclass EvaluationTests.StringConvert.StringRealformatSpecifier_g 4830 constant Real x = 1.23456789; 4831 constant String s = \"1.23\"; 4832 end EvaluationTests.StringConvert.StringRealformatSpecifier_g; 4833 ")}))); 4834 end StringRealformatSpecifier_g; 4835 4836 model StringIntegerformatSpecifier_g 4837 constant Integer x = 1234; 4838 constant String s = String(x, format = "1.3g"); 4839 4840 annotation(__JModelica(UnitTesting(tests={ 4841 FlatteningTestCase( 4842 name="StringIntegerformatSpecifier_g", 4843 description="String() operator, Integer, format using g specifier", 4844 flatModel=" 4845 fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_g 4846 constant Integer x = 1234; 4847 constant String s = \"1.23e+03\"; 4848 end EvaluationTests.StringConvert.StringIntegerformatSpecifier_g; 4849 ")}))); 4850 end StringIntegerformatSpecifier_g; 4851 4852 model StringRealformatSpecifier_G 4853 constant Real x = 1.23456789; 4854 constant String s = String(x, format = "1.3G"); 4855 4856 annotation(__JModelica(UnitTesting(tests={ 4857 FlatteningTestCase( 4858 name="StringRealformatSpecifier_G", 4859 description="String() operator, Real, format using G specifier", 4860 flatModel=" 4861 fclass EvaluationTests.StringConvert.StringRealformatSpecifier_G 4862 constant Real x = 1.23456789; 4863 constant String s = \"1.23\"; 4864 end EvaluationTests.StringConvert.StringRealformatSpecifier_G; 4865 ")}))); 4866 end StringRealformatSpecifier_G; 4867 4868 model StringIntegerformatSpecifier_G 4869 constant Integer x = 1234; 4870 constant String s = String(x, format = "1.3G"); 4871 4872 annotation(__JModelica(UnitTesting(tests={ 4873 FlatteningTestCase( 4874 name="StringIntegerformatSpecifier_G", 4875 description="String() operator, Integer, format using G specifier", 4876 flatModel=" 4877 fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_G 4878 constant Integer x = 1234; 4879 constant String s = \"1.23E+03\"; 4880 end EvaluationTests.StringConvert.StringIntegerformatSpecifier_G; 4881 ")}))); 4882 end StringIntegerformatSpecifier_G; 4883 4884 model StringRealformatSpecifier_d 4885 constant Real x = 1.23456789; 4886 constant String s = String(x, format = "3d"); 4887 4888 annotation(__JModelica(UnitTesting(tests={ 4889 ErrorTestCase( 4890 name="StringRealformatSpecifier_d", 4891 description="String() operator, Real, format using d specifier", 4892 errorMessage=" 4893 Error at line 3, column 25, in file '...': 4894 Could not evaluate binding expression for constant 's': 'String(x, \"3d\")' 4895 Cannot format the resulting value. The format '%3d' requires value of Integer type but Real value is provided. CValueReal (1.23456789) 4896 ")}))); 4897 end StringRealformatSpecifier_d; 4898 4899 model StringIntegerformatSpecifier_d 4900 constant Integer x = 1234; 4901 constant String s = String(x, format = "3d"); 4902 4903 annotation(__JModelica(UnitTesting(tests={ 4904 FlatteningTestCase( 4905 name="StringIntegerformatSpecifier_d", 4906 description="String() operator, Integer, format using d specifier", 4907 flatModel=" 4908 fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_d 4909 constant Integer x = 1234; 4910 constant String s = \"1234\"; 4911 end EvaluationTests.StringConvert.StringIntegerformatSpecifier_d; 4912 ")}))); 4913 end StringIntegerformatSpecifier_d; 4914 4915 model StringRealformatSpecifier_i 4916 constant Real x = 1.23456789; 4917 constant String s = String(x, format = "3i"); 4918 4919 annotation(__JModelica(UnitTesting(tests={ 4920 ErrorTestCase( 4921 name="StringRealformatSpecifier_i", 4922 description="String() operator, Real, format using i specifier", 4923 errorMessage=" 4924 Error at line 3, column 25, in file '...': 4925 Could not evaluate binding expression for constant 's': 'String(x, \"3i\")' 4926 Cannot format the resulting value. The format '%3i' requires value of Integer type but Real value is provided. CValueReal (1.23456789) 4927 ")}))); 4928 end StringRealformatSpecifier_i; 4929 4930 model StringIntegerformatSpecifier_i 4931 constant Integer x = 1234; 4932 constant String s = String(x, format = "3i"); 4933 4934 annotation(__JModelica(UnitTesting(tests={ 4935 FlatteningTestCase( 4936 name="StringIntegerformatSpecifier_i", 4937 description="String() operator, Integer, format using i specifier", 4938 flatModel=" 4939 fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_i 4940 constant Integer x = 1234; 4941 constant String s = \"1234\"; 4942 end EvaluationTests.StringConvert.StringIntegerformatSpecifier_i; 4943 ")}))); 4944 end StringIntegerformatSpecifier_i; 4945 4946 model StringRealformatSpecifier_o 4947 constant Real x = 1.23456789; 4948 constant String s = String(x, format = "3o"); 4949 4950 annotation(__JModelica(UnitTesting(tests={ 4951 ErrorTestCase( 4952 name="StringRealformatSpecifier_o", 4953 description="String() operator, Real, format using o specifier", 4954 errorMessage=" 4955 Error at line 3, column 25, in file '...': 4956 Could not evaluate binding expression for constant 's': 'String(x, \"3o\")' 4957 Cannot format the resulting value. The format '%3o' requires value of Integer type but Real value is provided. CValueReal (1.23456789) 4958 ")}))); 4959 end StringRealformatSpecifier_o; 4960 4961 model StringIntegerformatSpecifier_o 4962 constant Integer x = 1234; 4963 constant String s = String(x, format = "3o"); 4964 4965 annotation(__JModelica(UnitTesting(tests={ 4966 FlatteningTestCase( 4967 name="StringIntegerformatSpecifier_o", 4968 description="String() operator, Integer, format using o specifier", 4969 flatModel=" 4970 fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_o 4971 constant Integer x = 1234; 4972 constant String s = \"2322\"; 4973 end EvaluationTests.StringConvert.StringIntegerformatSpecifier_o; 4974 ")}))); 4975 end StringIntegerformatSpecifier_o; 4976 4977 model StringRealformatSpecifier_x 4978 constant Real x = 1.23456789; 4979 constant String s = String(x, format = "3x"); 4980 4981 annotation(__JModelica(UnitTesting(tests={ 4982 ErrorTestCase( 4983 name="StringRealformatSpecifier_x", 4984 description="String() operator, Real, format using x specifier", 4985 errorMessage=" 4986 Error at line 3, column 25, in file '...': 4987 Could not evaluate binding expression for constant 's': 'String(x, \"3x\")' 4988 Cannot format the resulting value. The format '%3x' requires value of Integer type but Real value is provided. CValueReal (1.23456789) 4989 ")}))); 4990 end StringRealformatSpecifier_x; 4991 4992 model StringIntegerformatSpecifier_x 4993 constant Integer x = 1234; 4994 constant String s = String(x, format = "3x"); 4995 4996 annotation(__JModelica(UnitTesting(tests={ 4997 FlatteningTestCase( 4998 name="StringIntegerformatSpecifier_x", 4999 description="String() operator, Integer, format using x specifier", 5000 flatModel=" 5001 fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_x 5002 constant Integer x = 1234; 5003 constant String s = \"4d2\"; 5004 end EvaluationTests.StringConvert.StringIntegerformatSpecifier_x; 5005 ")}))); 5006 end StringIntegerformatSpecifier_x; 5007 5008 model StringRealformatSpecifier_X 5009 constant Real x = 1.23456789; 5010 constant String s = String(x, format = "3X"); 5011 5012 annotation(__JModelica(UnitTesting(tests={ 5013 ErrorTestCase( 5014 name="StringRealformatSpecifier_X", 5015 description="String() operator, Real, format using X specifier", 5016 errorMessage=" 5017 Error at line 3, column 25, in file '...': 5018 Could not evaluate binding expression for constant 's': 'String(x, \"3X\")' 5019 Cannot format the resulting value. The format '%3X' requires value of Integer type but Real value is provided. CValueReal (1.23456789) 5020 ")}))); 5021 end StringRealformatSpecifier_X; 5022 5023 model StringIntegerformatSpecifier_X 5024 constant Integer x = 1234; 5025 constant String s = String(x, format = "3X"); 5026 5027 annotation(__JModelica(UnitTesting(tests={ 5028 FlatteningTestCase( 5029 name="StringIntegerformatSpecifier_X", 5030 description="String() operator, Integer, format using X specifier", 5031 flatModel=" 5032 fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_X 5033 constant Integer x = 1234; 5034 constant String s = \"4D2\"; 5035 end EvaluationTests.StringConvert.StringIntegerformatSpecifier_X; 5036 ")}))); 5037 end StringIntegerformatSpecifier_X; 5038 5039 model StringRealformatSpecifier_u 5040 constant Real x = 1.23456789; 5041 constant String s = String(x, format = "3u"); 5042 5043 annotation(__JModelica(UnitTesting(tests={ 5044 ErrorTestCase( 5045 name="StringRealformatSpecifier_u", 5046 description="String() operator, Real, format using u specifier", 5047 errorMessage=" 5048 Error at line 3, column 25, in file '...': 5049 Could not evaluate binding expression for constant 's': 'String(x, \"3u\")' 5050 Cannot format the resulting value. The format '%3u' requires value of Integer type but Real value is provided. CValueReal (1.23456789) 5051 ")}))); 5052 end StringRealformatSpecifier_u; 5053 5054 model StringIntegerformatSpecifier_u 5055 constant Integer x = -1234; 5056 constant String s = String(x, format = "3u"); 5057 5058 annotation(__JModelica(UnitTesting(tests={ 5059 FlatteningTestCase( 5060 name="StringIntegerformatSpecifier_u", 5061 description="String() operator, Integer, format using u specifier", 5062 flatModel=" 5063 fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_u 5064 constant Integer x = -1234; 5065 constant String s = \"4294966062\"; 5066 end EvaluationTests.StringConvert.StringIntegerformatSpecifier_u; 5067 ")}))); 5068 end StringIntegerformatSpecifier_u; 5069 5070 model StringRealformatSpecifier_c 5071 constant Real x = 1.23456789; 5072 constant String s = String(x, format = "3c"); 5073 5074 annotation(__JModelica(UnitTesting(tests={ 5075 ErrorTestCase( 5076 name="StringRealformatSpecifier_c", 5077 description="String() operator, Real, format using c specifier", 5078 errorMessage=" 5079 Error at line 3, column 25, in file '...': 5080 Could not evaluate binding expression for constant 's': 'String(x, \"3c\")' 5081 Cannot format the resulting value. The format '%3c' requires value of Integer type but Real value is provided. CValueReal (1.23456789) 5082 ")}))); 5083 5084 end StringRealformatSpecifier_c; 5085 5086 model StringIntegerformatSpecifier_c 5087 constant Integer x = 123; 5088 constant String s = String(x, format = "3c"); 5089 5090 annotation(__JModelica(UnitTesting(tests={ 5091 FlatteningTestCase( 5092 name="StringIntegerformatSpecifier_c", 5093 description="String() operator, Integer, format using c specifier", 5094 flatModel=" 5095 fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_c 5096 constant Integer x = 123; 5097 constant String s = \" {\"; 5098 end EvaluationTests.StringConvert.StringIntegerformatSpecifier_c; 5099 ")}))); 5100 end StringIntegerformatSpecifier_c; 5101 5102 model StringIncorrectformat 5103 constant Integer x = 1234; 5104 constant String s = String(x, format = "*.1.3c"); 5105 5106 annotation(__JModelica(UnitTesting(tests={ 5107 ErrorTestCase( 5108 name="StringIncorrectformat", 5109 description="String() operator, Real, format using c specifier", 5110 errorMessage=" 5111 Error at line 3, column 25, in file '...': 5112 Could not evaluate binding expression for constant 's': 'String(x, \"*.1.3c\")' 5113 Cannot format the resulting value. %*.1.3c is not a supported valid format stringCValueInteger (1234) 5114 ")}))); 5115 end StringIncorrectformat; 5116 4724 5117 end StringConvert; 4725 5118
Note: See TracChangeset
for help on using the changeset viewer.