feat: add input_number function
This commit is contained in:
parent
bcaf2cb3b7
commit
d562af9876
8 changed files with 61 additions and 11 deletions
|
@ -29,14 +29,16 @@ ToDo List:
|
|||
- [X] add modulus operator '%'
|
||||
- [ ] add support for multiple characters operators
|
||||
- [ ] add inclusive operators like '!=', '>=', '<='
|
||||
- [ ] add input_number() std function
|
||||
- [ ] add support for priority operators
|
||||
- [X] add input_number() std function
|
||||
- [ ] add type() std function
|
||||
- [ ] add ceil() and floor() std functions
|
||||
- [X] base of the CLI
|
||||
- [ ] evaluate expression from stdin
|
||||
- [X] read a file
|
||||
- [X] if statements
|
||||
- [ ] while statements (with break and continue)
|
||||
- [X] while statements (with break and continue)
|
||||
- [ ] repeat statement
|
||||
- [ ] add functions support
|
||||
- [ ] add config header file
|
||||
- [ ] ability to modify keywords and customize the lang
|
||||
|
@ -141,6 +143,8 @@ abs(nb)
|
|||
sqrt,sin,cos,exp,ln,log etc.
|
||||
print_number(nb)
|
||||
input_number()
|
||||
ceil(nb)
|
||||
floor(nb)
|
||||
random_int(min, max)
|
||||
random_float(min, max)
|
||||
type(var) -> return the type of a var as int
|
||||
|
|
|
@ -2,5 +2,4 @@
|
|||
set a to input_number()
|
||||
set b to input_number()
|
||||
set res to a+b
|
||||
|
||||
print_int(res)
|
||||
print_number(res)
|
||||
|
|
|
@ -1,2 +1,3 @@
|
|||
# test modulus operator
|
||||
print_number(20.2 % 10)
|
||||
set a to input_number()
|
||||
print_number(a)
|
||||
|
||||
|
|
|
@ -623,6 +623,10 @@ int evaluate(struct StateContainer* state, char* inputStr, int* resultPtr, unsig
|
|||
int typeRes = list_get_type(&evalList, 0);
|
||||
*typePtr = typeRes;
|
||||
list_get(&evalList, 0, resultPtr);
|
||||
if (EVALUATOR_DEBUG_LEVEL >= 2) {
|
||||
printf("End of evaluation, dumping evalList: \n");
|
||||
list_print(&evalList);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
31
src/funcs.c
31
src/funcs.c
|
@ -80,6 +80,36 @@ int print_newline_impl(int* res, unsigned char* resType, unsigned char* types, i
|
|||
return 0;
|
||||
}
|
||||
|
||||
int input_number_impl(int* res, unsigned char* resType, unsigned char* types, int* args)
|
||||
{
|
||||
printf("? ");
|
||||
|
||||
char* line;
|
||||
size_t len = 0;
|
||||
size_t lineSize = 0;
|
||||
|
||||
lineSize = getline(&line, &len, stdin);
|
||||
|
||||
// printf("len=%d, lineSize=%d '%s' \n", len, lineSize, line);
|
||||
|
||||
char* toParse[lineSize+1];
|
||||
str_extract(toParse, line, 0, lineSize-1);
|
||||
|
||||
int st = parse_int(toParse, res);
|
||||
if (st == 0) {
|
||||
*resType = TYPE_INT;
|
||||
return 0;
|
||||
}
|
||||
if (st != 0) {
|
||||
st = parse_float(toParse, (float*) res);
|
||||
if (st == 0) {
|
||||
*resType = TYPE_FLOAT;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int simple_float_func(float (*func)(float), int* res, unsigned char* resType, unsigned char* types, int* args)
|
||||
{
|
||||
float x = 0;
|
||||
|
@ -252,6 +282,7 @@ struct FuncIntro intros[] = {
|
|||
{"print_number", &print_number_impl, 1},
|
||||
{"print_ascii", &print_ascii_impl, 1},
|
||||
{"print_newline", &print_newline_impl, 0},
|
||||
{"input_number", &input_number_impl, 0},
|
||||
{"", 0, 0}
|
||||
};
|
||||
|
||||
|
|
|
@ -31,7 +31,7 @@ int parse_clean_positive_integer(int inputStrLen, char* inputStr, int* result) {
|
|||
|
||||
int parse_int(char* inputStr, int* resultPtr) {
|
||||
int i = 0;
|
||||
char cleanStr[strlen(inputStr)];
|
||||
char cleanStr[strlen(inputStr)+1];
|
||||
int cleanStrLen = 0;
|
||||
int hasNegSign = 0;
|
||||
while (inputStr[i] != 0) {
|
||||
|
@ -61,7 +61,11 @@ int parse_int(char* inputStr, int* resultPtr) {
|
|||
//printf("clean str: %s/ \n", cleanStr);
|
||||
|
||||
int inter = 0;
|
||||
parse_clean_positive_integer(cleanStrLen, cleanStr, &inter);
|
||||
byte stat = parse_clean_positive_integer(cleanStrLen, cleanStr, &inter);
|
||||
if (NB_PARSING_DEBUG_LEVEL >= 2) {
|
||||
printf("Parse clean positive integer stat: %d\n", stat);
|
||||
printf("Got value: %d\n", inter);
|
||||
}
|
||||
|
||||
*resultPtr = inter;
|
||||
if (hasNegSign) {
|
||||
|
@ -72,11 +76,12 @@ int parse_int(char* inputStr, int* resultPtr) {
|
|||
}
|
||||
|
||||
int parse_float(char* inputStr, float* resultPtr) {
|
||||
if (NB_PARSING_DEBUG_LEVEL >= 2) printf("Parsing as float: '%s'\n", inputStr);
|
||||
int i = 0;
|
||||
char cleanStrIntPart[strlen(inputStr)];
|
||||
char cleanStrIntPart[strlen(inputStr)+1];
|
||||
int cleanStrIntPartLen = 0;
|
||||
|
||||
char cleanStrFloatPart[strlen(inputStr)];
|
||||
char cleanStrFloatPart[strlen(inputStr)+1];
|
||||
int cleanStrFloatPartLen = 0;
|
||||
|
||||
int part = 0; // 0 for first part, 1 for snd part
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
#ifndef NB_PARSING_H_
|
||||
#define NB_PARSING_H_
|
||||
|
||||
#define NB_PARSING_DEBUG_LEVEL 0
|
||||
|
||||
int is_char_numeral(char candidate);
|
||||
int integer_pow(int base, int exponent);
|
||||
int parse_clean_positive_integer(int inputStrLen, char* inputStr, int* result);
|
||||
|
|
|
@ -69,6 +69,10 @@ 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, "random_int(1, 100)", &resVal, &resType);
|
||||
assert(resType == TYPE_INT);
|
||||
printf(" - random int: %d \n", resVal);
|
||||
|
|
Loading…
Reference in a new issue