Skip to content

Commit

Permalink
Implement integer? predicate
Browse files Browse the repository at this point in the history
  • Loading branch information
predatorray committed Mar 15, 2016
1 parent 0248e39 commit 448ae8a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package me.predatorray.bud.lisp.buitin;

import me.predatorray.bud.lisp.buitin.arithmetic.AddFunction;
import me.predatorray.bud.lisp.buitin.arithmetic.IntegerPredicate;
import me.predatorray.bud.lisp.buitin.arithmetic.MonoDecreasingPredicate;
import me.predatorray.bud.lisp.buitin.arithmetic.MonoIncreasingPredicate;
import me.predatorray.bud.lisp.buitin.arithmetic.MonoNonDecreasingPredicate;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class BuiltinsEnvironment {
initial.put(">", new MonoDecreasingPredicate());
initial.put(">=", new MonoNonIncreasingPredicate());
initial.put("<=", new MonoNonDecreasingPredicate());
initial.put("integer?", new IntegerPredicate());

initial.put("null-environment", new NullEnvironmentFunction());
initial.put("builtins-environment", new BuiltinsEnvironmentFunction());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package me.predatorray.bud.lisp.buitin.arithmetic;

import me.predatorray.bud.lisp.buitin.Predicate;
import me.predatorray.bud.lisp.evaluator.EvaluatingException;
import me.predatorray.bud.lisp.lang.BudNumber;
import me.predatorray.bud.lisp.lang.BudObject;
import me.predatorray.bud.lisp.lang.BudType;
import me.predatorray.bud.lisp.lang.FunctionType;

import java.math.BigDecimal;
import java.util.List;

public class IntegerPredicate extends Predicate {

private final FunctionType thisType = new FunctionType(this);

@Override
protected void checkArgumentTypes(List<BudType> argumentTypes) {
if (argumentTypes.size() != 1) {
throw new EvaluatingException("requires one argument");
}
}

@Override
protected boolean predicate(List<BudObject> arguments) {
BudObject arg = arguments.get(0);
if (!BudType.NUMBER.equals(arg.getType())) {
return false;
}
BudNumber number = (BudNumber) arg;
BigDecimal decimal = number.getValue();
return decimal.scale() <= 0;
}

@Override
public FunctionType getType() {
return thisType;
}
}

0 comments on commit 448ae8a

Please sign in to comment.