Skip to content

Commit

Permalink
Merge pull request #10 from IMAGINARY/feature/keep-parentheses-in-syn…
Browse files Browse the repository at this point in the history
…tax-tree

Feature/keep parentheses in syntax tree
  • Loading branch information
porst17 authored Nov 25, 2017
2 parents d86019d + 8945375 commit c5075af
Show file tree
Hide file tree
Showing 33 changed files with 598 additions and 2,817 deletions.
13 changes: 11 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
group = 'de.mfo.jsurf'
version = '0.3.0'
version = '0.4.0-SNAPSHOT'

apply plugin: 'application'
apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'maven-publish'
apply plugin: 'antlr'

targetCompatibility = 1.7
sourceCompatibility = 1.7
Expand All @@ -13,7 +14,15 @@ repositories {
mavenCentral()
}

// workaround for excluding certain ANTLR dependencies
configurations {
compile {
extendsFrom = extendsFrom.findAll { it != configurations.antlr }
}
}

dependencies {
antlr group: "org.antlr", name: "antlr", version: "3.4"
compile group: 'javax.vecmath', name: 'vecmath', version: '1.5.2'
compile 'org.antlr:antlr-runtime:3.4@jar'
compile 'commons-cli:commons-cli:1.2'
Expand All @@ -22,7 +31,7 @@ dependencies {
}

task wrapper(type: Wrapper) {
gradleVersion = '1.12'
gradleVersion = '2.12'
}

task sourceJar(type: Jar) {
Expand Down
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Thu Jul 03 15:55:29 CEST 2014
#Thu Nov 16 22:39:30 CET 2017
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-1.12-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-bin.zip
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ tokens {
DIV = '/' ;
POW = '^' ;
LPAR = '(' ;
RPAR = ')' ;
RPAR = ')' ;
PARENTHESES;
}

@header
Expand Down Expand Up @@ -123,16 +124,16 @@ pow_expr

unary_expr
: primary_expr
| IDENTIFIER^ LPAR! add_expr RPAR!
| IDENTIFIER LPAR add_expr RPAR -> ^(IDENTIFIER PARENTHESES add_expr)
;

primary_expr
: DECIMAL_LITERAL
| FLOATING_POINT_LITERAL
| IDENTIFIER
| LPAR! add_expr RPAR!
| LPAR add_expr RPAR -> PARENTHESES add_expr
;


/*------------------------------------------------------------------
* LEXER RULES
*------------------------------------------------------------------*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ import de.mfo.jsurf.algebra.*;

@members
{
public static PolynomialOperation createVariable( String name )
public static PolynomialOperation createVariable( String name, boolean hasParentheses )
{
try
{
return new PolynomialVariable( PolynomialVariable.Var.valueOf( name ) );
return new PolynomialVariable( PolynomialVariable.Var.valueOf( name ), hasParentheses );
}
catch( Exception e )
{
return new DoubleVariable( name );
return new DoubleVariable( name, hasParentheses );
}
}

Expand All @@ -50,96 +50,84 @@ import de.mfo.jsurf.algebra.*;
return 0;
}
}

public static double createDouble( String text )
{
try
{
return Double.parseDouble( text );
}
catch( NumberFormatException nfe )
{
return Double.NaN;
}
}
}

start returns [ PolynomialOperation op ]
: e = expr { $op = $e.op; }
;

expr returns [ PolynomialOperation op, Integer decimal ]
: ^( PLUS e1 = expr e2 = expr )
: ( p = PARENTHESES )? ^( PLUS e1 = expr e2 = expr )
{
try
{
$op = new DoubleBinaryOperation( DoubleBinaryOperation.Op.add, ( DoubleOperation ) $e1.op, ( DoubleOperation ) $e2.op );
$op = new DoubleBinaryOperation( DoubleBinaryOperation.Op.add, ( DoubleOperation ) $e1.op, ( DoubleOperation ) $e2.op, p != null );
}
catch( ClassCastException cce )
{
$op = new PolynomialAddition( $e1.op, $e2.op );
$op = new PolynomialAddition( $e1.op, $e2.op, p != null );
}
}
| ^( MINUS e1 = expr ( e2 = expr )? )
| ( p = PARENTHESES )? ^( MINUS e1 = expr ( e2 = expr )? )
{
if( e2 != null )
{
// subtraction
try
{
$op = new DoubleBinaryOperation( DoubleBinaryOperation.Op.sub, ( DoubleOperation ) $e1.op, ( DoubleOperation ) $e2.op );
$op = new DoubleBinaryOperation( DoubleBinaryOperation.Op.sub, ( DoubleOperation ) $e1.op, ( DoubleOperation ) $e2.op, p != null );
}
catch( ClassCastException cce )
{
$op = new PolynomialSubtraction( $e1.op, $e2.op );
$op = new PolynomialSubtraction( $e1.op, $e2.op, p != null );
}
}
else
{
try
{
$op = new DoubleUnaryOperation( DoubleUnaryOperation.Op.neg, ( DoubleOperation ) $e1.op );
$op = new DoubleUnaryOperation( DoubleUnaryOperation.Op.neg, ( DoubleOperation ) $e1.op, p != null );
}
catch( ClassCastException cce )
{
$op = new PolynomialNegation( $e1.op );
$op = new PolynomialNegation( $e1.op, p != null );
}
}
}
| ^( MULT e1 = expr e2 = expr )
| ( p = PARENTHESES )? ^( MULT e1 = expr e2 = expr )
{
try
{
$op = new DoubleBinaryOperation( DoubleBinaryOperation.Op.mult, ( DoubleOperation ) $e1.op, ( DoubleOperation ) $e2.op );
}
catch( ClassCastException cce )
{
$op = new PolynomialMultiplication( $e1.op, $e2.op );
$op = new PolynomialMultiplication( $e1.op, $e2.op, p != null );
}
}
| ^( DIV e1 = expr e2 = expr )
| ( p = PARENTHESES )? ^( DIV e1 = expr e2 = expr )
{
try
{
$op = new DoubleBinaryOperation( DoubleBinaryOperation.Op.div, ( DoubleOperation ) $e1.op, ( DoubleOperation ) $e2.op );
$op = new DoubleBinaryOperation( DoubleBinaryOperation.Op.div, ( DoubleOperation ) $e1.op, ( DoubleOperation ) $e2.op, p != null );
}
catch( ClassCastException cce1 )
{
try
{
$op = new PolynomialDoubleDivision( $e1.op, ( DoubleOperation ) $e2.op );
$op = new PolynomialDoubleDivision( $e1.op, ( DoubleOperation ) $e2.op, p != null );
}
catch( ClassCastException cce2 )
{
throw new RecognitionException();
}
}
}
| ^( POW e1 = expr e2 = expr )
| ( p = PARENTHESES )? ^( POW e1 = expr e2 = expr )
{
try
{
$op = new DoubleBinaryOperation( DoubleBinaryOperation.Op.pow, ( DoubleOperation ) $e1.op, ( DoubleOperation ) $e2.op );
$op = new DoubleBinaryOperation( DoubleBinaryOperation.Op.pow, ( DoubleOperation ) $e1.op, ( DoubleOperation ) $e2.op, p != null );
}
catch( ClassCastException cce )
{
Expand All @@ -149,17 +137,17 @@ expr returns [ PolynomialOperation op, Integer decimal ]
}
else
{
$op = new PolynomialPower( $e1.op, $e2.decimal );
$op = new PolynomialPower( $e1.op, $e2.decimal, p != null );
}
}
}
| ^( id = IDENTIFIER e1 = expr ( e2 = expr )? )
| ( p = PARENTHESES )? ^( id = IDENTIFIER e1 = expr ( e2 = expr )? )
{
if( e2 != null )
{
try
{
$op = new DoubleBinaryOperation( DoubleBinaryOperation.Op.valueOf( $id.text ), ( DoubleOperation ) $e1.op, ( DoubleOperation ) $e2.op );
$op = new DoubleBinaryOperation( DoubleBinaryOperation.Op.valueOf( $id.text ), ( DoubleOperation ) $e1.op, ( DoubleOperation ) $e2.op, p != null );
}
catch( ClassCastException cce )
{
Expand All @@ -174,7 +162,7 @@ expr returns [ PolynomialOperation op, Integer decimal ]
{
try
{
$op = new DoubleUnaryOperation( DoubleUnaryOperation.Op.valueOf( $id.text ), ( DoubleOperation ) $e1.op );
$op = new DoubleUnaryOperation( DoubleUnaryOperation.Op.valueOf( $id.text ), ( DoubleOperation ) $e1.op, p != null );
}
catch( ClassCastException cce )
{
Expand All @@ -185,13 +173,12 @@ expr returns [ PolynomialOperation op, Integer decimal ]
throw new RecognitionException();
}
}

}
| pe = primary_expr { $op = $pe.op; $decimal = pe.decimal; }
;

primary_expr returns [ PolynomialOperation op, Integer decimal ]
: i = DECIMAL_LITERAL { $op = new DoubleValue( createDouble( $i.text ) ); $decimal = Integer.valueOf( createInteger( $i.text ) ); }
| f = FLOATING_POINT_LITERAL { $op = new DoubleValue( createDouble( $f.text ) ); }
| id = IDENTIFIER { $op = createVariable( $id.text ); }
: ( p = PARENTHESES )? i = DECIMAL_LITERAL { $op = new DoubleValue( $i.text, p != null ); $decimal = Integer.valueOf( createInteger( $i.text ) ); }
| ( p = PARENTHESES )? f = FLOATING_POINT_LITERAL { $op = new DoubleValue( $f.text, p != null ); }
| ( p = PARENTHESES )? id = IDENTIFIER { $op = createVariable( $id.text, p != null ); }
;
22 changes: 11 additions & 11 deletions src/main/java/de/mfo/jsurf/algebra/CloneVisitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,32 @@ public class CloneVisitor extends AbstractVisitor< PolynomialOperation, Void > {

public PolynomialOperation visit( PolynomialAddition pa, Void param )
{
return new PolynomialAddition( pa.firstOperand.accept( this, ( Void ) null ), pa.secondOperand.accept( this, ( Void ) null ) );
return new PolynomialAddition( pa.getFirstOperand().accept( this, ( Void ) null ), pa.getSecondOperand().accept( this, ( Void ) null ) );
}

public PolynomialOperation visit( PolynomialSubtraction ps, Void param )
{
return new PolynomialSubtraction( ps.firstOperand.accept( this, ( Void ) null ), ps.secondOperand.accept( this, ( Void ) null ) );
return new PolynomialSubtraction( ps.getFirstOperand().accept( this, ( Void ) null ), ps.getSecondOperand().accept( this, ( Void ) null ) );
}

public PolynomialOperation visit( PolynomialMultiplication pm, Void param )
{
return new PolynomialMultiplication( pm.firstOperand.accept( this, ( Void ) null ), pm.secondOperand.accept( this, ( Void ) null ) );
return new PolynomialMultiplication( pm.getFirstOperand().accept( this, ( Void ) null ), pm.getSecondOperand().accept( this, ( Void ) null ) );
}

public PolynomialOperation visit( PolynomialPower pp, Void param )
{
return new PolynomialPower( pp.base.accept( this, ( Void ) null ), pp.exponent );
return new PolynomialPower( pp.getBase().accept( this, ( Void ) null ), pp.getExponent() );
}

public PolynomialOperation visit( PolynomialNegation pn, Void param )
{
return new PolynomialNegation( pn.operand.accept( this, ( Void ) null ) );
return new PolynomialNegation( pn.getOperand().accept( this, ( Void ) null ) );
}

public PolynomialOperation visit( PolynomialDoubleDivision pdd, Void param )
{
return new PolynomialDoubleDivision( pdd.dividend.accept( this, ( Void ) null ), ( DoubleOperation ) pdd.divisor.accept( this, ( Void ) null ) );
return new PolynomialDoubleDivision( pdd.getDividend().accept( this, ( Void ) null ), ( DoubleOperation ) pdd.getDivisor().accept( this, ( Void ) null ) );
}

public PolynomialOperation visit( PolynomialVariable pv, Void param )
Expand All @@ -55,14 +55,14 @@ public PolynomialOperation visit( PolynomialVariable pv, Void param )

public DoubleOperation visit( DoubleBinaryOperation dbop, Void param )
{
DoubleOperation firstOperand = ( DoubleOperation ) dbop.firstOperand.accept( this, ( Void ) null );
DoubleOperation secondOperand = ( DoubleOperation ) dbop.secondOperand.accept( this, ( Void ) null );
return new DoubleBinaryOperation( dbop.operator, firstOperand, secondOperand );
DoubleOperation firstOperand = ( DoubleOperation ) dbop.getFirstOperand().accept( this, ( Void ) null );
DoubleOperation secondOperand = ( DoubleOperation ) dbop.getSecondOperand().accept( this, ( Void ) null );
return new DoubleBinaryOperation( dbop.getOperator(), firstOperand, secondOperand );
}

public DoubleOperation visit( DoubleUnaryOperation duop, Void param )
{
return new DoubleUnaryOperation( duop.operator, ( DoubleOperation ) duop.operand.accept( this, ( Void ) null ) );
return new DoubleUnaryOperation( duop.getOperator(), ( DoubleOperation ) duop.getOperand().accept( this, ( Void ) null ) );
}

public DoubleOperation visit( DoubleValue dv, Void param )
Expand All @@ -72,6 +72,6 @@ public DoubleOperation visit( DoubleValue dv, Void param )

public DoubleOperation visit( DoubleVariable dv, Void param )
{
return new DoubleVariable( dv.name );
return new DoubleVariable( dv.getName() );
}
}
24 changes: 12 additions & 12 deletions src/main/java/de/mfo/jsurf/algebra/DegreeCalculator.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,44 +20,44 @@ public class DegreeCalculator extends AbstractVisitor< Integer, Void >
{
public Integer visit( PolynomialAddition pa, Void param )
{
return Math.max( pa.firstOperand.accept( this, ( Void ) null ), pa.secondOperand.accept( this, ( Void ) null ) );
return Math.max( pa.getFirstOperand().accept( this, ( Void ) null ), pa.getSecondOperand().accept( this, ( Void ) null ) );
}

public Integer visit( PolynomialSubtraction ps, Void param )
{
return Math.max( ps.firstOperand.accept( this, ( Void ) null ), ps.secondOperand.accept( this, ( Void ) null ) );
return Math.max( ps.getFirstOperand().accept( this, ( Void ) null ), ps.getSecondOperand().accept( this, ( Void ) null ) );
}

public Integer visit( PolynomialMultiplication pm, Void param )
{
return pm.firstOperand.accept( this, ( Void ) null ) + pm.secondOperand.accept( this, ( Void ) null );
return pm.getFirstOperand().accept( this, ( Void ) null ) + pm.getSecondOperand().accept( this, ( Void ) null );
}

public Integer visit( PolynomialPower pp, Void param )
{
return pp.exponent * pp.base.accept( this, ( Void ) null );
return pp.getExponent() * pp.getBase().accept( this, ( Void ) null );
}

public Integer visit( PolynomialNegation pn, Void param )
{
return pn.operand.accept( this, ( Void ) null );
return pn.getOperand().accept( this, ( Void ) null );
}

public Integer visit( PolynomialDoubleDivision pdd, Void param )
{
return pdd.dividend.accept( this, ( Void ) null );
return pdd.getDividend().accept( this, ( Void ) null );
}

public Integer visit( PolynomialVariable pv, Void param )
{
return 1;
}
}

public Integer visit( DoubleBinaryOperation dbop, Void param )
{
return 0;
}

public Integer visit( DoubleUnaryOperation duop, Void param )
{
return 0;
Expand Down
Loading

0 comments on commit c5075af

Please sign in to comment.