From 15e33edcf42f3a91166905fd5effdd5a78109150 Mon Sep 17 00:00:00 2001 From: Luiz Janeiro Date: Wed, 19 Oct 2016 15:07:00 -0200 Subject: [PATCH 1/2] add_minus_unary --- src/main/cup/parser.cup | 1 + src/main/java/absyn/ExpNegate.java | 28 ++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 src/main/java/absyn/ExpNegate.java diff --git a/src/main/cup/parser.cup b/src/main/cup/parser.cup index 24eff22..ed6729a 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..c8b0a58 --- /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 luiz on 10/19/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 zero = LLVMConstReal(LLVMDoubleType(), 0); + final LLVMValueRef v_arg = arg.codegen(module, builder); + + return LLVMBuildFSub(builder, zero, v_arg, "negtmp"); + } + + @Override + public Tree.Node toTree() { + return Tree.of("ExpNegate", arg.toTree()); + } +} From b3313c3fa9cfaba7083ca3dba5424afbeea81c25 Mon Sep 17 00:00:00 2001 From: Luiz Janeiro Date: Wed, 19 Oct 2016 16:01:24 -0200 Subject: [PATCH 2/2] minus_unary --- eplan.iml | 32 ++++++++++++++++++++++++++++++++ test1.dot | 7 +++++++ 2 files changed, 39 insertions(+) create mode 100644 eplan.iml create mode 100644 test1.dot diff --git a/eplan.iml b/eplan.iml new file mode 100644 index 0000000..728b489 --- /dev/null +++ b/eplan.iml @@ -0,0 +1,32 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/test1.dot b/test1.dot new file mode 100644 index 0000000..a1f01be --- /dev/null +++ b/test1.dot @@ -0,0 +1,7 @@ +digraph Tree { + lbl0 [label="ExpBinOp: PLUS"]; + lbl1 [label="ExpNum: 22.0"]; + lbl2 [label="ExpNum: 8765.0"]; + lbl0 -> lbl1; + lbl0 -> lbl2; +}