Implement expression computation by AST in StringCalc

This commit is contained in:
ChronosX88 2021-06-08 18:08:05 +03:00
parent 244099760e
commit d282ea8b8c
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
15 changed files with 243 additions and 476 deletions

1
StringCalc/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
out/

View File

@ -1,22 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
<entry name="!?*.aj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>

View File

@ -1,3 +0,0 @@
<component name="CopyrightManager">
<settings default="" />
</component>

View File

@ -1 +0,0 @@
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>

View File

@ -1,12 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" assert-keyword="true" jdk-15="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" default="true" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -2,7 +2,7 @@
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/UnnamedCalc.iml" filepath="$PROJECT_DIR$/UnnamedCalc.iml" />
<module fileurl="file://$PROJECT_DIR$/StringCalc.iml" filepath="$PROJECT_DIR$/StringCalc.iml" />
</modules>
</component>
</project>

View File

@ -1,3 +0,0 @@
<template>
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
</template>

6
StringCalc/.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>

View File

@ -1,240 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ChangeListManager">
<list default="true" id="1fea5f5f-b642-434d-a463-71cf57001c3e" name="Default" comment="" />
<ignored path="UnnamedCalc.iws" />
<ignored path=".idea/workspace.xml" />
<ignored path="$PROJECT_DIR$/out/" />
<option name="EXCLUDED_CONVERTED_TO_IGNORED" value="true" />
<list default="true" id="0d7153f8-0e49-471d-a8cf-5d96baa2b39c" name="Default Changelist" comment="">
<change afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/ASTNode.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/OperatorType.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/src/main/Token.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/compiler.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/copyright/profiles_settings.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/description.html" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/modules.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/project-template.xml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/UnnamedCalc.iml" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/UnnamedCalc/main/Main.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/Main.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="CreatePatchCommitExecutor">
<option name="PATCH_PATH" value="" />
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/.." />
</component>
<component name="FUSProjectUsageTrigger">
<session id="928510969">
<usages-collector id="statistics.lifecycle.project">
<counts>
<entry key="project.closed" value="3" />
<entry key="project.open.time.1" value="1" />
<entry key="project.open.time.7" value="1" />
<entry key="project.open.time.9" value="1" />
<entry key="project.opened" value="3" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.extensions.edit">
<counts>
<entry key="java" value="1415" />
</counts>
</usages-collector>
<usages-collector id="statistics.file.types.edit">
<counts>
<entry key="JAVA" value="1415" />
</counts>
</usages-collector>
</session>
</component>
<component name="FavoritesManager">
<favorites_list name="UnnamedCalc" />
</component>
<component name="FileEditorManager">
<leaf />
</component>
<component name="IdeDocumentHistory">
<option name="CHANGED_PATHS">
<list>
<option value="$PROJECT_DIR$/src/main/Main.java" />
</list>
</option>
</component>
<component name="ProjectFrameBounds">
<option name="y" value="28" />
<option name="width" value="1366" />
<option name="height" value="712" />
</component>
<component name="ProjectView">
<navigator proportions="" version="1">
<foldersAlwaysOnTop value="true" />
</navigator>
<panes />
<component name="ProjectId" id="1tfdluWvUlOkVNAQsDv18I2SiD9" />
<component name="ProjectLevelVcsManager" settingsEditedManually="true" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
</component>
<component name="RunDashboard">
<option name="ruleStates">
<list>
<RuleState>
<option name="name" value="ConfigurationTypeDashboardGroupingRule" />
</RuleState>
<RuleState>
<option name="name" value="StatusDashboardGroupingRule" />
</RuleState>
</list>
</option>
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
</component>
<component name="RunManager">
<configuration default="true" type="AndroidRunConfigurationType" factoryName="Android Application">
<module name="" />
<option name="DEPLOY" value="true" />
<option name="ARTIFACT_NAME" value="" />
<option name="PM_INSTALL_OPTIONS" value="" />
<option name="ACTIVITY_EXTRA_FLAGS" value="" />
<option name="MODE" value="default_activity" />
<option name="TARGET_SELECTION_MODE" value="SHOW_DIALOG" />
<option name="PREFERRED_AVD" value="" />
<option name="CLEAR_LOGCAT" value="false" />
<option name="SHOW_LOGCAT_AUTOMATICALLY" value="true" />
<option name="SKIP_NOOP_APK_INSTALLATIONS" value="true" />
<option name="FORCE_STOP_RUNNING_APP" value="true" />
<option name="DEBUGGER_TYPE" value="Java" />
<option name="USE_LAST_SELECTED_DEVICE" value="false" />
<option name="PREFERRED_AVD" value="" />
<Java />
<Profilers>
<option name="GAPID_DISABLE_PCS" value="false" />
</Profilers>
<option name="DEEP_LINK" value="" />
<option name="ACTIVITY_CLASS" value="" />
<method>
<option name="Make" enabled="true" />
<option name="Android.Gradle.BeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration default="true" type="AndroidTestRunConfigurationType" factoryName="Android Tests">
<module name="" />
<option name="TESTING_TYPE" value="0" />
<option name="INSTRUMENTATION_RUNNER_CLASS" value="" />
<option name="METHOD_NAME" value="" />
<option name="CLASS_NAME" value="" />
<option name="PACKAGE_NAME" value="" />
<option name="EXTRA_OPTIONS" value="" />
<option name="TARGET_SELECTION_MODE" value="SHOW_DIALOG" />
<option name="PREFERRED_AVD" value="" />
<option name="CLEAR_LOGCAT" value="false" />
<option name="SHOW_LOGCAT_AUTOMATICALLY" value="true" />
<option name="SKIP_NOOP_APK_INSTALLATIONS" value="true" />
<option name="FORCE_STOP_RUNNING_APP" value="true" />
<option name="DEBUGGER_TYPE" value="Java" />
<option name="USE_LAST_SELECTED_DEVICE" value="false" />
<option name="PREFERRED_AVD" value="" />
<Java />
<Profilers>
<option name="GAPID_DISABLE_PCS" value="false" />
</Profilers>
<method>
<option name="Make" enabled="true" />
<option name="Android.Gradle.BeforeRunTask" enabled="true" />
</method>
</configuration>
<configuration default="true" type="JUnitTestDiscovery" factoryName="JUnit Test Discovery" changeList="All">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<module name="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PACKAGE_NAME" />
<option name="MAIN_CLASS_NAME" />
<option name="METHOD_NAME" />
<option name="TEST_OBJECT" value="class" />
<option name="VM_PARAMETERS" />
<option name="PARAMETERS" />
<option name="WORKING_DIRECTORY" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="TEST_SEARCH_SCOPE">
<value defaultName="singleModule" />
</option>
<envs />
<patterns />
<method />
</configuration>
<configuration default="true" type="TestNGTestDiscovery" factoryName="TestNG Test Discovery" changeList="All">
<extension name="coverage" enabled="false" merge="false" sample_coverage="true" runner="idea" />
<module name="" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="SUITE_NAME" />
<option name="PACKAGE_NAME" />
<option name="MAIN_CLASS_NAME" />
<option name="METHOD_NAME" />
<option name="GROUP_NAME" />
<option name="TEST_OBJECT" value="CLASS" />
<option name="VM_PARAMETERS" />
<option name="PARAMETERS" />
<option name="WORKING_DIRECTORY" />
<option name="OUTPUT_DIRECTORY" />
<option name="ANNOTATION_TYPE" />
<option name="ENV_VARIABLES" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="TEST_SEARCH_SCOPE">
<value defaultName="singleModule" />
</option>
<option name="USE_DEFAULT_REPORTERS" value="false" />
<option name="PROPERTIES_FILE" />
<envs />
<properties />
<listeners />
<method />
</configuration>
<configuration name="Main" type="Application" factoryName="Application" temporary="true">
<configuration name="Main" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="main.Main" />
<module name="UnnamedCalc" />
<option name="WORKING_DIRECTORY" value="file://$PROJECT_DIR$" />
<RunnerSettings RunnerId="Run" />
<ConfigurationWrapper RunnerId="Run" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration default="true" type="JUnit" factoryName="JUnit">
<option name="VM_PARAMETERS" />
<option name="WORKING_DIRECTORY" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration default="true" type="KotlinStandaloneScriptRunConfigurationType" factoryName="Kotlin script">
<option name="filePath" />
<option name="vmParameters" />
<option name="alternativeJrePath" />
<option name="programParameters" />
<option name="passParentEnvs" value="true" />
<option name="workingDirectory" />
<option name="isAlternativeJrePathEnabled" value="false" />
<option name="VM_PARAMETERS" />
<option name="PROGRAM_PARAMETERS" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="MAIN_CLASS_NAME" />
<option name="WORKING_DIRECTORY" />
<module name="" />
<option name="filePath" />
<option name="vmParameters" />
<option name="alternativeJrePath" />
<option name="programParameters" />
<option name="passParentEnvs" value="true" />
<option name="workingDirectory" />
<option name="isAlternativeJrePathEnabled" value="false" />
<option name="VM_PARAMETERS" />
<option name="PROGRAM_PARAMETERS" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH" />
<option name="PASS_PARENT_ENVS" value="true" />
<option name="MAIN_CLASS_NAME" />
<option name="WORKING_DIRECTORY" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration default="true" type="TestNG" factoryName="TestNG">
<option name="VM_PARAMETERS" />
<option name="WORKING_DIRECTORY" />
<properties />
<listeners />
<module name="StringCalc" />
<extension name="coverage">
<pattern>
<option name="PATTERN" value="main.*" />
<option name="ENABLED" value="true" />
</pattern>
</extension>
<method v="2">
<option name="Make" enabled="true" />
</method>
@ -245,164 +55,15 @@
</list>
</recent_temporary>
</component>
<component name="SvnConfiguration">
<configuration />
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="1fea5f5f-b642-434d-a463-71cf57001c3e" name="Default" comment="" />
<created>1537276489642</created>
<changelist id="0d7153f8-0e49-471d-a8cf-5d96baa2b39c" name="Default Changelist" comment="" />
<created>1623164231258</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1537276489642</updated>
<updated>1623164231258</updated>
</task>
<servers />
</component>
<component name="ToolWindowManager">
<frame x="0" y="28" width="1366" height="712" extended-state="0" />
<layout>
<window_info content_ui="combo" id="Project" order="0" sideWeight="0.488" weight="0.25" />
<window_info id="Structure" order="1" weight="0.25" />
<window_info id="Capture Tool" order="2" />
<window_info id="UI Designer" order="3" />
<window_info id="Palette&#9;" order="4" />
<window_info id="Favorites" order="5" sideWeight="0.512" side_tool="true" weight="0.25" />
<window_info anchor="bottom" id="Message" order="0" />
<window_info anchor="bottom" id="Find" order="1" />
<window_info anchor="bottom" id="Run" order="2" weight="0.3281787" />
<window_info anchor="bottom" id="Debug" order="3" weight="0.39862543" />
<window_info anchor="bottom" id="Cvs" order="4" weight="0.25" />
<window_info anchor="bottom" id="Inspection" order="5" weight="0.4" />
<window_info anchor="bottom" id="TODO" order="6" />
<window_info anchor="bottom" id="Terminal" order="7" />
<window_info anchor="bottom" id="Event Log" order="8" side_tool="true" />
<window_info anchor="bottom" id="Version Control" order="9" show_stripe_button="false" />
<window_info anchor="bottom" id="Messages" order="10" weight="0.3281787" />
<window_info anchor="right" id="Commander" order="0" weight="0.4" />
<window_info anchor="right" id="Ant Build" order="1" weight="0.25" />
<window_info anchor="right" content_ui="combo" id="Hierarchy" order="2" weight="0.25" />
<window_info anchor="right" id="Palette" order="3" />
<window_info anchor="right" id="Capture Analysis" order="4" />
<window_info anchor="right" id="Designer" order="5" />
<window_info anchor="right" id="Maven Projects" order="6" />
</layout>
</component>
<component name="Vcs.Log.UiProperties">
<option name="RECENTLY_FILTERED_USER_GROUPS">
<collection />
</option>
<option name="RECENTLY_FILTERED_BRANCH_GROUPS">
<collection />
</option>
</component>
<component name="VcsContentAnnotationSettings">
<option name="myLimit" value="2678400000" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/Main.java</url>
<line>25</line>
<properties />
<option name="timeStamp" value="38" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/Main.java</url>
<line>33</line>
<properties />
<option name="timeStamp" value="39" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/Main.java</url>
<line>80</line>
<properties />
<option name="timeStamp" value="40" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/Main.java</url>
<line>53</line>
<properties />
<option name="timeStamp" value="42" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/Main.java</url>
<line>54</line>
<properties />
<option name="timeStamp" value="43" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/Main.java</url>
<line>55</line>
<properties />
<option name="timeStamp" value="44" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/Main.java</url>
<line>57</line>
<properties />
<option name="timeStamp" value="45" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/Main.java</url>
<line>58</line>
<properties />
<option name="timeStamp" value="46" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/Main.java</url>
<line>59</line>
<properties />
<option name="timeStamp" value="49" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/Main.java</url>
<line>63</line>
<properties />
<option name="timeStamp" value="51" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/Main.java</url>
<line>67</line>
<properties />
<option name="timeStamp" value="53" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/Main.java</url>
<line>71</line>
<properties />
<option name="timeStamp" value="56" />
</line-breakpoint>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/Main.java</url>
<line>76</line>
<properties />
<option name="timeStamp" value="58" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
<component name="antWorkspaceConfiguration">
<option name="IS_AUTOSCROLL_TO_SOURCE" value="false" />
<option name="FILTER_TARGETS" value="false" />
</component>
<component name="editorHistoryManager">
<entry file="jar:///usr/lib/jvm/java-8-oracle/src.zip!/java/lang/String.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="-1386">
<caret line="110" column="19" selection-start-line="110" selection-start-column="19" selection-end-line="110" selection-end-column="19" />
</state>
</provider>
</entry>
<entry file="file://$PROJECT_DIR$/src/main/Main.java">
<provider selected="true" editor-type-id="text-editor">
<state relative-caret-position="286">
<caret line="77" column="26" selection-start-line="77" selection-start-column="26" selection-end-line="77" selection-end-column="26" />
<folding>
<element signature="imports" expanded="true" />
</folding>
</state>
</provider>
</entry>
</component>
</project>

