added loading file and status

This commit is contained in:
Sem van der Hoeven
2020-01-05 23:01:17 +01:00
parent 7fd94e0a61
commit 72a7f56085
5 changed files with 96 additions and 21 deletions

View File

@@ -1,14 +1,17 @@
package brainfuck.gui;
import brainfuck.interpreter.BfInterpreter;
import brainfuck.interpreter.Main;
import javafx.fxml.FXML;
import javafx.scene.control.Alert;
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.*;
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 {
@FXML
@@ -29,22 +32,64 @@ public class Controller {
@FXML
private Button loadButton;
@FXML
private AnchorPane rootPane;
@FXML
private Label statusText;
public void initialize() {
changeStatus("Idle", "#9a9c9a");
saveMenu.setOnAction(e -> {
System.out.println("menu clicked");
});
interpretButton.setOnAction(e -> {
changeStatus("Interpreting...", "#28fff5");
try {
interpretFromText();
} catch (Exception e1) {
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));
}
private void changeStatus(String text, String color) {
statusText.setStyle("-fx-text-fill: " + color);
statusText.setText(text);
}
private void interpretFromText() throws Exception {
String inputCode = textinput.getText();
if (inputCode == null || inputCode.isEmpty()) {
@@ -56,7 +101,7 @@ public class Controller {
textoutput.setText(interpreted);
}
}
}

View File

@@ -1,7 +1,10 @@
package brainfuck.interpreter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;
import org.apache.commons.io.IOUtils;
@@ -36,11 +39,16 @@ public class BfInterpreter {
String res = "";
FileInputStream fis = new FileInputStream(fileName);
code = IOUtils.toString(fis, "UTF-8");
code = IOUtils.toString(fis, StandardCharsets.UTF_8);
// 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() {
int loop = 0;
StringBuilder result = new StringBuilder();

View File

@@ -14,6 +14,7 @@ import javafx.stage.Stage;
* Main
*/
public class Main extends Application {
private Stage stage;
public static void main(String[] args) {
String excl = ">,[[----------[ >>>[>>>>]+[[-]+<[->>>>++>>>>+[>>>>]++[->+<<<<<]]<<<] ++++++[>------<-]>--[>>[->>>>]+>+[<<<<]>-],< ]> ]>>>++>+>>[ <<[>>>>[-]+++++++++<[>-<-]+++++++++>[-[<->-]+[<<<<]]<[>+<-]>] >[>[>>>>]+[[-]<[+[->>>>]>+<]>[<+>[<<<<]]+<<<<]>>>[->>>>]+>+[<<<<]] >[[>+>>[<<<<+>>>>-]>]<<<<[-]>[-<<<<]]>>>>>>> ]>>+[[-]++++++>>>>]<<<<[[<++++++++>-]<.[-]<[-]<[-]<]<, ]";
@@ -24,6 +25,7 @@ public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
this.stage = stage;
Parent pane = FXMLLoader.load(getClass().getResource("/fxml/layout.fxml"));
Scene scene = new Scene(pane);
scene.getStylesheets().add("/css/menuBar.css");
@@ -33,4 +35,8 @@ public class Main extends Application {
stage.show();
}
public Stage getStage() {
return this.stage;
}
}

View File

@@ -13,13 +13,14 @@
.textbox {
-fx-text-fill: rgb(33, 189, 33);
}
.textbox .content {
-fx-background-color: rgb(43, 43, 43);
-fx-text-fill: rgb(33, 189, 33);
}
.dialog-pane>.content {
.dialog-pane > .content {
-fx-text-fill: -fx-button-color;
-fx-font-weight: bold;
-fx-font-size: 14px;
@@ -28,16 +29,16 @@
}
.dialog-pane>.header-panel {
.dialog-pane > .header-panel {
-fx-text-fill: -fx-button-color;
-fx-font-size: 14px;
-fx-padding: 0.833em 0 0 0.833em;
/* 10px 0px 0px 10px */
}
.dialog-pane>.header-panel>.label {
.dialog-pane > .header-panel > .label {
-fx-text-fill: -fx-button-color;
-fx-font-size: 14px;
-fx-padding: 0.833em 0 0 0.833em;
/* 10px 0px 0px 10px */
}
}

View File

@@ -1,15 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.text.*?>
<?import com.jfoenix.controls.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?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 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 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">
<children>
<MenuBar fx:id="menuBar">
<menus>
@@ -25,12 +30,22 @@
<Font name="Consolas" size="14.0" />
</font>
</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 name="Consolas" size="14.0" />
</font>
</TextArea>
<Button fx:id="interpretButton" layoutX="418.0" layoutY="499.0" mnemonicParsing="false" text="Interpret" />
<Button fx:id="loadButton" layoutX="124.0" layoutY="499.0" mnemonicParsing="false" text="Load from file" />
<HBox layoutX="637.0" layoutY="694.0" prefHeight="25.0" prefWidth="496.0" spacing="10.0">
<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>
</AnchorPane>