Skip to content

Instantly share code, notes, and snippets.

@simekadam
Created January 5, 2013 14:30
Show Gist options
  • Save simekadam/4461804 to your computer and use it in GitHub Desktop.
Save simekadam/4461804 to your computer and use it in GitHub Desktop.
homework5.java
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import java.util.HashSet;
import java.util.Set;
interface FJExpression {
}
interface ExtFJStatement {
public FJExpression translate();
}
interface ExtFJExpression {
public FJExpression translate();
}
class FJClass {
final String name;
final String superclassName;
final ImmutableList<String> fieldNames;
final ImmutableSet<FJMethod> methods;
FJClass(String name, String superclassName, ImmutableList<String> fieldNames, ImmutableSet<FJMethod> methods) {
this.name = name;
this.superclassName = superclassName;
this.fieldNames = fieldNames;
this.methods = methods;
}
}
class FJMethod {
final String name;
final String returnType;
final ImmutableList<String> parameterNames;
final FJExpression body;
FJMethod(String name, String returnType, ImmutableList<String> parameterNames, FJExpression body) {
this.name = name;
this.returnType = returnType;
this.parameterNames = parameterNames;
this.body = body;
}
}
class FJVarAccess implements FJExpression {
final String name;
FJVarAccess(String name) {
this.name = name;
}
}
class FJFieldAccess implements FJExpression {
final FJExpression receiver;
final String fieldName;
FJFieldAccess(FJExpression receiver, String fieldName) {
this.receiver = receiver;
this.fieldName = fieldName;
}
}
class FJInvocation implements FJExpression {
final FJExpression receiver;
final String methodName;
final ImmutableList<FJExpression> parameters;
FJInvocation(FJExpression receiver, String methodName, ImmutableList<FJExpression> parameters) {
this.receiver = receiver;
this.methodName = methodName;
this.parameters = parameters;
}
}
class FJInstantiation implements FJExpression {
final String className;
final ImmutableList<FJExpression> parameters;
FJInstantiation(String className, ImmutableList<FJExpression> parameters) {
this.className = className;
this.parameters = parameters;
}
}
class ExtFJClass {
final String name;
final String superclassName;
final ImmutableList<String> fieldNames;
final ImmutableSet<ExtFJMethod> methods;
ExtFJClass(String name, String superclassName, ImmutableList<String> fieldNames, ImmutableSet<ExtFJMethod> methods) {
this.name = name;
this.superclassName = superclassName;
this.fieldNames = fieldNames;
this.methods = methods;
}
public FJClass translate()
{
ImmutableSet.Builder<FJMethod> builder = new ImmutableSet.Builder<FJMethod>();
for(ExtFJMethod extFJMethod : methods)
{
builder.add(extFJMethod.translate());
}
ImmutableSet<FJMethod> newmethods = builder.build();
return new FJClass(this.name, this.superclassName, this.fieldNames, newmethods);
}
}
class ExtFJMethod {
final String name;
final String returnType;
final ImmutableList<String> parameterNames;
final ImmutableList<ExtFJStatement> statements;
final ExtFJExpression returnedExpression;
ExtFJMethod(String name, String returnType, ImmutableList<String> parameterNames, ImmutableList<ExtFJStatement> statements, ExtFJExpression returnedExpression) {
this.name = name;
this.returnType = returnType;
this.parameterNames = parameterNames;
this.statements = statements;
this.returnedExpression = returnedExpression;
}
public FJMethod translate()
{
ImmutableList.Builder<FJExpression> builder = new ImmutableList.Builder<FJExpression>();
for (ExtFJStatement extFJStatement : statements)
{
builder.add(extFJStatement.translate());
}
FJExpression expression = new FJInstantiation(returnType, builder.build());
return new FJMethod(name, returnType, parameterNames, returnedExpression.translate());
}
}
class ExtFJAssignment implements ExtFJStatement {
final String varName;
final ExtFJExpression expr;
ExtFJAssignment(String varName, ExtFJExpression expr) {
this.varName = varName;
this.expr = expr;
}
@Override
public FJExpression translate() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
class ExtFJIfStatement implements ExtFJStatement {
final ExtFJExpression condition;
final ExtFJStatement thenCase;
final ExtFJStatement elseCase;
ExtFJIfStatement(ExtFJExpression condition, ExtFJStatement thenCase, ExtFJStatement elseCase) {
this.condition = condition;
this.thenCase = thenCase;
this.elseCase = elseCase;
}
@Override
public FJExpression translate() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
class ExtFJWhileLoop implements ExtFJStatement {
final ExtFJExpression condition;
final ExtFJStatement body;
ExtFJWhileLoop(ExtFJExpression condition, ExtFJStatement body) {
this.condition = condition;
this.body = body;
}
@Override
public FJExpression translate() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
class ExtFJBlock implements ExtFJStatement {
final ImmutableList<ExtFJStatement> statements;
ExtFJBlock(ImmutableList<ExtFJStatement> statements) {
this.statements = statements;
}
@Override
public FJExpression translate() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
class ExtFJVarAccess implements ExtFJExpression {
final String name;
ExtFJVarAccess(String name) {
this.name = name;
}
@Override
public FJExpression translate() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
class ExtFJFieldAccess implements ExtFJExpression {
final ExtFJExpression receiver;
final String fieldName;
ExtFJFieldAccess(ExtFJExpression receiver, String fieldName) {
this.receiver = receiver;
this.fieldName = fieldName;
}
@Override
public FJExpression translate() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
class ExtFJInvocation implements ExtFJExpression {
final ExtFJExpression receiver;
final String methodName;
final ImmutableList<ExtFJExpression> parameters;
ExtFJInvocation(ExtFJExpression receiver, String methodName, ImmutableList<ExtFJExpression> parameters) {
this.receiver = receiver;
this.methodName = methodName;
this.parameters = parameters;
}
@Override
public FJExpression translate() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
class ExtFJInstantiation implements ExtFJExpression {
final String className;
final ImmutableList<ExtFJExpression> parameters;
ExtFJInstantiation(String className, ImmutableList<ExtFJExpression> parameters) {
this.className = className;
this.parameters = parameters;
}
@Override
public FJExpression translate() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
class ExtFJNumber implements ExtFJExpression {
final int value;
ExtFJNumber(int value) {
this.value = value;
}
@Override
public FJExpression translate() {
ImmutableList<FJExpression> params = new ImmutableList.Builder<FJExpression>().build();
ImmutableSet.Builder<FJMethod> methodsNatural = new ImmutableSet.Builder<FJMethod>();
ImmutableSet.Builder<FJMethod> methodsZero = new ImmutableSet.Builder<FJMethod>();
ImmutableSet.Builder<FJMethod> methodsSucc = new ImmutableSet.Builder<FJMethod>();
ImmutableList.Builder<String> paramsOfMethods = new ImmutableList.Builder<String>();
paramsOfMethods.add("second");
methodsNatural.add(new FJMethod("plus", "Natural", paramsOfMethods.build(), new FJInstantiation("Natural", new ImmutableList.Builder<FJExpression>().build())));
methodsNatural.add(new FJMethod("minus", "Natural", paramsOfMethods.build(), new FJInstantiation("Natural", new ImmutableList.Builder<FJExpression>().build())));
methodsNatural.add(new FJMethod("inverseMinus", "Natural", paramsOfMethods.build(), new FJInstantiation("Natural", new ImmutableList.Builder<FJExpression>().build())));
methodsNatural.add(new FJMethod("isZero", "Boolean", paramsOfMethods.build(), new FJInstantiation("Boolean", new ImmutableList.Builder<FJExpression>().build())));
methodsNatural.add(new FJMethod("numericEquals", "Boolean", paramsOfMethods.build(), new FJInstantiation("Boolean", new ImmutableList.Builder<FJExpression>().build())));
//ZERO
paramsOfMethods = new ImmutableList.Builder<String>();
paramsOfMethods.add("second");
ImmutableList<FJExpression> expressions;
ImmutableList.Builder<FJExpression> expressionBuilder = new ImmutableList.Builder<FJExpression>();
expressionBuilder.add(new FJVarAccess("second"));
methodsZero.add(new FJMethod("plus", "Natural", paramsOfMethods.build(), new FJVarAccess("second")));
methodsZero.add(new FJMethod("minus", "Natural", paramsOfMethods.build(), new FJVarAccess("this")));
methodsZero.add(new FJMethod("inverseMinus", "Natural", paramsOfMethods.build(), new FJVarAccess("second")));
methodsNatural.add(new FJMethod("isZero", "Boolean", paramsOfMethods.build(), new FJInstantiation("True", new ImmutableList.Builder<FJExpression>().build())));
paramsOfMethods = new ImmutableList.Builder<String>();
paramsOfMethods.add("second");
methodsNatural.add(new FJMethod("numericEquals", "Boolean", paramsOfMethods.build(), new FJInvocation(new FJVarAccess("second"), "isZero", new ImmutableList.Builder<FJExpression>().build())));
//SUCC
paramsOfMethods = new ImmutableList.Builder<String>();
paramsOfMethods.add("second");
// ImmutableList<FJExpression> expressions;
expressionBuilder = new ImmutableList.Builder<FJExpression>();
expressionBuilder.add(new FJVarAccess("second"));
FJInstantiation succ = new FJInstantiation("Succ", new ImmutableList.Builder<FJExpression>().add(new FJVarAccess("second")).build());
FJInvocation fjInvocation = new FJInvocation(new FJFieldAccess(new FJVarAccess("this"), "pred"), "plus", new ImmutableList.Builder<FJExpression>().add(succ).build());
methodsSucc.add(new FJMethod("plus", "Natural", paramsOfMethods.build(), fjInvocation));
FJInvocation fjInvocationInverseMinus = new FJInvocation(new FJVarAccess("second"), "inverseMinus", new ImmutableList.Builder<FJExpression>().add(new FJVarAccess("this")).build());
methodsSucc.add(new FJMethod("minus", "Natural", paramsOfMethods.build(), fjInvocationInverseMinus));
FJInvocation fjInvocationMinus = new FJInvocation(new FJFieldAccess(new FJVarAccess("second"), "pred"),"minus", new ImmutableList.Builder<FJExpression>().add(new FJFieldAccess(new FJVarAccess("this"), "pred")).build());
methodsSucc.add(new FJMethod("inverseMinus", "Natural", paramsOfMethods.build(), fjInvocationMinus));
methodsNatural.add(new FJMethod("isZero", "Boolean", paramsOfMethods.build(), new FJInstantiation("False", new ImmutableList.Builder<FJExpression>().build())));
paramsOfMethods = new ImmutableList.Builder<String>();
paramsOfMethods.add("second");
FJInvocation inner = new FJInvocation(new FJVarAccess("this"), "minus", new ImmutableList.Builder<FJExpression>().add(new FJVarAccess("second")).build());
methodsNatural.add(new FJMethod("numericEquals", "Boolean", paramsOfMethods.build(), new FJInvocation(inner, "isZero", new ImmutableList.Builder<FJExpression>().build())));
homework5.addToSet(new FJClass("Natural","Object", new ImmutableList.Builder<String>().build(), methodsZero.build()));
homework5.addToSet(new FJClass("Zero","Natural", new ImmutableList.Builder<String>().build(), methodsSucc.build()));
homework5.addToSet(new FJClass("Succ","Natural", new ImmutableList.Builder<String>().add("pred").build(), methodsNatural.build()));
FJExpression current = new FJInstantiation("Zero", params);
for(int i = 0 ; i < value; i++)
{
params = new ImmutableList.Builder<FJExpression>().add(current).build();
current = new FJInstantiation("Succ", params);
}
return current;
}
}
class ExtFJAddition implements ExtFJExpression {
final ExtFJExpression left, right;
ExtFJAddition(ExtFJExpression left, ExtFJExpression right) {
this.left = left;
this.right = right;
}
@Override
public FJExpression translate() {
FJExpression left = this.left.translate();
FJExpression right = this.right.translate();
return new FJInvocation(left, "plus", new ImmutableList.Builder<FJExpression>().add(right).build());
}
}
class ExtFJSubtraction implements ExtFJExpression {
final ExtFJExpression left, right;
ExtFJSubtraction(ExtFJExpression left, ExtFJExpression right) {
this.left = left;
this.right = right;
}
@Override
public FJExpression translate() {
FJExpression left = this.left.translate();
FJExpression right = this.right.translate();
return new FJInvocation(left, "minus", new ImmutableList.Builder<FJExpression>().add(right).build());
}
}
class ExtFJBoolean implements ExtFJExpression {
final boolean value;
ExtFJBoolean(boolean value) {
this.value = value;
}
@Override
public FJExpression translate() {
ImmutableList<FJExpression> params = new ImmutableList.Builder<FJExpression>().build();
ImmutableSet.Builder<FJMethod> methodsBoolean = new ImmutableSet.Builder<FJMethod>();
ImmutableSet.Builder<FJMethod> methodsTrue = new ImmutableSet.Builder<FJMethod>();
ImmutableSet.Builder<FJMethod> methodsFalse = new ImmutableSet.Builder<FJMethod>();
ImmutableList.Builder<String> paramsOfMethods = new ImmutableList.Builder<String>();
paramsOfMethods.add("second");
methodsBoolean.add(new FJMethod("and", "Boolean", paramsOfMethods.build(), new FJInstantiation("Boolean", new ImmutableList.Builder<FJExpression>().build())));
methodsBoolean.add(new FJMethod("or", "Boolean", paramsOfMethods.build(), new FJInstantiation("Boolean", new ImmutableList.Builder<FJExpression>().build())));
paramsOfMethods = new ImmutableList.Builder<String>();
methodsBoolean.add(new FJMethod("not", "Boolean", paramsOfMethods.build(), new FJInstantiation("Boolean", new ImmutableList.Builder<FJExpression>().build())));
//TRUE
paramsOfMethods = new ImmutableList.Builder<String>();
paramsOfMethods.add("second");
ImmutableList<FJExpression> expressions;
ImmutableList.Builder<FJExpression> expressionBuilder = new ImmutableList.Builder<FJExpression>();
expressionBuilder.add(new FJVarAccess("second"));
methodsTrue.add(new FJMethod("and", "Boolean", paramsOfMethods.build(), new FJVarAccess("second")));
methodsTrue.add(new FJMethod("or", "Boolean", paramsOfMethods.build(), new FJVarAccess("this")));
paramsOfMethods = new ImmutableList.Builder<String>();
methodsTrue.add(new FJMethod("not", "Boolean", paramsOfMethods.build(), new FJInstantiation("False", new ImmutableList.Builder<FJExpression>().build())));
//FALSE
paramsOfMethods = new ImmutableList.Builder<String>();
paramsOfMethods.add("second");
// ImmutableList<FJExpression> expressions;
expressionBuilder = new ImmutableList.Builder<FJExpression>();
expressionBuilder.add(new FJVarAccess("second"));
methodsFalse.add(new FJMethod("and", "Boolean", paramsOfMethods.build(), new FJVarAccess("this")));
methodsFalse.add(new FJMethod("or", "Boolean", paramsOfMethods.build(), new FJVarAccess("second")));
paramsOfMethods = new ImmutableList.Builder<String>();
methodsFalse.add(new FJMethod("not", "Boolean", paramsOfMethods.build(), new FJInstantiation("True", new ImmutableList.Builder<FJExpression>().build())));
homework5.addToSet(new FJClass("True","Boolean", new ImmutableList.Builder<String>().build(), methodsTrue.build()));
homework5.addToSet(new FJClass("False","Boolean", new ImmutableList.Builder<String>().build(), methodsFalse.build()));
homework5.addToSet(new FJClass("Boolean","Object", new ImmutableList.Builder<String>().build(), methodsBoolean.build()));
return this.value ? new FJInstantiation("True", params) : new FJInstantiation("False", params);
}
}
class ExtFJNegation implements ExtFJExpression {
final ExtFJExpression expr;
ExtFJNegation(ExtFJExpression expr) {
this.expr = expr;
}
@Override
public FJExpression translate() {
FJExpression param = this.expr.translate();
return new FJInvocation(param, "not", new ImmutableList.Builder<FJExpression>().build());
}
}
class ExtFJConjunction implements ExtFJExpression {
final ExtFJExpression left, right;
ExtFJConjunction(ExtFJExpression left, ExtFJExpression right) {
this.left = left;
this.right = right;
}
@Override
public FJExpression translate() {
FJExpression left = this.left.translate();
FJExpression right = this.right.translate();
return new FJInvocation(left, "and", new ImmutableList.Builder<FJExpression>().add(right).build());
}
}
class ExtFJDisjunction implements ExtFJExpression {
final ExtFJExpression left, right;
ExtFJDisjunction(ExtFJExpression left, ExtFJExpression right) {
this.left = left;
this.right = right;
}
@Override
public FJExpression translate() {
FJExpression left = this.left.translate();
FJExpression right = this.right.translate();
return new FJInvocation(left, "or", new ImmutableList.Builder<FJExpression>().add(right).build());
}
}
class ExtFJTernary implements ExtFJExpression {
final ExtFJExpression condition;
final ExtFJExpression thenCase, elseCase;
ExtFJTernary(ExtFJExpression condition, ExtFJExpression thenCase, ExtFJExpression elseCase) {
this.condition = condition;
this.thenCase = thenCase;
this.elseCase = elseCase;
}
@Override
public FJExpression translate() {
return null; //To change body of implemented methods use File | Settings | File Templates.
}
}
class ExtFJNumericEquals implements ExtFJExpression {
final ExtFJExpression first, second;
ExtFJNumericEquals(ExtFJExpression first, ExtFJExpression second) {
this.first = first;
this.second = second;
}
@Override
public FJExpression translate() {
FJExpression left = this.first.translate();
FJExpression right = this.second.translate();
return new FJInvocation(left, "numericEquals", new ImmutableList.Builder<FJExpression>().add(right).build());
}
}
class homework5 {
static Set<FJClass> returnedSet;
static Set<FJClass> translate(Set<ExtFJClass> classes) {
returnedSet = new HashSet<FJClass>();
for (ExtFJClass extclass : classes)
{
returnedSet.add(extclass.translate());
}
return returnedSet;
}
static void addToSet(FJClass addedClass)
{
// if(returnedSet == null)
// {
// returnedSet = new HashSet<FJClass>();
// }
returnedSet.add(addedClass);
}
public static void main(String[] args) {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment