@@ -27,7 +27,8 @@ public class Compiler extends PCBaseVisitor<String>{
2727
2828 private File file = null ;
2929 private FileWriter fw = null ;
30- private String type ;
30+ private String type , prevType ;
31+ private boolean floatBool = false ;
3132 private HashMap <String ,SymbolTableNode > symbolTable = new HashMap <>();
3233 private int lineNumber = 1 ;
3334 String begPrint = "\n getstatic java/lang/System/out Ljava/io/PrintStream;" ;
@@ -100,12 +101,17 @@ public String visitDivide(DivideContext ctx) {
100101 }
101102
102103 public String visitDigit (DigitContext ctx ) {
103- type = "i" ;
104104 appendToFile ("\n ldc " + ctx .digit .getText ());
105+ if (type !=null && type .equals ("f" ))
106+ appendToFile ("\n i2f" );
107+ else
108+ type = "i" ;
105109 return null ;
106110 }
107111
108112 public String visitDecimal (DecimalContext ctx ) {
113+ if (type !=null && type .equals ("i" ))
114+ appendToFile ("\n i2f" );
109115 type = "f" ;
110116 appendToFile ("\n ldc " + ctx .decimal .getText ());
111117 return null ;
@@ -115,7 +121,16 @@ public String visitVariable(VariableContext ctx) {
115121 if (symbolTable .get (ctx .var .getText ())!=null ) {
116122 SymbolTableNode tmp = symbolTable .get (ctx .var .getText ());
117123 type = tmp .getType ();
124+ if (!floatBool && type .equals ("f" ) && prevType !=null && prevType .equals ("i" ))
125+ appendToFile ("\n i2f" );
126+ if (type .equals ("f" ))
127+ floatBool = true ;
118128 appendToFile ("\n " + type .toLowerCase () + "load " + tmp );
129+ prevType = type ;
130+ if (type !=null && type .equals ("i" ) && floatBool ) {
131+ appendToFile ("\n i2f" );
132+ type = "f" ;
133+ }
119134 }
120135 else
121136 throw new VariableNotDefined (ctx .getText (), ctx .var .getText (), lineNumber );
@@ -125,10 +140,19 @@ public String visitVariable(VariableContext ctx) {
125140 public String visitLine (LineContext ctx ) {
126141 lineNumber ++;
127142 type = null ;
143+ prevType = null ;
144+ floatBool = false ;
128145 return null ;
129146 }
130147
131148 public String visitMultipleVariable (MultipleVariableContext ctx ) {
149+ visit (ctx .exp );
150+ if (symbolTable .get (ctx .var .getText ())==null )
151+ symbolTable .put (ctx .var .getText (), new SymbolTableNode (ctx .var .getText (), type , symbolTable .size ()));
152+ else
153+ throw new VariableAlreadyDefined (ctx .getText (), ctx .var .getText (), lineNumber );
154+ appendToFile ("\n " + type .toLowerCase () + "store " + symbolTable .get (ctx .var .getText ()));
155+ visitChildren (ctx );
132156 return null ;
133157 }
134158
0 commit comments