Permalink
Browse files

support for unsigned numeric arguments

  • Loading branch information...
1 parent 3315fa8 commit 29097eb564cc3f1ba1a086060bd3e49b7111fe2a @adh committed Feb 24, 2012
Showing with 40 additions and 2 deletions.
  1. +15 −2 dfsch/number.h
  2. +25 −0 src/number.c
View
@@ -73,6 +73,8 @@ extern "C" {
extern double dfsch_number_to_double(dfsch_object_t *n);
/** Returns value of given number as long. */
extern long dfsch_number_to_long(dfsch_object_t *n);
+ /** Returns value of given number as unsigned long. */
+ extern unsigned long dfsch_number_to_ulong(dfsch_object_t *n);
/** Returns value of given number as int64_t. */
extern int64_t dfsch_number_to_int64(dfsch_object_t *n);
extern uint64_t dfsch_number_to_uint64(dfsch_object_t *n);
@@ -167,10 +169,21 @@ dfsch_object_t* dfsch_number_lognot(dfsch_object_t* a);
#define DFSCH_LONG_ARG_OPT(al, name, default) \
DFSCH_GENERIC_ARG_OPT(al, name, default, long, dfsch_number_to_long)
+#define DFSCH_ULONG_ARG(al, name) \
+ DFSCH_GENERIC_ARG(al, name, unsigned long, dfsch_number_to_ulong)
+#define DFSCH_ULONG_ARG_OPT(al, name, default) \
+ DFSCH_GENERIC_ARG_OPT(al, name, default, unsigned long, dfsch_number_to_ulong)
+
+
#define DFSCH_INT64_ARG(al, name) \
- DFSCH_GENERIC_ARG(al, name, long, dfsch_number_to_int64)
+ DFSCH_GENERIC_ARG(al, name, int64_t, dfsch_number_to_int64)
#define DFSCH_INT64_ARG_OPT(al, name, default) \
- DFSCH_GENERIC_ARG_OPT(al, name, default, long, dfsch_number_to_int64)
+ DFSCH_GENERIC_ARG_OPT(al, name, default, int64_t, dfsch_number_to_int64)
+
+#define DFSCH_UINT64_ARG(al, name) \
+ DFSCH_GENERIC_ARG(al, name, uint64_t, dfsch_number_to_uint64)
+#define DFSCH_UINT64_ARG_OPT(al, name, default) \
+ DFSCH_GENERIC_ARG_OPT(al, name, default, uint64_t, dfsch_number_to_uint64)
View
@@ -547,6 +547,31 @@ long dfsch_number_to_long(dfsch_object_t *n){
dfsch_error("Not an integer", n);
}
+unsigned long dfsch_number_to_ulong(dfsch_object_t *n){
+ int64_t r;
+
+ if (DFSCH_TYPE_OF(n)==DFSCH_FIXNUM_TYPE){
+ if (DFSCH_FIXNUM_REF(n) < 0){
+ dfsch_error("Non-negative value expected", n);
+ }
+ return DFSCH_FIXNUM_REF(n);
+ } else if (DFSCH_TYPE_OF(n)==DFSCH_BIGNUM_TYPE){
+ if (!dfsch_bignum_to_int64((dfsch_bignum_t*)n, &r) ||
+ r > ULONG_MAX){
+ dfsch_error("Value too large", n);
+ }
+
+ if (r < 0){
+ dfsch_error("Non-negative value expected", n);
+ }
+
+
+ return r;
+ }
+ dfsch_error("Not an integer", n);
+}
+
+
int64_t dfsch_number_to_int64(dfsch_object_t *n){
int64_t r;

0 comments on commit 29097eb

Please sign in to comment.