From 39f542ee239c9fb7a7f0a6a5bcad382409db770c Mon Sep 17 00:00:00 2001 From: Anton Shvetz Date: Mon, 9 Sep 2024 17:54:08 +0300 Subject: [PATCH] Migrate to Java 21. Move the "gcd" operator to PsyRational. Update API documentation. --- .github/workflows/ci.yml | 8 +- .github/workflows/publish.yml | 4 +- Dockerfile | 2 +- README.md | 2 +- build.xml | 32 +- .../PsyllaReference_Installation.docbook | 8 +- ...llaReference_Operators_Details_gcd.docbook | 6 +- ...laReference_Operators_Details_idiv.docbook | 5 +- ...rence_Operators_Details_reciprocal.docbook | 35 ++- .../psylla/tools/ant/package-info.java | 4 + .../processors/TypeHierarchyBuilder.java | 2 +- .../processors/TypeMetadataCollector.java | 2 +- .../psylla/tools/processors/package-info.java | 4 + .../psylla/core/PsyBigFraction.java | 4 +- .../coneforest/psylla/core/PsyBigInteger.java | 162 +++++----- .../psylla/core/PsyBlockingQueue.java | 2 +- .../coneforest/psylla/core/PsyComplex.java | 25 +- .../coneforest/psylla/core/PsyFileSystem.java | 43 +-- .../coneforest/psylla/core/PsyFraction.java | 4 +- .../coneforest/psylla/core/PsyInteger.java | 279 +++++++++--------- .../coneforest/psylla/core/PsyIntegral.java | 113 ++++--- .../coneforest/psylla/core/PsyOperator.java | 4 +- .../coneforest/psylla/core/PsyRandom.java | 2 +- .../coneforest/psylla/core/PsyRational.java | 167 +++++++---- .../coneforest/psylla/core/PsyReader.java | 3 + .../coneforest/psylla/core/PsyReal.java | 35 +-- .../psylla/core/PsyRealNumeric.java | 76 ++--- .../psylla/runtime/ClassLoader.java | 4 +- .../psylla/runtime/parser/package-info.java | 4 +- src/obs/debian/control | 8 +- 30 files changed, 594 insertions(+), 455 deletions(-) create mode 100644 src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/ant/package-info.java create mode 100644 src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/processors/package-info.java diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ccc01ffb7..144caabbc 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,7 +31,7 @@ jobs: libjline-java{,-doc} \ libxerces2-java \ libxslthl-java \ - openjdk-17-{jdk-headless,doc} \ + openjdk-21-{jdk-headless,doc} \ python3-m2crypto \ osc \ sassc @@ -40,7 +40,7 @@ jobs: git config tar.tar.xz.command 'xz -9' - name: Build env: - JAVA_HOME: /usr/lib/jvm/java-17-openjdk-amd64 + JAVA_HOME: /usr/lib/jvm/java-21-openjdk-amd64 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | ant -lib /usr/share/java/xercesImpl.jar:/usr/share/java/ivy.jar:/usr/share/java/junit4.jar \ @@ -54,7 +54,7 @@ jobs: >target/doc/index.xhtml - name: Test env: - JAVA_HOME: /usr/lib/jvm/java-17-openjdk-amd64 + JAVA_HOME: /usr/lib/jvm/java-21-openjdk-amd64 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | ant -lib /usr/share/java/ivy.jar:/usr/share/java/junit4.jar \ @@ -68,7 +68,7 @@ jobs: FOLDER: target/doc - name: Commit to OBS env: - JAVA_HOME: /usr/lib/jvm/java-17-openjdk-amd64 + JAVA_HOME: /usr/lib/jvm/java-21-openjdk-amd64 OBS_PASSWORD: ${{ secrets.OBS_PASSWORD }} run: | ant -lib /usr/share/java/ivy.jar -v -noinput -Dlinux.flavour=debian obs.commit diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 2e284e38d..5ee93ac69 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -29,13 +29,13 @@ jobs: libjline-java{,-doc} \ libxerces2-java \ libxslthl-java \ - openjdk-17-{jdk-headless,doc} \ + openjdk-21-{jdk-headless,doc} \ - name: Setup run: | git config tar.tar.xz.command 'xz -9' - name: Deploy package env: - JAVA_HOME: /usr/lib/jvm/java-17-openjdk-amd64 + JAVA_HOME: /usr/lib/jvm/java-21-openjdk-amd64 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: | ant -lib /usr/share/java/xercesImpl.jar:/usr/share/java/ivy.jar:/usr/share/java/junit4.jar \ diff --git a/Dockerfile b/Dockerfile index 3af55c627..61a1958d0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -6,7 +6,7 @@ RUN zypper -n --gpg-auto-import-keys in -y \ ant{,-apache-resolver,-junit} \ ivy \ ivy-local \ - 'java-devel>=17' \ + 'java-devel>=21' \ javacc \ javapackages-tools \ docbook5-xsl-stylesheets \ diff --git a/README.md b/README.md index b4a65e86a..5fc90635f 100644 --- a/README.md +++ b/README.md @@ -72,7 +72,7 @@ More sample programs can be found in the dedicated ### Jar: -Java 17 (OpenJDK or Oracle JDK), JavaCC, Apache Ant, Apache Ivy, JUnit 4, JLine 1 +Java 21 (OpenJDK or Oracle JDK), JavaCC, Apache Ant, Apache Ivy, JUnit 4, JLine 1 ### Documentation: diff --git a/build.xml b/build.xml index a9ee0a2b1..58aaf7b3e 100644 --- a/build.xml +++ b/build.xml @@ -54,7 +54,7 @@ - + @@ -183,6 +183,7 @@ + @@ -225,7 +226,7 @@ - + @@ -428,4 +429,31 @@ /> + + + + + + + + + + + + + + + diff --git a/src/doc/docbook/PsyllaReference_Installation.docbook b/src/doc/docbook/PsyllaReference_Installation.docbook index 50e8bd8ce..7b46f65ef 100644 --- a/src/doc/docbook/PsyllaReference_Installation.docbook +++ b/src/doc/docbook/PsyllaReference_Installation.docbook @@ -23,7 +23,7 @@ Build Service. -Java 17 (Java 21 (OpenJDK or Oracle JDK) @@ -71,10 +71,10 @@ xlink:href="https://github.com/sass/sassc/">SassC -To install dependencies, run (openSUSE Leap 15.5) +To install dependencies, run (openSUSE Leap 15.6) % sudo zypper -n --gpg-auto-import-keys ar -p1 \ - obs://home:urbic:coneforest/15.5 home:urbic:coneforest + obs://home:urbic:coneforest/15.6 home:urbic:coneforest % sudo zypper -n --gpg-auto-import-keys in -y \ ant{,-apache-resolver,-junit} javacc jline1 \ docbook5-xsl-stylesheets docbook-xsl-saxon graphviz sassc saxon6 \ @@ -85,7 +85,7 @@ or (Debian 11) % sudo apt-get install -y \ ant{,-optional} docbook-{xsl-{ns,saxon},mathml} \ graphviz sassc javacc junit4 libjline-java{,-doc} libxerces2-java \ - libxslthl-java openjdk-17-{jdk-headless,doc} + libxslthl-java openjdk-21-{jdk-headless,doc} diff --git a/src/doc/docbook/PsyllaReference_Operators_Details_gcd.docbook b/src/doc/docbook/PsyllaReference_Operators_Details_gcd.docbook index 37d8f13d9..3a4484f13 100644 --- a/src/doc/docbook/PsyllaReference_Operators_Details_gcd.docbook +++ b/src/doc/docbook/PsyllaReference_Operators_Details_gcd.docbook @@ -24,7 +24,7 @@ Returns the greatest common divisor of two numbers. number1, number1, result: - + @@ -39,10 +39,6 @@ Returns the greatest common divisor of two numbers. &TitleErrors; - — -when the at least one of the numbers is negative - -  — &ErrorReason_stackunderflow; diff --git a/src/doc/docbook/PsyllaReference_Operators_Details_idiv.docbook b/src/doc/docbook/PsyllaReference_Operators_Details_idiv.docbook index fd47c3fd8..50efc620c 100644 --- a/src/doc/docbook/PsyllaReference_Operators_Details_idiv.docbook +++ b/src/doc/docbook/PsyllaReference_Operators_Details_idiv.docbook @@ -26,7 +26,10 @@ signs. dividend, -divisor, +divisor: + + + quotient: diff --git a/src/doc/docbook/PsyllaReference_Operators_Details_reciprocal.docbook b/src/doc/docbook/PsyllaReference_Operators_Details_reciprocal.docbook index 97ad90738..2c915ab56 100644 --- a/src/doc/docbook/PsyllaReference_Operators_Details_reciprocal.docbook +++ b/src/doc/docbook/PsyllaReference_Operators_Details_reciprocal.docbook @@ -8,22 +8,6 @@ Returns the multiplicative inverse of the number. - - &TitleSynopsis; @@ -48,8 +32,20 @@ linkend="PsyllaReference_Types_Details_complex"/> &TitleExamples; 4 reciprocal ▶ 1:4 +0 reciprocal + ▶ +-2 3 div reciprocal + ▶ -3:2 -4. reciprocal ▶ -0.25 +-0. reciprocal + ▶ -∞ +0. reciprocal + ▶ ∞ +-∞ reciprocal + ▶ -0.0 +∞ reciprocal + ▶ 0.0 3 4 complex reciprocal ▶ 0.12-0.16i @@ -66,8 +62,10 @@ linkend="PsyllaReference_Types_Details_complex"/> &ErrorReason_typecheck; - — -when the number is zero + — when the +number is zero @@ -80,6 +78,7 @@ when the number is zero &TitleSeeAlso; , +, . diff --git a/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/ant/package-info.java b/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/ant/package-info.java new file mode 100644 index 000000000..24d5e209c --- /dev/null +++ b/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/ant/package-info.java @@ -0,0 +1,4 @@ +/** +* The Psylla Ant tasks. +*/ +package coneforest.psylla.tools.ant; diff --git a/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/processors/TypeHierarchyBuilder.java b/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/processors/TypeHierarchyBuilder.java index e79180242..289da98c7 100644 --- a/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/processors/TypeHierarchyBuilder.java +++ b/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/processors/TypeHierarchyBuilder.java @@ -26,7 +26,7 @@ import javax.tools.Diagnostic; @SupportedAnnotationTypes({"coneforest.psylla.runtime.Type"}) -@SupportedSourceVersion(SourceVersion.RELEASE_17) +@SupportedSourceVersion(SourceVersion.RELEASE_21) public class TypeHierarchyBuilder extends AbstractProcessor { diff --git a/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/processors/TypeMetadataCollector.java b/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/processors/TypeMetadataCollector.java index 4d6627f2d..ad1dd58a2 100644 --- a/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/processors/TypeMetadataCollector.java +++ b/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/processors/TypeMetadataCollector.java @@ -24,7 +24,7 @@ @SupportedAnnotationTypes({"coneforest.psylla.runtime.Type", "coneforest.psylla.runtime.ErrorType", "coneforest.psylla.runtime.OperatorType"}) -@SupportedSourceVersion(SourceVersion.RELEASE_17) +@SupportedSourceVersion(SourceVersion.RELEASE_21) public class TypeMetadataCollector extends AbstractProcessor { diff --git a/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/processors/package-info.java b/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/processors/package-info.java new file mode 100644 index 000000000..ebc4586d3 --- /dev/null +++ b/src/main/java/coneforest.psylla.tools/coneforest/psylla/tools/processors/package-info.java @@ -0,0 +1,4 @@ +/** +* The Psylla annotation processors. +*/ +package coneforest.psylla.tools.processors; diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyBigFraction.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyBigFraction.java index 1d712d9d4..02fa2fab6 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyBigFraction.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyBigFraction.java @@ -105,9 +105,9 @@ public int intValue() } @Override - public PsyBoolean psyIsZero() + public boolean isZero() { - return PsyBoolean.FALSE; + return false; } @Override diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyBigInteger.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyBigInteger.java index 57c96be78..84762c558 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyBigInteger.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyBigInteger.java @@ -8,8 +8,7 @@ */ @Type("biginteger") public final class PsyBigInteger - implements - PsyIntegral + implements PsyIntegral { /** * Constructs a new {@code biginteger} for the given value. @@ -21,6 +20,12 @@ public PsyBigInteger(final BigInteger value) this.value=value; } + @Override + public boolean isZero() + { + return false; + } + @Override public int intValue() { @@ -106,88 +111,120 @@ public PsyIntegral psyAbs() } @Override - public PsyIntegral psyIdiv(final PsyIntegral oIntegral) + public PsyIntegral psyIdiv(final PsyRational oRational) throws PsyUndefinedResultException { - if(oIntegral.psyIsZero().booleanValue()) - throw new PsyUndefinedResultException(); - if(oIntegral instanceof PsyBigInteger oBigInteger) - return PsyIntegral.of(value.divide(oBigInteger.value)); - return PsyIntegral.of( - value.divide(BigInteger.valueOf(((PsyInteger)oIntegral).longValue()))); + switch(oRational) + { + case PsyInteger oInteger: + if(oInteger==PsyInteger.ZERO) + throw new PsyUndefinedResultException(); + return PsyIntegral.of( + value.divide(BigInteger.valueOf(((PsyInteger)oInteger).longValue()))); + case PsyBigInteger oBigInteger: + return PsyIntegral.of(value.divide(oBigInteger.value)); + default: + return ((PsyIntegral)psyMul(oRational.psyDenominator())) + .psyIdiv(oRational.psyNumerator()); + } } @Override - public PsyIntegral psyMod(final PsyIntegral oIntegral) + public PsyIntegral psyMod(final PsyRational oRational) throws PsyUndefinedResultException, PsyRangeCheckException { - if(oIntegral.psyIsZero().booleanValue()) - throw new PsyUndefinedResultException(); - try - { - return PsyIntegral.of(value.mod(oIntegral.bigIntegerValue())); - } - catch(final ArithmeticException ex) + switch(oRational) { - throw new PsyRangeCheckException(); + case PsyIntegral oIntegral: + if(oIntegral==PsyInteger.ZERO) + throw new PsyUndefinedResultException(); + try + { + return PsyIntegral.of(value.mod(oIntegral.bigIntegerValue())); + } + catch(final ArithmeticException ex) + { + throw new PsyRangeCheckException(); + } + default: + return ((PsyIntegral)psyMul(oRational.psyDenominator())) + .psyMod(oRational.psyNumerator()); } } @Override - public PsyIntegral psyGCD(final PsyIntegral oIntegral) + public PsyRational psyGCD(final PsyRational oRational) { - if(psyIsZero().booleanValue()) - return oIntegral; - if(oIntegral.psyIsZero().booleanValue()) - return this; - return PsyIntegral.of(value.gcd(oIntegral.bigIntegerValue())); + switch(oRational) + { + case PsyIntegral oIntegral: + if(oIntegral.isZero()) + return this; + return PsyIntegral.of(value.gcd(oIntegral.bigIntegerValue())); + default: + return oRational.psyGCD(this); + } } @Override public PsyRealNumeric psyMul(final PsyRealNumeric oRealNumeric) { - if(oRealNumeric instanceof PsyIntegral oIntegral) - return PsyIntegral.of(value.multiply(oIntegral.bigIntegerValue())); - if(oRealNumeric instanceof PsyRational oRational) - return PsyRational.of((PsyIntegral)psyMul(oRational.psyNumerator()), oRational.psyDenominator()); - if(oRealNumeric instanceof PsyReal oReal) - return new PsyReal(doubleValue()*oReal.doubleValue()); - throw new ClassCastException(); + return switch(oRealNumeric) + { + case PsyIntegral oIntegral-> + PsyIntegral.of(value.multiply(oIntegral.bigIntegerValue())); + case PsyRational oRational-> + PsyRational.of((PsyIntegral)psyMul(oRational.psyNumerator()), oRational.psyDenominator()); + case PsyReal oReal-> + new PsyReal(doubleValue()*oReal.doubleValue()); + }; } @Override public PsyRealNumeric psyDiv(final PsyRealNumeric oRealNumeric) throws PsyUndefinedResultException { - if(oRealNumeric instanceof PsyIntegral oIntegral) - return PsyRational.of(psyNumerator(), (PsyIntegral)psyDenominator().psyMul(oIntegral)); - if(oRealNumeric instanceof PsyRational oRational) - return PsyRational.of( - (PsyIntegral)psyNumerator().psyMul(oRational.psyDenominator()), - (PsyIntegral)psyDenominator().psyMul(oRational.psyNumerator())); - if(oRealNumeric instanceof PsyReal oReal) - return new PsyReal(doubleValue()*oReal.doubleValue()); - throw new ClassCastException(); + return switch(oRealNumeric) + { + case PsyIntegral oIntegral-> + PsyRational.of(psyNumerator(), (PsyIntegral)psyDenominator().psyMul(oIntegral)); + case PsyRational oRational-> + PsyRational.of( + (PsyIntegral)psyNumerator().psyMul(oRational.psyDenominator()), + (PsyIntegral)psyDenominator().psyMul(oRational.psyNumerator())); + case PsyReal oReal-> + new PsyReal(doubleValue()*oReal.doubleValue()); + }; } @Override public PsyRealNumeric psyAdd(final PsyRealNumeric oRealNumeric) { - if(oRealNumeric instanceof PsyIntegral oIntegral) - return PsyIntegral.of(value.add(oIntegral.bigIntegerValue())); - if(oRealNumeric instanceof PsyReal oReal) - return new PsyReal(doubleValue()+oReal.doubleValue()); - throw new ClassCastException(); + return switch(oRealNumeric) + { + case PsyIntegral oIntegral-> + PsyIntegral.of(value.add(oIntegral.bigIntegerValue())); + case PsyRational oRational-> + PsyRational.of((PsyIntegral)psyMul(oRational.psyDenominator()).psyAdd(oRational.psyNumerator()), + oRational.psyDenominator()); + case PsyReal oReal-> + new PsyReal(doubleValue()+oReal.doubleValue()); + }; } @Override public PsyRealNumeric psySub(final PsyRealNumeric oRealNumeric) { - if(oRealNumeric instanceof PsyIntegral oIntegral) - return PsyIntegral.of(value.subtract(oIntegral.bigIntegerValue())); - if(oRealNumeric instanceof PsyReal oReal) - return new PsyReal(doubleValue()-oReal.doubleValue()); - throw new ClassCastException(); + return switch(oRealNumeric) + { + case PsyIntegral oIntegral-> + PsyIntegral.of(value.subtract(oIntegral.bigIntegerValue())); + case PsyRational oRational-> + PsyRational.of((PsyIntegral)psyMul(oRational.psyDenominator()).psySub(oRational.psyNumerator()), + oRational.psyDenominator()); + case PsyReal oReal-> + new PsyReal(doubleValue()-oReal.doubleValue()); + }; } @Override @@ -196,12 +233,6 @@ public PsyIntegral psyNeg() return PsyIntegral.of(value.negate()); } - @Override - public PsyBoolean psyIsZero() - { - return PsyBoolean.of(value.equals(BigInteger.ZERO)); - } - @Override public PsyInteger psySignum() { @@ -211,15 +242,15 @@ public PsyInteger psySignum() @Override public int compareTo(final PsyRealNumeric oRealNumeric) { - if(oRealNumeric instanceof PsyIntegral oIntegral) - return value.compareTo(oIntegral.bigIntegerValue()); - if(oRealNumeric instanceof PsyRational oRational) - return psyMul(oRational.psyDenominator()).compareTo(oRational.psyNumerator()); - if(oRealNumeric instanceof PsyReal oReal) - return Double.compare(doubleValue(), oReal.doubleValue()); - - // TODO - throw new ClassCastException(); + return switch(oRealNumeric) + { + case PsyIntegral oIntegral-> + value.compareTo(oIntegral.bigIntegerValue()); + case PsyRational oRational-> + psyMul(oRational.psyDenominator()).compareTo(oRational.psyNumerator()); + case PsyReal oReal-> + Double.compare(doubleValue(), oReal.doubleValue()); + }; } @Override @@ -231,7 +262,6 @@ public String toSyntaxString() @Override public boolean equals(final Object obj) { - // TODO PsyInteger return obj instanceof PsyBigInteger oBigInteger && value.equals(oBigInteger.value); } diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyBlockingQueue.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyBlockingQueue.java index e804e9da5..dcf93a5eb 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyBlockingQueue.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyBlockingQueue.java @@ -8,7 +8,7 @@ * The representation of {@code blockingqueue}. */ @Type("blockingqueue") -public class PsyBlockingQueue +public final class PsyBlockingQueue implements PsyFormalQueue, PsyCloseable diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyComplex.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyComplex.java index ff9b4898d..acd465cde 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyComplex.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyComplex.java @@ -43,10 +43,9 @@ public double imagValue() } @Override - public PsyBoolean psyIsZero() + public boolean isZero() { - // TODO - return PsyBoolean.of(re==0.D && im==0.D); + return (re==0.D && im==0.D); } @Override @@ -58,7 +57,7 @@ public PsyReal psyAbs() @Override public PsyComplex psySignum() { - if(psyIsZero().booleanValue()) + if(isZero()) return this; return psyDiv(psyAbs()); } @@ -67,10 +66,10 @@ public PsyComplex psySignum() public String toSyntaxString() { var sb=new StringBuilder(); - sb.append(re); - if(im>=0.D) + sb.append(Double.isInfinite(re)? (re==Double.NEGATIVE_INFINITY? "-∞": "∞"): String.valueOf(re)); + if(Double.compare(im, 0.D)>=0) sb.append('+'); - sb.append(im); + sb.append(Double.isInfinite(im)? (im==Double.NEGATIVE_INFINITY? "-∞": "∞"): String.valueOf(im)); sb.append('i'); return sb.toString(); } @@ -137,6 +136,16 @@ public PsyComplex psySub(final PsyNumeric oNumeric) @Override public PsyComplex psyReciprocal() { + if(Double.compare(re, -0.D)==0) + if(Double.compare(im, -0.D)==0) + return new PsyComplex(Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY); + else if(Double.compare(im, 0.D)==0) + return new PsyComplex(Double.NEGATIVE_INFINITY, Double.NEGATIVE_INFINITY); + if(Double.compare(re, 0.D)==0) + if(Double.compare(im, -0.D)==0) + return new PsyComplex(Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY); + else if(Double.compare(im, 0.D)==0) + return new PsyComplex(Double.POSITIVE_INFINITY, Double.NEGATIVE_INFINITY); final var d=re*re+im*im; return new PsyComplex(re/d, -im/d); } @@ -173,7 +182,7 @@ public PsyComplex psyDiv(final PsyNumeric oNumeric) @Override public PsyNumeric psyPow(final PsyNumeric oNumeric) { - if(psyIsZero().booleanValue() && oNumeric.psyNonZero().booleanValue()) + if(isZero() && !oNumeric.isZero()) return this; return psyLog().psyMul(oNumeric).psyExp(); } diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyFileSystem.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyFileSystem.java index 2f24f4a1c..87ee0f517 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyFileSystem.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyFileSystem.java @@ -804,7 +804,7 @@ public static void psyChangeFileAttribute(final PsyTextual oFileName, final PsyT } } - public static PsyFormalStream psyFiles(final PsyTextual oFileName) + public static PsyStream psyFiles(final PsyTextual oFileName) throws PsyFileAccessDeniedException, PsyFileNotFoundException, @@ -841,27 +841,34 @@ public static PsyFormalStream psyFiles(final PsyTextual oFileName) private static PsyObject toPsyTObject(final Object obj) { - //System.out.println(obj.getClass()); - if(obj instanceof String stringobj) - return new PsyName(stringobj); - if(obj instanceof Integer integerobj) - return PsyInteger.of(integerobj.longValue()); - if(obj instanceof Long longobj) - return PsyInteger.of(longobj.longValue()); - if(obj instanceof Boolean booleanobj) - return PsyBoolean.of(booleanobj); - throw new ClassCastException(); + switch(obj) + { + case String stringobj: + return new PsyName(stringobj); + case Integer integerobj: + return PsyInteger.of(integerobj.longValue()); + case Long longobj: + return PsyInteger.of(longobj.longValue()); + case Boolean booleanobj: + return PsyBoolean.of(booleanobj); + default: + throw new ClassCastException(); + } } private static Object fromPsyTObject(final PsyObject o) { - if(o instanceof PsyTextual oTextual) - return oTextual.stringValue(); - if(o instanceof PsyInteger oInteger) - return oInteger.longValue(); - if(o instanceof PsyBoolean oBoolean) - return oBoolean.booleanValue(); - throw new ClassCastException(); + switch(o) + { + case PsyTextual oTextual: + return oTextual.stringValue(); + case PsyInteger oInteger: + return oInteger.longValue(); + case PsyBoolean oBoolean: + return oBoolean.booleanValue(); + default: + throw new ClassCastException(); + } } /** diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyFraction.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyFraction.java index 9c9ac1e6e..9c8b84041 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyFraction.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyFraction.java @@ -103,9 +103,9 @@ public PsyInteger psyCeiling() } @Override - public PsyBoolean psyIsZero() + public boolean isZero() { - return PsyBoolean.FALSE; + return false; } @Override diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyInteger.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyInteger.java index 9a57cbc5d..3a6c0f29b 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyInteger.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyInteger.java @@ -18,9 +18,9 @@ private PsyInteger(final long value) } @Override - public PsyBoolean psyIsZero() + public boolean isZero() { - return PsyBoolean.of(value==0L); + return value==0L; } @Override @@ -62,31 +62,37 @@ public PsyInteger psyNot() @Override public PsyIntegral psyOr(final PsyIntegral oIntegral) { - if(oIntegral instanceof PsyInteger oInteger) - return PsyInteger.of(value | oInteger.value); - if(oIntegral instanceof PsyBigInteger oBigInteger) - return PsyIntegral.of(bigIntegerValue().or(oBigInteger.bigIntegerValue())); - throw new ClassCastException(); + return switch(oIntegral) + { + case PsyInteger oInteger-> + of(value|oInteger.value); + case PsyBigInteger oBigInteger-> + PsyIntegral.of(bigIntegerValue().or(oBigInteger.bigIntegerValue())); + }; } @Override public PsyIntegral psyAnd(final PsyIntegral oIntegral) { - if(oIntegral instanceof PsyInteger oInteger) - return PsyInteger.of(value & oInteger.value); - if(oIntegral instanceof PsyBigInteger oBigInteger) - return PsyIntegral.of(bigIntegerValue().and(oBigInteger.bigIntegerValue())); - throw new ClassCastException(); + return switch(oIntegral) + { + case PsyInteger oInteger-> + of(value&oInteger.value); + case PsyBigInteger oBigInteger-> + PsyIntegral.of(bigIntegerValue().and(oBigInteger.bigIntegerValue())); + }; } @Override public PsyIntegral psyXor(final PsyIntegral oIntegral) { - if(oIntegral instanceof PsyInteger oInteger) - return PsyInteger.of(value^oInteger.value); - if(oIntegral instanceof PsyBigInteger oBigInteger) - return PsyIntegral.of(bigIntegerValue().xor(oBigInteger.bigIntegerValue())); - throw new ClassCastException(); + return switch(oIntegral) + { + case PsyInteger oInteger-> + of(value^oInteger.value); + case PsyBigInteger oBigInteger-> + PsyIntegral.of(bigIntegerValue().xor(oBigInteger.bigIntegerValue())); + }; } @Override @@ -151,109 +157,98 @@ public PsyInteger psySignum() @Override public PsyRealNumeric psyAdd(final PsyRealNumeric oRealNumeric) { - if(oRealNumeric instanceof PsyInteger oInteger) + switch(oRealNumeric) { - try - { - return PsyInteger.of(Math.addExact(value, oInteger.value)); - } - catch(final ArithmeticException ex) - { - return PsyIntegral.of(bigIntegerValue().add(oInteger.bigIntegerValue())); - } + case PsyInteger oInteger: + try + { + return PsyInteger.of(Math.addExact(value, oInteger.value)); + } + catch(final ArithmeticException ex) + { + return PsyIntegral.of(bigIntegerValue().add(oInteger.bigIntegerValue())); + } + case PsyBigInteger oBigInteger: + return PsyIntegral.of( + bigIntegerValue().add(oBigInteger.bigIntegerValue())); + case PsyRational oRational: + return PsyRational.of( + (PsyIntegral)psyMul(oRational.psyDenominator()).psyAdd(oRational.psyNumerator()), + oRational.psyDenominator()); + case PsyReal oReal: + return new PsyReal(value+oReal.doubleValue()); } - if(oRealNumeric instanceof PsyBigInteger oBigInteger) - return PsyIntegral.of( - bigIntegerValue().add(oBigInteger.bigIntegerValue())); - if(oRealNumeric instanceof PsyRational oRational) - return PsyRational.of( - (PsyIntegral)psyMul(oRational.psyDenominator()).psyAdd(oRational.psyNumerator()), - oRational.psyDenominator()); - if(oRealNumeric instanceof PsyReal oReal) - return new PsyReal(value+oReal.doubleValue()); - throw new ClassCastException(); } @Override public PsyRealNumeric psySub(final PsyRealNumeric oRealNumeric) { - if(oRealNumeric instanceof PsyInteger oInteger) + switch(oRealNumeric) { - try - { - return PsyInteger.of(Math.subtractExact(value, oInteger.value)); - } - catch(final ArithmeticException ex) - { - return PsyIntegral.of(bigIntegerValue().subtract(oInteger.bigIntegerValue())); - } + case PsyInteger oInteger: + try + { + return PsyInteger.of(Math.subtractExact(value, oInteger.value)); + } + catch(final ArithmeticException ex) + { + return PsyIntegral.of(bigIntegerValue().subtract(oInteger.bigIntegerValue())); + } + case PsyBigInteger oBigInteger: + return PsyIntegral.of(bigIntegerValue().subtract(oBigInteger.bigIntegerValue())); + case PsyRational oRational: + return PsyRational.of( + (PsyIntegral)psyMul(oRational.psyDenominator()).psySub(oRational.psyNumerator()), + oRational.psyDenominator()); + case PsyReal oReal: + return new PsyReal(value-oReal.doubleValue()); } - if(oRealNumeric instanceof PsyBigInteger oBigInteger) - return PsyIntegral.of(bigIntegerValue().subtract(oBigInteger.bigIntegerValue())); - if(oRealNumeric instanceof PsyRational oRational) - return PsyRational.of( - (PsyIntegral)psyMul(oRational.psyDenominator()).psySub(oRational.psyNumerator()), - oRational.psyDenominator()); - if(oRealNumeric instanceof PsyReal oReal) - return new PsyReal(value-oReal.doubleValue()); - throw new ClassCastException(); } @Override public PsyRealNumeric psyMul(final PsyRealNumeric oRealNumeric) { - if(oRealNumeric instanceof PsyInteger oInteger) - try - { - return of(Math.multiplyExact(value, oInteger.value)); - } - catch(final ArithmeticException ex) - { - return PsyIntegral.of(bigIntegerValue().multiply(oInteger.bigIntegerValue())); - } - if(oRealNumeric instanceof PsyBigInteger oBigInteger) - return PsyIntegral.of(bigIntegerValue().multiply(oBigInteger.bigIntegerValue())); - if(oRealNumeric instanceof PsyRational oRational) - return PsyRational.of((PsyIntegral)psyMul(oRational.psyNumerator()), - oRational.psyDenominator()); - if(oRealNumeric instanceof PsyReal oReal) - return new PsyReal(value*oReal.doubleValue()); - throw new ClassCastException(); + switch(oRealNumeric) + { + case PsyInteger oInteger: + try + { + return of(Math.multiplyExact(value, oInteger.value)); + } + catch(final ArithmeticException ex) + { + return PsyIntegral.of(bigIntegerValue().multiply(oInteger.bigIntegerValue())); + } + case PsyBigInteger oBigInteger: + return PsyIntegral.of(bigIntegerValue().multiply(oBigInteger.bigIntegerValue())); + case PsyRational oRational: + return PsyRational.of((PsyIntegral)psyMul(oRational.psyNumerator()), + oRational.psyDenominator()); + case PsyReal oReal: + return new PsyReal(value*oReal.doubleValue()); + } } @Override public int compareTo(final PsyRealNumeric oRealNumeric) { - if(oRealNumeric instanceof PsyInteger oInteger) - return Long.compare(value, oInteger.value); - if(oRealNumeric instanceof PsyBigInteger oBigInteger) - return bigIntegerValue().compareTo(oBigInteger.bigIntegerValue()); - if(oRealNumeric instanceof PsyRational oRational) - return psyMul(oRational.psyDenominator()).compareTo(oRational.psyNumerator()); - if(oRealNumeric instanceof PsyReal oReal) - return Double.compare(doubleValue(), oReal.doubleValue()); - throw new ClassCastException(); + return switch(oRealNumeric) + { + case PsyInteger oInteger-> + Long.compare(value, oInteger.value); + case PsyBigInteger oBigInteger-> + bigIntegerValue().compareTo(oBigInteger.bigIntegerValue()); + case PsyRational oRational-> + psyMul(oRational.psyDenominator()).compareTo(oRational.psyNumerator()); + case PsyReal oReal-> + Double.compare(doubleValue(), oReal.doubleValue()); + }; } @Override public PsyRealNumeric psyDiv(final PsyRealNumeric oRealNumeric) throws PsyUndefinedResultException { - if(oRealNumeric instanceof PsyInteger oInteger) - { - if(oInteger==PsyInteger.ZERO) - throw new PsyUndefinedResultException(); - return PsyFraction.of(value, oInteger.value); - } - if(oRealNumeric instanceof PsyBigInteger oBigInteger) - return PsyRational.of(this, oBigInteger); - if(oRealNumeric instanceof PsyRational oRational) - return PsyRational.of((PsyIntegral)psyMul(oRational.psyDenominator()), - oRational.psyNumerator()); - if(oRealNumeric instanceof PsyReal oReal) - return new PsyReal(value/oReal.doubleValue()); - throw new ClassCastException(); - /* if(oRealNumeric==ZERO) throw new PsyUndefinedResultException(); return switch(oRealNumeric) @@ -264,56 +259,59 @@ public PsyRealNumeric psyDiv(final PsyRealNumeric oRealNumeric) PsyRational.of(this, oBigInteger); case PsyRational oRational-> PsyRational.of((PsyIntegral)psyMul(oRational.psyDenominator()), - oRational.psyNumerator()); + oRational.psyNumerator()); case PsyReal oReal-> new PsyReal(value/oReal.doubleValue()); }; - */ } @Override - public PsyIntegral psyMod(final PsyIntegral oIntegral) + public PsyIntegral psyMod(final PsyRational oRational) throws PsyUndefinedResultException, PsyRangeCheckException { - if(oIntegral.psyIsZero().booleanValue()) - throw new PsyUndefinedResultException(); - if(oIntegral instanceof PsyInteger oInteger) - { - final var integer=oInteger.value; // TODO - if(integer<0) - throw new PsyRangeCheckException(); - if(integer==0) - throw new PsyUndefinedResultException(); - return PsyInteger.of(Math.floorMod(value, integer)); - } - else + switch(oRational) { - try - { - return PsyIntegral.of( - bigIntegerValue().mod(((PsyBigInteger)oIntegral).bigIntegerValue())); - } - catch(final ArithmeticException ex) - { - throw new PsyRangeCheckException(); - } + case PsyInteger oInteger: + final var integer=oInteger.value; // TODO + if(integer<0) + throw new PsyRangeCheckException(); + if(integer==0) + throw new PsyUndefinedResultException(); + return of(Math.floorMod(value, integer)); + case PsyBigInteger oBigInteger: + try + { + return PsyIntegral.of( + bigIntegerValue().mod(oBigInteger.bigIntegerValue())); + } + catch(final ArithmeticException ex) + { + throw new PsyRangeCheckException(); + } + default: + return ((PsyIntegral)psyMul(oRational.psyDenominator())) + .psyMod(oRational.psyNumerator()); } } @Override - public PsyIntegral psyIdiv(final PsyIntegral oIntegral) + public PsyIntegral psyIdiv(final PsyRational oRational) throws PsyUndefinedResultException { - if(oIntegral.psyIsZero().booleanValue()) - throw new PsyUndefinedResultException(); - if(oIntegral instanceof PsyInteger oInteger) + switch(oRational) { - if(value==Long.MIN_VALUE && oInteger.value==-1L) - return of(Long.MIN_VALUE).psyNeg(); - return of(value/oInteger.value); + case PsyInteger oInteger: + if(oInteger==ZERO) + throw new PsyUndefinedResultException(); + if(value==Long.MIN_VALUE && oInteger.value==-1L) + return of(Long.MIN_VALUE).psyNeg(); + return of(value/oInteger.value); + case PsyBigInteger oBigInteger: + return PsyIntegral.of(bigIntegerValue().divide(oBigInteger.bigIntegerValue())); + default: + return ((PsyIntegral)psyMul(oRational.psyDenominator())) + .psyIdiv(oRational.psyNumerator()); } - //if(oIntegral instanceof PsyBigInteger) - return PsyIntegral.of(bigIntegerValue().divide(oIntegral.bigIntegerValue())); } @Override @@ -332,16 +330,21 @@ public PsyBoolean psyInUnicodeBlock(final PsyTextual oTextual) @Override public PsyBoolean psyEq(final PsyObject o) { - if(o instanceof PsyInteger oInteger) - return PsyBoolean.of(value==oInteger.value); - if(o instanceof PsyBigInteger oBigInteger) - return PsyBoolean.of(bigIntegerValue().equals(oBigInteger.bigIntegerValue())); - else if(o instanceof PsyReal oReal) - return PsyBoolean.of(doubleValue()==oReal.doubleValue()); - else if(o instanceof PsyComplex oComplex) - return PsyBoolean.of(doubleValue()==oComplex.psyRealPart().doubleValue() - && oComplex.psyImagPart().doubleValue()==0.D); - return PsyBoolean.FALSE; + return PsyBoolean.of(switch(o) + { + // TODO + case PsyInteger oInteger-> + value==oInteger.value; + //case PsyBigInteger oBigInteger-> + // bigIntegerValue().equals(oBigInteger.bigIntegerValue()); + case PsyReal oReal-> + doubleValue()==oReal.doubleValue(); + case PsyComplex oComplex-> + doubleValue()==oComplex.psyRealPart().doubleValue() + && oComplex.psyImagPart().doubleValue()==0.D; // TODO + default-> + false; + }); } @Override diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyIntegral.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyIntegral.java index d1de7d2a5..b51f3fe79 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyIntegral.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyIntegral.java @@ -51,50 +51,52 @@ default public PsyIntegral psyRound() return this; } - public PsyIntegral psyIdiv(final PsyIntegral oIntegral) - throws PsyUndefinedResultException; + @Override + default public PsyIntegral psyIdiv(final PsyRational oRational) + throws PsyUndefinedResultException + { + return ((PsyIntegral)psyMul(oRational.psyDenominator())) + .psyIdiv(oRational.psyNumerator()); + } - /** - * Returns an {@code integral} representing this object modulo given modulus. - * - * @param oIntegral the given modulus. - * @throws PsyRangeCheckException when the modulus is negative. - * @throws PsyUndefinedResultException when the modulus is zero. - */ - public PsyIntegral psyMod(final PsyIntegral oIntegral) + @Override + public PsyIntegral psyMod(final PsyRational oRational) throws PsyRangeCheckException, PsyUndefinedResultException; - /** - * Returns an {@code integral} representing the greatest common divisor of this object and - * given object. - * - * @param oIntegral the given object. - * @return the greatest common divisor. - */ - default public PsyIntegral psyGCD(final PsyIntegral oIntegral) + @Override + default public PsyRational psyGCD(final PsyRational oRational) { - var oX=psyAbs(); - var oY=oIntegral.psyAbs(); - if(oY.psyIsZero().booleanValue()) - return oX; - while(oX.psyNonZero().booleanValue()) + switch(oRational) { - if(oX.compareTo(oY)>0) - { - var oT=oX; - oX=oY; - oY=oT; - } - try - { - oY=oY.psyMod(oX); - } - catch(final PsyUndefinedResultException|PsyRangeCheckException e) - { - // NOP - } + case PsyIntegral oIntegral: + var oX=psyAbs(); + var oY=oIntegral.psyAbs(); + if(oY.isZero()) + return oX; + while(!oX.isZero()) + { + if(oX.compareTo(oY)>0) + { + var oT=oX; + oX=oY; + oY=oT; + } + try + { + oY=oY.psyMod(oX); + } + catch(final PsyUndefinedResultException|PsyRangeCheckException e) + { + // NOP + } + } + return oY; + default: + return PsyRational.of( + (PsyIntegral)((PsyIntegral)psyMul(oRational.psyDenominator())) + .psyGCD(oRational.psyNumerator()), + oRational.psyDenominator()); } - return oY; } /** @@ -102,14 +104,20 @@ default public PsyIntegral psyGCD(final PsyIntegral oIntegral) * object. * * @param oIntegral given object. - * @return the least common multiple. + * @return the least common multiple of this object and given object. */ default PsyIntegral psyLCM(final PsyIntegral oIntegral) - throws PsyUndefinedResultException { - if(psyIsZero().booleanValue() || oIntegral.psyIsZero().booleanValue()) + if(isZero() || oIntegral.isZero()) return PsyInteger.ZERO; - return ((PsyIntegral)psyMul(oIntegral)).psyIdiv(psyGCD(oIntegral)); + try + { + return ((PsyIntegral)psyMul(oIntegral)).psyIdiv(psyGCD(oIntegral)).psyAbs(); + } + catch(final PsyUndefinedResultException e) + { + throw new AssertionError(); + } } @Override @@ -243,27 +251,6 @@ private static PsyInteger parseCharLiteral(final String image) public static final PsyInteger TWO=PsyInteger.TWO; public static final PsyInteger MINUS_ONE=PsyInteger.MINUS_ONE; - /** - * Context action of the {@code idiv} operator. - */ - @OperatorType("idiv") - public static final ContextAction PSY_IDIV - =ContextAction.ofBiFunction(PsyIntegral::psyIdiv); - - /** - * Context action of the {@code mod} operator. - */ - @OperatorType("mod") - public static final ContextAction PSY_MOD - =ContextAction.ofBiFunction(PsyIntegral::psyMod); - - /** - * Context action of the {@code gcd} operator. - */ - @OperatorType("gcd") - public static final ContextAction PSY_GCD - =ContextAction.ofBiFunction(PsyIntegral::psyGCD); - /** * Context action of the {@code lcm} operator. */ diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyOperator.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyOperator.java index 4b16c1af2..b4cd853c9 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyOperator.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyOperator.java @@ -7,9 +7,7 @@ */ @Type("operator") public abstract class PsyOperator - implements - PsyAtomic, - PsyExecutable + implements PsyExecutable, PsyValue { /** * Constructs a new {@code operator} with the given name. diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyRandom.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyRandom.java index f64ff40d5..324736498 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyRandom.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyRandom.java @@ -30,7 +30,7 @@ public PsyRealNumeric psyUniformDeviate(final PsyRealNumeric oRealNumeric) else if(oRealNumeric instanceof PsyInteger) { final var numeric=oRealNumeric.longValue(); - if(numeric>Long.MAX_VALUE) + if(numeric>Long.MAX_VALUE) // TODO throw new PsyRangeCheckException(); try { diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyRational.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyRational.java index ad39f3a26..bdb6420d2 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyRational.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyRational.java @@ -50,35 +50,35 @@ default public PsyRational psyNeg() @Override default public PsyRealNumeric psyAdd(final PsyRealNumeric oRealNumeric) { - if(oRealNumeric instanceof PsyIntegral oIntegral) - return of( - (PsyIntegral)psyNumerator().psyAdd(psyDenominator().psyMul(oIntegral)), - psyDenominator()); - if(oRealNumeric instanceof PsyRational oRational) - return of( - (PsyIntegral)psyNumerator().psyMul(oRational.psyDenominator()) - .psyAdd(psyDenominator().psyMul(oRational.psyNumerator())), - (PsyIntegral)psyDenominator().psyMul(oRational.psyDenominator())); - if(oRealNumeric instanceof PsyReal oReal) - return new PsyReal(doubleValue()+oReal.doubleValue()); - throw new ClassCastException(); + return switch(oRealNumeric) + { + case PsyIntegral oIntegral-> + of((PsyIntegral)psyNumerator().psyAdd(psyDenominator().psyMul(oIntegral)), + psyDenominator()); + case PsyRational oRational-> + of((PsyIntegral)psyNumerator().psyMul(oRational.psyDenominator()) + .psyAdd(psyDenominator().psyMul(oRational.psyNumerator())), + (PsyIntegral)psyDenominator().psyMul(oRational.psyDenominator())); + case PsyReal oReal-> + new PsyReal(doubleValue()+oReal.doubleValue()); + }; } @Override default public PsyRealNumeric psySub(final PsyRealNumeric oRealNumeric) { - if(oRealNumeric instanceof PsyIntegral oIntegral) - return of( - (PsyIntegral)psyNumerator().psySub(psyDenominator().psyMul(oIntegral)), - psyDenominator()); - if(oRealNumeric instanceof PsyRational oRational) - return of( - (PsyIntegral)psyNumerator().psyMul(oRational.psyDenominator()) - .psySub(psyDenominator().psyMul(oRational.psyNumerator())), - (PsyIntegral)psyDenominator().psyMul(oRational.psyDenominator())); - if(oRealNumeric instanceof PsyReal oReal) - return new PsyReal(doubleValue()+oReal.doubleValue()); - throw new ClassCastException(); + return switch(oRealNumeric) + { + case PsyIntegral oIntegral-> + of((PsyIntegral)psyNumerator().psySub(psyDenominator().psyMul(oIntegral)), + psyDenominator()); + case PsyRational oRational-> + of((PsyIntegral)psyNumerator().psyMul(oRational.psyDenominator()) + .psySub(psyDenominator().psyMul(oRational.psyNumerator())), + (PsyIntegral)psyDenominator().psyMul(oRational.psyDenominator())); + case PsyReal oReal-> + new PsyReal(doubleValue()+oReal.doubleValue()); + }; } @Override @@ -98,32 +98,73 @@ default public PsyRational psyReciprocal() @Override default public PsyRealNumeric psyMul(final PsyRealNumeric oRealNumeric) { - if(oRealNumeric instanceof PsyIntegral oIntegral) - return PsyRational.of( - (PsyIntegral)psyNumerator().psyMul(oIntegral), - psyDenominator()); - if(oRealNumeric instanceof PsyRational oRational) - return PsyRational.of( - (PsyIntegral)psyNumerator().psyMul(oRational.psyNumerator()), - (PsyIntegral)psyDenominator().psyMul(oRational.psyDenominator())); - if(oRealNumeric instanceof PsyReal oReal) - return new PsyReal(doubleValue()*oReal.doubleValue()); - throw new ClassCastException(); + return switch(oRealNumeric) + { + case PsyIntegral oIntegral-> + PsyRational.of((PsyIntegral)psyNumerator().psyMul(oIntegral), + psyDenominator()); + case PsyRational oRational-> + PsyRational.of((PsyIntegral)psyNumerator().psyMul(oRational.psyNumerator()), + (PsyIntegral)psyDenominator().psyMul(oRational.psyDenominator())); + case PsyReal oReal-> + new PsyReal(doubleValue()*oReal.doubleValue()); + }; } default public PsyRealNumeric psyDiv(final PsyRealNumeric oRealNumeric) throws PsyUndefinedResultException { - if(oRealNumeric instanceof PsyIntegral oIntegral) - return PsyRational.of(psyNumerator(), - (PsyIntegral)psyDenominator().psyMul(oIntegral)); - if(oRealNumeric instanceof PsyRational oRational) - return PsyRational.of( - (PsyIntegral)psyNumerator().psyMul(oRational.psyDenominator()), - (PsyIntegral)psyDenominator().psyMul(oRational.psyNumerator())); - if(oRealNumeric instanceof PsyReal oReal) - return new PsyReal(doubleValue()*oReal.doubleValue()); - throw new ClassCastException(); + return switch(oRealNumeric) + { + case PsyIntegral oIntegral-> + PsyRational.of(psyNumerator(), + (PsyIntegral)psyDenominator().psyMul(oIntegral)); + case PsyRational oRational-> + PsyRational.of((PsyIntegral)psyNumerator().psyMul(oRational.psyDenominator()), + (PsyIntegral)psyDenominator().psyMul(oRational.psyNumerator())); + case PsyReal oReal-> + new PsyReal(doubleValue()/oReal.doubleValue()); + }; + } + + /** + * Returns an {@code integral} representing this object modulo given modulus. + * + * @param oRational the given modulus. + * @return an {@code integral} representing this object modulo given modulus. + * @throws PsyRangeCheckException when the modulus is negative. + * @throws PsyUndefinedResultException when the modulus is zero. + */ + default public PsyRational psyMod(final PsyRational oRational) + throws PsyRangeCheckException, PsyUndefinedResultException + { + return of( + ((PsyIntegral)psyNumerator().psyMul(oRational.psyDenominator())) + .psyMod((PsyIntegral)psyDenominator().psyMul(oRational.psyNumerator())), + (PsyIntegral)psyDenominator().psyMul(oRational.psyDenominator())); + } + + // TODO javadoc + default public PsyIntegral psyIdiv(final PsyRational oRational) + throws PsyUndefinedResultException + { + return ((PsyIntegral)psyNumerator().psyMul(oRational.psyDenominator())) + .psyIdiv((PsyIntegral)oRational.psyNumerator().psyMul(psyDenominator())); + } + + /** + * Returns a {@code rational} representing the greatest common divisor of this object and given + * object. + * + * @param oRational the given object. + * @return the greatest common divisor. + */ + default public PsyRational psyGCD(final PsyRational oRational) + { + return of( + (PsyIntegral)((PsyIntegral)psyNumerator().psyMul(oRational.psyDenominator())) + .psyGCD((PsyIntegral)psyDenominator().psyMul(oRational.psyNumerator())), + (PsyIntegral)psyDenominator().psyMul(oRational.psyDenominator())); } @Override @@ -132,7 +173,7 @@ default public PsyRealNumeric psyDiv(final PsyRealNumeric oRealNumeric) @Override default public PsyRational psyAbs() { - if(psyCmp(PsyInteger.ZERO).equals(PsyInteger.MINUS_ONE)) + if(compareTo(PsyInteger.ZERO)<0) return of(psyNumerator().psyNeg(), psyDenominator()); return this; } @@ -177,6 +218,34 @@ public static PsyRational parseLiteral(final String image) PsyIntegral.parseLiteral(image.substring(slashIndex))); } + /** + * Context action of the {@code denominator} operator. + */ + @OperatorType("denominator") + public static final ContextAction PSY_DENOMINATOR + =ContextAction.ofFunction(PsyRational::psyDenominator); + + /** + * Context action of the {@code gcd} operator. + */ + @OperatorType("gcd") + public static final ContextAction PSY_GCD + =ContextAction.ofBiFunction(PsyRational::psyGCD); + + /** + * Context action of the {@code idiv} operator. + */ + @OperatorType("idiv") + public static final ContextAction PSY_IDIV + =ContextAction.ofBiFunction(PsyRational::psyIdiv); + + /** + * Context action of the {@code mod} operator. + */ + @OperatorType("mod") + public static final ContextAction PSY_MOD + =ContextAction.ofBiFunction(PsyRational::psyMod); + /** * Context action of the {@code numerator} operator. */ @@ -184,10 +253,4 @@ public static PsyRational parseLiteral(final String image) public static final ContextAction PSY_NUMERATOR =ContextAction.ofFunction(PsyRational::psyNumerator); - /** - * Context action of the {@code denominator} operator. - */ - @OperatorType("denominator") - public static final ContextAction PSY_DENOMINATOR - =ContextAction.ofFunction(PsyRational::psyDenominator); } diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyReader.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyReader.java index eee6f5aae..45f6cd9cd 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyReader.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyReader.java @@ -46,6 +46,9 @@ public void psyEval(final PsyContext oContext) oContext.interpret(this); } + /** + * {@return the {@link Reader} object backed by this object} + */ public Reader reader() { return reader; diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyReal.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyReal.java index f1b0e7c8f..277fb9520 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyReal.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyReal.java @@ -31,9 +31,9 @@ public PsyIntegral psyToIntegral() } @Override - public PsyBoolean psyIsZero() + public boolean isZero() { - return PsyBoolean.of(value==0.D); + return value==0.D; } @Override @@ -80,10 +80,10 @@ public PsyRational rationalValue() if((bits&0x8000000000000000L)!=0L) retval=retval.psyNeg(); return (PsyRational)(pow>=0? - retval.psyMul((pow<63)? + retval.psyMul((1+powDouble.POSITIVE_INFINITY; + case "-∞"->Double.NEGATIVE_INFINITY; + default->Double.parseDouble(image); + }); } /** diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyRealNumeric.java b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyRealNumeric.java index 50c0342c9..25e6c468b 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyRealNumeric.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/core/PsyRealNumeric.java @@ -67,40 +67,46 @@ default public PsyReal psyToReal() @Override public PsyRealNumeric psyNeg(); - public PsyRealNumeric psyAdd(final PsyRealNumeric oNumber); + public PsyRealNumeric psyAdd(final PsyRealNumeric oRealNumeric); @Override - default public PsyNumeric psyAdd(final PsyNumeric oNumber) + default public PsyNumeric psyAdd(final PsyNumeric oNumeric) { - if(oNumber instanceof PsyRealNumeric oRealNumeric) - return psyAdd(oRealNumeric); - if(oNumber instanceof PsyComplex oComplex) - return new PsyComplex(this).psyAdd(oComplex); - throw new ClassCastException(); + return switch(oNumeric) + { + case PsyRealNumeric oRealNumeric-> + psyAdd(oRealNumeric); + case PsyComplex oComplex-> + new PsyComplex(this).psyAdd(oComplex); + }; } - public PsyRealNumeric psySub(final PsyRealNumeric oNumber); + public PsyRealNumeric psySub(final PsyRealNumeric oRealNumeric); @Override - default public PsyNumeric psySub(final PsyNumeric oNumber) + default public PsyNumeric psySub(final PsyNumeric oNumeric) { - if(oNumber instanceof PsyRealNumeric oRealNumeric) - return psySub(oRealNumeric); - if(oNumber instanceof PsyComplex oComplex) - return new PsyComplex(this).psySub(oComplex); - throw new ClassCastException(); + return switch(oNumeric) + { + case PsyRealNumeric oRealNumeric-> + psySub(oRealNumeric); + case PsyComplex oComplex-> + new PsyComplex(this).psySub(oComplex); + }; } - public PsyRealNumeric psyMul(final PsyRealNumeric oNumber); + public PsyRealNumeric psyMul(final PsyRealNumeric oRealNumeric); @Override - default public PsyNumeric psyMul(final PsyNumeric oNumber) + default public PsyNumeric psyMul(final PsyNumeric oNumeric) { - if(oNumber instanceof PsyRealNumeric oRealNumeric) - return psyMul(oRealNumeric); - if(oNumber instanceof PsyComplex oComplex) - return new PsyComplex(this).psyMul(oComplex); - throw new ClassCastException(); + return switch(oNumeric) + { + case PsyRealNumeric oRealNumeric-> + psyMul(oRealNumeric); + case PsyComplex oComplex-> + new PsyComplex(this).psyMul(oComplex); + }; } public PsyRealNumeric psyDiv(final PsyRealNumeric oRealNumeric) @@ -110,11 +116,13 @@ public PsyRealNumeric psyDiv(final PsyRealNumeric oRealNumeric) default public PsyNumeric psyDiv(final PsyNumeric oNumeric) throws PsyUndefinedResultException { - if(oNumeric instanceof PsyRealNumeric oRealNumeric) - return psyDiv(oRealNumeric); - if(oNumeric instanceof PsyComplex oComplex) - return new PsyComplex(this).psyDiv(oComplex); - throw new ClassCastException(); + return switch(oNumeric) + { + case PsyRealNumeric oRealNumeric-> + psyDiv(oRealNumeric); + case PsyComplex oComplex-> + new PsyComplex(this).psyDiv(oComplex); + }; } default public PsyReal psyPow(final PsyRealNumeric oNumber) @@ -123,14 +131,16 @@ default public PsyReal psyPow(final PsyRealNumeric oNumber) } @Override - default public PsyNumeric psyPow(final PsyNumeric oNumber) + default public PsyNumeric psyPow(final PsyNumeric oNumeric) { - if(oNumber instanceof PsyRealNumeric oRealNumeric) - return psyPow(oRealNumeric); - if(oNumber instanceof PsyComplex oComplex) - // TODO - return new PsyComplex(this).psyPow(oComplex); - throw new ClassCastException(); + return switch(oNumeric) + { + case PsyRealNumeric oRealNumeric-> + psyPow(oRealNumeric); + case PsyComplex oComplex-> + // TODO + new PsyComplex(this).psyPow(oComplex); + }; } @Override diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/runtime/ClassLoader.java b/src/main/java/coneforest.psylla/coneforest/psylla/runtime/ClassLoader.java index b58859725..d1f870c07 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/runtime/ClassLoader.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/runtime/ClassLoader.java @@ -2,7 +2,7 @@ import coneforest.psylla.core.*; import java.io.IOException; -import java.net.URL; +import java.net.URI; import java.nio.file.Files; import java.nio.file.Path; import java.util.Hashtable; @@ -84,7 +84,7 @@ protected java.net.URL findResource(final String name) final var entry=jar.getJarEntry(name); if(entry==null) return null; - return new java.net.URL("jar:"+itemPath.toUri().toURL()+"!/"+entry); + return new URI("jar:"+itemPath.toUri().toURL()+"!/"+entry).toURL(); } else if(Files.isDirectory(itemPath)) { diff --git a/src/main/java/coneforest.psylla/coneforest/psylla/runtime/parser/package-info.java b/src/main/java/coneforest.psylla/coneforest/psylla/runtime/parser/package-info.java index 8e2480aa8..625b6e655 100644 --- a/src/main/java/coneforest.psylla/coneforest/psylla/runtime/parser/package-info.java +++ b/src/main/java/coneforest.psylla/coneforest/psylla/runtime/parser/package-info.java @@ -1,2 +1,4 @@ - +/** +* The Psylla language parser. +*/ package coneforest.psylla.runtime.parser; diff --git a/src/obs/debian/control b/src/obs/debian/control index 1dc7352c8..28718aa59 100644 --- a/src/obs/debian/control +++ b/src/obs/debian/control @@ -4,8 +4,8 @@ Priority: optional Maintainer: @project.developer.name@ <@project.developer.email@> Build-Depends: debhelper (>=10), - openjdk-17-jdk-headless, - openjdk-17-doc, + openjdk-21-jdk-headless, + openjdk-21-doc, javacc, junit4 (>=4.11), ant-optional, @@ -29,7 +29,7 @@ Package: @obs.package.name@ Architecture: all Depends: debhelper (>=10), - openjdk-17-jre-headless, + openjdk-21-jre-headless, javacc, junit4 (>=4.11), ant-optional, @@ -49,6 +49,6 @@ Recommends: ant-doc, ant-optional, libclianthus-java-doc, - openjdk-17-doc, + openjdk-21-doc, Description: Psylla programming language (documentation) This package contains documentation for @obs.package.name@.