-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInterface.java
More file actions
92 lines (83 loc) · 3.86 KB
/
Interface.java
File metadata and controls
92 lines (83 loc) · 3.86 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
import java.io.FileNotFoundException;
import java.util.*;
import static java.lang.System.exit;
/**
* @name Sterling Blevins
*
* @file Interface.java
* @description The interface for the Interpreter
*
* @date 5/3/17
*/
public class Interface {
/**
* Main will take the input and repeat a prompt. The input string is processed so that the parenthesis do not account
* for spaces. The input is separated by spaces, after the parenthesis are removed and tokenized and inserted into an
* array. The array is then processed.
*
* The program will exit with the term "quit", and can be changed from static to dynamic, with the default being static.
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(System.in);
Interpreter interpreter = new Interpreter();
int mapFlag = 0;
while(true) {
ArrayList<List<Token>> list = new ArrayList<List<Token>>();
Stack<Token> stack = new Stack<Token>();
try {
System.out.print("Prompt> ");
String input = scan.nextLine();
// Fixes the malformed spaces in the commands put into the interpreter
// preceding parenthesis replacement
input = input.replaceAll("(\\s+([(]))", "(");
input = input.replaceAll("(\\s+([)]))", ")");
//suceeding parenthesis replacement
input = input.replaceAll("(([(])+\\s)" , "(");
input = input.replaceAll("(([)])+\\s)", ")");
// removes the parenthesis
String[] splitString = input.split("[()]");
// splits string into an array separated by the parenthesis
List<String> split = new ArrayList<String>(Arrays.asList(splitString));
// removes extra elements
split.removeAll(Arrays.asList("", null));
Token token;
for (int i = 0; i < split.size(); i++) {
// splits each array element further by space
String[] content = split.get(i).split("\\s+");
List<Token> contentParsed = new ArrayList<Token>();
for (int j = 0; j < content.length; j++) {
// tokenizes each component of the term within parenthesis
token = Token.parseToken(content[j]);
contentParsed.add(token);
}
list.add(contentParsed);
}
try {
// if the user quits
if (list.get(0).get(0).toString().equals(Token.parseToken("quit").toString())) {
exit(0);
// if the user wants a static scoping system enabled (enabled by default)
} else if (list.get(0).get(0).toString().equals(Token.parseToken("static").toString())) {
mapFlag = 0;
// if the user wants a dynamic scoping system enabled (shares variable mapping between variables
// defined inside and outside of functions
} else if (list.get(0).get(0).toString().equals(Token.parseToken("dynamic").toString())) {
mapFlag = 2;
} else {
// if the user inputs a normal expression
interpreter.process(list, 0, mapFlag);
}
} catch(IndexOutOfBoundsException e) {
System.out.print("");
} catch(NullPointerException e) {
System.out.print("");
}
} catch(NoSuchElementException e) {
System.out.println("EXITING");
exit(0);
}
}
}
}