View File

@ -8,5 +8,4 @@
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
</module>

View File

@ -0,0 +1,25 @@
package main;
class ASTNode {
ASTNode left;
ASTNode right;
Token token;
public ASTNode(Token token, ASTNode left, ASTNode right) {
this.left = left;
this.right = right;
this.token = token;
}
public ASTNode(Token token) {
this(token, null, null);
}
@Override
public String toString() {
if (right == null && left == null) {
return String.format("ASTNode{token=%s}", token);
}
return String.format("ASTNode{token=%s,left=%s,right=%s}", token, left, right);
}
}

View File

@ -1,88 +1,153 @@
package main;
import java.util.Scanner;
import java.util.Stack;
import java.util.stream.IntStream;
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String line;
Stack<String> operators = new Stack<>();
Stack<Float> numbers = new Stack<>();
line = scanner.nextLine();
char[] lineArray = new char[line.length()];
line = line.replace(" ", "");
line = line.replace("+-", "-");
line = line.replace("--", "+");
char[] lineArray;
lineArray = line.toCharArray();
System.out.println(calcExpression(lineArray, operators, numbers));
List<Token> tokens = tokenize(lineArray);
System.out.println("[\n"+tokens.stream()
.map(item -> "\t"+item)
.collect(Collectors.joining(",\n"))+"\n]");
ASTNode node = parse(tokens);
System.out.println(node.toString());
int result = evaluateAST(node);
System.out.println(result);
}
static float calcExpression(char[] charArr, Stack<String> operators, Stack<Float> numbers) {
for(int i = 0; i < charArr.length; i++) {
static List<Token> tokenize(char[] charArr) {
List<Token> tokens = new ArrayList<>();
StringBuilder tmpNumStr = new StringBuilder();
boolean isLastCharOperator = false;
for (int i = 0; i < charArr.length; i++) {
char c = charArr[i];
Token token = new Token(TokenType.Unknown, null);
if (Character.isDigit(c) || c == '-' && isLastCharOperator) {
isLastCharOperator = false;
tmpNumStr.append(c);
// if next char is also digit, then, instead of creating token, parse next digit
// if it's the end of array, then just create token and finish
if (i+1 != charArr.length) {
if(Character.isDigit(charArr[i+1])) {
continue;
}
}
token = new Token(TokenType.Number, Integer.parseInt(tmpNumStr.toString()));
tmpNumStr = new StringBuilder();
} else {
switch (c) {
case '(': {
token = new Token(TokenType.Operator, OperatorType.LeftParenthesis);
break;
}
case '+': {
token = new Token(TokenType.Operator, OperatorType.Add);
break;
}
case '-': {
token = new Token(TokenType.Operator, OperatorType.Sub);
break;
}
case '*': {
token = new Token(TokenType.Operator, OperatorType.Mul);
break;
}
case '/': {
token = new Token(TokenType.Operator, OperatorType.Div);
break;
}
case ')': {
token = new Token(TokenType.Operator, OperatorType.RightParenthesis);
break;
}
}
isLastCharOperator = true;
}
tokens.add(token);
}
String operatorTemp;
float numberTemp;
return tokens;
}
if (charArr == null)
throw new IllegalArgumentException("Num не должен быть null");
static ASTNode parse(List<Token> tokens) {
Stack<ASTNode> output = new Stack<>();
Stack<Token> ops = new Stack<>();
for (Token token : tokens) {
// https://en.wikipedia.org/wiki/Shunting-yard_algorithm
switch(charArr[i]) {
case '(': {
break;
}
case '+': {
operators.push("+");
break;
}
case '-': {
operators.push("-");
break;
}
case '*': {
operators.push("*");
break;
}
case '/': {
operators.push("/");
break;
}
case ')': {
operatorTemp = operators.pop();
numberTemp = numbers.pop();
if (token.type == TokenType.Number) {
output.add(new ASTNode(token));
continue;
}
switch(operatorTemp) {
case "+": {
numberTemp = numbers.pop() + numberTemp;
break;
}
case "-": {
numberTemp = numbers.pop() - numberTemp;
break;
}
case "*": {
numberTemp = numbers.pop() * numberTemp;
break;
}
case "/": {
numberTemp = numbers.pop() / numberTemp;
if (token.type == TokenType.Operator) {
if (token.value == OperatorType.LeftParenthesis) {
ops.push(token);
continue;
}
while (ops.size() != 0) {
if (ops.peek().value == OperatorType.LeftParenthesis) {
break;
}
if (token.value != OperatorType.RightParenthesis) {
if (OperatorType.getPrecedence((OperatorType) ops.peek().value) < OperatorType.getPrecedence((OperatorType) token.value)) {
break;
}
}
numbers.push(numberTemp);
break;
Token op = ops.pop();
ASTNode right = output.pop();
ASTNode left = output.pop();
output.push(new ASTNode(op, left, right));
}
default: {
numbers.push(Float.parseFloat(String.valueOf(charArr[i])));
if (token.value == OperatorType.RightParenthesis) {
ops.pop();
} else {
ops.push(token);
}
}
}
return numbers.pop();
while(ops.size() != 0) {
Token op = ops.pop();
ASTNode right = output.pop();
ASTNode left = output.pop();
output.push(new ASTNode(op, left, right));
}
return output.pop();
}
static int evaluateAST(ASTNode node) {
int val = 0;
if (node.token.type == TokenType.Operator) {
switch ((OperatorType) node.token.value) {
case Add:
val = evaluateAST(node.left) + evaluateAST(node.right);
break;
case Sub:
val = evaluateAST(node.left) - evaluateAST(node.right);
break;
case Mul:
val = evaluateAST(node.left) * evaluateAST(node.right);
break;
case Div:
val = evaluateAST(node.left) / evaluateAST(node.right);
break;
}
} else {
val = (int) node.token.value;
}
return val;
}
}

View File

@ -0,0 +1,23 @@
package main;
public enum OperatorType {
Add,
Sub,
Mul,
Div,
LeftParenthesis,
RightParenthesis;
public static int getPrecedence(OperatorType type) {
switch (type) {
case Add:
case Sub:
return 0;
case Mul:
case Div:
return 1;
default:
return -1;
}
}
}

View File

@ -0,0 +1,22 @@
package main;
enum TokenType {
Number,
Operator,
Unknown
}
class Token {
TokenType type;
Object value;
public Token(TokenType type, Object value) {
this.type = type;
this.value = value;
}
@Override
public String toString() {
return "Token{type="+type+", value="+value+"}";
}
}