transcendental-lisp/src/sexpression/LispNumber.java

51 lines
1.0 KiB
Java

package sexpression;
import java.text.MessageFormat;
import error.LispException;
public class LispNumber extends Atom {
private int value;
public LispNumber(String text) {
super(text.replaceFirst("^0+(?!$)", ""));
try {
this.value = Integer.parseInt(text);
} catch (NumberFormatException e) {
throw new InvalidNumberException(text);
}
}
public LispNumber(int value) {
super(Integer.toString(value));
this.value = value;
}
public boolean numberp() {
return true;
}
public int getValue() {
return value;
}
public class InvalidNumberException extends LispException {
private static final long serialVersionUID = 1L;
private String text;
public InvalidNumberException(String text) {
this.text = text;
}
@Override
public String getMessage() {
return MessageFormat.format("{0} is not a valid integer", text);
}
}
}