Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
Abgabe4Java
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Dominic Daniel Krämer
Abgabe4Java
Commits
93ae9bf7
Commit
93ae9bf7
authored
2 years ago
by
Dominic Daniel Krämer
Browse files
Options
Downloads
Patches
Plain Diff
commented and finished the ASTPrinter.java
parent
6b8d8d51
No related branches found
No related tags found
1 merge request
!14
Dominicsbranch
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/main/ASTPrinter.java
+44
-8
44 additions, 8 deletions
src/main/ASTPrinter.java
with
44 additions
and
8 deletions
src/main/ASTPrinter.java
+
44
−
8
View file @
93ae9bf7
package
main
;
/**
* <h1>The ASTPrinter</h1>
* The ASTPrinter class creates a String out of an AST(represented through an Expression object)
* It prints it in a form the Evaluator class can work with it, it isn't primarily designed for
* returning a String that is perfect for human readability (e.g. Pow(x,3) would be printed instead of x^3)
*
* @version 1.0
* @since 09.01.2023
*/
public
class
ASTPrinter
implements
Visitor
<
String
>{
/**
* Builds a String representation of a given BinaryOperation
* It gets a String representation of its left and right Expressions (which may result in a
* recursive call) and combines them into one String together with the operator
* @param binaryOP The BinaryOperation that should be printed into a String
* @return String Returns the String that was build
*/
@Override
public
String
visit
(
final
Parser
.
BinaryOperation
binaryOP
)
{
StringBuffer
sb
=
new
StringBuffer
();
//adds left bracket if the BinaryOperation is capsuled
if
(
binaryOP
.
capsuled
)
{
sb
.
append
(
"("
);
}
if
(
binaryOP
.
operator
.
contains
(
"^"
))
{
//Exponents will be printed in the form of Pow(basis, exponent) so the Evaluator can work with it
sb
.
append
(
"Pow("
);
sb
.
append
(
binaryOP
.
leftExpression
.
accept
(
this
));
sb
.
append
(
","
);
...
...
@@ -15,36 +34,53 @@ public class ASTPrinter implements Visitor<String>{
sb
.
append
(
")"
);
}
else
{
//Adds the String representations of the left expression, operator and right expression
sb
.
append
(
binaryOP
.
leftExpression
.
accept
(
this
));
sb
.
append
(
binaryOP
.
operator
);
sb
.
append
(
binaryOP
.
rightExpression
.
accept
(
this
));
}
//adds right bracket if the BinaryOperation is capsuled
if
(
binaryOP
.
capsuled
)
{
sb
.
append
(
")"
);
}
return
sb
.
toString
();
}
/**
* Returns the result of ast.accept(this), which will be the core values of a Value(digits),
* Variable(variableName) or BinaryOperation(will lead to a recursive call)
* @param ast The Expression
* @return ast.accept(this)
*/
public
String
visit
(
final
Parser
.
Expression
ast
)
{
if
(
ast
==
null
)
{
throw
new
RuntimeException
(
"AST is null"
);
}
return
ast
.
accept
(
this
);
}
/**
* @see #visit(Parser.Expression)
*/
@Override
public
String
visit
(
Parser
.
Variable
variable
)
{
return
variable
.
variableName
;
}
/**
* @see #visit(Parser.Expression)
*/
@Override
public
String
visit
(
Parser
.
Number
number
)
{
return
number
.
digits
;
}
/**
* @see #visit(Parser.Expression)
*/
@Override
public
String
visit
(
Parser
.
Decimal
decimal
)
{
return
decimal
.
beforeDot
.
digits
+
"."
+
decimal
.
afterDot
.
digits
;
}
public
String
visit
(
final
Parser
.
Expression
ast
)
{
if
(
ast
==
null
)
{
throw
new
RuntimeException
(
"AST is null"
);
}
return
ast
.
accept
(
this
);
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment