-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCodeGenerator.java
159 lines (136 loc) · 3.48 KB
/
CodeGenerator.java
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.util.Stack;
public class CodeGenerator
{
static Stack<String> codeStack = new Stack<String>();
static String declarations = "int resultado;" + "\n";
static String inputDataCode = "";
static String newLine = "\n";
static char quote = '"';
//
public static String getObjectiveCode()
{
String code;
//
code = "";
if(!codeStack.isEmpty())
code = codeStack.pop();
//end if
return code;
}//end getObjectiveCode
private static String startingCode()
{
String startingCode;
//
startingCode =
"import java.util.Scanner;"+ newLine +
"public class ObjectiveCode" + newLine +
"{" + newLine + newLine +
"public static int readInt() " + newLine +
"{" + newLine +
"Scanner entrada;" + newLine +
"int dato;" + newLine +
"entrada = new Scanner(System.in);" + newLine +
"dato = entrada.nextInt();" + newLine +
"return dato; " + newLine +
"}//end readInt" + newLine + newLine +
"public static void main(String[] args)" + newLine +
"{" + newLine;
return startingCode;
}//end startingCode
private static String declarationCode()
{
String declarationCode;
//
declarationCode = declarations;
return declarationCode;
}//end declarationCode
private static String inputCode()
{
String inputCode;
//
inputCode = inputDataCode;
return inputCode;
}//end inputCode
private static String outputCode()
{
String outputCode;
//
outputCode = "System.out.print(" + quote +
"El Resultado es " + quote + ");" + newLine +
"System.out.println(resultado);" + newLine ;
return outputCode;
}//end outputCode
private static String endingCode()
{
String endingCode;
//
endingCode =
"}//end main" + newLine +
"}//end class" + newLine;
return endingCode;
}//end startingCode
public static void translation(int ruleNumber, Stack<Token> symbolStack)
{
String firstToken;
String secondToken;
String code;
//
switch(ruleNumber)
{
case 0:
{
// compilamos los pedazos de codigo objetivo
firstToken = codeStack.pop();
code = startingCode() +
declarationCode() +
inputCode() +
"resultado = "+ firstToken + ";" + newLine +
outputCode() +
endingCode();
codeStack.push(code);
}//end case0
break;
case 1:
{
secondToken = codeStack.pop();
firstToken = codeStack.pop();
code = firstToken + " + " + secondToken;
codeStack.push(code);
}//end case1
break;
case 3:
{
secondToken = codeStack.pop();
firstToken = codeStack.pop();
code = firstToken + " * " + secondToken;
codeStack.push(code);
}//end case3
break;
case 5:
{
firstToken = codeStack.pop();
code = "(" + firstToken + ")";
codeStack.push(code);
}//end case5
break;
case 6:
{
firstToken = symbolStack.peek().getContent();
if(!declarations.contains("int " + firstToken + ";"))
{
declarations = declarations +
"int " + firstToken + ";" + newLine;
inputDataCode = inputDataCode +
"System.out.print(" + quote + "Introduce "
+ firstToken + " " + quote +
");" + newLine +
firstToken +
" = readInt();" + newLine;
}//end if
code = firstToken;
codeStack.push(code);
}//end case6
break;
}//end switch
}//end translation
}//end class