-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForthCompiler.java
More file actions
92 lines (81 loc) · 3 KB
/
Copy pathForthCompiler.java
File metadata and controls
92 lines (81 loc) · 3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// Main-metodi Forth- tulkin toiminnalle
// Suoritus ilman parametreja ( java ForthCompiler )
import java.util.Scanner;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileNotFoundException;
import java.io.IOException;
class ForthCompiler{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
boolean running = true;
boolean input = false;
String cmd;
String line;
Decoder kl = new Decoder(); // Olio syötteiden tulkintaan
help();
while(running){
cmd = sc.nextLine();
if( cmd.length() > 4 && cmd.substring(cmd.length()-4,cmd.length()).equalsIgnoreCase(".txt")){
try (BufferedReader br = new BufferedReader(new FileReader(cmd))) {
line = br.readLine();
while (line != null){
if(line.equals("") || line.substring(0,1).equals("?")){ // Jätetään tyhjät rivit ja kommentit huomiotta
} else {
kl.listen(line); // Syötetään hyväksytyt rivit keyListener-oliolle
}
line = br.readLine();
}
br.close();
System.out.println("File loaded.");
} catch (Exception f){System.out.println(f);}
}
// Manuaalinen syöte
else if(cmd.equalsIgnoreCase("input")){
System.out.println("Manual input. Type 'run' to execute, 'end' to end input.");
input = true;
while (input){
if (cmd.equals("end")){
System.out.println("Manual input stopped.");
kl.reset();
input = false;
break;
}
else if(cmd.equalsIgnoreCase("reset")){
kl.reset();
System.out.println("Stacks cleared.");
}
cmd = sc.nextLine();
kl.listen(cmd);
}
}
// Koodin toteutus
else if(cmd.equalsIgnoreCase("run")){
kl.listen(cmd);
}
// Pinojen tulostus (testausmielessä)
else if(cmd.equalsIgnoreCase("show")){
kl.listen(cmd);
}
// Ladatun koodin & pinojen resetointi
else if(cmd.equalsIgnoreCase("reset")){
kl.reset();
System.out.println("File & stacks cleared.");
}
else if (cmd.equalsIgnoreCase("help")) help();
else if(cmd.equalsIgnoreCase("exit")){
running = false;
System.out.println("Ending.");
}
else{
System.out.println("Input not recognized.");
}
}
}
// Syöttöpaneelin käyttöohje
static void help(){
System.out.println("################# COMMANDS #################");
System.out.println("Read from txt-file: <name>.txt\nRemove loaded file and clear stacks: reset\nManual input: input");
System.out.println("Run program: run\nStop manual input: end\nExit: exit\nShow commands: help\n############################################\n");
}
}