From 5863aab9f281ccdeeb97d336e2b809d181c18eac Mon Sep 17 00:00:00 2001 From: Alex Kotov Date: Sat, 6 May 2023 15:05:33 +0400 Subject: [PATCH] type number --- src/builtins.c | 20 ++++++++++---------- src/main-test.c | 44 ++++++++++++++++++++++---------------------- src/object.c | 4 ++-- src/object.h | 12 ++++++++++-- 4 files changed, 44 insertions(+), 36 deletions(-) diff --git a/src/builtins.c b/src/builtins.c index 357b37f..005cbff 100644 --- a/src/builtins.c +++ b/src/builtins.c @@ -318,7 +318,7 @@ struct Object *func_number_TO_string( if (args_count == 2) { struct Object *radix_obj = args_array[1]; assert(Object_is_number(radix_obj)); - radix = radix_obj->i; + radix = radix_obj->number.i64; } char buffer[70]; @@ -328,13 +328,13 @@ struct Object *func_number_TO_string( assert(0); // TODO break; case 8: - snprintf(buffer, 70, "%lo", z->i); + snprintf(buffer, 70, "%lo", z->number.i64); break; case 10: - snprintf(buffer, 70, "%ld", z->i); + snprintf(buffer, 70, "%ld", z->number.i64); break; case 16: - snprintf(buffer, 70, "%lx", z->i); + snprintf(buffer, 70, "%lx", z->number.i64); break; default: assert(0); @@ -394,7 +394,7 @@ struct Object *func_EQ( struct Object *const cur = args_array[index]; assert(Object_is_number(cur)); - if (cur->i != first->i) { + if (cur->number.i64 != first->number.i64) { result->boolean = false; break; } @@ -413,11 +413,11 @@ struct Object *func_PLUS( struct Object *const arg = args_array[0]; assert(Object_is_number(arg)); - object->i = arg->i; + object->number.i64 = arg->number.i64; if (args_count == 1) return object; struct Object *const b_sum = func_PLUS(args_count - 1, &args_array[1]); - object->i += b_sum->i; + object->number.i64 += b_sum->number.i64; return object; } @@ -429,12 +429,12 @@ struct Object *func_MINUS( struct Object *const arg = args_array[0]; assert(Object_is_number(arg)); - struct Object *const object = Object_new_number(arg->i); + struct Object *const object = Object_new_number(arg->number.i64); if (args_count >= 2) { struct Object *const sum = func_PLUS(args_count - 1, &args_array[1]); assert(Object_is_number(sum)); - object->i -= sum->i; + object->number.i64 -= sum->number.i64; } return object; @@ -482,7 +482,7 @@ struct Object *func_display( printf("%s", object->s); break; case TYPE_NUMBER: - printf("%li", object->i); + printf("%li", object->number.i64); break; case TYPE_STRING: printf("\"%s\"", object->s); diff --git a/src/main-test.c b/src/main-test.c index 4c7c0d2..39c7ce8 100644 --- a/src/main-test.c +++ b/src/main-test.c @@ -126,19 +126,19 @@ void test_begin() // 123 result = eval_str("(begin 123)"); assert(Object_is_number(result)); - assert(result->i == 123); + assert(result->number.i64 == 123); // (begin 123 456) // 456 result = eval_str("(begin 123 456)"); assert(Object_is_number(result)); - assert(result->i == 456); + assert(result->number.i64 == 456); // (begin 123 456 789) // 789 result = eval_str("(begin 123 456 789)"); assert(Object_is_number(result)); - assert(result->i == 789); + assert(result->number.i64 == 789); } void test_if() @@ -148,17 +148,17 @@ void test_if() // (if #true 123 456) result = eval_str("(if #true 123 456)"); assert(Object_is_number(result)); - assert(result->i == 123); + assert(result->number.i64 == 123); // (if "foo" 123 456) result = eval_str("(if \"foo\" 123 456)"); assert(Object_is_number(result)); - assert(result->i == 123); + assert(result->number.i64 == 123); // (if #false 123 456) result = eval_str("(if #false 123 456)"); assert(Object_is_number(result)); - assert(result->i == 456); + assert(result->number.i64 == 456); } /******************************** @@ -353,7 +353,7 @@ void test_car() // 123 result = eval_str("(car (cons 123 456))"); assert(Object_is_number(result)); - assert(result->i == 123); + assert(result->number.i64 == 123); } void test_cdr() @@ -364,7 +364,7 @@ void test_cdr() // 456 result = eval_str("(cdr (cons 123 456))"); assert(Object_is_number(result)); - assert(result->i == 456); + assert(result->number.i64 == 456); } void test_list() @@ -380,7 +380,7 @@ void test_list() result = eval_str("(list 123)"); assert(Object_is_pair(result)); assert(Object_is_number(result->pair.car)); - assert(result->pair.car->i == 123); + assert(result->pair.car->number.i64 == 123); assert(OBJECT_IS_NULL(result->pair.cdr)); // (list 123 456) @@ -388,10 +388,10 @@ void test_list() result = eval_str("(list 123 456)"); assert(Object_is_pair(result)); assert(Object_is_number(result->pair.car)); - assert(result->pair.car->i == 123); + assert(result->pair.car->number.i64 == 123); assert(Object_is_pair(result->pair.cdr)); assert(Object_is_number(result->pair.cdr->pair.car)); - assert(result->pair.cdr->pair.car->i == 456); + assert(result->pair.cdr->pair.car->number.i64 == 456); assert(OBJECT_IS_NULL(result->pair.cdr->pair.cdr)); } @@ -914,31 +914,31 @@ void test_PLUS() // 0 result = eval_str("(+)"); assert(Object_is_number(result)); - assert(result->i == 0); + assert(result->number.i64 == 0); // (+ 123) // 123 result = eval_str("(+ 123)"); assert(Object_is_number(result)); - assert(result->i == 123); + assert(result->number.i64 == 123); // (+ 1 10) // 11 result = eval_str("(+ 1 10)"); assert(Object_is_number(result)); - assert(result->i == 11); + assert(result->number.i64 == 11); // (+ 1 10 100) // 111 result = eval_str("(+ 1 10 100)"); assert(Object_is_number(result)); - assert(result->i == 111); + assert(result->number.i64 == 111); // (+ 1 10 100 1000) // 1111 result = eval_str("(+ 1 10 100 1000)"); assert(Object_is_number(result)); - assert(result->i == 1111); + assert(result->number.i64 == 1111); } void test_MINUS() @@ -949,35 +949,35 @@ void test_MINUS() // 0 result = eval_str("(- 0)"); assert(Object_is_number(result)); - assert(result->i == 0); + assert(result->number.i64 == 0); // (- 123) // 123 result = eval_str("(- 123)"); assert(Object_is_number(result)); - assert(result->i == 123); + assert(result->number.i64 == 123); // (- 100 1) // 99 result = eval_str("(- 100 1)"); assert(Object_is_number(result)); - assert(result->i == 99); + assert(result->number.i64 == 99); // (- 100 1 2) // 97 result = eval_str("(- 100 1 2)"); assert(Object_is_number(result)); - assert(result->i == 97); + assert(result->number.i64 == 97); // (- 100 1 2 3) // 94 result = eval_str("(- 100 1 2 3)"); assert(Object_is_number(result)); - assert(result->i == 94); + assert(result->number.i64 == 94); // (- 100 1 2 3 4) // 90 result = eval_str("(- 100 1 2 3 4)"); assert(Object_is_number(result)); - assert(result->i == 90); + assert(result->number.i64 == 90); } diff --git a/src/object.c b/src/object.c index 5a88b28..32252e0 100644 --- a/src/object.c +++ b/src/object.c @@ -87,10 +87,10 @@ struct Object *Object_new_string(const char *const s) return object; } -struct Object *Object_new_number(const int64_t i) +struct Object *Object_new_number(const int64_t i64) { struct Object *const object = new(TYPE_NUMBER); - object->i = i; + object->number.i64 = i64; return object; } diff --git a/src/object.h b/src/object.h index c182a9e..b6ad4dc 100644 --- a/src/object.h +++ b/src/object.h @@ -37,6 +37,14 @@ struct Pair { struct Object *car, *cdr; }; +/********** + * Number * + **********/ + +struct Number { + int64_t i64; +}; + /********** * Object * **********/ @@ -55,7 +63,7 @@ struct Object { // For SYMBOL, STRING char *s; // For NUMBER - int64_t i; + struct Number number; }; }; @@ -70,7 +78,7 @@ struct Object *Object_new_boolean(bool boolean); struct Object *Object_new_char(char chr); struct Object *Object_new_symbol(const char *s); struct Object *Object_new_string(const char *s); -struct Object *Object_new_number(int64_t i); +struct Object *Object_new_number(int64_t i64); struct Object *Object_build_list(int count, ...);