Skip to content

Commit

Permalink
Completed code generator.
Browse files Browse the repository at this point in the history
  • Loading branch information
aj-michael committed May 2, 2016
1 parent 77e6d99 commit 991a020
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 23 deletions.
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
group 'edu.rosehulman.minijavac'
version '0.0.4'
version '0.0.5'

apply plugin: 'java'
apply plugin: 'application'
Expand Down
31 changes: 25 additions & 6 deletions src/main/java/edu/rosehulman/minijavac/ast/BinaryOperation.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package edu.rosehulman.minijavac.ast;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.google.common.collect.ImmutableList;
import edu.rosehulman.minijavac.generator.ConstantPool;
import edu.rosehulman.minijavac.generator.Variable;
import edu.rosehulman.minijavac.typechecker.Scope;
import edu.rosehulman.minijavac.typechecker.Type;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import static edu.rosehulman.minijavac.ast.BinaryOperation.BinaryOperator.EQ;
import static edu.rosehulman.minijavac.ast.BinaryOperation.BinaryOperator.GT;
import static edu.rosehulman.minijavac.ast.BinaryOperation.BinaryOperator.GTE;
Expand Down Expand Up @@ -70,8 +70,27 @@ public void addConstantPoolEntries(ConstantPool cp) {
public List<Byte> generateCode(ConstantPool cp, Map<String, Variable> variables) {
ArrayList<Byte> bytes = new ArrayList<>();
bytes.addAll(left.generateCode(cp, variables));
bytes.addAll(right.generateCode(cp, variables));
bytes.addAll(operatorBytes);
List<Byte> rightBytes = right.generateCode(cp, variables);
if (operator == BinaryOperator.AND) {
bytes.add((byte) 89); // dup
bytes.add((byte) 153); // ifeq means top of stack is zero
int jumpLength = rightBytes.size() + operatorBytes.size() + 3;
bytes.add((byte) (jumpLength >> 8));
bytes.add((byte) jumpLength);
bytes.addAll(rightBytes);
bytes.addAll(operatorBytes);
} else if (operator == BinaryOperator.OR) {
bytes.add((byte) 89); // dup
bytes.add((byte) 154); // ifne means top of stack is not zero
int jumpLength = rightBytes.size() + operatorBytes.size() + 3;
bytes.add((byte) (jumpLength >> 8));
bytes.add((byte) jumpLength);
bytes.addAll(rightBytes);
bytes.addAll(operatorBytes);
} else {
bytes.addAll(rightBytes);
bytes.addAll(operatorBytes);
}
return bytes;
}

Expand Down
10 changes: 0 additions & 10 deletions src/main/java/edu/rosehulman/minijavac/cli/Minijavac.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,8 @@
package edu.rosehulman.minijavac.cli;

import com.google.common.io.Files;
import edu.rosehulman.minijavac.ast.Program;
import edu.rosehulman.minijavac.generated.Lexer;
import edu.rosehulman.minijavac.generated.Parser;
import edu.rosehulman.minijavac.generator.CodeGenerator;
import edu.rosehulman.minijavac.typechecker.TypeChecker;
import io.airlift.airline.Cli;
import io.airlift.airline.Help;

import java.io.File;
import java.io.FileReader;
import java.util.Map;

public class Minijavac {
public static void main(String args[]) throws Exception {
Cli.CliBuilder<Runnable> builder = Cli.<Runnable>builder("minijavac")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;
Expand Down
5 changes: 0 additions & 5 deletions src/test/java/edu/rosehulman/minijavac/TypeCheckerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,17 @@
import edu.rosehulman.minijavac.ast.Program;
import edu.rosehulman.minijavac.generated.Lexer;
import edu.rosehulman.minijavac.generated.Parser;
import edu.rosehulman.minijavac.generator.CodeGenerator;
import edu.rosehulman.minijavac.typechecker.TypeChecker;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.io.File;
import java.io.FileReader;
import java.lang.reflect.Method;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.Assert.assertEquals;

Expand Down

0 comments on commit 991a020

Please sign in to comment.