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

simplified null checks #12

Merged
merged 1 commit into from
Jan 2, 2024
Merged
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
15 changes: 15 additions & 0 deletions Assembler/src/main/java/org/eastars/asm/utilities/Utilities.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package org.eastars.asm.utilities;

import java.util.function.Consumer;

public class Utilities {

public static <V> boolean ifNotNull(V value, Consumer<V> consumer) {
if (value != null) {
consumer.accept(value);
return true;
}
return false;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,19 @@
import org.eastars.asm.ast.Instruction;
import org.eastars.asm.ast.InstructionLine;

import static org.eastars.asm.utilities.Utilities.ifNotNull;

@Getter
@AllArgsConstructor
public class AssemblerLineVisitor {

private AbstractParseTreeVisitor<? extends Instruction> instructionVisitor;

public AssemblerLine visitAssemblerLine(TerminalNode label, ParserRuleContext instruction, TerminalNode comment) {
if (label == null && instruction == null && comment == null) {
return null;
}
InstructionLine line = new InstructionLine();

if (label != null) {
line.setLabel(label.getText());
}
if (instruction != null) {
line.setInstruction(getInstructionVisitor().visit(instruction));
}
if (comment != null) {
line.setComment(comment.getText());
}

return line;
return ifNotNull(label, l -> line.setLabel(l.getText()))
| ifNotNull(instruction, i -> line.setInstruction(getInstructionVisitor().visit(i)))
| ifNotNull(comment, c -> line.setComment(c.getText())) ? line : null;
}

}