feat: add ceil and floor funcs
This commit is contained in:
parent
d562af9876
commit
547c5d0c63
4 changed files with 48 additions and 6 deletions
|
@ -32,7 +32,7 @@ ToDo List:
|
||||||
- [ ] add support for priority operators
|
- [ ] add support for priority operators
|
||||||
- [X] add input_number() std function
|
- [X] add input_number() std function
|
||||||
- [ ] add type() std function
|
- [ ] add type() std function
|
||||||
- [ ] add ceil() and floor() std functions
|
- [X] add ceil() and floor() std functions
|
||||||
- [X] base of the CLI
|
- [X] base of the CLI
|
||||||
- [ ] evaluate expression from stdin
|
- [ ] evaluate expression from stdin
|
||||||
- [X] read a file
|
- [X] read a file
|
||||||
|
@ -48,6 +48,7 @@ ToDo List:
|
||||||
- [ ] add fully features strings support
|
- [ ] add fully features strings support
|
||||||
- [ ] add [classic problem solving](https://rosettacode.org) with code examples
|
- [ ] add [classic problem solving](https://rosettacode.org) with code examples
|
||||||
- [ ] add Web Assembly support and publish a demo website
|
- [ ] add Web Assembly support and publish a demo website
|
||||||
|
- [ ] refactor: remove inconsistency on how functions returns error codes
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
|
|
|
@ -10,9 +10,13 @@
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
int main () {
|
int main () {
|
||||||
struct VariableStore* store = var_store_init();
|
float ex = 2.00001430511;
|
||||||
printf("%d", var_store_hash_name(store, "var"));
|
printf("as bin %d \n", (int) ex);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
// struct VariableStore* store = var_store_init();
|
||||||
|
// printf("%d", var_store_hash_name(store, "var"));
|
||||||
|
// return 0;
|
||||||
// struct List l1;
|
// struct List l1;
|
||||||
|
|
||||||
// short val = 17;
|
// short val = 17;
|
||||||
|
|
29
src/funcs.c
29
src/funcs.c
|
@ -28,6 +28,33 @@ int abs_impl(int* res, unsigned char* resType, unsigned char* types, int* args)
|
||||||
return 1;
|
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)
|
int print_number_impl(int* res, unsigned char* resType, unsigned char* types, int* args)
|
||||||
{
|
{
|
||||||
*resType = TYPE_INT;
|
*resType = TYPE_INT;
|
||||||
|
@ -277,6 +304,8 @@ struct FuncIntro intros[] = {
|
||||||
{"random_int", &random_int_impl, 2},
|
{"random_int", &random_int_impl, 2},
|
||||||
|
|
||||||
{"max", &max_impl, 2},
|
{"max", &max_impl, 2},
|
||||||
|
{"floor", &floor_impl, 1},
|
||||||
|
{"ceil", &ceil_impl, 1},
|
||||||
{"get_pi", &get_pi_impl, 0},
|
{"get_pi", &get_pi_impl, 0},
|
||||||
|
|
||||||
{"print_number", &print_number_impl, 1},
|
{"print_number", &print_number_impl, 1},
|
||||||
|
|
|
@ -69,9 +69,17 @@ void test_evaluation()
|
||||||
assert(resType == TYPE_FLOAT);
|
assert(resType == TYPE_FLOAT);
|
||||||
assert(float_almost_equal(1, get_float_from_int_rep(resVal)));
|
assert(float_almost_equal(1, get_float_from_int_rep(resVal)));
|
||||||
|
|
||||||
evaluate(state, "get_pi()", &resVal, &resType);
|
evaluate(state, "ceil(1.5)", &resVal, &resType);
|
||||||
assert(resType == TYPE_FLOAT);
|
assert(resType == TYPE_INT);
|
||||||
assert(float_almost_equal(3.14159, get_float_from_int_rep(resVal)));
|
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);
|
evaluate(state, "random_int(1, 100)", &resVal, &resType);
|
||||||
assert(resType == TYPE_INT);
|
assert(resType == TYPE_INT);
|
||||||
|
|
Loading…
Reference in a new issue