diff --git a/src/main/cup/parser.cup b/src/main/cup/parser.cup index 24eff22..b64e3b1 100644 --- a/src/main/cup/parser.cup +++ b/src/main/cup/parser.cup @@ -53,5 +53,6 @@ term ::= factor ::= LITNUM:x {: RESULT = new ExpNum(x); :} +| MINUS factor:x {: RESULT = new ExpNegate(x); :} | LPAREN exp:x RPAREN {: RESULT = x; :} ; diff --git a/src/main/java/absyn/ExpNegate.java b/src/main/java/absyn/ExpNegate.java new file mode 100644 index 0000000..fa79998 --- /dev/null +++ b/src/main/java/absyn/ExpNegate.java @@ -0,0 +1,28 @@ +package absyn; + +import javaslang.collection.Tree; +import static org.bytedeco.javacpp.LLVM.*; + +/** + * Created by mvosouza on 19.10.16. + */ +public class ExpNegate extends Exp{ + + public final Exp arg; + + public ExpNegate(Exp arg) { + this.arg = arg; + } + + @Override + public LLVMValueRef codegen(LLVMModuleRef module, LLVMBuilderRef builder) { + final LLVMValueRef v_arg = arg.codegen(module,builder); + final LLVMValueRef zero = LLVMConstReal(LLVMDoubleType(), 0); + return LLVMBuildFSub(builder, zero, v_arg, "subtmp"); + } + + @Override + public Tree.Node toTree() { + return Tree.of("ExpNegate", arg.toTree()); + } +}