java: example: rely on autoboxing

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.
This commit is contained in:
Akim Demaille
2020-01-31 06:34:50 +01:00
parent c5b215b5e6
commit d5f929d407
3 changed files with 31 additions and 28 deletions

View File

@@ -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

View File

@@ -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; }
;

View File

@@ -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.