-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimpleToken.java
More file actions
40 lines (32 loc) · 1.49 KB
/
SimpleToken.java
File metadata and controls
40 lines (32 loc) · 1.49 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
package Tokens;
class SimpleToken extends Token {
SimpleToken(String input, int scope) {
super(input, scope);
}
/**
* To String method returns the string representation of the token, and the sub-Tokens
*/
@Override
public String toString() {
// New output, filled at the beginning with the correct amount of tabs for the scope
String output = gimmeTabs();
// Check if it is an output statement
if(input.trim().startsWith("System.out.println")) {
return output + "print(" + input.substring(input.indexOf("ln(") + 3, input.indexOf(");")) + ")\n";
}
// Checks if the token is a scanner function, then gets rid of it, because there is not a python equivalent
if(input.startsWith("Scanner") || input.endsWith(".close();")) {
return "";
}
// Checks if the token is a user input statement, then formats the python likewise
if(input.endsWith(".next();") || input.endsWith(".nextLine();") || input.endsWith(".nextInt();")) {
String altOutput = output + input.replaceAll("=\\w*\\..*", "=") + "input()\n";
return altOutput.replaceAll("int|String|double|boolean|char", "").replaceAll(";", "");
}
// Remove the type if it exists (declarations)
// Remove the semicolon at the end of the statement
output += input.replaceAll("int|String|double|boolean|char", "").replaceAll(";", "");
output += "\n";
return output;
}
}