Ich möcht einen einfachen Parser um eine Phantasiesprache in eine Java ähnliche Sprache zu konvertieren schreiben. Aber leider funktioniert irgendetwas nicht.
Beispiel Eingabe:
Die Ausgabe ist allerdings:
und wie könnte ich if-else hinzufügen?
Beispiel Eingabe:
Code:
global int temp1
global int temp2
allocation temp2 0
var int i
allocation i 5
var int j
allocation j 10
while i > 0
while j > 0
op temp2 + 1
op j - 1
end
allocation j 10
op i - 1
end
allocation i 5
var int k
allocation k 42
op temp2 + k
allocation temp1 temp2
Die Ausgabe ist allerdings:
Code:
public static int temp1
global;
Java:
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Parser implements ActionListener {
private final JTextArea area1 = new JTextArea();
private final JTextArea area2 = new JTextArea();
private final JButton button1 = new JButton("Convert");
public Parser() {
JFrame frame = new JFrame("Code...");
JPanel p1 = new JPanel(new BorderLayout());
JPanel p2 = new JPanel(new BorderLayout());
JPanel p3 = new JPanel(new GridLayout(1, 1));
area1.setFont(Font.decode(Font.MONOSPACED));
area2.setFont(Font.decode(Font.MONOSPACED));
button1.addActionListener(this);
p3.add(button1);
p2.add(p3, BorderLayout.SOUTH);
p1.add(new JScrollPane(area2), BorderLayout.CENTER);
p1.add(p2, BorderLayout.WEST);
frame.setLayout(new GridLayout(1, 2));
frame.add(new JScrollPane(area1));
frame.add(p1);
frame.setSize(800, 800);
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
frame.setVisible(true);
}
public static void main(String[] args) {
new Parser();
}
private static class LogicEntry {
public static int static_id = 0;
int id, lineNumber;
String type, one, two, three;
boolean atomar, opens, closes;
LogicEntry pair;
public LogicEntry(int lineNumber, String type, String one, String two, String three, boolean atomar, boolean opens, boolean closes, LogicEntry pair) {
this.id = static_id;
static_id++;
this.lineNumber = lineNumber;
this.type = type;
this.one = one;
this.two = two;
this.three = three;
this.atomar = atomar;
this.opens = opens;
this.closes = closes;
this.pair = pair;
}
}
private void parse(String code) {
List<LogicEntry> entries = new ArrayList<>();
String[] lines = code.split(System.getProperty("line.separator"));
for (int i = 0; i < lines.length; i++) {
String l = lines[i].trim();
if (l.startsWith("global")) {
String[] split = l.split(" ");
entries.add(new LogicEntry(i, "global", split[1], split[2], null, true, true, false, null));
}
if (l.startsWith("var")) {
String[] split = l.split(" ");
entries.add(new LogicEntry(i, "var", split[1], split[2], null, true, true, false, null));
}
if (l.startsWith("while")) {
String[] split = l.split(" ");
entries.add(new LogicEntry(i, "while", split[1], split[2], split[3], false, true, false, null));
}
if (l.startsWith("end")) {
for (int j = entries.size() - 1; j >= 0; j--) {
LogicEntry le = entries.get(j);
if (!le.atomar && le.opens) {
entries.add(new LogicEntry(i, le.type, null, null, null, false, false, true, le));
le.pair = entries.get(entries.size() - 1);
break;
}
}
}
if (l.startsWith("allocation")) {
String[] split = l.split(" ");
entries.add(new LogicEntry(i, "allocation", split[1], split[2], null, true, true, false, null));
}
if (l.startsWith("op")) {
String[] split = l.split(" ");
entries.add(new LogicEntry(i, "op", split[1], split[2], split[3], true, true, false, null));
}
}
int indention = 1;
for (int i = 0; i < entries.size(); i++) {
LogicEntry le = entries.get(i);
if (le.type.equals("global")) {
char[] whitespaces = new char[indention * 2];
Arrays.fill(whitespaces, ' ');
area2.append(String.valueOf(whitespaces) + "public static " + le.one + " " + le.two + ";\n");
}
}
for (int i = 0; i < entries.size(); i++) {
LogicEntry le = entries.get(i);
if (le.type.equals("var")) {
char[] whitespaces = new char[indention * 2];
Arrays.fill(whitespaces, ' ');
area2.append(String.valueOf(whitespaces) + le.one + " " + le.two + ";\n");
}
if (le.type.equals("allocation")) {
char[] whitespaces = new char[indention * 2];
Arrays.fill(whitespaces, ' ');
area2.append(String.valueOf(whitespaces) + le.one + " = " + le.two + ";\n");
}
if (le.type.equals("op")) {
char[] whitespaces = new char[indention * 2];
Arrays.fill(whitespaces, ' ');
area2.append(String.valueOf(whitespaces) + le.one + " = " + le.one + " " + le.two + " " + le.three + ";\n");
}
if (le.type.equals("while")) {
// ???
if (le.opens) {
char[] whitespaces = new char[indention * 2];
Arrays.fill(whitespaces, ' ');
area2.append(String.valueOf(whitespaces) + "while (" + le.one + " " + le.two + " " + le.three + ") {\n");
indention++;
}
if (le.closes) {
indention--;
char[] whitespaces = new char[indention * 2];
Arrays.fill(whitespaces, ' ');
area2.append(String.valueOf(whitespaces) + "}\n");
}
// ???
}
}
}
@Override
public void actionPerformed(ActionEvent e) {
if (e.getSource() == button1) {
area2.setText("");
String code = area1.getText();
parse(code);
area2.setCaretPosition(area2.getText().length());
area2.repaint();
}
}
}
und wie könnte ich if-else hinzufügen?