added loading file and status
This commit is contained in:
@@ -1,14 +1,17 @@
|
|||||||
package brainfuck.gui;
|
package brainfuck.gui;
|
||||||
|
|
||||||
import brainfuck.interpreter.BfInterpreter;
|
import brainfuck.interpreter.BfInterpreter;
|
||||||
|
import brainfuck.interpreter.Main;
|
||||||
import javafx.fxml.FXML;
|
import javafx.fxml.FXML;
|
||||||
import javafx.scene.control.Alert;
|
import javafx.scene.control.*;
|
||||||
import javafx.scene.control.Button;
|
|
||||||
import javafx.scene.control.Menu;
|
|
||||||
import javafx.scene.control.MenuBar;
|
|
||||||
import javafx.scene.control.MenuItem;
|
|
||||||
import javafx.scene.control.TextArea;
|
|
||||||
import javafx.scene.control.Alert.AlertType;
|
import javafx.scene.control.Alert.AlertType;
|
||||||
|
import javafx.scene.layout.AnchorPane;
|
||||||
|
import javafx.stage.FileChooser;
|
||||||
|
import org.apache.commons.io.IOUtils;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
|
|
||||||
public class Controller {
|
public class Controller {
|
||||||
@FXML
|
@FXML
|
||||||
@@ -29,22 +32,64 @@ public class Controller {
|
|||||||
@FXML
|
@FXML
|
||||||
private Button loadButton;
|
private Button loadButton;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private AnchorPane rootPane;
|
||||||
|
|
||||||
|
@FXML
|
||||||
|
private Label statusText;
|
||||||
|
|
||||||
public void initialize() {
|
public void initialize() {
|
||||||
|
changeStatus("Idle", "#9a9c9a");
|
||||||
saveMenu.setOnAction(e -> {
|
saveMenu.setOnAction(e -> {
|
||||||
System.out.println("menu clicked");
|
System.out.println("menu clicked");
|
||||||
});
|
});
|
||||||
|
|
||||||
interpretButton.setOnAction(e -> {
|
interpretButton.setOnAction(e -> {
|
||||||
|
changeStatus("Interpreting...", "#28fff5");
|
||||||
try {
|
try {
|
||||||
interpretFromText();
|
interpretFromText();
|
||||||
} catch (Exception e1) {
|
} catch (Exception e1) {
|
||||||
e1.printStackTrace();
|
e1.printStackTrace();
|
||||||
}
|
}
|
||||||
|
changeStatus("Done!", "#00ff12");
|
||||||
|
});
|
||||||
|
|
||||||
|
loadButton.setOnAction(e -> {
|
||||||
|
FileChooser fileChooser = new FileChooser();
|
||||||
|
fileChooser.setSelectedExtensionFilter(new FileChooser.ExtensionFilter("Brainfuck file", ".bf", ".b"));
|
||||||
|
fileChooser.setTitle("Choose file to import");
|
||||||
|
|
||||||
|
changeStatus("Interpreting...", "#28fff5");
|
||||||
|
try {
|
||||||
|
File selected = fileChooser.showOpenDialog(rootPane.getScene().getWindow());
|
||||||
|
|
||||||
|
if (selected == null) {
|
||||||
|
changeStatus("Idle", "#9a9c9a");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
FileInputStream fileInputStream = new FileInputStream(selected);
|
||||||
|
String input = IOUtils.toString(fileInputStream, StandardCharsets.UTF_8);
|
||||||
|
|
||||||
|
textinput.setText(input);
|
||||||
|
BfInterpreter interpreter = new BfInterpreter("");
|
||||||
|
interpreter.setCodeFromFile(selected);
|
||||||
|
String interpreted = interpreter.interpret();
|
||||||
|
textoutput.setText(interpreted);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
changeStatus("Done!", "#00ff12");
|
||||||
});
|
});
|
||||||
|
|
||||||
// interpretButton.setSkin(new ScaleSkin(interpretButton));
|
// interpretButton.setSkin(new ScaleSkin(interpretButton));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void changeStatus(String text, String color) {
|
||||||
|
statusText.setStyle("-fx-text-fill: " + color);
|
||||||
|
statusText.setText(text);
|
||||||
|
}
|
||||||
|
|
||||||
private void interpretFromText() throws Exception {
|
private void interpretFromText() throws Exception {
|
||||||
String inputCode = textinput.getText();
|
String inputCode = textinput.getText();
|
||||||
if (inputCode == null || inputCode.isEmpty()) {
|
if (inputCode == null || inputCode.isEmpty()) {
|
||||||
@@ -56,7 +101,7 @@ public class Controller {
|
|||||||
textoutput.setText(interpreted);
|
textoutput.setText(interpreted);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,10 @@
|
|||||||
package brainfuck.interpreter;
|
package brainfuck.interpreter;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
import java.io.FileInputStream;
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileNotFoundException;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.nio.charset.StandardCharsets;
|
||||||
import java.util.Scanner;
|
import java.util.Scanner;
|
||||||
|
|
||||||
import org.apache.commons.io.IOUtils;
|
import org.apache.commons.io.IOUtils;
|
||||||
@@ -36,11 +39,16 @@ public class BfInterpreter {
|
|||||||
String res = "";
|
String res = "";
|
||||||
|
|
||||||
FileInputStream fis = new FileInputStream(fileName);
|
FileInputStream fis = new FileInputStream(fileName);
|
||||||
code = IOUtils.toString(fis, "UTF-8");
|
code = IOUtils.toString(fis, StandardCharsets.UTF_8);
|
||||||
// System.out.println("code is: " + code);
|
// System.out.println("code is: " + code);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setCodeFromFile(File file) throws IOException {
|
||||||
|
FileInputStream fileInputStream = new FileInputStream(file);
|
||||||
|
code = IOUtils.toString(fileInputStream, StandardCharsets.UTF_8);
|
||||||
|
}
|
||||||
|
|
||||||
public String interpret() {
|
public String interpret() {
|
||||||
int loop = 0;
|
int loop = 0;
|
||||||
StringBuilder result = new StringBuilder();
|
StringBuilder result = new StringBuilder();
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ import javafx.stage.Stage;
|
|||||||
* Main
|
* Main
|
||||||
*/
|
*/
|
||||||
public class Main extends Application {
|
public class Main extends Application {
|
||||||
|
private Stage stage;
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
String excl = ">,[[----------[ >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<] ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],< ]> ]>>>++>+>>[ <<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>] >[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]] >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>> ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<, ]";
|
String excl = ">,[[----------[ >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<] ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],< ]> ]>>>++>+>>[ <<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>] >[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]] >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>> ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<, ]";
|
||||||
@@ -24,6 +25,7 @@ public class Main extends Application {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start(Stage stage) throws Exception {
|
public void start(Stage stage) throws Exception {
|
||||||
|
this.stage = stage;
|
||||||
Parent pane = FXMLLoader.load(getClass().getResource("/fxml/layout.fxml"));
|
Parent pane = FXMLLoader.load(getClass().getResource("/fxml/layout.fxml"));
|
||||||
Scene scene = new Scene(pane);
|
Scene scene = new Scene(pane);
|
||||||
scene.getStylesheets().add("/css/menuBar.css");
|
scene.getStylesheets().add("/css/menuBar.css");
|
||||||
@@ -33,4 +35,8 @@ public class Main extends Application {
|
|||||||
stage.show();
|
stage.show();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Stage getStage() {
|
||||||
|
return this.stage;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -13,13 +13,14 @@
|
|||||||
.textbox {
|
.textbox {
|
||||||
-fx-text-fill: rgb(33, 189, 33);
|
-fx-text-fill: rgb(33, 189, 33);
|
||||||
}
|
}
|
||||||
|
|
||||||
.textbox .content {
|
.textbox .content {
|
||||||
-fx-background-color: rgb(43, 43, 43);
|
-fx-background-color: rgb(43, 43, 43);
|
||||||
-fx-text-fill: rgb(33, 189, 33);
|
-fx-text-fill: rgb(33, 189, 33);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
.dialog-pane>.content {
|
.dialog-pane > .content {
|
||||||
-fx-text-fill: -fx-button-color;
|
-fx-text-fill: -fx-button-color;
|
||||||
-fx-font-weight: bold;
|
-fx-font-weight: bold;
|
||||||
-fx-font-size: 14px;
|
-fx-font-size: 14px;
|
||||||
@@ -28,16 +29,16 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
.dialog-pane>.header-panel {
|
.dialog-pane > .header-panel {
|
||||||
-fx-text-fill: -fx-button-color;
|
-fx-text-fill: -fx-button-color;
|
||||||
-fx-font-size: 14px;
|
-fx-font-size: 14px;
|
||||||
-fx-padding: 0.833em 0 0 0.833em;
|
-fx-padding: 0.833em 0 0 0.833em;
|
||||||
/* 10px 0px 0px 10px */
|
/* 10px 0px 0px 10px */
|
||||||
}
|
}
|
||||||
|
|
||||||
.dialog-pane>.header-panel>.label {
|
.dialog-pane > .header-panel > .label {
|
||||||
-fx-text-fill: -fx-button-color;
|
-fx-text-fill: -fx-button-color;
|
||||||
-fx-font-size: 14px;
|
-fx-font-size: 14px;
|
||||||
-fx-padding: 0.833em 0 0 0.833em;
|
-fx-padding: 0.833em 0 0 0.833em;
|
||||||
/* 10px 0px 0px 10px */
|
/* 10px 0px 0px 10px */
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,15 +1,20 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
|
||||||
<?import javafx.scene.text.*?>
|
|
||||||
<?import com.jfoenix.controls.*?>
|
|
||||||
<?import java.lang.*?>
|
<?import java.lang.*?>
|
||||||
<?import java.util.*?>
|
|
||||||
<?import javafx.scene.*?>
|
|
||||||
<?import javafx.scene.control.*?>
|
<?import javafx.scene.control.*?>
|
||||||
<?import javafx.scene.layout.*?>
|
<?import javafx.scene.layout.*?>
|
||||||
|
<?import javafx.scene.text.*?>
|
||||||
|
<?import javafx.scene.control.Button?>
|
||||||
|
<?import javafx.scene.control.Label?>
|
||||||
|
<?import javafx.scene.control.Menu?>
|
||||||
|
<?import javafx.scene.control.MenuBar?>
|
||||||
|
<?import javafx.scene.control.MenuItem?>
|
||||||
|
<?import javafx.scene.control.TextArea?>
|
||||||
|
<?import javafx.scene.layout.AnchorPane?>
|
||||||
|
<?import javafx.scene.layout.HBox?>
|
||||||
|
<?import javafx.scene.text.Font?>
|
||||||
|
|
||||||
|
<AnchorPane fx:id="rootPane" prefHeight="800.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="brainfuck.gui.Controller">
|
||||||
<AnchorPane prefHeight="800.0" prefWidth="1200.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="brainfuck.gui.Controller">
|
|
||||||
<children>
|
<children>
|
||||||
<MenuBar fx:id="menuBar">
|
<MenuBar fx:id="menuBar">
|
||||||
<menus>
|
<menus>
|
||||||
@@ -25,12 +30,22 @@
|
|||||||
<Font name="Consolas" size="14.0" />
|
<Font name="Consolas" size="14.0" />
|
||||||
</font>
|
</font>
|
||||||
</TextArea>
|
</TextArea>
|
||||||
<TextArea fx:id="textoutput" editable="false" layoutX="637.0" layoutY="182.0" prefHeight="317.0" prefWidth="357.0" styleClass="textbox" wrapText="true">
|
<TextArea fx:id="textoutput" editable="false" layoutX="637.0" layoutY="182.0" prefHeight="512.0" prefWidth="496.0" styleClass="textbox">
|
||||||
<font>
|
<font>
|
||||||
<Font name="Consolas" size="14.0" />
|
<Font name="Consolas" size="14.0" />
|
||||||
</font>
|
</font>
|
||||||
</TextArea>
|
</TextArea>
|
||||||
<Button fx:id="interpretButton" layoutX="418.0" layoutY="499.0" mnemonicParsing="false" text="Interpret" />
|
<HBox layoutX="637.0" layoutY="694.0" prefHeight="25.0" prefWidth="496.0" spacing="10.0">
|
||||||
<Button fx:id="loadButton" layoutX="124.0" layoutY="499.0" mnemonicParsing="false" text="Load from file" />
|
<children>
|
||||||
|
<Label styleClass="status" text="Status:" textFill="#b5b5b5" />
|
||||||
|
<Label fx:id="statusText" styleClass="status" text="idle" />
|
||||||
|
</children>
|
||||||
|
</HBox>
|
||||||
|
<HBox layoutX="124.0" layoutY="499.0" prefHeight="25.0" prefWidth="357.0" spacing="10.0">
|
||||||
|
<children>
|
||||||
|
<Button fx:id="loadButton" mnemonicParsing="false" text="Load from file" />
|
||||||
|
<Button fx:id="interpretButton" mnemonicParsing="false" text="Interpret" />
|
||||||
|
</children>
|
||||||
|
</HBox>
|
||||||
</children>
|
</children>
|
||||||
</AnchorPane>
|
</AnchorPane>
|
||||||
|
|||||||
Reference in New Issue
Block a user