Changeset 13512


Ignore:
Timestamp:
Sep 17, 2019 6:00:48 PM (3 months ago)
Author:
mjohnsson
Message:

#5835 Added needed type casts needed for Java Formatter to match Modelica spec (ASCI C) formats handling. Handling Format specifiers.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/CHANGELOG.txt

    r13404 r13512  
    11================= Unreleased ==================
     2# Fixed ; Minor ; Compiler; #5835
     3Format string argument to the String function is now useable.
     4
    25# Change ; Minor ; Compiler; #5830
    36ParseHandler now have a target for Subscript.
  • trunk/Compiler/ModelicaFlatTree/src/jastadd/ConstantEvaluation/ConstantEvaluation.jrag

    r13431 r13512  
    27922792            return CValue.UNKNOWN;
    27932793        }
     2794       
    27942795        CValue cval = getValue().ceval(evaluator);
    2795         if (getValue().type().isReal()) {
     2796        boolean isReal = getValue().type().isReal();
     2797        if (isReal) {
    27962798            cval = cval.convertReal();
    27972799        }
    27982800        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        }
    28012835    }
    28022836    eq FGetInstanceName.cevalCalc(VariableEvaluator evaluator) = new CValueString(calcInstanceName());
  • trunk/Compiler/ModelicaFlatTree/test/modelica/EvaluationTests.mo

    r13431 r13512  
    47224722end StringConvertWithParam2;
    47234723
     4724model StringRealformatSpecifier_f
     4725    constant Real x = 1.23456789;
     4726    constant String s = String(x, format = "1.3f");
     4727
     4728annotation(__JModelica(UnitTesting(tests={
     4729    FlatteningTestCase(
     4730        name="StringRealformatSpecifier_f",
     4731        description="String() operator,  Real, format using f specifier",
     4732        flatModel="
     4733fclass EvaluationTests.StringConvert.StringRealformatSpecifier_f
     4734 constant Real x = 1.23456789;
     4735 constant String s = \"1.235\";
     4736end EvaluationTests.StringConvert.StringRealformatSpecifier_f;
     4737")})));
     4738end StringRealformatSpecifier_f;
     4739
     4740model StringIntegerformatSpecifier_f
     4741    constant Integer x = 1234;
     4742    constant String s = String(x, format = "1.3f");
     4743
     4744annotation(__JModelica(UnitTesting(tests={
     4745    FlatteningTestCase(
     4746        name="StringIntegerformatSpecifier_f",
     4747        description="String() operator,  Integer, format using f specifier",
     4748        flatModel="
     4749fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_f
     4750 constant Integer x = 1234;
     4751 constant String s = \"1234.000\";
     4752end EvaluationTests.StringConvert.StringIntegerformatSpecifier_f;
     4753")})));
     4754end StringIntegerformatSpecifier_f;
     4755
     4756model StringRealformatSpecifier_e
     4757    constant Real x = 1.23456789;
     4758    constant String s = String(x, format = "1.3e");
     4759
     4760annotation(__JModelica(UnitTesting(tests={
     4761    FlatteningTestCase(
     4762        name="StringRealformatSpecifier_e",
     4763        description="String() operator,  Real, format using e specifier",
     4764        flatModel="
     4765fclass EvaluationTests.StringConvert.StringRealformatSpecifier_e
     4766 constant Real x = 1.23456789;
     4767 constant String s = \"1.235e+00\";
     4768end EvaluationTests.StringConvert.StringRealformatSpecifier_e;
     4769")})));
     4770end StringRealformatSpecifier_e;
     4771
     4772model StringIntegerformatSpecifier_e
     4773    constant Integer x = 1234;
     4774    constant String s = String(x, format = "1.3e");
     4775
     4776annotation(__JModelica(UnitTesting(tests={
     4777    FlatteningTestCase(
     4778        name="StringIntegerformatSpecifier_e",
     4779        description="String() operator, Integer, format using e specifier",
     4780        flatModel="
     4781fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_e
     4782 constant Integer x = 1234;
     4783 constant String s = \"1.234e+03\";
     4784end EvaluationTests.StringConvert.StringIntegerformatSpecifier_e;
     4785")})));
     4786end StringIntegerformatSpecifier_e;
     4787
     4788model StringRealformatSpecifier_E
     4789    constant Real x = 1.23456789;
     4790    constant String s = String(x, format = "1.3E");
     4791
     4792annotation(__JModelica(UnitTesting(tests={
     4793    FlatteningTestCase(
     4794        name="StringRealformatSpecifier_E",
     4795        description="String() operator,  Real, format using E specifier",
     4796        flatModel="
     4797fclass EvaluationTests.StringConvert.StringRealformatSpecifier_E
     4798 constant Real x = 1.23456789;
     4799 constant String s = \"1.235E+00\";
     4800end EvaluationTests.StringConvert.StringRealformatSpecifier_E;
     4801")})));
     4802end StringRealformatSpecifier_E;
     4803
     4804model StringIntegerformatSpecifier_E
     4805    constant Integer x = 1234;
     4806    constant String s = String(x, format = "1.3E");
     4807
     4808annotation(__JModelica(UnitTesting(tests={
     4809    FlatteningTestCase(
     4810        name="StringIntegerformatSpecifier_E",
     4811        description="String() operator,  Integer, format using E specifier",
     4812        flatModel="
     4813fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_E
     4814 constant Integer x = 1234;
     4815 constant String s = \"1.234E+03\";
     4816end EvaluationTests.StringConvert.StringIntegerformatSpecifier_E;
     4817")})));
     4818end StringIntegerformatSpecifier_E;
     4819
     4820model StringRealformatSpecifier_g
     4821    constant Real x = 1.23456789;
     4822    constant String s = String(x, format = "1.3g");
     4823
     4824annotation(__JModelica(UnitTesting(tests={
     4825    FlatteningTestCase(
     4826        name="StringRealformatSpecifier_g",
     4827        description="String() operator, Real, format using g specifier",
     4828        flatModel="
     4829fclass EvaluationTests.StringConvert.StringRealformatSpecifier_g
     4830 constant Real x = 1.23456789;
     4831 constant String s = \"1.23\";
     4832end EvaluationTests.StringConvert.StringRealformatSpecifier_g;
     4833")})));
     4834end StringRealformatSpecifier_g;
     4835
     4836model StringIntegerformatSpecifier_g
     4837    constant Integer x = 1234;
     4838    constant String s = String(x, format = "1.3g");
     4839
     4840annotation(__JModelica(UnitTesting(tests={
     4841    FlatteningTestCase(
     4842        name="StringIntegerformatSpecifier_g",
     4843        description="String() operator,  Integer, format using g specifier",
     4844        flatModel="
     4845fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_g
     4846 constant Integer x = 1234;
     4847 constant String s = \"1.23e+03\";
     4848end EvaluationTests.StringConvert.StringIntegerformatSpecifier_g;
     4849")})));
     4850end StringIntegerformatSpecifier_g;
     4851
     4852model StringRealformatSpecifier_G
     4853    constant Real x = 1.23456789;
     4854    constant String s = String(x, format = "1.3G");
     4855
     4856annotation(__JModelica(UnitTesting(tests={
     4857    FlatteningTestCase(
     4858        name="StringRealformatSpecifier_G",
     4859        description="String() operator, Real, format using G specifier",
     4860        flatModel="
     4861fclass EvaluationTests.StringConvert.StringRealformatSpecifier_G
     4862 constant Real x = 1.23456789;
     4863 constant String s = \"1.23\";
     4864end EvaluationTests.StringConvert.StringRealformatSpecifier_G;
     4865")})));
     4866end StringRealformatSpecifier_G;
     4867
     4868model StringIntegerformatSpecifier_G
     4869    constant Integer x = 1234;
     4870    constant String s = String(x, format = "1.3G");
     4871
     4872annotation(__JModelica(UnitTesting(tests={
     4873    FlatteningTestCase(
     4874        name="StringIntegerformatSpecifier_G",
     4875        description="String() operator,  Integer, format using G specifier",
     4876        flatModel="
     4877fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_G
     4878 constant Integer x = 1234;
     4879 constant String s = \"1.23E+03\";
     4880end EvaluationTests.StringConvert.StringIntegerformatSpecifier_G;
     4881")})));
     4882end StringIntegerformatSpecifier_G;
     4883
     4884model StringRealformatSpecifier_d
     4885    constant Real x = 1.23456789;
     4886    constant String s = String(x, format = "3d");
     4887
     4888annotation(__JModelica(UnitTesting(tests={
     4889      ErrorTestCase(
     4890            name="StringRealformatSpecifier_d",
     4891            description="String() operator, Real, format using d specifier",
     4892            errorMessage="
     4893Error 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")})));
     4897end StringRealformatSpecifier_d;
     4898
     4899model StringIntegerformatSpecifier_d
     4900    constant Integer x = 1234;
     4901    constant String s = String(x, format = "3d");
     4902
     4903annotation(__JModelica(UnitTesting(tests={
     4904    FlatteningTestCase(
     4905        name="StringIntegerformatSpecifier_d",
     4906        description="String() operator,  Integer, format using d specifier",
     4907        flatModel="
     4908fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_d
     4909 constant Integer x = 1234;
     4910 constant String s = \"1234\";
     4911end EvaluationTests.StringConvert.StringIntegerformatSpecifier_d;
     4912")})));
     4913end StringIntegerformatSpecifier_d;
     4914
     4915model StringRealformatSpecifier_i
     4916    constant Real x = 1.23456789;
     4917    constant String s = String(x, format = "3i");
     4918
     4919annotation(__JModelica(UnitTesting(tests={
     4920    ErrorTestCase(
     4921        name="StringRealformatSpecifier_i",
     4922        description="String() operator, Real, format using i specifier",
     4923        errorMessage="
     4924Error 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")})));
     4928end StringRealformatSpecifier_i;
     4929
     4930model StringIntegerformatSpecifier_i
     4931    constant Integer x = 1234;
     4932    constant String s = String(x, format = "3i");
     4933
     4934annotation(__JModelica(UnitTesting(tests={
     4935    FlatteningTestCase(
     4936        name="StringIntegerformatSpecifier_i",
     4937        description="String() operator,  Integer, format using i specifier",
     4938        flatModel="
     4939fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_i
     4940 constant Integer x = 1234;
     4941 constant String s = \"1234\";
     4942end EvaluationTests.StringConvert.StringIntegerformatSpecifier_i;
     4943")})));
     4944end StringIntegerformatSpecifier_i;
     4945
     4946model StringRealformatSpecifier_o
     4947    constant Real x = 1.23456789;
     4948    constant String s = String(x, format = "3o");
     4949
     4950annotation(__JModelica(UnitTesting(tests={
     4951    ErrorTestCase(
     4952        name="StringRealformatSpecifier_o",
     4953        description="String() operator, Real, format using o specifier",
     4954        errorMessage="
     4955Error 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")})));
     4959end StringRealformatSpecifier_o;
     4960
     4961model StringIntegerformatSpecifier_o
     4962    constant Integer x = 1234;
     4963    constant String s = String(x, format = "3o");
     4964
     4965annotation(__JModelica(UnitTesting(tests={
     4966    FlatteningTestCase(
     4967        name="StringIntegerformatSpecifier_o",
     4968        description="String() operator,  Integer, format using o specifier",
     4969        flatModel="
     4970fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_o
     4971 constant Integer x = 1234;
     4972 constant String s = \"2322\";
     4973end EvaluationTests.StringConvert.StringIntegerformatSpecifier_o;
     4974")})));
     4975end StringIntegerformatSpecifier_o;
     4976
     4977model StringRealformatSpecifier_x
     4978    constant Real x = 1.23456789;
     4979    constant String s = String(x, format = "3x");
     4980
     4981annotation(__JModelica(UnitTesting(tests={
     4982    ErrorTestCase(
     4983        name="StringRealformatSpecifier_x",
     4984        description="String() operator, Real, format using x specifier",
     4985        errorMessage="
     4986Error 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")})));
     4990end StringRealformatSpecifier_x;
     4991
     4992model StringIntegerformatSpecifier_x
     4993    constant Integer x = 1234;
     4994    constant String s = String(x, format = "3x");
     4995
     4996annotation(__JModelica(UnitTesting(tests={
     4997    FlatteningTestCase(
     4998        name="StringIntegerformatSpecifier_x",
     4999        description="String() operator,  Integer, format using x specifier",
     5000        flatModel="
     5001fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_x
     5002 constant Integer x = 1234;
     5003 constant String s = \"4d2\";
     5004end EvaluationTests.StringConvert.StringIntegerformatSpecifier_x;
     5005")})));
     5006end StringIntegerformatSpecifier_x;
     5007
     5008model StringRealformatSpecifier_X
     5009    constant Real x = 1.23456789;
     5010    constant String s = String(x, format = "3X");
     5011
     5012annotation(__JModelica(UnitTesting(tests={
     5013    ErrorTestCase(
     5014        name="StringRealformatSpecifier_X",
     5015        description="String() operator, Real, format using X specifier",
     5016        errorMessage="
     5017Error 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")})));
     5021end StringRealformatSpecifier_X;
     5022
     5023model StringIntegerformatSpecifier_X
     5024    constant Integer x = 1234;
     5025    constant String s = String(x, format = "3X");
     5026
     5027annotation(__JModelica(UnitTesting(tests={
     5028    FlatteningTestCase(
     5029        name="StringIntegerformatSpecifier_X",
     5030        description="String() operator,  Integer, format using X specifier",
     5031        flatModel="
     5032fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_X
     5033 constant Integer x = 1234;
     5034 constant String s = \"4D2\";
     5035end EvaluationTests.StringConvert.StringIntegerformatSpecifier_X;
     5036")})));
     5037end StringIntegerformatSpecifier_X;
     5038
     5039model StringRealformatSpecifier_u
     5040    constant Real x = 1.23456789;
     5041    constant String s = String(x, format = "3u");
     5042
     5043annotation(__JModelica(UnitTesting(tests={
     5044    ErrorTestCase(
     5045        name="StringRealformatSpecifier_u",
     5046        description="String() operator, Real, format using u specifier",
     5047        errorMessage="
     5048Error 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")})));
     5052end StringRealformatSpecifier_u;
     5053
     5054model StringIntegerformatSpecifier_u
     5055    constant Integer x = -1234;
     5056    constant String s = String(x, format = "3u");
     5057
     5058annotation(__JModelica(UnitTesting(tests={
     5059    FlatteningTestCase(
     5060        name="StringIntegerformatSpecifier_u",
     5061        description="String() operator,  Integer, format using u specifier",
     5062        flatModel="
     5063fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_u
     5064 constant Integer x = -1234;
     5065 constant String s = \"4294966062\";
     5066end EvaluationTests.StringConvert.StringIntegerformatSpecifier_u;
     5067")})));
     5068end StringIntegerformatSpecifier_u;
     5069
     5070model StringRealformatSpecifier_c
     5071    constant Real x = 1.23456789;
     5072    constant String s = String(x, format = "3c");
     5073
     5074annotation(__JModelica(UnitTesting(tests={
     5075    ErrorTestCase(
     5076        name="StringRealformatSpecifier_c",
     5077        description="String() operator, Real, format using c specifier",
     5078        errorMessage="
     5079Error 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
     5084end StringRealformatSpecifier_c;
     5085
     5086model StringIntegerformatSpecifier_c
     5087    constant Integer x = 123;
     5088    constant String s = String(x, format = "3c");
     5089
     5090annotation(__JModelica(UnitTesting(tests={
     5091    FlatteningTestCase(
     5092        name="StringIntegerformatSpecifier_c",
     5093        description="String() operator,  Integer, format using c specifier",
     5094        flatModel="
     5095fclass EvaluationTests.StringConvert.StringIntegerformatSpecifier_c
     5096 constant Integer x = 123;
     5097 constant String s = \"  {\";
     5098end EvaluationTests.StringConvert.StringIntegerformatSpecifier_c;
     5099")})));
     5100end StringIntegerformatSpecifier_c;
     5101
     5102model StringIncorrectformat
     5103    constant Integer x = 1234;
     5104    constant String s = String(x, format = "*.1.3c");
     5105
     5106annotation(__JModelica(UnitTesting(tests={
     5107    ErrorTestCase(
     5108        name="StringIncorrectformat",
     5109        description="String() operator, Real, format using c specifier",
     5110        errorMessage="
     5111Error 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")})));
     5115end StringIncorrectformat;
     5116
    47245117end StringConvert;
    47255118
Note: See TracChangeset for help on using the changeset viewer.