string to float fixed. stupid mistake
This commit is contained in:
49
lexer.c
49
lexer.c
@@ -6,7 +6,6 @@
|
|||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
|
|
||||||
|
|
||||||
typedef struct{
|
typedef struct{
|
||||||
char* mstr;
|
char* mstr;
|
||||||
} mstring;
|
} mstring;
|
||||||
@@ -24,6 +23,13 @@ int str_to_int(char *strint){
|
|||||||
return new_int;
|
return new_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
float str_to_float(char *strif){
|
||||||
|
char *fptr;
|
||||||
|
float new_int = strtof(strif, &fptr);
|
||||||
|
return new_int;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
typedef enum{
|
typedef enum{
|
||||||
TOKEN_PLUS,
|
TOKEN_PLUS,
|
||||||
@@ -278,7 +284,6 @@ char* token_type_to_string(symbols type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void main2() {
|
void main2() {
|
||||||
Token newtok;
|
|
||||||
char* input = "323.23 + Hello world 102102";
|
char* input = "323.23 + Hello world 102102";
|
||||||
int length1 = strlen(input);
|
int length1 = strlen(input);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
@@ -292,17 +297,33 @@ void main2() {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
void astparser(const char* input) {
|
void mathparser(const char* input) {
|
||||||
TokenArr stack = tokenize_all(input);
|
TokenArr stack = tokenize_all(input);
|
||||||
int result = 0;
|
float result = 0;
|
||||||
int current = 0;
|
float current = 0;
|
||||||
int sign = 1;
|
float sign = 1;
|
||||||
int op = 0;
|
float op = 0;
|
||||||
|
|
||||||
for (size_t i = 0; i < stack.size; ++i) {
|
for (size_t i = 0; i < stack.size; ++i) {
|
||||||
switch (stack.unit[i].type) {
|
switch (stack.unit[i].type) {
|
||||||
case TOKEN_INTEGER: {
|
case TOKEN_INTEGER:
|
||||||
int value = str_to_int(stack.unit[i].text);
|
{
|
||||||
|
float value = str_to_float(stack.unit[i].text);
|
||||||
|
if (op == 1) {
|
||||||
|
current *= value;
|
||||||
|
op = 0;
|
||||||
|
} else if (op == 2) {
|
||||||
|
current /= value;
|
||||||
|
op = 0;
|
||||||
|
} else {
|
||||||
|
current = value;
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
case TOKEN_FLOAT:
|
||||||
|
{
|
||||||
|
float value = str_to_float(stack.unit[i].text);
|
||||||
if (op == 1) {
|
if (op == 1) {
|
||||||
current *= value;
|
current *= value;
|
||||||
op = 0;
|
op = 0;
|
||||||
@@ -334,8 +355,8 @@ void astparser(const char* input) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
result += sign * current; // add the last term
|
result += sign * current;
|
||||||
printf("%d\n", result);
|
printf("%f\n", result);
|
||||||
for (size_t j = 0; j < stack.size; ++j) {
|
for (size_t j = 0; j < stack.size; ++j) {
|
||||||
free(stack.unit[j].text);
|
free(stack.unit[j].text);
|
||||||
}
|
}
|
||||||
@@ -373,7 +394,7 @@ int main4() {
|
|||||||
|
|
||||||
|
|
||||||
int main(){
|
int main(){
|
||||||
char* input = "3*69+3";
|
char* input = "40/2.3 * 10 + 400";
|
||||||
printf("input: %s\n\n", input);
|
printf("input: %s\n", input);
|
||||||
astparser(input);
|
mathparser(input);
|
||||||
}
|
}
|
||||||
|
|||||||
3
nob.c
3
nob.c
@@ -4,7 +4,7 @@ int main(void){
|
|||||||
nb_arr cmd;
|
nb_arr cmd;
|
||||||
|
|
||||||
nb_append(&cmd, "gcc");
|
nb_append(&cmd, "gcc");
|
||||||
//nb_append(&cmd, "-Wall -Wextra");
|
nb_append(&cmd, "-Wall -Wextra");
|
||||||
nb_append(&cmd, "lexer.c");
|
nb_append(&cmd, "lexer.c");
|
||||||
nb_append(&cmd, "-o lex");
|
nb_append(&cmd, "-o lex");
|
||||||
|
|
||||||
@@ -19,5 +19,4 @@ int main(void){
|
|||||||
nb_print_info(&cmd);
|
nb_print_info(&cmd);
|
||||||
|
|
||||||
nb_cmd(&cmd);
|
nb_cmd(&cmd);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user