Compare commits
16 Commits
6377515558
...
feature-AS
| Author | SHA1 | Date | |
|---|---|---|---|
| 7ad4eba123 | |||
| e4ec102cb9 | |||
| fb27e1e34c | |||
| ef8cf84456 | |||
| a486ed62f4 | |||
| 4cddb24405 | |||
| 1ce64d8e9e | |||
| 92d142b9cf | |||
| 845673fb0e | |||
| f24671bd19 | |||
| f11b6f8c12 | |||
| 17be815ed0 | |||
| 7d28b69790 | |||
| 0d883ae978 | |||
| acd5e9781e | |||
| 27787308f2 |
@@ -3,6 +3,14 @@ project(calculator VERSION 1.0 LANGUAGES C)
|
|||||||
|
|
||||||
set(CMAKE_C_STANDARD 11)
|
set(CMAKE_C_STANDARD 11)
|
||||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
|
include(FetchContent)
|
||||||
|
|
||||||
|
FetchContent_Declare(
|
||||||
|
arena
|
||||||
|
GIT_REPOSITORY https://laentropia-homelab.tail7368da.ts.net/laentropia/Arena.git
|
||||||
|
GIT_TAG main
|
||||||
|
SOURCE_DIR ${CMAKE_SOURCE_DIR}/external/arena
|
||||||
|
)
|
||||||
|
|
||||||
# Export compile_commands.json (para clangd)
|
# Export compile_commands.json (para clangd)
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
@@ -14,13 +22,29 @@ add_compile_options(
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
include_directories(include)
|
FetchContent_MakeAvailable(arena)
|
||||||
|
|
||||||
|
add_library(arena STATIC
|
||||||
|
external/arena/src/arena.c
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(arena
|
||||||
|
PUBLIC ${CMAKE_SOURCE_DIR}/external/arena/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
|
||||||
|
src/ASTNodeArray.c
|
||||||
|
)
|
||||||
|
|
||||||
|
target_include_directories(calculator_lib
|
||||||
|
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(calculator_lib
|
||||||
|
PUBLIC arena
|
||||||
)
|
)
|
||||||
|
|
||||||
add_executable(calculator src/main.c)
|
add_executable(calculator src/main.c)
|
||||||
|
|||||||
1
external/arena
vendored
Submodule
1
external/arena
vendored
Submodule
Submodule external/arena added at 3d3b8596cc
@@ -0,0 +1,11 @@
|
|||||||
|
#ifndef EVALUATOR_H
|
||||||
|
#define EVALUATOR_H
|
||||||
|
|
||||||
|
#include "lexer.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
int64_t evaluate(ParseResult context);
|
||||||
|
int64_t evaluate_tree(ASTNode *tree);
|
||||||
|
|
||||||
|
#endif // !EVALUATOR_H
|
||||||
|
|||||||
@@ -59,7 +59,6 @@ typedef struct {
|
|||||||
ASTNode *data;
|
ASTNode *data;
|
||||||
} ASTNodeArray;
|
} ASTNodeArray;
|
||||||
|
|
||||||
// Basic array functionality
|
|
||||||
ASTNodeArray ASTNodeArray_init(size_t size);
|
ASTNodeArray ASTNodeArray_init(size_t size);
|
||||||
void ASTNodeArray_free(ASTNodeArray *arr);
|
void ASTNodeArray_free(ASTNodeArray *arr);
|
||||||
ASTNodeArrayErr ASTNodeArray_push(ASTNodeArray *arr, ASTNode node);
|
ASTNodeArrayErr ASTNodeArray_push(ASTNodeArray *arr, ASTNode node);
|
||||||
@@ -74,5 +73,6 @@ LexerErr tokenize_number(const char* input, size_t *offset, ASTNode *out);
|
|||||||
LexerErr string_to_integer(const char buf[], int64_t *number);
|
LexerErr string_to_integer(const char buf[], int64_t *number);
|
||||||
bool isoperator(int c);
|
bool isoperator(int c);
|
||||||
Operator char_to_operator(int c);
|
Operator char_to_operator(int c);
|
||||||
|
char operator_to_char(Operator op);
|
||||||
|
|
||||||
#endif // !LEXER_H
|
#endif // !LEXER_H
|
||||||
|
|||||||
@@ -1,14 +1,35 @@
|
|||||||
|
#ifndef PARSER_H
|
||||||
|
#define PARSER_H
|
||||||
|
|
||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
|
#include "arena.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
ASTNode *head;
|
ASTNode *head;
|
||||||
} AST;
|
} AST;
|
||||||
|
|
||||||
typedef enum {
|
typedef struct {
|
||||||
PARSER_NUD, // Null Denotation
|
ASTNodeArray *arr;
|
||||||
PARSER_LED, // Left Denotation
|
size_t pos;
|
||||||
} ParserState;
|
} ASTNodeSlice;
|
||||||
|
|
||||||
size_t node_lbp(Operator op);
|
typedef struct {
|
||||||
size_t node_rbp(Operator op);
|
Arena arena;
|
||||||
AST parse(ASTNodeArray arr);
|
ASTNode *tree;
|
||||||
|
} ParseResult;
|
||||||
|
|
||||||
|
ASTNode ASTNodeSlice_peek(ASTNodeSlice *slice);
|
||||||
|
ASTNode ASTNodeSlice_next(ASTNodeSlice *slice);
|
||||||
|
bool ASTNodeSlice_is_valid(ASTNodeSlice *slice);
|
||||||
|
|
||||||
|
ASTNode *nud(ASTNodeSlice *slice);
|
||||||
|
ASTNode *led(ASTNodeSlice *slice, size_t right_precedence);
|
||||||
|
|
||||||
|
uint8_t node_lbp(ASTNode node);
|
||||||
|
uint8_t node_rbp(ASTNode node);
|
||||||
|
|
||||||
|
ParseResult parse(ASTNodeArray *arr);
|
||||||
|
ASTNode *parse_expr(ASTNodeSlice *slice, Arena *arena, uint8_t min_bp);
|
||||||
|
|
||||||
|
#endif // !PARSER_H
|
||||||
|
|||||||
108
src/ASTNodeArray.c
Normal file
108
src/ASTNodeArray.c
Normal file
@@ -0,0 +1,108 @@
|
|||||||
|
#include "lexer.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define NODE_ARRAY_DEFAULT_SIZE 64
|
||||||
|
// Helps state machine for the lexer :)
|
||||||
|
typedef enum {
|
||||||
|
WAIT_FOR_NUMBER,
|
||||||
|
WAIT_FOR_OPERATOR,
|
||||||
|
} LexerState;
|
||||||
|
|
||||||
|
ASTNodeArray ASTNodeArray_init(size_t size) {
|
||||||
|
ASTNodeArray new;
|
||||||
|
new.len = 0; // if 0 then use default
|
||||||
|
new.cap = size == 0 ? NODE_ARRAY_DEFAULT_SIZE : size;
|
||||||
|
new.data = malloc(new.cap * sizeof(ASTNode));
|
||||||
|
return new;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ASTNodeArray_free(ASTNodeArray *arr) {
|
||||||
|
free(arr->data);
|
||||||
|
arr->cap = 0;
|
||||||
|
arr->len = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTNodeArrayErr ASTNodeArray_get(const ASTNodeArray *arr, size_t index, ASTNode *out) {
|
||||||
|
if (arr == NULL) {
|
||||||
|
return ARRAY_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (out == NULL) {
|
||||||
|
return ARRAY_NULL_ARG;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arr->len == 0) {
|
||||||
|
return ARRAY_EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (index >= arr->len) {
|
||||||
|
return ARRAY_OUT_OF_BOUNDS;
|
||||||
|
}
|
||||||
|
|
||||||
|
*out = arr->data[index];
|
||||||
|
|
||||||
|
return ARRAY_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTNodeArrayErr ASTNodeArray_push(ASTNodeArray *arr, ASTNode node) {
|
||||||
|
if (arr == NULL) {
|
||||||
|
return ARRAY_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arr->len >= arr->cap) {
|
||||||
|
size_t new_cap = arr->cap * 2;
|
||||||
|
ASTNode *tmp = realloc(arr->data, new_cap * sizeof(ASTNode));
|
||||||
|
if (tmp == NULL) {
|
||||||
|
return ARRAY_ALLOC;
|
||||||
|
}
|
||||||
|
arr->data = tmp;
|
||||||
|
arr->cap = new_cap;
|
||||||
|
}
|
||||||
|
|
||||||
|
arr->data[arr->len] = node;
|
||||||
|
arr->len = arr->len + 1;
|
||||||
|
|
||||||
|
return ARRAY_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTNodeArrayErr ASTNodeArray_pop(ASTNodeArray *arr, size_t index, ASTNode *out) {
|
||||||
|
if (arr == NULL) {
|
||||||
|
return ARRAY_NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arr->len == 0) {
|
||||||
|
return ARRAY_EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (index >= arr->len) {
|
||||||
|
return ARRAY_OUT_OF_BOUNDS;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (arr->cap / 4 > arr->len) {
|
||||||
|
size_t new_cap = arr->cap / 2;
|
||||||
|
ASTNode *tmp = realloc(arr->data, new_cap * sizeof(ASTNode));
|
||||||
|
if (tmp == NULL) {
|
||||||
|
return ARRAY_ALLOC;
|
||||||
|
}
|
||||||
|
arr->data = tmp;
|
||||||
|
arr->cap = new_cap;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (out != NULL) {
|
||||||
|
ASTNode node_to_delete = arr->data[index];
|
||||||
|
*out = node_to_delete;
|
||||||
|
}
|
||||||
|
|
||||||
|
for (size_t i = index; i < arr->len - 1; i++) {
|
||||||
|
arr->data[index] = arr->data[index + 1];
|
||||||
|
}
|
||||||
|
|
||||||
|
return ARRAY_OK;
|
||||||
|
}
|
||||||
|
|
||||||
|
size_t ASTNodeArray_len(ASTNodeArray *arr) {
|
||||||
|
if (arr == NULL) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
return arr->len;
|
||||||
|
}
|
||||||
@@ -0,0 +1,36 @@
|
|||||||
|
#include "evaluator.h"
|
||||||
|
#include "arena.h"
|
||||||
|
#include "lexer.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
|
||||||
|
|
||||||
|
int64_t evaluate_tree(ASTNode *tree) {
|
||||||
|
if (tree->type == NODE_BINARY_OP) {
|
||||||
|
Operator op = tree->data.binary.op;
|
||||||
|
ASTNode *left = tree->data.binary.left;
|
||||||
|
ASTNode *right = tree->data.binary.right;
|
||||||
|
|
||||||
|
switch (op) {
|
||||||
|
case OP_ADD:
|
||||||
|
return evaluate_tree(left) + evaluate_tree(right);
|
||||||
|
case OP_SUB:
|
||||||
|
return evaluate_tree(left) - evaluate_tree(right);
|
||||||
|
case OP_MUL:
|
||||||
|
return evaluate_tree(left) * evaluate_tree(right);
|
||||||
|
case OP_DIV:
|
||||||
|
return evaluate_tree(left) / evaluate_tree(right);
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t return_val = tree->data.integer;
|
||||||
|
return return_val;
|
||||||
|
}
|
||||||
|
|
||||||
|
int64_t evaluate(ParseResult context) {
|
||||||
|
int64_t result = evaluate_tree(context.tree);
|
||||||
|
arena_destroy(&context.arena);
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|||||||
115
src/lexer.c
115
src/lexer.c
@@ -6,111 +6,11 @@
|
|||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
#define NODE_ARRAY_DEFAULT_SIZE 64
|
|
||||||
// Helps state machine for the lexer :)
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
WAIT_FOR_NUMBER,
|
WAIT_FOR_NUMBER,
|
||||||
WAIT_FOR_OPERATOR,
|
WAIT_FOR_OPERATOR
|
||||||
} LexerState;
|
} LexerState;
|
||||||
|
|
||||||
ASTNodeArray ASTNodeArray_init(size_t size) {
|
|
||||||
ASTNodeArray new;
|
|
||||||
new.len = 0; // if 0 then use default
|
|
||||||
new.cap = size == 0 ? NODE_ARRAY_DEFAULT_SIZE : size;
|
|
||||||
new.data = malloc(new.cap * sizeof(ASTNode));
|
|
||||||
return new;
|
|
||||||
}
|
|
||||||
|
|
||||||
void ASTNodeArray_free(ASTNodeArray *arr) {
|
|
||||||
free(arr->data);
|
|
||||||
arr->cap = 0;
|
|
||||||
arr->len = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTNodeArrayErr ASTNodeArray_get(const ASTNodeArray *arr, size_t index, ASTNode *out) {
|
|
||||||
if (arr == NULL) {
|
|
||||||
return ARRAY_NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (out == NULL) {
|
|
||||||
return ARRAY_NULL_ARG;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (arr->len == 0) {
|
|
||||||
return ARRAY_EMPTY;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (index >= arr->len) {
|
|
||||||
return ARRAY_OUT_OF_BOUNDS;
|
|
||||||
}
|
|
||||||
|
|
||||||
*out = arr->data[index];
|
|
||||||
|
|
||||||
return ARRAY_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTNodeArrayErr ASTNodeArray_push(ASTNodeArray *arr, ASTNode node) {
|
|
||||||
if (arr == NULL) {
|
|
||||||
return ARRAY_NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (arr->len >= arr->cap) {
|
|
||||||
size_t new_cap = arr->cap * 2;
|
|
||||||
ASTNode *tmp = realloc(arr->data, new_cap * sizeof(ASTNode));
|
|
||||||
if (tmp == NULL) {
|
|
||||||
return ARRAY_ALLOC;
|
|
||||||
}
|
|
||||||
arr->data = tmp;
|
|
||||||
arr->cap = new_cap;
|
|
||||||
}
|
|
||||||
|
|
||||||
arr->data[arr->len] = node;
|
|
||||||
arr->len = arr->len + 1;
|
|
||||||
|
|
||||||
return ARRAY_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
ASTNodeArrayErr ASTNodeArray_pop(ASTNodeArray *arr, size_t index, ASTNode *out) {
|
|
||||||
if (arr == NULL) {
|
|
||||||
return ARRAY_NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (arr->len == 0) {
|
|
||||||
return ARRAY_EMPTY;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (index >= arr->len) {
|
|
||||||
return ARRAY_OUT_OF_BOUNDS;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (arr->cap / 4 > arr->len) {
|
|
||||||
size_t new_cap = arr->cap / 2;
|
|
||||||
ASTNode *tmp = realloc(arr->data, new_cap * sizeof(ASTNode));
|
|
||||||
if (tmp == NULL) {
|
|
||||||
return ARRAY_ALLOC;
|
|
||||||
}
|
|
||||||
arr->data = tmp;
|
|
||||||
arr->cap = new_cap;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (out != NULL) {
|
|
||||||
ASTNode node_to_delete = arr->data[index];
|
|
||||||
*out = node_to_delete;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (size_t i = index; i < arr->len - 1; i++) {
|
|
||||||
arr->data[index] = arr->data[index + 1];
|
|
||||||
}
|
|
||||||
|
|
||||||
return ARRAY_OK;
|
|
||||||
}
|
|
||||||
|
|
||||||
size_t ASTNodeArray_len(ASTNodeArray *arr) {
|
|
||||||
if (arr == NULL) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
return arr->len;
|
|
||||||
}
|
|
||||||
|
|
||||||
LexerErr tokenize(const char *input, ASTNodeArray *out) {
|
LexerErr tokenize(const char *input, ASTNodeArray *out) {
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
@@ -248,3 +148,16 @@ Operator char_to_operator(int c) {
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
char operator_to_char(Operator op) {
|
||||||
|
switch (op) {
|
||||||
|
case OP_ADD:
|
||||||
|
return '+';
|
||||||
|
case OP_SUB:
|
||||||
|
return '-';
|
||||||
|
case OP_MUL:
|
||||||
|
return '*';
|
||||||
|
case OP_DIV:
|
||||||
|
return '/';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
28
src/main.c
28
src/main.c
@@ -1,7 +1,31 @@
|
|||||||
|
#include "arena.h"
|
||||||
|
#include "evaluator.h"
|
||||||
|
#include "lexer.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <inttypes.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
|
|
||||||
int main(int argc, char *argv[]) {
|
int main(void) {
|
||||||
puts("Hello");
|
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';
|
||||||
|
|
||||||
|
ASTNodeArray context;
|
||||||
|
tokenize(buf, &context);
|
||||||
|
|
||||||
|
ParseResult par = parse(&context);
|
||||||
|
int64_t result = evaluate(par);
|
||||||
|
|
||||||
|
|
||||||
|
printf("El resultado es: %" PRIi64 "\n", result);
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
119
src/parser.c
119
src/parser.c
@@ -1,9 +1,18 @@
|
|||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
|
#include "arena.h"
|
||||||
|
#include <stdalign.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
size_t node_lbp(Operator op) {
|
|
||||||
switch (op) {
|
uint8_t node_lbp(ASTNode node) {
|
||||||
|
if (node.type == NODE_INTEGER) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (node.data.binary.op) {
|
||||||
case OP_ADD:
|
case OP_ADD:
|
||||||
case OP_SUB:
|
case OP_SUB:
|
||||||
return 10;
|
return 10;
|
||||||
@@ -11,17 +20,115 @@ size_t node_lbp(Operator op) {
|
|||||||
case OP_DIV:
|
case OP_DIV:
|
||||||
case OP_MUL:
|
case OP_MUL:
|
||||||
return 20;
|
return 20;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t node_rbp(Operator op) {
|
uint8_t node_rbp(ASTNode node) {
|
||||||
switch (op) {
|
if (node.type == NODE_INTEGER) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (node.data.binary.op) {
|
||||||
case OP_ADD:
|
case OP_ADD:
|
||||||
case OP_SUB:
|
case OP_SUB:
|
||||||
return 10;
|
return 11;
|
||||||
break;
|
break;
|
||||||
case OP_DIV:
|
case OP_DIV:
|
||||||
case OP_MUL:
|
case OP_MUL:
|
||||||
return 20;
|
return 21;
|
||||||
|
default:
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ASTNode ASTNodeSlice_next(ASTNodeSlice *slice) {
|
||||||
|
return slice->arr->data[slice->pos++];
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTNode ASTNodeSlice_peek(ASTNodeSlice *slice) {
|
||||||
|
return slice->arr->data[slice->pos];
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ASTNodeSlice_is_valid(ASTNodeSlice *slice) {
|
||||||
|
if (slice->arr->len < 1) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (slice->pos >= slice->arr->len) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
ParseResult parse(ASTNodeArray *arr) {
|
||||||
|
ASTNodeSlice context = {
|
||||||
|
.arr = arr,
|
||||||
|
.pos = 0,
|
||||||
|
};
|
||||||
|
Arena arena = arena_init(sizeof(ASTNode) * arr->len).arena;
|
||||||
|
|
||||||
|
return (ParseResult) {
|
||||||
|
.arena = arena,
|
||||||
|
.tree = parse_expr(&context, &arena, 0)};
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTNode *parse_expr(ASTNodeSlice *slice, Arena *arena, uint8_t min_bp) {
|
||||||
|
arena_ensure_capacity(
|
||||||
|
arena,
|
||||||
|
sizeof(ASTNode),
|
||||||
|
alignof(ASTNode)
|
||||||
|
);
|
||||||
|
|
||||||
|
ASTNode *left_side = arena_unwrap_pointer(
|
||||||
|
arena_alloc(
|
||||||
|
arena,
|
||||||
|
sizeof(ASTNode),
|
||||||
|
alignof(ASTNode)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
*left_side = ASTNodeSlice_next(slice);
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
if (!ASTNodeSlice_is_valid(slice)) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTNode operator = ASTNodeSlice_peek(slice);
|
||||||
|
uint8_t rbp = node_rbp(operator);
|
||||||
|
uint8_t lbp = node_lbp(operator);
|
||||||
|
|
||||||
|
if (lbp < min_bp) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASTNodeSlice_next(slice);
|
||||||
|
ASTNode *right_side = parse_expr(slice, arena, rbp);
|
||||||
|
|
||||||
|
arena_ensure_capacity(
|
||||||
|
arena,
|
||||||
|
sizeof(ASTNode),
|
||||||
|
alignof(ASTNode));
|
||||||
|
ASTNode *new_node = arena_unwrap_pointer(
|
||||||
|
arena_alloc(
|
||||||
|
arena,
|
||||||
|
sizeof(ASTNode),
|
||||||
|
alignof(ASTNode)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
*new_node = operator;
|
||||||
|
|
||||||
|
new_node->data.binary.left = left_side;
|
||||||
|
new_node->data.binary.right = right_side;
|
||||||
|
|
||||||
|
left_side = new_node;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
return left_side;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,6 +2,8 @@ find_package(cmocka REQUIRED)
|
|||||||
|
|
||||||
add_executable(test_nodeArray test_ASTNodeArray.c)
|
add_executable(test_nodeArray test_ASTNodeArray.c)
|
||||||
add_executable(test_lexer test_lexer.c)
|
add_executable(test_lexer test_lexer.c)
|
||||||
|
add_executable(test_parser test_parser.c)
|
||||||
|
add_executable(test_evaluator test_evaluator.c)
|
||||||
|
|
||||||
target_link_libraries(test_nodeArray
|
target_link_libraries(test_nodeArray
|
||||||
calculator_lib
|
calculator_lib
|
||||||
@@ -13,5 +15,17 @@ target_link_libraries(test_lexer
|
|||||||
cmocka::cmocka
|
cmocka::cmocka
|
||||||
)
|
)
|
||||||
|
|
||||||
|
target_link_libraries(test_parser
|
||||||
|
calculator_lib
|
||||||
|
cmocka::cmocka
|
||||||
|
)
|
||||||
|
|
||||||
|
target_link_libraries(test_evaluator
|
||||||
|
calculator_lib
|
||||||
|
cmocka::cmocka
|
||||||
|
)
|
||||||
|
|
||||||
add_test(NAME nodeArray_tests COMMAND test_nodeArray)
|
add_test(NAME nodeArray_tests COMMAND test_nodeArray)
|
||||||
add_test(NAME lexer_tests COMMAND test_lexer)
|
add_test(NAME lexer_tests COMMAND test_lexer)
|
||||||
|
add_test(NAME parser_tests COMMAND test_parser)
|
||||||
|
add_test(NAME evaluator_tests COMMAND test_evaluator)
|
||||||
|
|||||||
32
test/test_evaluator.c
Normal file
32
test/test_evaluator.c
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
#include "lexer.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#include "evaluator.h"
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <cmocka.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
static void test_basic_evaluation(void** state) {
|
||||||
|
(void) state;
|
||||||
|
|
||||||
|
char expr[256] = "2 + 4 * 40 / 2";
|
||||||
|
ASTNodeArray context;
|
||||||
|
|
||||||
|
tokenize(expr, &context);
|
||||||
|
ParseResult result = parse(&context);
|
||||||
|
int64_t value = evaluate(result);
|
||||||
|
|
||||||
|
assert_int_equal(value, 82);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
const struct CMUnitTest tests[] = {
|
||||||
|
cmocka_unit_test(test_basic_evaluation),
|
||||||
|
};
|
||||||
|
|
||||||
|
cmocka_run_group_tests(tests, NULL, NULL);
|
||||||
|
return EXIT_SUCCESS;
|
||||||
|
}
|
||||||
@@ -1,5 +1,81 @@
|
|||||||
#include <stdlib.h>
|
#include "arena.h"
|
||||||
|
#include "lexer.h"
|
||||||
|
#include "parser.h"
|
||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stddef.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <setjmp.h>
|
||||||
|
#include <cmocka.h>
|
||||||
|
|
||||||
int main() {
|
static void test_parsing_basic_expression(void **state) {
|
||||||
return EXIT_SUCCESS;
|
(void) state;
|
||||||
|
|
||||||
|
char expr[256] = "2 + 3 / 66 * 789";
|
||||||
|
ASTNodeArray tokens;
|
||||||
|
ASTNode node;
|
||||||
|
|
||||||
|
assert_int_equal(tokenize(expr, &tokens), LEXER_OK);
|
||||||
|
assert_int_equal(tokens.len, 7);
|
||||||
|
|
||||||
|
ParseResult result = parse(&tokens);
|
||||||
|
// Assert head is +
|
||||||
|
assert_int_equal(result.tree->type, NODE_BINARY_OP);
|
||||||
|
assert_int_equal(result.tree->data.binary.op, OP_ADD);
|
||||||
|
|
||||||
|
assert_int_equal(result.tree->data.binary.left->type, NODE_INTEGER);
|
||||||
|
assert_int_equal(result.tree->data.binary.left->data.integer, 2);
|
||||||
|
|
||||||
|
|
||||||
|
assert_int_equal(
|
||||||
|
result.tree->data.binary.right->type,
|
||||||
|
NODE_BINARY_OP
|
||||||
|
);
|
||||||
|
assert_int_equal(
|
||||||
|
result.tree->data.binary.right->data.binary.op,
|
||||||
|
OP_MUL
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_int_equal(
|
||||||
|
result.tree->data.binary.right->data.binary.right->type,
|
||||||
|
NODE_INTEGER);
|
||||||
|
assert_int_equal(
|
||||||
|
result.tree->data.binary.right->data.binary.right->data.integer,
|
||||||
|
789);
|
||||||
|
|
||||||
|
assert_int_equal(
|
||||||
|
result.tree->data.binary.right->data.binary.left->type,
|
||||||
|
NODE_BINARY_OP
|
||||||
|
);
|
||||||
|
assert_int_equal(
|
||||||
|
result.tree->data.binary.right->data.binary.left->data.binary.op,
|
||||||
|
OP_DIV
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_int_equal(
|
||||||
|
result.tree->data.binary.right->data.binary.left->data.binary.right->type,
|
||||||
|
NODE_INTEGER
|
||||||
|
);
|
||||||
|
assert_int_equal(
|
||||||
|
result.tree->data.binary.right->data.binary.left->data.binary.right->data.integer,
|
||||||
|
66
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_int_equal(
|
||||||
|
result.tree->data.binary.right->data.binary.left->data.binary.left->type,
|
||||||
|
NODE_INTEGER
|
||||||
|
);
|
||||||
|
assert_int_equal(
|
||||||
|
result.tree->data.binary.right->data.binary.left->data.binary.left->data.integer,
|
||||||
|
3
|
||||||
|
);
|
||||||
|
arena_destroy(&result.arena);
|
||||||
|
}
|
||||||
|
|
||||||
|
int main(void) {
|
||||||
|
const struct CMUnitTest tests [] = {
|
||||||
|
cmocka_unit_test(test_parsing_basic_expression),
|
||||||
|
};
|
||||||
|
|
||||||
|
return cmocka_run_group_tests(tests, NULL, NULL);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user