-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTranslator.java
More file actions
73 lines (70 loc) · 2.71 KB
/
Translator.java
File metadata and controls
73 lines (70 loc) · 2.71 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
import java.util.*;
import util.*;
class Translator {
public static String translateToPython(List<Token> tokens) {
StringBuilder pythonCode = new StringBuilder();
int indentLevel = 0;
String indent = " "; // 4 spaces per level
boolean newLine = true;
for (Token token : tokens) {
if (newLine) {
pythonCode.append(indent.repeat(indentLevel)); // Apply indentation at the start of a new line
newLine = false;
}
switch (token.type) {
case "FUNCTION":
pythonCode.append("def ");
break;
case "IDENTIFIER":
case "NUMBER":
pythonCode.append(token.value).append(" ");
break;
case "EQUALS":
pythonCode.append("= ");
break;
case "PLUS":
pythonCode.append("+ ");
break;
case "MINUS":
pythonCode.append("- ");
break;
case "MULTIPLY":
pythonCode.append("* ");
break;
case "DIVIDE":
pythonCode.append("/ ");
break;
case "PARENTHESES_OPEN":
pythonCode.append("(");
break;
case "PARENTHESES_CLOSE":
pythonCode.append(")");
break;
case "BRACE_OPEN":
pythonCode.append(":\n"); // `{` replaced with `:`
indentLevel++; // Increase indentation
newLine = true;
break;
case "BRACE_CLOSE":
indentLevel = Math.max(0, indentLevel - 1); // Decrease indentation
pythonCode.append("\n").append(indent.repeat(indentLevel)); // Ensure indentation reduction
newLine = true;
break;
case "SEMICOLON":
pythonCode.append("\n"); // Ensure proper line breaks
newLine = true; // Set flag for indentation in next line
break;
case "RETURN":
//System.out.println(indentLevel);
pythonCode.append("return ");
newLine = false;
break;
default:
pythonCode.append(token.value).append(" ");
break;
}
}
// Remove trailing blank lines for clean formatting
return pythonCode.toString().replaceAll("\n{2,}", "\n").trim();
}
}