Skip to content

Commit

Permalink
add commands impl
Browse files Browse the repository at this point in the history
  • Loading branch information
marcin-chwedczuk committed Nov 16, 2024
1 parent 196a41e commit 153122f
Show file tree
Hide file tree
Showing 9 changed files with 561 additions and 8 deletions.
29 changes: 29 additions & 0 deletions engine/src/main/java/mscalc/engine/commands/CBinaryCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package mscalc.engine.commands;

public class CBinaryCommand implements IBinaryCommand {
private int command;

public CBinaryCommand(int command) {
this.command = command;
}

@Override
public void setCommand(int command) {
this.command = command;
}

@Override
public int getCommand() {
return command;
}

@Override
public CommandType getCommandType() {
return CommandType.BinaryCommand;
}

@Override
public void accept(ISerializeCommandVisitor commandVisitor) {
commandVisitor.visit(this);
}
}
218 changes: 218 additions & 0 deletions engine/src/main/java/mscalc/engine/commands/COpndCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package mscalc.engine.commands;

import mscalc.engine.Rational;
import mscalc.engine.cpp.uint;
import mscalc.engine.ratpack.RatPack;
import mscalc.engine.ratpack.RatPack.NumberFormat;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import static mscalc.engine.Commands.*;

public class COpndCommand implements IOpndCommand {
private static final char chNegate = '-';
private static final char chExp = 'e';
private static final char chPlus = '+';

private final List<Integer> commands = new ArrayList<>();
boolean fNegative;
boolean fSciFmt;
boolean fDecimal;
boolean fInitialized;
private final StringBuilder token = new StringBuilder();
private Rational value;

public COpndCommand(List<Integer> commands, boolean fNegative, boolean fDecimal, boolean fSciFmt) {
this.commands.addAll(commands);
this.fNegative = fNegative;
this.fDecimal = fDecimal;
this.fSciFmt = fSciFmt;
this.fInitialized = false;
this.value = new Rational();
}

public void initialize(Rational r) {
this.value = r;
this.fInitialized = true;
}

@Override
public List<Integer> getCommands() {
return Collections.unmodifiableList(this.commands);
}

@Override
public void appendCommand(int command) {
if (fSciFmt)
{
ClearAllAndAppendCommand(CalculationManagerCommand.fromCommandInt(command));
}
else
{
commands.add(command);
}

if (command == IDC_PNT)
{
fDecimal = true;
}
}

void ClearAllAndAppendCommand(CalculationManagerCommand command)
{
commands.clear();
commands.add(command.toCommandInt());
fSciFmt = false;
fNegative = false;
fDecimal = false;
}

@Override
public void toggleSign() {
for (int nOpCode : commands)
{
if (nOpCode != IDC_0)
{
fNegative = !fNegative;
break;
}
}
}

@Override
public void removeFromEnd() {
if (fSciFmt)
{
ClearAllAndAppendCommand(CalculationManagerCommand.Command0);
}
else
{
int nCommands = commands.size();

if (nCommands == 1)
{
ClearAllAndAppendCommand(CalculationManagerCommand.Command0);
}
else
{
int nOpCode = commands.get(nCommands - 1);

if (nOpCode == IDC_PNT)
{
fDecimal = false;
}

commands.removeLast();
}
}
}

@Override
public boolean isNegative() {
return fNegative;
}

@Override
public boolean isSciFmt() {
return fSciFmt;
}

@Override
public boolean isDecimalPresent() {
return fDecimal;
}

@Override
public String getToken(char decimalSymbol) {
final char chZero = '0';

int nCommands = commands.size();
token.setLength(0);

for (int i = 0; i < nCommands; i++)
{
int nOpCode = commands.get(i);

if (nOpCode == IDC_PNT)
{
token.append(decimalSymbol);
}
else if (nOpCode == IDC_EXP)
{
token.append(chExp);
int nextOpCode = commands.get(i + 1);
if (nextOpCode != IDC_SIGN)
{
token.append(chPlus);
}
}
else if (nOpCode == IDC_SIGN)
{
token.append(chNegate);
}
else
{
char num = (char)(nOpCode - IDC_0);
token.append(num);
}
}

// Remove zeros
for (int i = 0; i < token.length(); i++)
{
if (token.charAt(i) != chZero)
{
if (token.charAt(i) == decimalSymbol)
{
// token.erase(0, i - 1);
token.delete(0, i-1);
}
else
{
// token.erase(0, i);
token.delete(0, i);
}

if (fNegative)
{
token.insert(0, chNegate);
}

return token.toString();
}
}

token.setLength(0);
token.append(chZero);

return token.toString();
}

@Override
public void setCommands(List<Integer> commands) {
this.commands.clear();
this.commands.addAll(commands);
}

@Override
public CommandType getCommandType() {
return CommandType.OperandCommand;
}

public String getString(uint radix, int precision)
{
if (fInitialized)
{
return value.toString(radix, NumberFormat.Float, precision);
}

return "";
}

@Override
public void accept(ISerializeCommandVisitor commandVisitor) {
commandVisitor.visit(this);
}
}
24 changes: 24 additions & 0 deletions engine/src/main/java/mscalc/engine/commands/CParentheses.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package mscalc.engine.commands;

public class CParentheses implements IParenthesisCommand {
private final int command;

public CParentheses(int command) {
this.command = command;
}

@Override
public int getCommand() {
return command;
}

@Override
public CommandType getCommandType() {
return CommandType.Parentheses;
}

@Override
public void accept(ISerializeCommandVisitor commandVisitor) {
commandVisitor.visit(this);
}
}
46 changes: 46 additions & 0 deletions engine/src/main/java/mscalc/engine/commands/CUnaryCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package mscalc.engine.commands;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CUnaryCommand implements IUnaryCommand {
private final List<Integer> command = new ArrayList<>();

public CUnaryCommand(int command) {
this.command.add(command);
}

public CUnaryCommand(int command1, int command2) {
this.command.add(command1);
this.command.add(command2);
}

@Override
public List<Integer> getCommands() {
return Collections.unmodifiableList(command);
}

@Override
public void setCommands(int command1, int command2) {
this.command.clear();
this.command.add(command1);
this.command.add(command2);
}

@Override
public void setCommand(int command) {
this.command.clear();
this.command.add(command);
}

@Override
public CommandType getCommandType() {
return CommandType.UnaryCommand;
}

@Override
public void accept(ISerializeCommandVisitor commandVisitor) {
commandVisitor.visit(this);
}
}
Loading

0 comments on commit 153122f

Please sign in to comment.