Compare commits
32 Commits
feature-AS
...
21853362a4
| Author | SHA1 | Date | |
|---|---|---|---|
| 21853362a4 | |||
| 964034b203 | |||
| 9ea1da549f | |||
| ab791dbc9b | |||
| 3ec73559ee | |||
| b56a368244 | |||
| 6294121e91 | |||
| 70ab06964c | |||
| 90c426f3a4 | |||
| efa0e3bacd | |||
| 542a94ef81 | |||
| 80e05a9acf | |||
| f3373123e1 | |||
| 2a73f5f9d6 | |||
| e3d64596ab | |||
| 56c80fa071 | |||
| 7f390a8c6b | |||
| e30b3d7175 | |||
| 59f99059bb | |||
| c41847e120 | |||
| f2c906c6aa | |||
| fee33ff1f0 | |||
| ac2e783ccc | |||
| 630d9f53e1 | |||
| b7e1cdf3a6 | |||
| cef046f7db | |||
| 19c84c382b | |||
| 855d683005 | |||
| 576bcd9504 | |||
| e6420cb1c9 | |||
| f50546bd07 | |||
| c99f307827 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -9,6 +9,7 @@ out/Release/
|
|||||||
|
|
||||||
# Cmake files
|
# Cmake files
|
||||||
CMakeCache.txt
|
CMakeCache.txt
|
||||||
|
cmake
|
||||||
CMakeFiles/
|
CMakeFiles/
|
||||||
cmake_install.cmake
|
cmake_install.cmake
|
||||||
CTestTestfile.cmake
|
CTestTestfile.cmake
|
||||||
|
|||||||
120
CMakeLists.txt
120
CMakeLists.txt
@@ -1,60 +1,100 @@
|
|||||||
cmake_minimum_required(VERSION 3.20)
|
cmake_minimum_required(VERSION 3.20)
|
||||||
project(calculator VERSION 1.0 LANGUAGES C)
|
project(
|
||||||
|
calculator
|
||||||
|
VERSION 1.0
|
||||||
|
LANGUAGES C)
|
||||||
|
|
||||||
|
# ------------------------------------------------
|
||||||
|
# C standard — no compiler extensions
|
||||||
|
# ------------------------------------------------
|
||||||
set(CMAKE_C_STANDARD 11)
|
set(CMAKE_C_STANDARD 11)
|
||||||
set(CMAKE_C_STANDARD_REQUIRED ON)
|
set(CMAKE_C_STANDARD_REQUIRED ON)
|
||||||
include(FetchContent)
|
set(CMAKE_C_EXTENSIONS OFF)
|
||||||
|
|
||||||
FetchContent_Declare(
|
# Export compile_commands.json (clangd / IDEs)
|
||||||
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)
|
|
||||||
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
|
||||||
|
|
||||||
add_compile_options(
|
# ------------------------------------------------
|
||||||
-Wall
|
# Options
|
||||||
-Wextra
|
# ------------------------------------------------
|
||||||
-Wpedantic
|
option(CALCULATOR_BUILD_TESTS "Build calculator tests" ON)
|
||||||
)
|
option(CALCULATOR_ENABLE_SANITIZERS "Enable Address Sanitizer in tests" ON)
|
||||||
|
|
||||||
|
# ------------------------------------------------
|
||||||
|
# Portable warning flags
|
||||||
|
# ------------------------------------------------
|
||||||
|
if(MSVC)
|
||||||
|
add_compile_options(/W4)
|
||||||
|
else()
|
||||||
|
add_compile_options(-Wall -Wextra -Wpedantic)
|
||||||
|
endif()
|
||||||
|
|
||||||
FetchContent_MakeAvailable(arena)
|
# ================================================================
|
||||||
|
# CPM.cmake bootstrap Descarga CPM solo si no existe; usa caché para builds
|
||||||
|
# offline.
|
||||||
|
# ================================================================
|
||||||
|
set(CPM_DOWNLOAD_VERSION 0.40.2)
|
||||||
|
set(CPM_DOWNLOAD_LOCATION "${CMAKE_BINARY_DIR}/cmake/CPM.cmake")
|
||||||
|
|
||||||
add_library(arena STATIC
|
if(NOT EXISTS "${CPM_DOWNLOAD_LOCATION}")
|
||||||
external/arena/src/arena.c
|
message(
|
||||||
)
|
STATUS "[calculator] Downloading CPM.cmake v${CPM_DOWNLOAD_VERSION}...")
|
||||||
|
file(
|
||||||
|
DOWNLOAD
|
||||||
|
"https://github.com/cpm-cmake/CPM.cmake/releases/download/v${CPM_DOWNLOAD_VERSION}/CPM.cmake"
|
||||||
|
"${CPM_DOWNLOAD_LOCATION}"
|
||||||
|
STATUS download_status
|
||||||
|
TLS_VERIFY ON)
|
||||||
|
list(GET download_status 0 status_code)
|
||||||
|
list(GET download_status 1 status_msg)
|
||||||
|
if(NOT status_code EQUAL 0)
|
||||||
|
message(
|
||||||
|
FATAL_ERROR "[calculator] Failed to download CPM.cmake: ${status_msg}")
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
target_include_directories(arena
|
include("${CPM_DOWNLOAD_LOCATION}")
|
||||||
PUBLIC ${CMAKE_SOURCE_DIR}/external/arena/include
|
|
||||||
)
|
|
||||||
|
|
||||||
add_library(calculator_lib
|
# ================================================================
|
||||||
src/lexer.c
|
# Dependencies via CPM
|
||||||
src/parser.c
|
# ================================================================
|
||||||
src/evaluator.c
|
cpmaddpackage(
|
||||||
src/ASTNodeArray.c
|
NAME arena GIT_REPOSITORY
|
||||||
)
|
https://laentropia-homelab.tail7368da.ts.net/laentropia/Arena.git GIT_TAG
|
||||||
|
main)
|
||||||
|
|
||||||
target_include_directories(calculator_lib
|
cpmaddpackage(
|
||||||
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
|
NAME arraylist GIT_REPOSITORY
|
||||||
)
|
https://laentropia-homelab.tail7368da.ts.net/laentropia/ArrayList.git GIT_TAG
|
||||||
|
main)
|
||||||
|
|
||||||
target_link_libraries(calculator_lib
|
# ================================================================
|
||||||
|
# LIBRARY (lógica reutilizable, separada del ejecutable)
|
||||||
|
# ================================================================
|
||||||
|
add_library(calculator_lib src/lexer.c src/parser.c src/evaluator.c)
|
||||||
|
|
||||||
|
target_include_directories(
|
||||||
|
calculator_lib PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||||
|
$<INSTALL_INTERFACE:include>)
|
||||||
|
|
||||||
|
target_compile_features(calculator_lib PUBLIC c_std_11)
|
||||||
|
|
||||||
|
target_link_libraries(
|
||||||
|
calculator_lib
|
||||||
PUBLIC arena
|
PUBLIC arena
|
||||||
)
|
PUBLIC arraylist
|
||||||
|
PRIVATE m) # libm — solo en el linker interno, no se propaga
|
||||||
|
|
||||||
|
# ================================================================
|
||||||
|
# EXECUTABLE
|
||||||
|
# ================================================================
|
||||||
add_executable(calculator src/main.c)
|
add_executable(calculator src/main.c)
|
||||||
|
target_link_libraries(calculator PRIVATE calculator_lib)
|
||||||
|
|
||||||
target_link_libraries(calculator calculator_lib)
|
# ================================================================
|
||||||
|
# TESTS
|
||||||
# ------------------------
|
# ================================================================
|
||||||
# Testing
|
if(CALCULATOR_BUILD_TESTS)
|
||||||
# ------------------------
|
|
||||||
|
|
||||||
enable_testing()
|
enable_testing()
|
||||||
add_subdirectory(test)
|
add_subdirectory(test)
|
||||||
|
endif()
|
||||||
|
|||||||
1
external/arena
vendored
1
external/arena
vendored
Submodule external/arena deleted from 3d3b8596cc
@@ -1,11 +1,29 @@
|
|||||||
#ifndef EVALUATOR_H
|
#ifndef EVALUATOR_H
|
||||||
#define EVALUATOR_H
|
#define EVALUATOR_H
|
||||||
|
|
||||||
#include "lexer.h"
|
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
int64_t evaluate(ParseResult context);
|
typedef enum {
|
||||||
int64_t evaluate_tree(ASTNode *tree);
|
EVALUATOR_OK,
|
||||||
|
EVALUATOR_MATH_ERR,
|
||||||
|
EVALUATOR_DIVISION_BY_ZERO,
|
||||||
|
EVALUATOR_INVALID_PARSING,
|
||||||
|
EVALUATOR_INVALID_TREE, // just to shut up the compiler with the swithces
|
||||||
|
} EvaluatorErr;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool is_valid;
|
||||||
|
union {
|
||||||
|
int64_t val;
|
||||||
|
EvaluatorErr err;
|
||||||
|
};
|
||||||
|
} EvaluatorResult;
|
||||||
|
|
||||||
|
EvaluatorResult evaluate_binary(Node *tree);
|
||||||
|
EvaluatorResult evaluate_unary(Node *tree);
|
||||||
|
|
||||||
|
EvaluatorResult evaluate(ParserResult context);
|
||||||
|
EvaluatorResult evaluate_tree(Node *tree);
|
||||||
|
|
||||||
#endif // !EVALUATOR_H
|
#endif // !EVALUATOR_H
|
||||||
|
|||||||
@@ -1,76 +1,74 @@
|
|||||||
#ifndef LEXER_H
|
#ifndef LEXER_H
|
||||||
#define LEXER_H
|
#define LEXER_H
|
||||||
|
|
||||||
|
#include "arraylist.h"
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
// For identifing
|
// For identifing
|
||||||
typedef enum {
|
typedef enum {
|
||||||
NODE_INTEGER,
|
TOKEN_INTEGER,
|
||||||
NODE_BINARY_OP,
|
TOKEN_OPERATOR,
|
||||||
} ASTNodeType;
|
} TokenType;
|
||||||
|
|
||||||
// For classify operators
|
// For classify operators
|
||||||
typedef enum {
|
typedef enum {
|
||||||
OP_ADD,
|
OP_ADD,
|
||||||
OP_SUB,
|
OP_SUB,
|
||||||
OP_MUL,
|
OP_MUL,
|
||||||
OP_DIV
|
OP_DIV,
|
||||||
|
OP_POW,
|
||||||
|
OP_FACTORIAL,
|
||||||
|
OP_START_PAR,
|
||||||
|
OP_END_PAR,
|
||||||
} Operator;
|
} Operator;
|
||||||
|
|
||||||
typedef enum {
|
|
||||||
ARRAY_OK = 0,
|
|
||||||
ARRAY_NULL,
|
|
||||||
ARRAY_EMPTY,
|
|
||||||
ARRAY_OUT_OF_BOUNDS,
|
|
||||||
ARRAY_NULL_ARG,
|
|
||||||
ARRAY_ALLOC,
|
|
||||||
} ASTNodeArrayErr;
|
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
LEXER_OK = 0,
|
LEXER_OK = 0,
|
||||||
LEXER_INT_OVERFLOW,
|
LEXER_INT_OVERFLOW,
|
||||||
LEXER_FAILED_NUMBER_CONVERSION,
|
LEXER_FAILED_NUMBER_CONVERSION,
|
||||||
LEXER_NOT_RECOGNIZED_SYMBOL,
|
LEXER_NOT_RECOGNIZED_SYMBOL,
|
||||||
LEXER_EMPTY_INPUT,
|
LEXER_EMPTY_INPUT,
|
||||||
LEXER_NULL_ARG,
|
|
||||||
LEXER_WRONG_SYNTAX,
|
|
||||||
LEXER_BUF_OVERFLOW,
|
LEXER_BUF_OVERFLOW,
|
||||||
} LexerErr;
|
} LexerErr;
|
||||||
|
|
||||||
// Can be thought as tokens, they will be used by the parser.
|
// Can be thought as tokens, they will be used by the parser.
|
||||||
typedef struct ASTNode {
|
|
||||||
ASTNodeType type;
|
|
||||||
union {
|
|
||||||
int64_t integer;
|
|
||||||
struct {
|
|
||||||
struct ASTNode *left;
|
|
||||||
struct ASTNode *right;
|
|
||||||
Operator op;
|
|
||||||
} binary;
|
|
||||||
} data;
|
|
||||||
} ASTNode;
|
|
||||||
|
|
||||||
// I prefer ot have a dynamic array for storing the "tokens"
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
size_t len;
|
TokenType type;
|
||||||
size_t cap;
|
union {
|
||||||
ASTNode *data;
|
int64_t num;
|
||||||
} ASTNodeArray;
|
Operator op;
|
||||||
|
};
|
||||||
|
} Token;
|
||||||
|
|
||||||
ASTNodeArray ASTNodeArray_init(size_t size);
|
typedef struct {
|
||||||
void ASTNodeArray_free(ASTNodeArray *arr);
|
bool is_valid;
|
||||||
ASTNodeArrayErr ASTNodeArray_push(ASTNodeArray *arr, ASTNode node);
|
union {
|
||||||
ASTNodeArrayErr ASTNodeArray_get(const ASTNodeArray *arr, size_t index, ASTNode *out);
|
LexerErr err;
|
||||||
// Out in pop can be NULL so it doesn't return anything
|
ArrayList *arr;
|
||||||
ASTNodeArrayErr ASTNodeArray_pop(ASTNodeArray *arr, size_t index, ASTNode *out);
|
};
|
||||||
size_t ASTNodeArray_len(ASTNodeArray *arr);
|
} TokenizeResult;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool is_valid;
|
||||||
|
union {
|
||||||
|
LexerErr err;
|
||||||
|
Token token;
|
||||||
|
};
|
||||||
|
} TokenResult;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
bool is_valid;
|
||||||
|
union {
|
||||||
|
LexerErr err;
|
||||||
|
int64_t num;
|
||||||
|
};
|
||||||
|
} LexerI64Result;
|
||||||
|
|
||||||
// Lexer funtions as well as few functionality
|
// Lexer funtions as well as few functionality
|
||||||
LexerErr tokenize(const char* input, ASTNodeArray *out);
|
TokenizeResult tokenize(const char* input);
|
||||||
LexerErr tokenize_number(const char* input, size_t *offset, ASTNode *out);
|
TokenResult tokenize_number(const char* input, size_t *offset);
|
||||||
LexerErr string_to_integer(const char buf[], int64_t *number);
|
LexerI64Result string_to_integer(const char buf[]);
|
||||||
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);
|
char operator_to_char(Operator op);
|
||||||
|
|||||||
@@ -3,33 +3,87 @@
|
|||||||
|
|
||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
#include "arena.h"
|
#include "arena.h"
|
||||||
|
#include "arraylist.h"
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
typedef struct {
|
typedef enum {
|
||||||
ASTNode *head;
|
NODE_INT,
|
||||||
} AST;
|
NODE_BINARY_OP,
|
||||||
|
NODE_UNARY_OP,
|
||||||
|
} NodeType;
|
||||||
|
|
||||||
|
typedef struct Node {
|
||||||
|
NodeType type;
|
||||||
|
union {
|
||||||
|
int64_t num;
|
||||||
|
struct {
|
||||||
|
Operator op;
|
||||||
|
struct Node *left;
|
||||||
|
struct Node *right;
|
||||||
|
}binary;
|
||||||
|
struct {
|
||||||
|
Operator op;
|
||||||
|
struct Node *to;
|
||||||
|
}unary;
|
||||||
|
Operator par;
|
||||||
|
};
|
||||||
|
} Node;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
PARSER_OK = 0,
|
||||||
|
PARSER_UNEXPECTED_TOKEN,
|
||||||
|
PARSER_MISSING_OPERAND,
|
||||||
|
PARSER_UNMATCHED_PAREN,
|
||||||
|
PARSER_OUT_OF_MEMORY,
|
||||||
|
PARSER_INVALID_TOKENIZE,
|
||||||
|
PARSER_UNEXPECTED_EOF,
|
||||||
|
} ParserErr;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
ASTNodeArray *arr;
|
bool is_valid;
|
||||||
size_t pos;
|
union {
|
||||||
} ASTNodeSlice;
|
ParserErr err;
|
||||||
|
struct {
|
||||||
|
Arena *arena;
|
||||||
|
Node *tree;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
} ParserResult;
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
Arena arena;
|
bool is_valid;
|
||||||
ASTNode *tree;
|
union {
|
||||||
} ParseResult;
|
ParserErr err;
|
||||||
|
Node *node;
|
||||||
|
};
|
||||||
|
} TreeResult;
|
||||||
|
|
||||||
ASTNode ASTNodeSlice_peek(ASTNodeSlice *slice);
|
typedef struct {
|
||||||
ASTNode ASTNodeSlice_next(ASTNodeSlice *slice);
|
bool is_valid;
|
||||||
bool ASTNodeSlice_is_valid(ASTNodeSlice *slice);
|
union {
|
||||||
|
ParserErr err;
|
||||||
|
Node node;
|
||||||
|
};
|
||||||
|
} NodeResult;
|
||||||
|
|
||||||
ASTNode *nud(ASTNodeSlice *slice);
|
typedef struct {
|
||||||
ASTNode *led(ASTNodeSlice *slice, size_t right_precedence);
|
bool is_valid;
|
||||||
|
union {
|
||||||
|
ParserErr err;
|
||||||
|
uint8_t num;
|
||||||
|
};
|
||||||
|
} ParserU8Result;
|
||||||
|
|
||||||
uint8_t node_lbp(ASTNode node);
|
TreeResult nud(ArraySlice *slice, Arena *arena, Token token); // Null denotation
|
||||||
uint8_t node_rbp(ASTNode node);
|
TreeResult led(ArraySlice *slice, Arena *arena, Node *left, Token token); // Left denotation
|
||||||
|
|
||||||
ParseResult parse(ASTNodeArray *arr);
|
ParserU8Result prefix_rbp(Token token);
|
||||||
ASTNode *parse_expr(ASTNodeSlice *slice, Arena *arena, uint8_t min_bp);
|
ParserU8Result postfix_lbp(Token token);
|
||||||
|
ParserU8Result infix_lbp(Token token);
|
||||||
|
ParserU8Result infix_rbp(Token token);
|
||||||
|
|
||||||
|
ParserResult parse(TokenizeResult tokens);
|
||||||
|
TreeResult parse_expr(ArraySlice *slice, Arena *arena, uint8_t min_bp);
|
||||||
|
|
||||||
#endif // !PARSER_H
|
#endif // !PARSER_H
|
||||||
|
|||||||
@@ -1,108 +0,0 @@
|
|||||||
#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;
|
|
||||||
}
|
|
||||||
105
src/evaluator.c
105
src/evaluator.c
@@ -2,34 +2,113 @@
|
|||||||
#include "arena.h"
|
#include "arena.h"
|
||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
|
||||||
int64_t evaluate_tree(ASTNode *tree) {
|
EvaluatorResult evaluate_tree(Node *tree) {
|
||||||
if (tree->type == NODE_BINARY_OP) {
|
if (tree->type == NODE_BINARY_OP) {
|
||||||
Operator op = tree->data.binary.op;
|
return evaluate_binary(tree);
|
||||||
ASTNode *left = tree->data.binary.left;
|
} else if (tree->type == NODE_UNARY_OP) {
|
||||||
ASTNode *right = tree->data.binary.right;
|
return evaluate_unary(tree);
|
||||||
|
}
|
||||||
|
|
||||||
|
return (EvaluatorResult) {
|
||||||
|
.is_valid = true,
|
||||||
|
.val = tree->num,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
EvaluatorResult evaluate_binary(Node *tree) {
|
||||||
|
Operator op = tree->binary.op;
|
||||||
|
Node *left = tree->binary.left;
|
||||||
|
Node *right = tree->binary.right;
|
||||||
|
|
||||||
|
EvaluatorResult left_result = evaluate_tree(left);
|
||||||
|
EvaluatorResult right_result = evaluate_tree(right);
|
||||||
|
if (!left_result.is_valid) {
|
||||||
|
return left_result;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!left_result.is_valid) {
|
||||||
|
return left_result;
|
||||||
|
}
|
||||||
|
|
||||||
switch (op) {
|
switch (op) {
|
||||||
case OP_ADD:
|
case OP_ADD:
|
||||||
return evaluate_tree(left) + evaluate_tree(right);
|
return (EvaluatorResult) {
|
||||||
|
.is_valid = true,
|
||||||
|
.val = left_result.val + right_result.val,
|
||||||
|
};
|
||||||
case OP_SUB:
|
case OP_SUB:
|
||||||
return evaluate_tree(left) - evaluate_tree(right);
|
return (EvaluatorResult) {
|
||||||
|
.is_valid = true,
|
||||||
|
.val = left_result.val - right_result.val,
|
||||||
|
};
|
||||||
case OP_MUL:
|
case OP_MUL:
|
||||||
return evaluate_tree(left) * evaluate_tree(right);
|
return (EvaluatorResult) {
|
||||||
|
.is_valid = true,
|
||||||
|
.val = left_result.val * right_result.val,
|
||||||
|
};
|
||||||
case OP_DIV:
|
case OP_DIV:
|
||||||
return evaluate_tree(left) / evaluate_tree(right);
|
return (EvaluatorResult) {
|
||||||
|
.is_valid = true,
|
||||||
|
.val = left_result.val / right_result.val,
|
||||||
|
};
|
||||||
|
case OP_POW:
|
||||||
|
return (EvaluatorResult) {
|
||||||
|
.is_valid = true,
|
||||||
|
.val = pow(left_result.val, right_result.val),
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
return (EvaluatorResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = EVALUATOR_INVALID_TREE,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t return_val = tree->data.integer;
|
EvaluatorResult evaluate_unary(Node *tree) {
|
||||||
return return_val;
|
Operator op = tree->unary.op;
|
||||||
|
Node *to = tree->unary.to;
|
||||||
|
|
||||||
|
EvaluatorResult result = evaluate_tree(to);
|
||||||
|
if (!result.is_valid) {
|
||||||
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t evaluate(ParseResult context) {
|
switch (op) {
|
||||||
int64_t result = evaluate_tree(context.tree);
|
case OP_ADD:
|
||||||
|
return result;
|
||||||
|
case OP_SUB:
|
||||||
|
return (EvaluatorResult) {
|
||||||
|
.is_valid = true,
|
||||||
|
.val = -result.val,
|
||||||
|
};
|
||||||
|
case OP_FACTORIAL:
|
||||||
|
return (EvaluatorResult) {
|
||||||
|
.is_valid = true,
|
||||||
|
.val = tgamma(result.val + 1),
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
return (EvaluatorResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = EVALUATOR_INVALID_TREE,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
EvaluatorResult evaluate(ParserResult context) {
|
||||||
|
if (!context.is_valid) {
|
||||||
|
return (EvaluatorResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = EVALUATOR_INVALID_PARSING,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
EvaluatorResult result = evaluate_tree(context.tree);
|
||||||
|
|
||||||
arena_destroy(&context.arena);
|
arena_destroy(&context.arena);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
|
|||||||
142
src/lexer.c
142
src/lexer.c
@@ -1,9 +1,11 @@
|
|||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
|
#include "arraylist.h"
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <math.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <strings.h>
|
#include <string.h>
|
||||||
#include <limits.h>
|
#include <limits.h>
|
||||||
|
|
||||||
typedef enum {
|
typedef enum {
|
||||||
@@ -12,110 +14,114 @@ typedef enum {
|
|||||||
} LexerState;
|
} LexerState;
|
||||||
|
|
||||||
|
|
||||||
LexerErr tokenize(const char *input, ASTNodeArray *out) {
|
TokenizeResult tokenize(const char *input) {
|
||||||
|
ArrayList *arr;
|
||||||
|
arraylist_init(&arr, 64, sizeof(Token));
|
||||||
size_t offset = 0;
|
size_t offset = 0;
|
||||||
LexerState state = WAIT_FOR_NUMBER;
|
|
||||||
ASTNodeArray arr = ASTNodeArray_init(0); // 0 defaults to 64
|
|
||||||
|
|
||||||
while (input[offset] != '\n' && input[offset] != '\0') {
|
while (input[offset] != '\0') {
|
||||||
int current = input[offset];
|
|
||||||
|
|
||||||
if (isdigit(current)) {
|
if (isdigit(input[offset])) {
|
||||||
if (state != WAIT_FOR_NUMBER) {
|
TokenResult result = tokenize_number(input, &offset);
|
||||||
ASTNodeArray_free(&arr);
|
|
||||||
return LEXER_WRONG_SYNTAX;
|
|
||||||
}
|
|
||||||
ASTNode new_node;
|
|
||||||
LexerErr result = tokenize_number(input, &offset, &new_node);
|
|
||||||
|
|
||||||
if (result != LEXER_OK) {
|
if (!result.is_valid) {
|
||||||
ASTNodeArray_free(&arr);
|
arraylist_destroy(&arr);
|
||||||
return result;
|
return (TokenizeResult) {.is_valid = false, .err = result.err};
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodeArray_push(&arr, new_node);
|
arraylist_push_back(arr, &result.token);
|
||||||
state = WAIT_FOR_OPERATOR;
|
} else if (isoperator(input[offset])) {
|
||||||
} else if (isoperator(current)) {
|
Token op_node = {
|
||||||
if (state != WAIT_FOR_OPERATOR) {
|
.type = TOKEN_OPERATOR,
|
||||||
return LEXER_WRONG_SYNTAX;
|
.op = char_to_operator(input[offset]),
|
||||||
}
|
|
||||||
ASTNode new_node = {
|
|
||||||
.type = NODE_BINARY_OP,
|
|
||||||
.data.binary.op = char_to_operator(current),
|
|
||||||
.data.binary.right = NULL,
|
|
||||||
.data.binary.left = NULL,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
ASTNodeArray_push(&arr, new_node);
|
arraylist_push_back(arr, &op_node);
|
||||||
state = WAIT_FOR_NUMBER;
|
} else if (isspace(input[offset])) {
|
||||||
} else if (isspace(current)) {
|
|
||||||
// Nothing...
|
// Nothing...
|
||||||
} else {
|
} else {
|
||||||
ASTNodeArray_free(&arr);
|
arraylist_destroy(&arr);
|
||||||
return LEXER_NOT_RECOGNIZED_SYMBOL;
|
return (TokenizeResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = LEXER_NOT_RECOGNIZED_SYMBOL};
|
||||||
}
|
}
|
||||||
|
|
||||||
offset++;
|
offset++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (arr.len < 1) {
|
if (arraylist_size(arr) < 1) {
|
||||||
return LEXER_EMPTY_INPUT;
|
arraylist_destroy(&arr);
|
||||||
|
return (TokenizeResult) {.is_valid = false, .err = LEXER_EMPTY_INPUT};
|
||||||
}
|
}
|
||||||
|
|
||||||
*out = arr;
|
return (TokenizeResult) {.is_valid = true, .arr = arr};
|
||||||
return LEXER_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// CURRENTLY, it only supports ints, not clear how floating
|
// CURRENTLY, it only supports ints, not clear how floating
|
||||||
// point is implemented but i'll figure it out
|
// point is implemented but i'll figure it out
|
||||||
LexerErr tokenize_number(const char *input, size_t *offset, ASTNode *out) {
|
TokenResult tokenize_number(const char *input, size_t *offset) {
|
||||||
char buf[128] = { '\0' };
|
char buf[64] = { '\0' };
|
||||||
size_t buf_pos = 0;
|
size_t buf_pos = 0;
|
||||||
bool is_integer = true; // Will later be used to differentiate fractions
|
bool is_integer = true; // Will later be used to differentiate fractions
|
||||||
|
|
||||||
|
// read number
|
||||||
size_t current = *offset;
|
size_t current = *offset;
|
||||||
while (isdigit(input[current])) {
|
while (isdigit(input[current])) {
|
||||||
|
if (buf_pos >= sizeof(buf) - 1) {
|
||||||
|
return (TokenResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = LEXER_BUF_OVERFLOW};
|
||||||
|
}
|
||||||
|
|
||||||
buf[buf_pos] = input[current];
|
buf[buf_pos] = input[current];
|
||||||
|
|
||||||
if (buf_pos >= sizeof(buf)) {
|
|
||||||
return LEXER_BUF_OVERFLOW;
|
|
||||||
}
|
|
||||||
current++;
|
current++;
|
||||||
buf_pos++;
|
buf_pos++;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNode new_node;
|
Token new_token;
|
||||||
if (is_integer) {
|
if (is_integer) {
|
||||||
new_node.type = NODE_INTEGER;
|
new_token.type = TOKEN_INTEGER;
|
||||||
LexerErr status = string_to_integer(buf, &new_node.data.integer);
|
LexerI64Result result = string_to_integer(buf);
|
||||||
if (status == LEXER_OK) {
|
|
||||||
*out = new_node;
|
|
||||||
}
|
if (!result.is_valid) {
|
||||||
*offset = current;
|
return (TokenResult) {.is_valid = false, .err = result.err};
|
||||||
return status;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return LEXER_FAILED_NUMBER_CONVERSION;
|
new_token.num = result.num;
|
||||||
|
|
||||||
|
*offset = current - 1;
|
||||||
|
return (TokenResult) {.is_valid = true, .token = new_token};
|
||||||
}
|
}
|
||||||
|
|
||||||
LexerErr string_to_integer(const char *buf, int64_t *number) {
|
return (TokenResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = LEXER_FAILED_NUMBER_CONVERSION};
|
||||||
|
}
|
||||||
|
|
||||||
|
LexerI64Result string_to_integer(const char *buf) {
|
||||||
int c = 0;
|
int c = 0;
|
||||||
int64_t count = 0;
|
int64_t count = 0;
|
||||||
|
|
||||||
while (buf[c] != '\0') {
|
while (buf[c] != '\0') {
|
||||||
|
|
||||||
|
// Extracts number from char
|
||||||
int digit = buf[c] - '0';
|
int digit = buf[c] - '0';
|
||||||
|
|
||||||
if (count > (INT64_MAX - digit) / 10) {
|
if (count > (INT64_MAX - digit) / 10) {
|
||||||
return LEXER_INT_OVERFLOW;
|
return (LexerI64Result) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = LEXER_INT_OVERFLOW};
|
||||||
}
|
}
|
||||||
|
|
||||||
count = count * 10;
|
count = count * 10;
|
||||||
count += digit;
|
count += digit;
|
||||||
|
|
||||||
c++;
|
c++;
|
||||||
}
|
}
|
||||||
|
|
||||||
*number = count;
|
return (LexerI64Result) {.is_valid = true, .num = count};
|
||||||
return LEXER_OK;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool isoperator(int c) {
|
bool isoperator(int c) {
|
||||||
@@ -124,6 +130,10 @@ bool isoperator(int c) {
|
|||||||
case '-':
|
case '-':
|
||||||
case '/':
|
case '/':
|
||||||
case '*':
|
case '*':
|
||||||
|
case '^':
|
||||||
|
case '!':
|
||||||
|
case '(':
|
||||||
|
case ')':
|
||||||
return true;
|
return true;
|
||||||
default:
|
default:
|
||||||
return false;
|
return false;
|
||||||
@@ -144,6 +154,18 @@ Operator char_to_operator(int c) {
|
|||||||
case '/':
|
case '/':
|
||||||
return OP_DIV;
|
return OP_DIV;
|
||||||
break;
|
break;
|
||||||
|
case '^':
|
||||||
|
return OP_POW;
|
||||||
|
break;
|
||||||
|
case '!':
|
||||||
|
return OP_FACTORIAL;
|
||||||
|
break;
|
||||||
|
case '(':
|
||||||
|
return OP_START_PAR;
|
||||||
|
break;
|
||||||
|
case ')':
|
||||||
|
return OP_END_PAR;
|
||||||
|
break;
|
||||||
default: // I mean shouldn't be used, we assume
|
default: // I mean shouldn't be used, we assume
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
@@ -159,5 +181,15 @@ char operator_to_char(Operator op) {
|
|||||||
return '*';
|
return '*';
|
||||||
case OP_DIV:
|
case OP_DIV:
|
||||||
return '/';
|
return '/';
|
||||||
|
case OP_POW:
|
||||||
|
return '^';
|
||||||
|
case OP_FACTORIAL:
|
||||||
|
return '!';
|
||||||
|
case OP_START_PAR:
|
||||||
|
return '(';
|
||||||
|
case OP_END_PAR:
|
||||||
|
return ')';
|
||||||
|
default:
|
||||||
|
return EOF;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
13
src/main.c
13
src/main.c
@@ -1,4 +1,3 @@
|
|||||||
#include "arena.h"
|
|
||||||
#include "evaluator.h"
|
#include "evaluator.h"
|
||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
@@ -19,13 +18,11 @@ int main(void) {
|
|||||||
}
|
}
|
||||||
buf[pos] = '\0';
|
buf[pos] = '\0';
|
||||||
|
|
||||||
ASTNodeArray context;
|
EvaluatorResult result = evaluate(parse(tokenize(buf)));
|
||||||
tokenize(buf, &context);
|
if (!result.is_valid) {
|
||||||
|
puts("Error checando expresion");
|
||||||
|
}
|
||||||
|
|
||||||
ParseResult par = parse(&context);
|
printf("El resultado es: %" PRIi64 "\n", result.val);
|
||||||
int64_t result = evaluate(par);
|
|
||||||
|
|
||||||
|
|
||||||
printf("El resultado es: %" PRIi64 "\n", result);
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
466
src/parser.c
466
src/parser.c
@@ -1,134 +1,394 @@
|
|||||||
#include "parser.h"
|
#include "parser.h"
|
||||||
|
#include "arraylist.h"
|
||||||
#include "lexer.h"
|
#include "lexer.h"
|
||||||
#include "arena.h"
|
#include "arena.h"
|
||||||
|
#include <cmocka.h>
|
||||||
#include <stdalign.h>
|
#include <stdalign.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
|
||||||
|
|
||||||
|
ParserU8Result prefix_rbp(Token token) {
|
||||||
uint8_t node_lbp(ASTNode node) {
|
if (token.type == TOKEN_INTEGER) {
|
||||||
if (node.type == NODE_INTEGER) {
|
return (ParserU8Result) {
|
||||||
return 0;
|
.is_valid = false,
|
||||||
}
|
.err = PARSER_UNEXPECTED_TOKEN,
|
||||||
|
|
||||||
switch (node.data.binary.op) {
|
|
||||||
case OP_ADD:
|
|
||||||
case OP_SUB:
|
|
||||||
return 10;
|
|
||||||
break;
|
|
||||||
case OP_DIV:
|
|
||||||
case OP_MUL:
|
|
||||||
return 20;
|
|
||||||
default:
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
uint8_t node_rbp(ASTNode node) {
|
|
||||||
if (node.type == NODE_INTEGER) {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
switch (node.data.binary.op) {
|
|
||||||
case OP_ADD:
|
|
||||||
case OP_SUB:
|
|
||||||
return 11;
|
|
||||||
break;
|
|
||||||
case OP_DIV:
|
|
||||||
case OP_MUL:
|
|
||||||
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;
|
}
|
||||||
|
switch (token.op) {
|
||||||
|
case OP_SUB:
|
||||||
|
case OP_ADD:
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = true,
|
||||||
|
.num = 30,
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = PARSER_UNEXPECTED_TOKEN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return (ParseResult) {
|
ParserU8Result postfix_lbp(Token token) {
|
||||||
|
if (token.type != TOKEN_OPERATOR) {
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = PARSER_UNEXPECTED_TOKEN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (token.op) {
|
||||||
|
case OP_FACTORIAL:
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = true,
|
||||||
|
.num = 40,
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = PARSER_UNEXPECTED_TOKEN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ParserU8Result infix_lbp(Token token) {
|
||||||
|
if (token.type != TOKEN_OPERATOR) {
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = PARSER_UNEXPECTED_TOKEN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (token.op) {
|
||||||
|
case OP_ADD:
|
||||||
|
case OP_SUB:
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = true,
|
||||||
|
.num = 10,
|
||||||
|
};
|
||||||
|
case OP_DIV:
|
||||||
|
case OP_MUL:
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = true,
|
||||||
|
.num = 20,
|
||||||
|
};
|
||||||
|
case OP_POW:
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = true,
|
||||||
|
.num = 51,
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = PARSER_UNEXPECTED_TOKEN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
ParserU8Result infix_rbp(Token token) {
|
||||||
|
if (token.type != TOKEN_OPERATOR) {
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = PARSER_UNEXPECTED_TOKEN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (token.op) {
|
||||||
|
case OP_ADD:
|
||||||
|
case OP_SUB:
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = true,
|
||||||
|
.num = 11,
|
||||||
|
};
|
||||||
|
case OP_DIV:
|
||||||
|
case OP_MUL:
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = true,
|
||||||
|
.num = 21,
|
||||||
|
};
|
||||||
|
case OP_POW:
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = true,
|
||||||
|
.num = 50,
|
||||||
|
};
|
||||||
|
default:
|
||||||
|
return (ParserU8Result) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = PARSER_UNEXPECTED_TOKEN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeResult led(
|
||||||
|
ArraySlice *slice,
|
||||||
|
Arena *arena,
|
||||||
|
Node *left,
|
||||||
|
Token token
|
||||||
|
) {
|
||||||
|
arena_ensure_capacity(
|
||||||
|
arena,
|
||||||
|
sizeof(Node),
|
||||||
|
alignof(Node)
|
||||||
|
);
|
||||||
|
|
||||||
|
Node *node = arena_unwrap_pointer(
|
||||||
|
arena_alloc(
|
||||||
|
arena,
|
||||||
|
sizeof(Node),
|
||||||
|
alignof(Node)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
switch (token.op) {
|
||||||
|
|
||||||
|
// Binary operators
|
||||||
|
case OP_ADD:
|
||||||
|
case OP_SUB:
|
||||||
|
case OP_MUL:
|
||||||
|
case OP_DIV:
|
||||||
|
case OP_POW: {
|
||||||
|
node->type = NODE_BINARY_OP;
|
||||||
|
node->binary.op = token.op;
|
||||||
|
|
||||||
|
ParserU8Result rbp_result = infix_rbp(token);
|
||||||
|
if (!rbp_result.is_valid) {
|
||||||
|
return (TreeResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = rbp_result.err,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeResult right = parse_expr(
|
||||||
|
slice,
|
||||||
|
arena,
|
||||||
|
rbp_result.num
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!right.is_valid) {
|
||||||
|
return right;
|
||||||
|
}
|
||||||
|
|
||||||
|
node->binary.left = left;
|
||||||
|
node->binary.right = right.node;
|
||||||
|
|
||||||
|
return (TreeResult) {
|
||||||
|
.is_valid = true,
|
||||||
|
.node = node,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// Postfix operators
|
||||||
|
case OP_FACTORIAL: {
|
||||||
|
node->type = NODE_UNARY_OP;
|
||||||
|
node->unary.op = token.op;
|
||||||
|
node->unary.to = left;
|
||||||
|
|
||||||
|
return (TreeResult) {
|
||||||
|
.is_valid = true,
|
||||||
|
.node = node,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
return (TreeResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = PARSER_UNEXPECTED_TOKEN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeResult nud(ArraySlice *slice, Arena *arena, Token token) {
|
||||||
|
arena_ensure_capacity(
|
||||||
|
arena,
|
||||||
|
sizeof(Node),
|
||||||
|
alignof(Node)
|
||||||
|
);
|
||||||
|
|
||||||
|
Node *node = arena_unwrap_pointer(
|
||||||
|
arena_alloc(
|
||||||
|
arena,
|
||||||
|
sizeof(Node),
|
||||||
|
alignof(Node)
|
||||||
|
)
|
||||||
|
);
|
||||||
|
|
||||||
|
if (token.type == TOKEN_INTEGER) {
|
||||||
|
node->type = NODE_INT;
|
||||||
|
node->num = token.num;
|
||||||
|
|
||||||
|
return (TreeResult) {
|
||||||
|
.is_valid = true,
|
||||||
|
.node = node,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
switch (token.op) {
|
||||||
|
case OP_START_PAR: {
|
||||||
|
TreeResult expr = parse_expr(slice, arena, 0);
|
||||||
|
if (!expr.is_valid) {
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
|
||||||
|
Token end_par;
|
||||||
|
if (arrayslice_next(slice, &end_par) != ARRLIST_OK) {
|
||||||
|
return (TreeResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = PARSER_UNMATCHED_PAREN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (end_par.type != TOKEN_OPERATOR ||
|
||||||
|
end_par.op != OP_END_PAR) {
|
||||||
|
return (TreeResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = PARSER_UNMATCHED_PAREN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return expr;
|
||||||
|
}
|
||||||
|
case OP_ADD:
|
||||||
|
|
||||||
|
case OP_SUB: {
|
||||||
|
node->type = NODE_UNARY_OP;
|
||||||
|
node->unary.op = token.op;
|
||||||
|
|
||||||
|
ParserU8Result rbp_result = prefix_rbp(token);
|
||||||
|
if (!rbp_result.is_valid) {
|
||||||
|
return (TreeResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = rbp_result.err,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
TreeResult right = parse_expr(
|
||||||
|
slice,
|
||||||
|
arena,
|
||||||
|
rbp_result.num
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!right.is_valid) {
|
||||||
|
return right;
|
||||||
|
}
|
||||||
|
|
||||||
|
node->unary.to = right.node;
|
||||||
|
|
||||||
|
return (TreeResult) {
|
||||||
|
.is_valid = true,
|
||||||
|
.node = node,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return (TreeResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = PARSER_UNEXPECTED_TOKEN,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
ParserResult parse(TokenizeResult tokens) {
|
||||||
|
if (!tokens.is_valid) {
|
||||||
|
return (ParserResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = PARSER_INVALID_TOKENIZE,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
ArraySlice *context;
|
||||||
|
arraylist_slice(&context, tokens.arr, 0, arraylist_size(tokens.arr));
|
||||||
|
Arena *arena;
|
||||||
|
arena_init(&arena, sizeof(Node) * arraylist_size(tokens.arr));
|
||||||
|
|
||||||
|
TreeResult result = parse_expr(context, arena, 0);
|
||||||
|
if (!result.is_valid) {
|
||||||
|
arena_destroy(&arena);
|
||||||
|
arraylist_destroy(&tokens.arr);
|
||||||
|
return (ParserResult) {
|
||||||
|
.is_valid = false,
|
||||||
|
.err = result.err,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
arraylist_destroy(&tokens.arr);
|
||||||
|
return (ParserResult) {
|
||||||
|
.is_valid = true,
|
||||||
.arena = arena,
|
.arena = arena,
|
||||||
.tree = parse_expr(&context, &arena, 0)};
|
.tree = result.node};
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNode *parse_expr(ASTNodeSlice *slice, Arena *arena, uint8_t min_bp) {
|
TreeResult parse_expr(ArraySlice *slice, Arena *arena, uint8_t min_bp) {
|
||||||
arena_ensure_capacity(
|
Token current_token;
|
||||||
arena,
|
|
||||||
sizeof(ASTNode),
|
|
||||||
alignof(ASTNode)
|
|
||||||
);
|
|
||||||
|
|
||||||
ASTNode *left_side = arena_unwrap_pointer(
|
if (arrayslice_next(slice, ¤t_token) != ARRLIST_OK) {
|
||||||
arena_alloc(
|
return (TreeResult) {
|
||||||
arena,
|
.is_valid = false,
|
||||||
sizeof(ASTNode),
|
.err = PARSER_UNEXPECTED_EOF,
|
||||||
alignof(ASTNode)
|
};
|
||||||
)
|
}
|
||||||
);
|
|
||||||
|
|
||||||
*left_side = ASTNodeSlice_next(slice);
|
TreeResult left_result = nud(slice, arena, current_token);
|
||||||
|
|
||||||
while (true) {
|
if (!left_result.is_valid) {
|
||||||
if (!ASTNodeSlice_is_valid(slice)) {
|
return left_result;
|
||||||
|
}
|
||||||
|
|
||||||
|
Node *left_side = left_result.node;
|
||||||
|
|
||||||
|
while (arrayslice_is_valid(slice)) {
|
||||||
|
Token operator_token;
|
||||||
|
arrayslice_peek(slice, &operator_token);
|
||||||
|
|
||||||
|
if (operator_token.type != TOKEN_OPERATOR) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNode operator = ASTNodeSlice_peek(slice);
|
ParserU8Result postfix_lbp_result = postfix_lbp(operator_token);
|
||||||
uint8_t rbp = node_rbp(operator);
|
|
||||||
uint8_t lbp = node_lbp(operator);
|
|
||||||
|
|
||||||
if (lbp < min_bp) {
|
if (postfix_lbp_result.is_valid) {
|
||||||
|
if (postfix_lbp_result.num < min_bp) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
ASTNodeSlice_next(slice);
|
arrayslice_next(slice, NULL);
|
||||||
ASTNode *right_side = parse_expr(slice, arena, rbp);
|
|
||||||
|
|
||||||
arena_ensure_capacity(
|
TreeResult result = led(slice, arena, left_side, operator_token);
|
||||||
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;
|
if (!result.is_valid) {
|
||||||
new_node->data.binary.right = right_side;
|
return result;
|
||||||
|
|
||||||
left_side = new_node;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
left_side = result.node;
|
||||||
|
|
||||||
return left_side;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Path for infix basically
|
||||||
|
ParserU8Result lbp_result = infix_lbp(operator_token);
|
||||||
|
|
||||||
|
if (!lbp_result.is_valid) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (lbp_result.num < min_bp) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
arrayslice_next(slice, NULL);
|
||||||
|
|
||||||
|
TreeResult result = led(slice, arena, left_side, operator_token);
|
||||||
|
|
||||||
|
if (!result.is_valid) {
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
left_side = result.node;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Final: return left side
|
||||||
|
return (TreeResult){
|
||||||
|
.is_valid = true,
|
||||||
|
.node = left_side,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,31 +1,114 @@
|
|||||||
find_package(cmocka REQUIRED)
|
# ================================================================
|
||||||
|
# FIND CMOCKA (system → FetchContent as fallback)
|
||||||
|
# ================================================================
|
||||||
|
find_package(cmocka QUIET)
|
||||||
|
|
||||||
add_executable(test_nodeArray test_ASTNodeArray.c)
|
if(NOT cmocka_FOUND)
|
||||||
add_executable(test_lexer test_lexer.c)
|
message(
|
||||||
add_executable(test_parser test_parser.c)
|
STATUS
|
||||||
add_executable(test_evaluator test_evaluator.c)
|
"[calculator] cmocka not found on system — fetching with FetchContent...")
|
||||||
|
|
||||||
target_link_libraries(test_nodeArray
|
include(FetchContent)
|
||||||
calculator_lib
|
FetchContent_Declare(
|
||||||
cmocka::cmocka
|
cmocka
|
||||||
|
GIT_REPOSITORY https://git.cryptomilk.org/projects/cmocka.git
|
||||||
|
GIT_TAG cmocka-1.1.7
|
||||||
|
GIT_SHALLOW TRUE)
|
||||||
|
|
||||||
|
# Static lib only, no cmocka examples or self-tests
|
||||||
|
set(WITH_STATIC_LIB
|
||||||
|
ON
|
||||||
|
CACHE BOOL "" FORCE)
|
||||||
|
set(WITH_SHARED_LIB
|
||||||
|
OFF
|
||||||
|
CACHE BOOL "" FORCE)
|
||||||
|
set(WITH_CMOCKERY_SUPPORT
|
||||||
|
OFF
|
||||||
|
CACHE BOOL "" FORCE)
|
||||||
|
set(WITH_EXAMPLES
|
||||||
|
OFF
|
||||||
|
CACHE BOOL "" FORCE)
|
||||||
|
set(UNIT_TESTING
|
||||||
|
OFF
|
||||||
|
CACHE BOOL "" FORCE)
|
||||||
|
set(PICKY_DEVELOPER
|
||||||
|
OFF
|
||||||
|
CACHE BOOL "" FORCE)
|
||||||
|
|
||||||
|
FetchContent_MakeAvailable(cmocka)
|
||||||
|
|
||||||
|
# Normalize the target name (varies across cmocka versions)
|
||||||
|
if(NOT TARGET cmocka::cmocka)
|
||||||
|
if(TARGET cmocka-static)
|
||||||
|
add_library(cmocka::cmocka ALIAS cmocka-static)
|
||||||
|
elseif(TARGET cmocka)
|
||||||
|
add_library(cmocka::cmocka ALIAS cmocka)
|
||||||
|
else()
|
||||||
|
message(
|
||||||
|
WARNING "[calculator] Could not create cmocka::cmocka — skipping tests."
|
||||||
)
|
)
|
||||||
|
return()
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
target_link_libraries(test_lexer
|
# ================================================================
|
||||||
calculator_lib
|
# DETECT ADDRESS SANITIZER SUPPORT
|
||||||
cmocka::cmocka
|
# ================================================================
|
||||||
)
|
set(CALCULATOR_USE_ASAN OFF)
|
||||||
|
|
||||||
target_link_libraries(test_parser
|
if(CALCULATOR_ENABLE_SANITIZERS)
|
||||||
calculator_lib
|
if(MSVC)
|
||||||
cmocka::cmocka
|
if(MSVC_VERSION GREATER_EQUAL 1928)
|
||||||
)
|
set(CALCULATOR_USE_ASAN ON)
|
||||||
|
else()
|
||||||
|
message(STATUS "[calculator] MSVC < 16.9: ASAN not available.")
|
||||||
|
endif()
|
||||||
|
elseif(CMAKE_C_COMPILER_ID MATCHES "GNU|Clang" AND NOT WIN32)
|
||||||
|
include(CheckCCompilerFlag)
|
||||||
|
check_c_compiler_flag(-fsanitize=address CALCULATOR_COMPILER_HAS_ASAN)
|
||||||
|
if(CALCULATOR_COMPILER_HAS_ASAN)
|
||||||
|
set(CALCULATOR_USE_ASAN ON)
|
||||||
|
else()
|
||||||
|
message(
|
||||||
|
STATUS "[calculator] Compiler does not support -fsanitize=address.")
|
||||||
|
endif()
|
||||||
|
else()
|
||||||
|
message(STATUS "[calculator] Unknown platform/compiler — ASAN skipped.")
|
||||||
|
endif()
|
||||||
|
|
||||||
target_link_libraries(test_evaluator
|
if(CALCULATOR_USE_ASAN)
|
||||||
calculator_lib
|
message(STATUS "[calculator] Address Sanitizer enabled.")
|
||||||
cmocka::cmocka
|
endif()
|
||||||
)
|
endif()
|
||||||
|
|
||||||
add_test(NAME nodeArray_tests COMMAND test_nodeArray)
|
# ================================================================
|
||||||
add_test(NAME lexer_tests COMMAND test_lexer)
|
# Helper: apply common settings to every test target
|
||||||
add_test(NAME parser_tests COMMAND test_parser)
|
# ================================================================
|
||||||
add_test(NAME evaluator_tests COMMAND test_evaluator)
|
function(calculator_add_test target source)
|
||||||
|
add_executable(${target} ${source})
|
||||||
|
|
||||||
|
target_link_libraries(${target} PRIVATE calculator_lib cmocka::cmocka)
|
||||||
|
|
||||||
|
target_compile_features(${target} PRIVATE c_std_11)
|
||||||
|
|
||||||
|
if(CALCULATOR_USE_ASAN)
|
||||||
|
if(MSVC)
|
||||||
|
target_compile_options(${target} PRIVATE /fsanitize=address)
|
||||||
|
else()
|
||||||
|
target_compile_options(${target} PRIVATE -fsanitize=address
|
||||||
|
-fno-omit-frame-pointer)
|
||||||
|
target_link_options(${target} PRIVATE -fsanitize=address)
|
||||||
|
endif()
|
||||||
|
endif()
|
||||||
|
|
||||||
|
add_test(NAME ${target} COMMAND ${target})
|
||||||
|
set_tests_properties(${target} PROPERTIES TIMEOUT 30)
|
||||||
|
endfunction()
|
||||||
|
|
||||||
|
# ================================================================
|
||||||
|
# TEST TARGETS
|
||||||
|
# ================================================================
|
||||||
|
calculator_add_test(test_lexer test_lexer.c)
|
||||||
|
calculator_add_test(test_parser test_parser.c)
|
||||||
|
calculator_add_test(test_evaluator test_evaluator.c)
|
||||||
|
|||||||
@@ -1,86 +0,0 @@
|
|||||||
#include "lexer.h"
|
|
||||||
#include <stdarg.h>
|
|
||||||
#include <stdbool.h>
|
|
||||||
#include <stddef.h>
|
|
||||||
#include <stdint.h>
|
|
||||||
#include <setjmp.h>
|
|
||||||
#include <cmocka.h>
|
|
||||||
|
|
||||||
static void test_array_push(void **state) {
|
|
||||||
(void) state;
|
|
||||||
|
|
||||||
// We use 2 to force resize and checking anything wrong with malloc
|
|
||||||
ASTNodeArray arr = ASTNodeArray_init(2);
|
|
||||||
ASTNode node1 = {
|
|
||||||
.type = NODE_INTEGER,
|
|
||||||
.data = { .integer = 90 }
|
|
||||||
};
|
|
||||||
|
|
||||||
ASTNode node2 = {
|
|
||||||
.type = NODE_INTEGER,
|
|
||||||
.data = { .integer = 80 }
|
|
||||||
};
|
|
||||||
|
|
||||||
ASTNode node3 = {
|
|
||||||
.type = NODE_INTEGER,
|
|
||||||
.data = { .integer = 70 }
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_int_equal(ASTNodeArray_push(&arr, node1), ARRAY_OK);
|
|
||||||
assert_int_equal(ASTNodeArray_len(&arr), 1);
|
|
||||||
|
|
||||||
assert_int_equal(ASTNodeArray_push(&arr, node2), ARRAY_OK);
|
|
||||||
assert_int_equal(ASTNodeArray_len(&arr), 2);
|
|
||||||
|
|
||||||
assert_int_equal(ASTNodeArray_push(&arr, node3), ARRAY_OK);
|
|
||||||
assert_int_equal(ASTNodeArray_len(&arr), 3);
|
|
||||||
|
|
||||||
ASTNodeArray_free(&arr);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_array_pop(void **state) {
|
|
||||||
(void) state;
|
|
||||||
|
|
||||||
// Set to force desize
|
|
||||||
ASTNodeArray arr = ASTNodeArray_init(16);
|
|
||||||
ASTNode node1 = {
|
|
||||||
.type = NODE_INTEGER,
|
|
||||||
.data = { .integer = 90 }
|
|
||||||
};
|
|
||||||
|
|
||||||
ASTNode node2 = {
|
|
||||||
.type = NODE_INTEGER,
|
|
||||||
.data = { .integer = 80 }
|
|
||||||
};
|
|
||||||
|
|
||||||
ASTNode node3 = {
|
|
||||||
.type = NODE_INTEGER,
|
|
||||||
.data = { .integer = 70 }
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_int_equal(ASTNodeArray_push(&arr, node1), ARRAY_OK);
|
|
||||||
assert_int_equal(ASTNodeArray_len(&arr), 1);
|
|
||||||
|
|
||||||
assert_int_equal(ASTNodeArray_push(&arr, node2), ARRAY_OK);
|
|
||||||
assert_int_equal(ASTNodeArray_len(&arr), 2);
|
|
||||||
|
|
||||||
assert_int_equal(ASTNodeArray_push(&arr, node3), ARRAY_OK);
|
|
||||||
assert_int_equal(ASTNodeArray_len(&arr), 3);
|
|
||||||
|
|
||||||
ASTNode node4;
|
|
||||||
assert_int_equal(ASTNodeArray_pop(&arr, 1, &node4), ARRAY_OK);
|
|
||||||
assert_int_equal(node4.type, NODE_INTEGER);
|
|
||||||
assert_int_equal(node4.data.integer, 80);
|
|
||||||
|
|
||||||
ASTNodeArray_free(&arr);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
int main(void) {
|
|
||||||
const struct CMUnitTest tests[] = {
|
|
||||||
cmocka_unit_test(test_array_push),
|
|
||||||
cmocka_unit_test(test_array_pop),
|
|
||||||
};
|
|
||||||
|
|
||||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
|
||||||
}
|
|
||||||
@@ -1,6 +1,3 @@
|
|||||||
#include "lexer.h"
|
|
||||||
#include "parser.h"
|
|
||||||
#include "evaluator.h"
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
@@ -9,24 +6,8 @@
|
|||||||
#include <cmocka.h>
|
#include <cmocka.h>
|
||||||
#include <stdlib.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) {
|
int main(void) {
|
||||||
const struct CMUnitTest tests[] = {
|
|
||||||
cmocka_unit_test(test_basic_evaluation),
|
|
||||||
};
|
|
||||||
|
|
||||||
cmocka_run_group_tests(tests, NULL, NULL);
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,112 +1,11 @@
|
|||||||
#include "lexer.h"
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include <cmocka.h>
|
#include <cmocka.h>
|
||||||
|
#include <stdlib.h>
|
||||||
static void test_tokenize_normal_expresion(void **state) {
|
|
||||||
(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);
|
|
||||||
|
|
||||||
ASTNodeArray_get(&tokens, 0, &node);
|
|
||||||
assert_int_equal(node.type, NODE_INTEGER);
|
|
||||||
assert_int_equal(node.data.integer, 2);
|
|
||||||
|
|
||||||
ASTNodeArray_get(&tokens, 1, &node);
|
|
||||||
assert_int_equal(node.type, NODE_BINARY_OP);
|
|
||||||
assert_int_equal(node.data.binary.op, OP_ADD);
|
|
||||||
|
|
||||||
ASTNodeArray_get(&tokens, 2, &node);
|
|
||||||
assert_int_equal(node.type, NODE_INTEGER);
|
|
||||||
assert_int_equal(node.data.integer, 3);
|
|
||||||
|
|
||||||
ASTNodeArray_get(&tokens, 3, &node);
|
|
||||||
assert_int_equal(node.type, NODE_BINARY_OP);
|
|
||||||
assert_int_equal(node.data.binary.op, OP_DIV);
|
|
||||||
|
|
||||||
ASTNodeArray_get(&tokens, 4, &node);
|
|
||||||
assert_int_equal(node.type, NODE_INTEGER);
|
|
||||||
assert_int_equal(node.data.integer, 66);
|
|
||||||
|
|
||||||
ASTNodeArray_get(&tokens, 5, &node);
|
|
||||||
assert_int_equal(node.type, NODE_BINARY_OP);
|
|
||||||
assert_int_equal(node.data.binary.op, OP_MUL);
|
|
||||||
|
|
||||||
ASTNodeArray_get(&tokens, 6, &node);
|
|
||||||
assert_int_equal(node.type, NODE_INTEGER);
|
|
||||||
assert_int_equal(node.data.integer, 789);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_tokenize_unrecognized_symbol(void **state) {
|
|
||||||
(void) state;
|
|
||||||
|
|
||||||
char expr[256] = " 2 j 3 / 66 } 789";
|
|
||||||
ASTNodeArray tokens = {
|
|
||||||
.len = 0,
|
|
||||||
.cap = 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_int_equal(tokenize(expr, &tokens), LEXER_NOT_RECOGNIZED_SYMBOL);
|
|
||||||
assert_int_equal(tokens.len, 0);
|
|
||||||
assert_int_equal(tokens.cap, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_tokenize_wrong_sintax(void **state) {
|
|
||||||
(void) state;
|
|
||||||
|
|
||||||
char expr[256] = "2 3 / 66 789";
|
|
||||||
ASTNodeArray tokens = {
|
|
||||||
.len = 0,
|
|
||||||
.cap = 0,
|
|
||||||
};
|
|
||||||
|
|
||||||
assert_int_equal(tokenize(expr, &tokens), LEXER_WRONG_SYNTAX);
|
|
||||||
assert_int_equal(tokens.len, 0);
|
|
||||||
assert_int_equal(tokens.cap, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_string_to_number_normal(void **state) {
|
|
||||||
(void) state;
|
|
||||||
|
|
||||||
char num[16] = "2333t55";
|
|
||||||
size_t offset = 0;
|
|
||||||
ASTNode result;
|
|
||||||
|
|
||||||
assert_int_equal(tokenize_number(num, &offset, &result), LEXER_OK);
|
|
||||||
|
|
||||||
assert_int_equal(offset, 4); // equal to t position in string
|
|
||||||
assert_int_equal(result.type, NODE_INTEGER);
|
|
||||||
assert_int_equal(result.data.integer, 2333);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void test_string_to_number_overflow(void **state) {
|
|
||||||
(void) state;
|
|
||||||
|
|
||||||
// Number is INT64_MAX but with a extra 8 at the end
|
|
||||||
char num[32] = "92233720368547758078yy7";
|
|
||||||
size_t offset = 0;
|
|
||||||
ASTNode result;
|
|
||||||
assert_int_equal(tokenize_number(num, &offset, &result), LEXER_INT_OVERFLOW);
|
|
||||||
// Technically it can trigger a buf overflow error but obvioulsy
|
|
||||||
// it will trigger int overflow error first
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
const struct CMUnitTest tests[] = {
|
return EXIT_SUCCESS;
|
||||||
cmocka_unit_test(test_string_to_number_normal),
|
|
||||||
cmocka_unit_test(test_string_to_number_overflow),
|
|
||||||
cmocka_unit_test(test_tokenize_normal_expresion),
|
|
||||||
cmocka_unit_test(test_tokenize_unrecognized_symbol),
|
|
||||||
cmocka_unit_test(test_tokenize_wrong_sintax),
|
|
||||||
};
|
|
||||||
|
|
||||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,81 +1,11 @@
|
|||||||
#include "arena.h"
|
|
||||||
#include "lexer.h"
|
|
||||||
#include "parser.h"
|
|
||||||
#include <stdarg.h>
|
#include <stdarg.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
#include <stddef.h>
|
#include <stddef.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <setjmp.h>
|
#include <setjmp.h>
|
||||||
#include <cmocka.h>
|
#include <cmocka.h>
|
||||||
|
#include <stdlib.h>
|
||||||
static void test_parsing_basic_expression(void **state) {
|
|
||||||
(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) {
|
int main(void) {
|
||||||
const struct CMUnitTest tests [] = {
|
return EXIT_SUCCESS;
|
||||||
cmocka_unit_test(test_parsing_basic_expression),
|
|
||||||
};
|
|
||||||
|
|
||||||
return cmocka_run_group_tests(tests, NULL, NULL);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user