diff options
27 files changed, 245 insertions, 587 deletions
diff --git a/code/Interpreter/.idea/misc.xml b/code/Interpreter/.idea/misc.xml deleted file mode 100644 index 06e4882..0000000 --- a/code/Interpreter/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="false" project-jdk-name="openjdk-15 (2)" project-jdk-type="JavaSDK"> - <output url="file://$PROJECT_DIR$/out" /> - </component> -</project>
\ No newline at end of file diff --git a/code/Interpreter/.idea/modules.xml b/code/Interpreter/.idea/modules.xml deleted file mode 100644 index b0b7e77..0000000 --- a/code/Interpreter/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="ProjectModuleManager"> - <modules> - <module fileurl="file://$PROJECT_DIR$/Interpreter/Interpreter/Interpreter.iml" filepath="$PROJECT_DIR$/Interpreter/Interpreter/Interpreter.iml" /> - </modules> - </component> -</project>
\ No newline at end of file diff --git a/code/Interpreter/.idea/uiDesigner.xml b/code/Interpreter/.idea/uiDesigner.xml deleted file mode 100644 index e96534f..0000000 --- a/code/Interpreter/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="Palette2"> - <group name="Swing"> - <item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" /> - </item> - <item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" /> - </item> - <item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" /> - </item> - <item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true"> - <default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" /> - </item> - <item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" /> - <initial-values> - <property name="text" value="Button" /> - </initial-values> - </item> - <item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> - <initial-values> - <property name="text" value="RadioButton" /> - </initial-values> - </item> - <item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> - <initial-values> - <property name="text" value="CheckBox" /> - </initial-values> - </item> - <item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" /> - <initial-values> - <property name="text" value="Label" /> - </initial-values> - </item> - <item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> - <preferred-size width="150" height="-1" /> - </default-constraints> - </item> - <item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> - <preferred-size width="150" height="-1" /> - </default-constraints> - </item> - <item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> - <preferred-size width="150" height="-1" /> - </default-constraints> - </item> - <item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> - <preferred-size width="150" height="50" /> - </default-constraints> - </item> - <item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> - <preferred-size width="150" height="50" /> - </default-constraints> - </item> - <item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> - <preferred-size width="150" height="50" /> - </default-constraints> - </item> - <item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" /> - </item> - <item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> - <preferred-size width="150" height="50" /> - </default-constraints> - </item> - <item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3"> - <preferred-size width="150" height="50" /> - </default-constraints> - </item> - <item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> - <preferred-size width="150" height="50" /> - </default-constraints> - </item> - <item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> - <preferred-size width="200" height="200" /> - </default-constraints> - </item> - <item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> - <preferred-size width="200" height="200" /> - </default-constraints> - </item> - <item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> - </item> - <item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> - </item> - <item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" /> - </item> - <item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" /> - </item> - <item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1"> - <preferred-size width="-1" height="20" /> - </default-constraints> - </item> - <item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" /> - </item> - <item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" /> - </item> - </group> - </component> -</project>
\ No newline at end of file diff --git a/code/Interpreter/.idea/vcs.xml b/code/Interpreter/.idea/vcs.xml deleted file mode 100644 index b2bdec2..0000000 --- a/code/Interpreter/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="VcsDirectoryMappings"> - <mapping directory="$PROJECT_DIR$/../.." vcs="Git" /> - </component> -</project>
\ No newline at end of file diff --git a/code/Interpreter/.idea/workspace.xml b/code/Interpreter/.idea/workspace.xml deleted file mode 100644 index bd535b9..0000000 --- a/code/Interpreter/.idea/workspace.xml +++ /dev/null @@ -1,100 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="ChangeListManager"> - <list default="true" id="fdab160a-5534-4464-be5d-f72de2d17a3d" name="Default Changelist" comment="" /> - <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="Git.Settings"> - <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../.." /> - </component> - <component name="ProjectId" id="20PrQviXsmSOSsQMypbOCdQFiSy" /> - <component name="ProjectLevelVcsManager" settingsEditedManually="true" /> - <component name="ProjectViewState"> - <option name="hideEmptyMiddlePackages" value="true" /> - <option name="showLibraryContents" value="true" /> - </component> - <component name="PropertiesComponent"> - <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" /> - <property name="last_opened_file_path" value="$PROJECT_DIR$" /> - <property name="project.structure.last.edited" value="Project" /> - <property name="project.structure.proportion" value="0.15" /> - <property name="project.structure.side.proportion" value="0.2" /> - <property name="settings.editor.selected.configurable" value="project.propCompiler" /> - </component> - <component name="RecentsManager"> - <key name="MoveFile.RECENT_KEYS"> - <recent name="C:\Users\Alfie\Documents\Year 3\Advanced Programming\EsotericProject\code\Interpreter\Interpreter\Interpreter" /> - <recent name="C:\Users\Alfie\Documents\Year 3\Advanced Programming\EsotericProject\code\Interpreter\Interpreter" /> - </key> - </component> - <component name="RunManager"> - <configuration name="Language" type="Application" factoryName="Application" nameIsGenerated="true"> - <option name="MAIN_CLASS_NAME" value="Interpreter.Language" /> - <module name="Interpreter" /> - <option name="PROGRAM_PARAMETERS" value="example.txt" /> - <method v="2"> - <option name="Make" enabled="true" /> - </method> - </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="fdab160a-5534-4464-be5d-f72de2d17a3d" name="Default Changelist" comment="" /> - <created>1635957051799</created> - <option name="number" value="Default" /> - <option name="presentableId" value="Default" /> - <updated>1635957051799</updated> - </task> - <servers /> - </component> - <component name="WindowStateProjectService"> - <state x="731" y="140" width="1092" height="738" key="#com.intellij.execution.impl.EditConfigurationsDialog" timestamp="1635957953219"> - <screen x="0" y="0" width="1920" height="1050" /> - </state> - <state x="731" y="140" width="1092" height="738" key="#com.intellij.execution.impl.EditConfigurationsDialog/0.0.1920.1050/-1920.0.1920.1050@0.0.1920.1050" timestamp="1635957953219" /> - <state x="689" y="327" key="#com.intellij.ide.util.TreeClassChooserDialog" timestamp="1635957949823"> - <screen x="0" y="0" width="1920" height="1050" /> - </state> - <state x="689" y="327" key="#com.intellij.ide.util.TreeClassChooserDialog/0.0.1920.1050/-1920.0.1920.1050@0.0.1920.1050" timestamp="1635957949823" /> - <state width="1877" height="285" key="GridCell.Tab.0.bottom" timestamp="1635957910184"> - <screen x="0" y="0" width="1920" height="1050" /> - </state> - <state width="1877" height="285" key="GridCell.Tab.0.bottom/0.0.1920.1050/-1920.0.1920.1050@0.0.1920.1050" timestamp="1635957910184" /> - <state width="1877" height="285" key="GridCell.Tab.0.center" timestamp="1635957910184"> - <screen x="0" y="0" width="1920" height="1050" /> - </state> - <state width="1877" height="285" key="GridCell.Tab.0.center/0.0.1920.1050/-1920.0.1920.1050@0.0.1920.1050" timestamp="1635957910184" /> - <state width="1877" height="285" key="GridCell.Tab.0.left" timestamp="1635957910184"> - <screen x="0" y="0" width="1920" height="1050" /> - </state> - <state width="1877" height="285" key="GridCell.Tab.0.left/0.0.1920.1050/-1920.0.1920.1050@0.0.1920.1050" timestamp="1635957910184" /> - <state width="1877" height="285" key="GridCell.Tab.0.right" timestamp="1635957910184"> - <screen x="0" y="0" width="1920" height="1050" /> - </state> - <state width="1877" height="285" key="GridCell.Tab.0.right/0.0.1920.1050/-1920.0.1920.1050@0.0.1920.1050" timestamp="1635957910184" /> - <state width="1877" height="352" key="GridCell.Tab.1.bottom" timestamp="1635957910178"> - <screen x="0" y="0" width="1920" height="1050" /> - </state> - <state width="1877" height="352" key="GridCell.Tab.1.bottom/0.0.1920.1050/-1920.0.1920.1050@0.0.1920.1050" timestamp="1635957910178" /> - <state width="1877" height="352" key="GridCell.Tab.1.center" timestamp="1635957910178"> - <screen x="0" y="0" width="1920" height="1050" /> - </state> - <state width="1877" height="352" key="GridCell.Tab.1.center/0.0.1920.1050/-1920.0.1920.1050@0.0.1920.1050" timestamp="1635957910178" /> - <state width="1877" height="352" key="GridCell.Tab.1.left" timestamp="1635957910178"> - <screen x="0" y="0" width="1920" height="1050" /> - </state> - <state width="1877" height="352" key="GridCell.Tab.1.left/0.0.1920.1050/-1920.0.1920.1050@0.0.1920.1050" timestamp="1635957910178" /> - <state width="1877" height="352" key="GridCell.Tab.1.right" timestamp="1635957910178"> - <screen x="0" y="0" width="1920" height="1050" /> - </state> - <state width="1877" height="352" key="GridCell.Tab.1.right/0.0.1920.1050/-1920.0.1920.1050@0.0.1920.1050" timestamp="1635957910178" /> - <state x="880" y="106" key="SettingsEditor" timestamp="1635957806168"> - <screen x="0" y="0" width="1920" height="1050" /> - </state> - <state x="880" y="106" key="SettingsEditor/0.0.1920.1050/-1920.0.1920.1050@0.0.1920.1050" timestamp="1635957806168" /> - </component> -</project>
\ No newline at end of file diff --git a/code/Interpreter/Interpreter.java b/code/Interpreter/Interpreter.java deleted file mode 100644 index 65cdeb4..0000000 --- a/code/Interpreter/Interpreter.java +++ /dev/null @@ -1,131 +0,0 @@ -package Interpreter; - -import java.util.List; - -import Interpreter.Expression.*; -import Interpreter.Statement.ExpressionStatement; -import Interpreter.Statement.PrintStatement; -import Interpreter.Statement.VariableDeclaration; - -public class Interpreter { - - private Environment environment = new Environment(); - - void interpret(List<Statement> statements){ - try{ - for (Statement statement: statements){ - evaluateStatement(statement); - } - } catch (Error e){ - - } - } - - private Object evaluateStatement(Statement statement){ - switch(statement.getStatmentType()){ - case "exprStmt": - return evalExpressionStatement((ExpressionStatement)statement); - case "vardec": - return evalVariableDeclaration((VariableDeclaration)statement); - case "print": - return evalPrintStatement((PrintStatement)statement); - default: - return null; - } - } - private Object evalExpressionStatement(ExpressionStatement stmt){ - return evaluateExpression(stmt.expr); - } - - private Object evalVariableDeclaration(VariableDeclaration vardec){ - environment.defineVariable(vardec.name.text, null); - return null; - } - - private Object evalPrintStatement(PrintStatement print){ - System.out.println(evaluateExpression(print.expr)); - return null; - } - - private Object evaluateExpression(Expression expression){ - switch(expression.getExpressionType()){ - case "binary": - return evaluateBinaryExpression((Binary)expression); - case "literal": - return evaluateLiteralExpression((Literal)expression); - case "bracket": - return evaluateBracketedExpression((BracketedExpression)expression); - case "assign": - return evaluateAssignmentExpression((AssignmentExpression)expression); - case "var": - return evaluateVariableExpression((Variable)expression); - default: - return null; - } - } - - private Object evaluateBinaryExpression(Binary expr){ - Object leftEval = evaluateExpression(expr.left); - Object rightEval = evaluateExpression(expr.right); - switch (expr.op.type){ - case PLUS: - if (checkOperandsNum(leftEval, leftEval)){ - return (double)leftEval + (double)rightEval; - } - case STAR: - if (checkOperandsNum(leftEval, leftEval)){ - return (double)leftEval * (double)rightEval; - } - case MINUS: - if (checkOperandsNum(leftEval, leftEval)){ - return (double)leftEval - (double)rightEval; - } - case SLASH: - if (checkOperandsNum(leftEval, leftEval)){ - return (double)leftEval / (double)rightEval; - } - - case GREATER: - if (checkOperandsNum(leftEval, leftEval)){ - return (double)leftEval > (double)rightEval; - } - case LESS: - if (checkOperandsNum(leftEval, leftEval)){ - return (double)leftEval < (double)rightEval; - } - - case EQUALITY: - return leftEval.equals(rightEval); - default: - break; - } - return null; - } - - private Object evaluateLiteralExpression(Literal expr){ - return expr.value.value; - } - - private Object evaluateBracketedExpression(BracketedExpression expr){ - return evaluateExpression(expr.expr); - } - - private Object evaluateAssignmentExpression(AssignmentExpression expr){ - Object assignedValue = evaluateExpression(expr.value); - environment.assignVariable(expr.name.text, assignedValue); - return null; - } - - private Object evaluateVariableExpression(Variable expr){ - return environment.getVariable(expr.name.text); - } - - private boolean checkOperandsNum(Object left, Object right){ - if (left instanceof Double && right instanceof Double){ - return true; - } else { - Language.displayError("Operands must be numbers"); - throw new Error(); - } - } -} diff --git a/code/Interpreter/Interpreter/.idea/misc.xml b/code/Interpreter/Interpreter/.idea/misc.xml deleted file mode 100644 index 40674af..0000000 --- a/code/Interpreter/Interpreter/.idea/misc.xml +++ /dev/null @@ -1,6 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="ProjectRootManager" version="2" languageLevel="JDK_15" project-jdk-name="15" project-jdk-type="JavaSDK"> - <output url="file://$PROJECT_DIR$/out" /> - </component> -</project>
\ No newline at end of file diff --git a/code/Interpreter/Interpreter/.idea/modules.xml b/code/Interpreter/Interpreter/.idea/modules.xml deleted file mode 100644 index bbaf613..0000000 --- a/code/Interpreter/Interpreter/.idea/modules.xml +++ /dev/null @@ -1,8 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="ProjectModuleManager"> - <modules> - <module fileurl="file://$PROJECT_DIR$/Interpreter.iml" filepath="$PROJECT_DIR$/Interpreter.iml" /> - </modules> - </component> -</project>
\ No newline at end of file diff --git a/code/Interpreter/Interpreter/.idea/uiDesigner.xml b/code/Interpreter/Interpreter/.idea/uiDesigner.xml deleted file mode 100644 index e96534f..0000000 --- a/code/Interpreter/Interpreter/.idea/uiDesigner.xml +++ /dev/null @@ -1,124 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="Palette2"> - <group name="Swing"> - <item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" /> - </item> - <item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" /> - </item> - <item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" /> - </item> - <item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true"> - <default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" /> - </item> - <item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" /> - <initial-values> - <property name="text" value="Button" /> - </initial-values> - </item> - <item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> - <initial-values> - <property name="text" value="RadioButton" /> - </initial-values> - </item> - <item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" /> - <initial-values> - <property name="text" value="CheckBox" /> - </initial-values> - </item> - <item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" /> - <initial-values> - <property name="text" value="Label" /> - </initial-values> - </item> - <item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> - <preferred-size width="150" height="-1" /> - </default-constraints> - </item> - <item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> - <preferred-size width="150" height="-1" /> - </default-constraints> - </item> - <item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1"> - <preferred-size width="150" height="-1" /> - </default-constraints> - </item> - <item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> - <preferred-size width="150" height="50" /> - </default-constraints> - </item> - <item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> - <preferred-size width="150" height="50" /> - </default-constraints> - </item> - <item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> - <preferred-size width="150" height="50" /> - </default-constraints> - </item> - <item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" /> - </item> - <item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> - <preferred-size width="150" height="50" /> - </default-constraints> - </item> - <item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3"> - <preferred-size width="150" height="50" /> - </default-constraints> - </item> - <item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3"> - <preferred-size width="150" height="50" /> - </default-constraints> - </item> - <item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> - <preferred-size width="200" height="200" /> - </default-constraints> - </item> - <item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3"> - <preferred-size width="200" height="200" /> - </default-constraints> - </item> - <item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> - </item> - <item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" /> - </item> - <item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" /> - </item> - <item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" /> - </item> - <item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1"> - <preferred-size width="-1" height="20" /> - </default-constraints> - </item> - <item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false"> - <default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" /> - </item> - <item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false"> - <default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" /> - </item> - </group> - </component> -</project>
\ No newline at end of file diff --git a/code/Interpreter/Interpreter/.idea/vcs.xml b/code/Interpreter/Interpreter/.idea/vcs.xml deleted file mode 100644 index b2bdec2..0000000 --- a/code/Interpreter/Interpreter/.idea/vcs.xml +++ /dev/null @@ -1,6 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="VcsDirectoryMappings"> - <mapping directory="$PROJECT_DIR$/../.." vcs="Git" /> - </component> -</project>
\ No newline at end of file diff --git a/code/Interpreter/Interpreter/.idea/workspace.xml b/code/Interpreter/Interpreter/.idea/workspace.xml deleted file mode 100644 index c4f325d..0000000 --- a/code/Interpreter/Interpreter/.idea/workspace.xml +++ /dev/null @@ -1,34 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<project version="4"> - <component name="ChangeListManager"> - <list default="true" id="fdab160a-5534-4464-be5d-f72de2d17a3d" name="Default Changelist" comment="" /> - <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="Git.Settings"> - <option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$/../.." /> - </component> - <component name="ProjectId" id="20PrQviXsmSOSsQMypbOCdQFiSy" /> - <component name="ProjectLevelVcsManager" settingsEditedManually="true" /> - <component name="ProjectViewState"> - <option name="hideEmptyMiddlePackages" value="true" /> - <option name="showLibraryContents" value="true" /> - </component> - <component name="PropertiesComponent"> - <property name="RunOnceActivity.OpenProjectViewOnStart" value="true" /> - <property name="last_opened_file_path" value="$PROJECT_DIR$" /> - </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="fdab160a-5534-4464-be5d-f72de2d17a3d" name="Default Changelist" comment="" /> - <created>1635957051799</created> - <option name="number" value="Default" /> - <option name="presentableId" value="Default" /> - <updated>1635957051799</updated> - </task> - <servers /> - </component> -</project>
\ No newline at end of file diff --git a/code/Interpreter/Interpreter/Interpreter/Interpreter.iml b/code/Interpreter/Interpreter/Interpreter/Interpreter.iml deleted file mode 100644 index b107a2d..0000000 --- a/code/Interpreter/Interpreter/Interpreter/Interpreter.iml +++ /dev/null @@ -1,11 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<module type="JAVA_MODULE" version="4"> - <component name="NewModuleRootManager" inherit-compiler-output="true"> - <exclude-output /> - <content url="file://$MODULE_DIR$"> - <sourceFolder url="file://$MODULE_DIR$" isTestSource="false" /> - </content> - <orderEntry type="inheritedJdk" /> - <orderEntry type="sourceFolder" forTests="false" /> - </component> -</module>
\ No newline at end of file diff --git a/code/example.txt b/code/example.txt deleted file mode 100644 index cf6adc8..0000000 --- a/code/example.txt +++ /dev/null @@ -1,12 +0,0 @@ -var :: a -a=5 -a=a+1 -print a - -a=7 -a=a*2 -print a - -var :: b -b = 10 -print a+b
\ No newline at end of file diff --git a/src/.vscode/launch.json b/src/.vscode/launch.json new file mode 100644 index 0000000..a4b8347 --- /dev/null +++ b/src/.vscode/launch.json @@ -0,0 +1,22 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "java", + "name": "Launch Current File", + "request": "launch", + "mainClass": "${file}" + }, + { + "type": "java", + "name": "Launch Language", + "request": "launch", + "mainClass": "Compiler.Language", + "projectName": "src_1da2a030", + "args":"example.txt" + } + ] +}
\ No newline at end of file diff --git a/code/Interpreter/Environment.java b/src/Compiler/Environment.java index d191bde..0e682a4 100644 --- a/code/Interpreter/Environment.java +++ b/src/Compiler/Environment.java @@ -1,5 +1,4 @@ -package Interpreter; - +package Compiler; import java.util.HashMap; import java.util.Map; diff --git a/src/Compiler/ExecuteC.java b/src/Compiler/ExecuteC.java new file mode 100644 index 0000000..5e32eea --- /dev/null +++ b/src/Compiler/ExecuteC.java @@ -0,0 +1,76 @@ +package Compiler; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.List; + + +public class ExecuteC { + public void compileAndExecuteC(List<String> code){ + writeProgram(code); + if (!compileC()){ + String output = runProgram(); + System.out.println(output); + } + else{ + Language.displayError("Runtime Error"); + } + } + + public void writeProgram(List<String> codeLines){ + BufferedWriter output = null; + try { + File file = new File("main.c"); + output = new BufferedWriter(new FileWriter(file)); + for(String line:codeLines){ + output.write(line+"\n"); + } + output.close(); + } catch ( IOException e ) { + e.printStackTrace(); + } + + } + + public Boolean compileC(){ + try{ + String s= null; + Process p = Runtime.getRuntime().exec("cmd /C gcc main.c -o main.exe"); + BufferedReader stdError = new BufferedReader(new + InputStreamReader(p.getErrorStream())); + boolean error=false; + while ((s = stdError.readLine()) != null) { + error=true; + } + return error; + } catch (IOException e){ + e.printStackTrace(); + } + return false; + } + + public String runProgram(){ + + try{ + String[] command = {"cmd", "/C", "main.exe"}; + ProcessBuilder probuilder = new ProcessBuilder(command); + Process p = probuilder.start(); + BufferedReader stdInput = new BufferedReader(new + InputStreamReader(p.getInputStream())); + + String s = null; + String output=""; + while ((s = stdInput.readLine()) != null) { + output+=s; + } + return output; + } catch (IOException e){ + e.printStackTrace(); + } + return null; + } +} diff --git a/code/Interpreter/Expression.java b/src/Compiler/Expression.java index 85ade48..63ed7f2 100644 --- a/code/Interpreter/Expression.java +++ b/src/Compiler/Expression.java @@ -1,4 +1,4 @@ -package Interpreter; +package Compiler; abstract class Expression { static class Binary extends Expression{ diff --git a/code/Interpreter/Language.java b/src/Compiler/Language.java index 80aa1e3..9151bd5 100644 --- a/code/Interpreter/Language.java +++ b/src/Compiler/Language.java @@ -1,4 +1,4 @@ -package Interpreter; +package Compiler; import java.io.IOException; import java.nio.file.Files; @@ -51,8 +51,11 @@ public class Language { Parser parser = new Parser(tokens); List<Statement> ast = parser.parse(); if (hadError) return; - Interpreter interpreter = new Interpreter(); - interpreter.interpret(ast); + Translator translator = new Translator(); + List<String> code = translator.compileToC(ast); + if (hadError) return; + ExecuteC cExecutor = new ExecuteC(); + cExecutor.compileAndExecuteC(code); } static void displayError(String message){ diff --git a/code/Interpreter/Parser.java b/src/Compiler/Parser.java index 6b55299..4339fd4 100644 --- a/code/Interpreter/Parser.java +++ b/src/Compiler/Parser.java @@ -1,4 +1,4 @@ -package Interpreter; +package Compiler; import java.util.ArrayList; import java.util.List; diff --git a/code/Interpreter/Statement.java b/src/Compiler/Statement.java index 5a9aef7..885e462 100644 --- a/code/Interpreter/Statement.java +++ b/src/Compiler/Statement.java @@ -1,4 +1,4 @@ -package Interpreter; +package Compiler; abstract class Statement { diff --git a/code/Interpreter/Token.java b/src/Compiler/Token.java index 0129b78..4608a3d 100644 --- a/code/Interpreter/Token.java +++ b/src/Compiler/Token.java @@ -1,4 +1,4 @@ -package Interpreter; +package Compiler; public class Token { diff --git a/code/Interpreter/TokenScanner.java b/src/Compiler/TokenScanner.java index c9249a4..f00f7b0 100644 --- a/code/Interpreter/TokenScanner.java +++ b/src/Compiler/TokenScanner.java @@ -1,4 +1,4 @@ -package Interpreter; +package Compiler; import java.util.ArrayList; import java.util.HashMap; diff --git a/code/Interpreter/TokenType.java b/src/Compiler/TokenType.java index 756fab6..83ce6cc 100644 --- a/code/Interpreter/TokenType.java +++ b/src/Compiler/TokenType.java @@ -1,4 +1,4 @@ -package Interpreter; +package Compiler; public enum TokenType { EQUALS, LEFT_PAREN, RIGHT_PAREN, diff --git a/src/Compiler/Translator.java b/src/Compiler/Translator.java new file mode 100644 index 0000000..b62df63 --- /dev/null +++ b/src/Compiler/Translator.java @@ -0,0 +1,123 @@ +package Compiler; + +import java.util.ArrayList; +import java.util.List; + +import Compiler.Expression.*; +import Compiler.Statement.*; + + +public class Translator{ + + List<String> CCode = new ArrayList<>(); + private Environment environment = new Environment(); + + + public List<String> compileToC(List<Statement> statements){ + CCode.add("#include <stdio.h>"); + CCode.add("int main(){"); + try{ + for (Statement statement: statements){ + evaluateStatement(statement); + } + } catch (Error e){ + + } + CCode.add("}"); + + for(String t:CCode){ + System.out.println(t); + } + System.out.println(""); + return CCode; + } + + private void evaluateStatement(Statement statement){ + switch(statement.getStatmentType()){ + case "exprStmt": + evalExpressionStatement((ExpressionStatement)statement); + break; + case "vardec": + evalVariableDeclaration((VariableDeclaration)statement); + break; + case "print": + evalPrintStatement((PrintStatement)statement); + break; + } + } + private void evalExpressionStatement(ExpressionStatement stmt){ + evaluateExpression(stmt.expr); + } + + private void evalVariableDeclaration(VariableDeclaration vardec){ + environment.defineVariable(vardec.name.text, null); + CCode.add("int "+vardec.name.text+";"); + } + + private void evalPrintStatement(PrintStatement print){ + CCode.add("printf(\"%d\","+evaluateExpression(print.expr)+");"); + } + + private String evaluateExpression(Expression expression){ + switch(expression.getExpressionType()){ + case "binary": + return evaluateBinaryExpression((Binary)expression); + case "literal": + return evaluateLiteralExpression((Literal)expression); + case "bracket": + return evaluateBracketedExpression((BracketedExpression)expression); + case "assign": + evaluateAssignmentExpression((AssignmentExpression)expression); + return ""; + case "var": + return evaluateVariableExpression((Variable)expression); + default: + return null; + } + } + + private String evaluateBinaryExpression(Binary expr){ + switch (expr.op.type){ + case PLUS: + //return "leftEval+rightEval" + return evaluateExpression(expr.left)+"+"+evaluateExpression(expr.right); + case STAR: + return evaluateExpression(expr.left)+"*"+evaluateExpression(expr.right); + case MINUS: + return evaluateExpression(expr.left)+"-"+evaluateExpression(expr.right); + case SLASH: + return evaluateExpression(expr.left)+"/"+evaluateExpression(expr.right); + case GREATER: + return evaluateExpression(expr.left)+">"+evaluateExpression(expr.right); + case LESS: + return evaluateExpression(expr.left)+"<"+evaluateExpression(expr.right); + case EQUALITY: + return evaluateExpression(expr.left)+"=="+evaluateExpression(expr.right); + default: + break; + } + return null; + } + + private String evaluateLiteralExpression(Literal expr){ + return (expr.value.value).toString(); + } + + private String evaluateBracketedExpression(BracketedExpression expr){ + return evaluateExpression(expr.expr); + } + + private void evaluateAssignmentExpression(AssignmentExpression expr){ + Object assignedValue = evaluateExpression(expr.value); + environment.assignVariable(expr.name.text, assignedValue); + CCode.add(expr.name.text+"="+evaluateExpression(expr.value)+";"); + } + + private String evaluateVariableExpression(Variable expr){ + return expr.name.text; + } + + +} + + diff --git a/src/example.txt b/src/example.txt new file mode 100644 index 0000000..a9202e5 --- /dev/null +++ b/src/example.txt @@ -0,0 +1,4 @@ +var :: a +a=5 +a=a+1 +print a diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..0403b49 --- /dev/null +++ b/src/main.c @@ -0,0 +1,7 @@ +#include <stdio.h> +int main(){ +int a; +a=5.0; +a=a+1.0; +printf("%d",a); +} diff --git a/src/main.exe b/src/main.exe Binary files differnew file mode 100644 index 0000000..dbfd6ba --- /dev/null +++ b/src/main.exe |