[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 3/9] java: example: rely on autoboxing
From: |
Akim Demaille |
Subject: |
[PATCH 3/9] java: example: rely on autoboxing |
Date: |
Sun, 2 Feb 2020 15:26:21 +0100 |
AFAICT, autoboxing/unboxing was added in Java 5 (September 30, 2004).
I think we can afford to use it. It should help us merge some Java
tests with the main ones.
However, beware that != does not unbox: it compares the object
addresses.
* examples/java/Calc.y, tests/java.at: Simplify.
* examples/java/Calc.test, tests/java.at: Improve tests.
---
examples/java/Calc.test | 7 +++++++
examples/java/Calc.y | 24 +++++++++++-------------
tests/java.at | 28 +++++++++++++---------------
3 files changed, 31 insertions(+), 28 deletions(-)
diff --git a/examples/java/Calc.test b/examples/java/Calc.test
index d5e483f4..996b20e0 100644
--- a/examples/java/Calc.test
+++ b/examples/java/Calc.test
@@ -20,6 +20,13 @@ cat >input <<EOF
EOF
run 0 7
+cat >input <<EOF
+1 + 2 * 3 = 7
+(1 + 2) * 3 = 9
+EOF
+run 0 '7
+9'
+
cat >input <<EOF
1 + 2 * * 3
EOF
diff --git a/examples/java/Calc.y b/examples/java/Calc.y
index b69241b5..28195ddd 100644
--- a/examples/java/Calc.y
+++ b/examples/java/Calc.y
@@ -47,24 +47,22 @@ line:
;
exp:
- NUM { $$ = $1; }
+ NUM { $$ = $1; }
| exp '=' exp
{
if ($1.intValue () != $3.intValue ())
yyerror (@$, "calc: error: " + $1 + " != " + $3);
}
-| exp '+' exp { $$ = new Integer ($1.intValue () + $3.intValue ()); }
-| exp '-' exp { $$ = new Integer ($1.intValue () - $3.intValue ()); }
-| exp '*' exp { $$ = new Integer ($1.intValue () * $3.intValue ()); }
-| exp '/' exp { $$ = new Integer ($1.intValue () / $3.intValue ()); }
-| '-' exp %prec NEG { $$ = new Integer (-$2.intValue ()); }
-| exp '^' exp { $$ = new Integer ((int)
- Math.pow ($1.intValue (),
- $3.intValue ())); }
-| '(' exp ')' { $$ = $2; }
-| '(' error ')' { $$ = new Integer (1111); }
-| '!' { $$ = new Integer (0); return YYERROR; }
-| '-' error { $$ = new Integer (0); return YYERROR; }
+| exp '+' exp { $$ = $1 + $3; }
+| exp '-' exp { $$ = $1 - $3; }
+| exp '*' exp { $$ = $1 * $3; }
+| exp '/' exp { $$ = $1 / $3; }
+| '-' exp %prec NEG { $$ = -$2; }
+| exp '^' exp { $$ = (int) Math.pow ($1, $3); }
+| '(' exp ')' { $$ = $2; }
+| '(' error ')' { $$ = 1111; }
+| '!' { $$ = 0; return YYERROR; }
+| '-' error { $$ = 0; return YYERROR; }
;
diff --git a/tests/java.at b/tests/java.at
index 0383ddd4..61740c25 100644
--- a/tests/java.at
+++ b/tests/java.at
@@ -97,24 +97,22 @@ line:
;
exp:
- NUM { $$ = $1; }
+ NUM { $$ = $1; }
| exp '=' exp
{
if ($1.intValue () != $3.intValue ())
- yyerror (]AT_LOCATION_IF([[@$,]])[ "calc: error: " + $1 + " != " + $3);
+ yyerror (]AT_LOCATION_IF([[@$, ]])["calc: error: " + $1 + " != " + $3);
}
-| exp '+' exp { $$ = new Integer ($1.intValue () + $3.intValue ()); }
-| exp '-' exp { $$ = new Integer ($1.intValue () - $3.intValue ()); }
-| exp '*' exp { $$ = new Integer ($1.intValue () * $3.intValue ()); }
-| exp '/' exp { $$ = new Integer ($1.intValue () / $3.intValue ()); }
-| '-' exp %prec NEG { $$ = new Integer (-$2.intValue ()); }
-| exp '^' exp { $$ = new Integer ((int)
- Math.pow ($1.intValue (),
- $3.intValue ())); }
-| '(' exp ')' { $$ = $2; }
-| '(' error ')' { $$ = new Integer (1111); }
-| '!' { $$ = new Integer (0); return YYERROR; }
-| '-' error { $$ = new Integer (0); return YYERROR; }
+| exp '+' exp { $$ = $1 + $3; }
+| exp '-' exp { $$ = $1 - $3; }
+| exp '*' exp { $$ = $1 * $3; }
+| exp '/' exp { $$ = $1 / $3; }
+| '-' exp %prec NEG { $$ = -$2; }
+| exp '^' exp { $$ = (int) Math.pow ($1, $3); }
+| '(' exp ')' { $$ = $2; }
+| '(' error ')' { $$ = 1111; }
+| '!' { $$ = 0; return YYERROR; }
+| '-' error { $$ = 0; return YYERROR; }
;
]AT_LEXPARAM_IF([[%code lexer {]],
@@ -257,7 +255,7 @@ AT_DATA([[input]],
2^2^3 = 256
(2^2)^3 = 64
]])
-AT_JAVA_PARSER_CHECK([Calc < input], 0, [], [stderr])
+AT_JAVA_PARSER_CHECK([Calc < input])
# Some syntax errors.
--
2.25.0
- [PATCH 0/9] Prepare Java for parse.error custom|detailed, Akim Demaille, 2020/02/02
- [PATCH 2/9] tests: comment changes, Akim Demaille, 2020/02/02
- [PATCH 4/9] java: example: improve, Akim Demaille, 2020/02/02
- [PATCH 1/9] skeletons: add support for %code epilogue, Akim Demaille, 2020/02/02
- [PATCH 3/9] java: example: rely on autoboxing,
Akim Demaille <=
- [PATCH 5/9] java: example: properly track the locations, Akim Demaille, 2020/02/02
- [PATCH 7/9] java: formatting changes, Akim Demaille, 2020/02/02
- [PATCH 8/9] java: add access to the number of errors, Akim Demaille, 2020/02/02
- [PATCH 6/9] java: avoid trailing white spaces, Akim Demaille, 2020/02/02
- [PATCH 9/9] java: use the same calc tests as the other skeletons, Akim Demaille, 2020/02/02