Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add unary negation #23

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/cup/parser.cup
Original file line number Diff line number Diff line change
Expand Up @@ -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; :}
;
33 changes: 33 additions & 0 deletions src/main/java/absyn/ExpNegate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package absyn;

import javaslang.collection.Tree;

import static org.bytedeco.javacpp.LLVM.*;

import static org.bytedeco.javacpp.LLVM.LLVMBuildFSub;
import static org.bytedeco.javacpp.LLVM.LLVMConstReal;
import static org.bytedeco.javacpp.LLVM.LLVMDoubleType;

/**
* Created by brenokeller 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 v_arg = arg.codegen(module, builder);
final LLVMValueRef zero = LLVMConstReal(LLVMDoubleType(), 0);
return LLVMBuildFSub(builder, zero, v_arg, "negatetmp");
}

@Override
public Tree.Node<String> toTree() {
return Tree.of("ExpNegate ", arg.toTree());
}
}