Skip to content

Commit 479c03f

Browse files
committed
Variables and basic data types implemented
1 parent 66e6071 commit 479c03f

File tree

4 files changed

+44
-9
lines changed

4 files changed

+44
-9
lines changed

Compiler/Demo.j

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,21 @@
44
.method static public main([Ljava/lang/String;)V
55
.limit stack 100
66
.limit locals 100
7-
ldc 5
8-
istore 0
7+
ldc 4.3
8+
fstore 0
9+
ldc 4.3
10+
ldc 4.5
11+
fstore 1
12+
ldc 4.5
13+
ldc 4
14+
i2f
15+
fstore 2
916
getstatic java/lang/System/out Ljava/io/PrintStream;
10-
iload 0
11-
invokevirtual java/io/PrintStream/print(I)V
17+
fload 0
18+
fload 1
19+
fadd
20+
fload 2
21+
fadd
22+
invokevirtual java/io/PrintStream/println(F)V
1223
return
1324
.end method

Compiler/code.pc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
a=5
2-
print(a)
1+
a=4.3,b=4.5,c=4
2+
println(a+b+c)

Compiler/src/pc/compiler/Compiler.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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 = "\ngetstatic 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("\nldc " + ctx.digit.getText());
105+
if(type!=null && type.equals("f"))
106+
appendToFile("\ni2f");
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("\ni2f");
109115
type = "f";
110116
appendToFile("\nldc " + 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("\ni2f");
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("\ni2f");
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

Compiler/src/pc/compiler/Main.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static void main(String[] args) throws IOException {
3232
CommonTokenStream tokens = new CommonTokenStream(lexer);
3333
PCParser parser = new PCParser(tokens);
3434
ParseTree tree = parser.program();
35-
showTree(parser, tree);
35+
//showTree(parser, tree);
3636
new Compiler("Demo.j").visit(tree); //Begins the creation of .j File
3737
runProcess(compile);
3838
runProcess(run);

0 commit comments

Comments
 (0)