diff --git a/README.md b/README.md index 11722cb..33c9119 100644 --- a/README.md +++ b/README.md @@ -32,7 +32,7 @@ ToDo List: - [ ] add support for priority operators - [X] add input_number() std function - [ ] add type() std function -- [ ] add ceil() and floor() std functions +- [X] add ceil() and floor() std functions - [X] base of the CLI - [ ] evaluate expression from stdin - [X] read a file @@ -48,6 +48,7 @@ ToDo List: - [ ] add fully features strings support - [ ] add [classic problem solving](https://rosettacode.org) with code examples - [ ] add Web Assembly support and publish a demo website +- [ ] refactor: remove inconsistency on how functions returns error codes ## Installation diff --git a/sandbox.c b/sandbox.c index e755b64..f6b7ce1 100644 --- a/sandbox.c +++ b/sandbox.c @@ -10,9 +10,13 @@ #include int main () { - struct VariableStore* store = var_store_init(); - printf("%d", var_store_hash_name(store, "var")); + float ex = 2.00001430511; + printf("as bin %d \n", (int) ex); + return 0; + // struct VariableStore* store = var_store_init(); + // printf("%d", var_store_hash_name(store, "var")); + // return 0; // struct List l1; // short val = 17; diff --git a/src/funcs.c b/src/funcs.c index 8249449..af89baf 100644 --- a/src/funcs.c +++ b/src/funcs.c @@ -28,6 +28,33 @@ int abs_impl(int* res, unsigned char* resType, unsigned char* types, int* args) return 1; } +int floor_impl(int* res, unsigned char* resType, unsigned char* types, int* args) +{ + *resType = TYPE_INT; + if (types[0] == TYPE_INT) { + *res = args[0]; + return 0; + } + if (types[0] == TYPE_FLOAT) { + float val = get_float_from_int_rep(args[0]); + + *res = (int) val; + return 0; + } + return 1; +} + +int ceil_impl(int* res, unsigned char* resType, unsigned char* types, int* args) +{ + int resInterm = 0; + if (floor_impl(&resInterm, resType, types, args)) { + return 1; + } + resInterm += 1; + *res = resInterm; + return 0; +} + int print_number_impl(int* res, unsigned char* resType, unsigned char* types, int* args) { *resType = TYPE_INT; @@ -277,6 +304,8 @@ struct FuncIntro intros[] = { {"random_int", &random_int_impl, 2}, {"max", &max_impl, 2}, + {"floor", &floor_impl, 1}, + {"ceil", &ceil_impl, 1}, {"get_pi", &get_pi_impl, 0}, {"print_number", &print_number_impl, 1}, diff --git a/tests/test_evaluation.c b/tests/test_evaluation.c index 3391706..b9ab7e6 100644 --- a/tests/test_evaluation.c +++ b/tests/test_evaluation.c @@ -69,9 +69,17 @@ void test_evaluation() assert(resType == TYPE_FLOAT); assert(float_almost_equal(1, get_float_from_int_rep(resVal))); - evaluate(state, "get_pi()", &resVal, &resType); - assert(resType == TYPE_FLOAT); - assert(float_almost_equal(3.14159, get_float_from_int_rep(resVal))); + evaluate(state, "ceil(1.5)", &resVal, &resType); + assert(resType == TYPE_INT); + assert(2 == resVal); + + evaluate(state, "floor(1.5)", &resVal, &resType); + assert(resType == TYPE_INT); + assert(1 == resVal); + + evaluate(state, "floor(1.001)", &resVal, &resType); + assert(resType == TYPE_INT); + assert(1 == resVal); evaluate(state, "random_int(1, 100)", &resVal, &resType); assert(resType == TYPE_INT);