package visitor.codegeneration; import ast.expression.classes.*; /** * Code function for Expression */ public class ValueCGVisitor extends AbstractCGVisitor<Void, Void> { public ValueCGVisitor(CodeGenerator cg) { super(cg); } @Override public Void visit(Arithmetic ast, Void param) { ast.getOperand_1().accept(this, null); cg.convert(ast.getOperand_1().getType(), ast.getType()); ast.getOperand_2().accept(this, null); cg.convert(ast.getOperand_2().getType(), ast.getType()); cg.arithmetic(ast.getOperator(), ast.getType()); return null; } @Override public Void visit(ArrayIndexing ast, Void param) { ast.accept(addressCGVisitor, null); cg.write("load" + ast.getType().suffix()); return null; } @Override public Void visit(Cast ast, Void param) { ast.getValue().accept(this, null); cg.convert(ast.getValue().getType(), ast.getDataType()); return null; } @Override public Void visit(CharLiteral ast, Void param) { cg.push(ast.getType().suffix(), (int) ast.getValue()); return null; } @Override public Void visit(IntLiteral ast, Void param) { cg.push(ast.getType().suffix(), ast.getValue()); return null; } @Override public Void visit(DoubleLiteral ast, Void param) { cg.push(ast.getType().suffix(), ast.getValue()); return null; } @Override public Void visit(Variable ast, Void param) { ast.accept(addressCGVisitor, null); cg.write("load" + ast.getType().suffix()); return null; } @Override public Void visit(Comparison ast, Void param) { ast.getOperand_1().accept(this, null); ast.getOperand_2().accept(this, null); cg.comparison(ast.getOperator(), ast.getType()); return null; } @Override public Void visit(Logical ast, Void param) { ast.getOperand_1().accept(this, null); ast.getOperand_2().accept(this, null); cg.logical(ast.getOperator(), ast.getType()); return null; } @Override public Void visit(Negation ast, Void param) { ast.getValue().accept(this, null); cg.write("not"); return null; } @Override public Void visit(UnaryMinus ast, Void param) { ast.getValue().accept(this, null); cg.push("i", -1); cg.write("muli"); return null; } @Override public Void visit(FieldAccess ast, Void param) { ast.accept(addressCGVisitor, null); cg.write("load" + ast.getType().suffix()); return null; } @Override public Void visit(FunctionInvocation ast, Void param) { ast.getArguments().forEach(arg -> arg.accept(valueCGVisitor, null)); cg.write("call " + ast.getFunctionName()); return null; } }