Skip to content

Commit

Permalink
version 3.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasz-herman committed Sep 29, 2019
1 parent e4b7e45 commit d67a5e7
Show file tree
Hide file tree
Showing 13 changed files with 112 additions and 58 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ Also many functions was simplified for easier use, got more obvious names and in
# How to use?
Since program was designed for beginner programmers helper scripts was provided to make easier access to it's functionality.
Java 12+ is required to run emulator.
On Windows double-click run.bat to run a program or debug.bat to debug a program.
On Linux/MacOS run run.sh/debug.sh in terminal to get started(remember to make it executable).
Since version 3.1.0 graphical interface is provided.
On Windows double-click run.bat to run a program or debug.bat to debug or gui.bat to run emulator in graphical mode.
On Linux/MacOS/Windows(with bash installed) run run.sh/debug.sh/gui.sh in terminal to get started(remember to make it executable).
4 changes: 3 additions & 1 deletion scripts/debug.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash
echo Specify input file:
read -r InputPath
java -jar PseudoAssemblerEmulator.jar -di "$InputPath"
java -jar PseudoAssemblerEmulator.jar -di "$InputPath"
read -n 1 -s -r -p "Press any key to continue..."
echo
5 changes: 5 additions & 0 deletions scripts/gui.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@echo off
@echo on
java -jar PseudoAssemblerEmulator.jar
@echo off
pause
4 changes: 4 additions & 0 deletions scripts/gui.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
java -jar PseudoAssemblerEmulator.jar
read -n 1 -s -r -p "Press any key to continue..."
echo
4 changes: 3 additions & 1 deletion scripts/run.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
#!/bin/bash
echo Specify input file:
read -r InputPath
java -jar PseudoAssemblerEmulator.jar -i "$InputPath"
java -jar PseudoAssemblerEmulator.jar -i "$InputPath"
read -n 1 -s -r -p "Press any key to continue..."
echo
22 changes: 19 additions & 3 deletions src/com/hermant/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
import com.hermant.cli.*;
import com.hermant.gui.Window;
import com.hermant.machine.Machine;
import com.hermant.parser.ParseException;
import com.hermant.parser.Parser;
import com.hermant.program.Program;
import com.hermant.serializer.SerializationException;
import com.hermant.serializer.Serializer;

import java.io.IOException;

public class Main {

public static void main(String[] args) {
Expand All @@ -15,9 +19,21 @@ public static void main(String[] args) {
Options options = ArgsParser.parse(args);
if(options.version())Version.print();
if(options.help()) Help.printUsage();
Program program = options.binary() ?
Serializer.deserializeBinary(options.input()) : Parser.parse(options.input());
if(!options.output().isEmpty()) Serializer.serializeProgram(program, options.output());
Program program = null;
try {
program = options.binary() ?
Serializer.deserializeBinary(options.input()) : Parser.parse(options.input());
} catch (IOException | SerializationException | ParseException e) {
e.printStackTrace();
System.exit(-1);
}
if(!options.output().isEmpty()) {
try {
Serializer.serializeProgram(program, options.output());
} catch (IOException e) {
e.printStackTrace();
}
}
if(options.abandon())System.exit(0);
Machine m = new Machine(options.debug(), options.unsafe());
m.loadProgram(program);
Expand Down
5 changes: 4 additions & 1 deletion src/com/hermant/cli/Version.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
public class Version {

private static final int MAJOR = 3;
private static final int MINOR = 0;
private static final int MINOR = 1;
private static final int REVISION = 0;

private static final String PROJECT_NAME = "PseudoAssembler Emulator";
Expand All @@ -12,5 +12,8 @@ public class Version {
public static void print(){
System.out.println(FULL);
}
public static String getFull(){
return FULL;
}

}
55 changes: 45 additions & 10 deletions src/com/hermant/gui/Form.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.hermant.gui;

import com.hermant.machine.Machine;
import com.hermant.parser.ParseException;
import com.hermant.parser.Parser;
import com.hermant.program.Program;
import com.hermant.serializer.SerializationException;
import com.hermant.serializer.Serializer;

import javax.swing.*;
Expand Down Expand Up @@ -40,13 +42,15 @@ public class Form {
if(returnVal == JFileChooser.APPROVE_OPTION){
File file = inputChooser.getSelectedFile();
input.setText(file.getPath());
input.setToolTipText(file.getPath());
}
});
selectOutputFileButton.addActionListener(e -> {
int returnVal = outputChooser.showSaveDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION){
File file = outputChooser.getSelectedFile();
output.setText(file.getPath());
output.setToolTipText(file.getPath());
}
});
sleep_slider.addChangeListener(
Expand All @@ -60,18 +64,20 @@ public class Form {
routine.addItem(new Combo(1, "Debug"));
routine.addActionListener(e -> run_button.setText(Objects.requireNonNull(routine.getSelectedItem()).toString()));

CustomInputStream streamer = new CustomInputStream();
terminal.addKeyListener(streamer);
System.setIn(streamer);
CustomInputStream inputStream = new CustomInputStream();
terminal.addKeyListener(inputStream);
System.setIn(inputStream);
CustomOutputStream outputStream = new CustomOutputStream(terminal);
PrintStream printStream = new PrintStream(outputStream);
System.setOut(printStream);
System.setErr(printStream);

run_button.addActionListener(e -> {
Thread t = new Thread(() -> {
running = true;
terminal.setText("");
outputStream.reset();
inputStream.reset();
setControlsEnabled(false);
boolean unsafe = unsafeCheckBox.isSelected();
boolean abandon = abandonCheckBox.isSelected();
Expand All @@ -80,16 +86,42 @@ public class Form {
int sleep = sleep_slider.getValue() * 10;
String inputFile = input.getText();
String outputFile = output.getText();
if(debug && sleep == 0)
JOptionPane.showMessageDialog(null, "Debugging without sleep is not recommended!");
int warning = JOptionPane.OK_OPTION;
if(!abandon && debug && sleep == 0)
warning = JOptionPane.showConfirmDialog(null,
"Debugging without sleep is not recommended!\n Continue?",
"Confirm", JOptionPane.YES_NO_OPTION);
if(warning == JOptionPane.NO_OPTION){
setControlsEnabled(true);
running = false;
return;
}
if(inputFile.isEmpty()) JOptionPane.showMessageDialog(null, "You must provide input file!");
else {
Program program = binary ?
Serializer.deserializeBinary(inputFile) : Parser.parse(inputFile);
if(!outputFile.isEmpty()) Serializer.serializeProgram(program, outputFile);
Program program;
try {
program = binary ?
Serializer.deserializeBinary(inputFile) : Parser.parse(inputFile);
} catch (IOException | SerializationException | ParseException ex) {
ex.printStackTrace();
setControlsEnabled(true);
running = false;
return;
}
if(!outputFile.isEmpty()) {
try {
Serializer.serializeProgram(program, outputFile);
} catch (IOException ex) {
ex.printStackTrace();
}
}
Machine m = new Machine(debug, unsafe);
m.loadProgram(program);
if(!abandon) m.runProgram(sleep);
if(!abandon)
try { m.runProgram(sleep); }
catch (Exception ex){
ex.printStackTrace();
}
m.free();
}
setControlsEnabled(true);
Expand Down Expand Up @@ -185,7 +217,10 @@ static class CustomInputStream extends InputStream implements KeyListener {
private StringBuilder buffer = new StringBuilder();
private int pos = 0;

CustomInputStream() {
public void reset(){
str = "";
pos = 0;
buffer = new StringBuilder();
}

@Override
Expand Down
4 changes: 3 additions & 1 deletion src/com/hermant/gui/Window.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.hermant.gui;

import com.hermant.cli.Version;

import javax.swing.*;
import java.awt.*;

public class Window {
public Window(){
JFrame frame = new JFrame("gui");
JFrame frame = new JFrame(Version.getFull());
Form form = new Form();
frame.setContentPane(form.getMain());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Expand Down
2 changes: 1 addition & 1 deletion src/com/hermant/parser/ParseException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hermant.parser;

class ParseException extends Exception{
public class ParseException extends Exception{
ParseException(String errorMessage, int lineNumber) {
super(errorMessage + " at line " + lineNumber);
}
Expand Down
11 changes: 2 additions & 9 deletions src/com/hermant/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -482,15 +482,8 @@ public Declaration create(int count, String value) {
public abstract Declaration create(int count, String value);
}

public static Program parse(String path){
Program program = new Program();
try {
program = parse(path, analyzeLabels(path));
} catch (IOException | ParseException e) {
e.printStackTrace();
System.exit(1);
}
return program;
public static Program parse(String path) throws IOException, ParseException {
return parse(path, analyzeLabels(path));
}

private static Program parse(String path, Map<String, String> labelMemoryTranslation) throws ParseException, IOException {
Expand Down
2 changes: 1 addition & 1 deletion src/com/hermant/serializer/SerializationException.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hermant.serializer;

class SerializationException extends Exception {
public class SerializationException extends Exception {
SerializationException(String errorMessage) {
super(errorMessage);
}
Expand Down
47 changes: 19 additions & 28 deletions src/com/hermant/serializer/Serializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,39 +78,30 @@ Declaration deserialize(ByteBuffer buffer) {
abstract Declaration deserialize(ByteBuffer buffer);
}

public static Program deserializeBinary(String path){
public static Program deserializeBinary(String path) throws IOException, SerializationException, BufferUnderflowException {
Program program = new Program();
try {
byte[] bytes = Files.readAllBytes(Paths.get(path));
ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
if(buffer.remaining() < 2)throw new SerializationException("File is too small.");
int declarations = Short.toUnsignedInt(buffer.getShort());
int maxDeclarationType = DeclarationType.values().length - 1;
while(declarations > 0 && buffer.hasRemaining()){
int typeIndex = buffer.get();
if(typeIndex > maxDeclarationType || typeIndex < 0)
throw new SerializationException("invalid internal structure");
DeclarationType type = DeclarationType.values()[typeIndex];
program.addDeclaration(type.deserialize(buffer));
declarations--;
}
if(declarations > 0) throw new SerializationException("invalid internal structure");
while(buffer.hasRemaining()){
program.addInstruction(deserializeInstruction(buffer));
}
} catch (IOException | SerializationException | BufferUnderflowException e) {
e.printStackTrace();
System.exit(1);
byte[] bytes = Files.readAllBytes(Paths.get(path));
ByteBuffer buffer = ByteBuffer.wrap(bytes).order(ByteOrder.LITTLE_ENDIAN);
if(buffer.remaining() < 2)throw new SerializationException("File is too small.");
int declarations = Short.toUnsignedInt(buffer.getShort());
int maxDeclarationType = DeclarationType.values().length - 1;
while(declarations > 0 && buffer.hasRemaining()){
int typeIndex = buffer.get();
if(typeIndex > maxDeclarationType || typeIndex < 0)
throw new SerializationException("invalid internal structure");
DeclarationType type = DeclarationType.values()[typeIndex];
program.addDeclaration(type.deserialize(buffer));
declarations--;
}
if(declarations > 0) throw new SerializationException("invalid internal structure");
while(buffer.hasRemaining()){
program.addInstruction(deserializeInstruction(buffer));
}
return program;
}

public static void serializeProgram(Program program, String path){
try {
Files.write(Paths.get(path), program.serialize());
} catch (IOException e) {
e.printStackTrace();
}
public static void serializeProgram(Program program, String path) throws IOException {
Files.write(Paths.get(path), program.serialize());
}

private static LoadableInstruction deserializeInstruction(ByteBuffer buffer){
Expand Down

0 comments on commit d67a5e7

Please sign in to comment.