Fixed many minor things, tested it on main and is amazing, i think i need to start adding fractions next so further testing can be done with more powerful operators
This commit is contained in:
@@ -19,7 +19,6 @@ include_directories(include)
|
|||||||
add_library(calculator_lib
|
add_library(calculator_lib
|
||||||
src/lexer.c
|
src/lexer.c
|
||||||
src/parser.c
|
src/parser.c
|
||||||
src/ast.c
|
|
||||||
src/evaluator.c
|
src/evaluator.c
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -4,6 +4,6 @@
|
|||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
uint64_t evaluate(ASTNode *tree);
|
int64_t evaluate(ASTNode *tree);
|
||||||
|
|
||||||
#endif // !EVALUATOR_H
|
#endif // !EVALUATOR_H
|
||||||
|
|||||||
@@ -2,17 +2,17 @@
|
|||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
uint64_t evaluate(ASTNode *tree) {
|
int64_t evaluate(ASTNode *tree) {
|
||||||
if (tree->type == NODE_BINARY_OP) {
|
if (tree->type == NODE_BINARY_OP) {
|
||||||
switch (tree->data.binary.op) {
|
switch (tree->data.binary.op) {
|
||||||
case OP_ADD:
|
case OP_ADD:
|
||||||
return evaluate(tree->data.binary.left) + evaluate(tree->data.binary.left);
|
return evaluate(tree->data.binary.left) + evaluate(tree->data.binary.right);
|
||||||
case OP_SUB:
|
case OP_SUB:
|
||||||
return evaluate(tree->data.binary.left) - evaluate(tree->data.binary.left);
|
return evaluate(tree->data.binary.left) - evaluate(tree->data.binary.right);
|
||||||
case OP_MUL:
|
case OP_MUL:
|
||||||
return evaluate(tree->data.binary.left) * evaluate(tree->data.binary.left);
|
return evaluate(tree->data.binary.left) * evaluate(tree->data.binary.right);
|
||||||
case OP_DIV:
|
case OP_DIV:
|
||||||
return evaluate(tree->data.binary.left) / evaluate(tree->data.binary.left);
|
return evaluate(tree->data.binary.left) / evaluate(tree->data.binary.right);
|
||||||
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
20
src/main.c
20
src/main.c
@@ -1,16 +1,30 @@
|
|||||||
|
#include "evaluator.h"
|
||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <inttypes.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
ASTNodeArray context;
|
char buf[256];
|
||||||
|
printf("Insert a valid mathematical expression: ");
|
||||||
|
|
||||||
|
int c;
|
||||||
|
int pos = 0;
|
||||||
|
while ((c = getc(stdin)) != '\n' && c != EOF) {
|
||||||
|
buf[pos] = c;
|
||||||
|
pos++;
|
||||||
|
}
|
||||||
|
buf[pos] = '\0';
|
||||||
|
|
||||||
tokenize("3 + 4 * 5", &context);
|
ASTNodeArray context;
|
||||||
|
tokenize(buf, &context);
|
||||||
|
|
||||||
AST tree = parse(&context);
|
AST tree = parse(&context);
|
||||||
|
int64_t result = evaluate(tree.head);
|
||||||
|
|
||||||
printf("Hola\n");
|
|
||||||
|
|
||||||
|
printf("El resultado es: %" PRIi64 "\n", result);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
12
src/parser.c
12
src/parser.c
@@ -17,7 +17,7 @@ uint8_t node_lbp(ASTNode node) {
|
|||||||
break;
|
break;
|
||||||
case OP_DIV:
|
case OP_DIV:
|
||||||
case OP_MUL:
|
case OP_MUL:
|
||||||
return 30;
|
return 20;
|
||||||
default:
|
default:
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
@@ -31,11 +31,13 @@ uint8_t node_rbp(ASTNode node) {
|
|||||||
switch (node.data.binary.op) {
|
switch (node.data.binary.op) {
|
||||||
case OP_ADD:
|
case OP_ADD:
|
||||||
case OP_SUB:
|
case OP_SUB:
|
||||||
return 20;
|
return 11;
|
||||||
break;
|
break;
|
||||||
case OP_DIV:
|
case OP_DIV:
|
||||||
case OP_MUL:
|
case OP_MUL:
|
||||||
return 40;
|
return 21;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,7 +53,7 @@ bool ASTNodeSlice_is_valid(ASTNodeSlice *slice) {
|
|||||||
if (slice->arr->len < 1) {
|
if (slice->arr->len < 1) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (slice->pos >= slice->arr->len - 1) {
|
if (slice->pos >= slice->arr->len) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -78,7 +80,7 @@ ASTNode *parse_expr(ASTNodeSlice *slice, uint8_t min_bp) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNode operator = operator = ASTNodeSlice_peek(slice);
|
ASTNode operator = ASTNodeSlice_peek(slice);
|
||||||
uint8_t rbp = node_rbp(operator);
|
uint8_t rbp = node_rbp(operator);
|
||||||
uint8_t lbp = node_lbp(operator);
|
uint8_t lbp = node_lbp(operator);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user