diff --git a/eplan.iml b/eplan.iml new file mode 100644 index 0000000..7de21c8 --- /dev/null +++ b/eplan.iml @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/main/cup/parser.cup b/src/main/cup/parser.cup index 24eff22..31736c1 100644 --- a/src/main/cup/parser.cup +++ b/src/main/cup/parser.cup @@ -52,6 +52,7 @@ term ::= ; factor ::= - LITNUM:x {: RESULT = new ExpNum(x); :} -| LPAREN exp:x RPAREN {: RESULT = x; :} -; + LITNUM:x {: RESULT = new ExpNum(x); :} + | MINUS factor:x {: RESULT = new ExpNegate(x); :} + | LPAREN exp:x RPAREN {: RESULT = x; :} + ; \ No newline at end of file diff --git a/src/main/java/absyn/ExpNegate.java b/src/main/java/absyn/ExpNegate.java new file mode 100644 index 0000000..90579fc --- /dev/null +++ b/src/main/java/absyn/ExpNegate.java @@ -0,0 +1,30 @@ +package absyn; + + +import javaslang.collection.Tree; +import static org.bytedeco.javacpp.LLVM.*; + +/** + * Created by christian on 10/20/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()); + } +} \ No newline at end of file