diff --git a/README.md b/README.md index dc612bc..54b093f 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,3 @@ # SCL: Simple Calculator Language -Project structure: - -```bash -$ tree -d -. -├── build # Build directory; where binaries (except the main one) go. -│   ├── obj # Project objects. -│   └── test # Test build directory. -│   └── obj # Test objects. -├── src # Source files (*.c). -│   └── include # Header files (*.h) -└── test # Tests (file names match those in src/*.c). - └── unity # Unity test framework files. -``` +A rewrite of halk. diff --git a/src/include/lexer.h b/src/include/lexer.h index d5ec072..d5791fe 100644 --- a/src/include/lexer.h +++ b/src/include/lexer.h @@ -21,7 +21,7 @@ typedef struct { char* cchar; // The current character. Token** tokens; // The tokens produced. size_t ntokens; // The number of tokens. - LexerState state; // What the lexxer is looking at. + LexerState state; // What the lexer is looking at. } Lexer; // Create a lexer. @@ -42,4 +42,7 @@ void lexer_do_call(Lexer* lexer); // Convert text to tokens. void lexer_lex(Lexer* lexer); +// Add a token to the lexer. +void lexer_add_token(Lexer* lexer, Token* token); + #endif diff --git a/src/lexer.c b/src/lexer.c index d5b6946..fe225cf 100644 --- a/src/lexer.c +++ b/src/lexer.c @@ -1,4 +1,6 @@ #include "include/lexer.h" +#include "include/token.h" +#include Lexer* lexer_init(char* src) { Lexer* lexer = malloc(sizeof(Lexer)); @@ -20,7 +22,15 @@ void lexer_destroy(Lexer* lexer) { for (int i = 0; i < lexer->ntokens; token_destroy(lexer->tokens[i++])); } -void lexer_do_confused(Lexer* lexer) {} +void lexer_do_confused(Lexer* lexer) { + int c = atoi(lexer->cchar); + + if (c) lexer_add_token(lexer, token_init(TOKEN_TYPE_NUMBER, lexer->cchar)); + else lexer_add_token(lexer, token_init(TOKEN_TYPE_CALL, lexer->cchar)); +} + +void lexer_do_number(Lexer* lexer) {} +void lexer_do_call(Lexer* lexer) {} void lexer_lex(Lexer* lexer) { while (*lexer->cchar) { @@ -32,3 +42,8 @@ void lexer_lex(Lexer* lexer) { } } } + +void lexer_add_token(Lexer* lexer, Token* token) { + (void)reallocarray(lexer->tokens, lexer->ntokens++, sizeof(Token)); + lexer->tokens[lexer->ntokens-1] = token; +} diff --git a/test/main.c b/test/main.c index 7a793a0..e576d4c 100644 --- a/test/main.c +++ b/test/main.c @@ -1,5 +1,5 @@ -#include "unity/unity.h" #include "registry.h" +#include "unity/unity.h" void (*tests[30])(); diff --git a/test/registry.c b/test/registry.c index bef64c5..9edcb45 100644 --- a/test/registry.c +++ b/test/registry.c @@ -1,5 +1,5 @@ -#include "unity/unity_internals.h" #include "registry.h" +#include "unity/unity_internals.h" #define TESTS_MAX 128 @@ -7,13 +7,14 @@ static Test tests[TESTS_MAX]; static int test_count = 0; void register_test(Test t) { - if (test_count < TESTS_MAX) { - tests[test_count] = t; - test_count++; - } else - printf("Maximum number of tests (%d) exceeded.\n", TESTS_MAX); + if (test_count < TESTS_MAX) { + tests[test_count] = t; + test_count++; + } else printf("Maximum number of tests (%d) exceeded.\n", TESTS_MAX); } void run_all_tests() { - for (int i = 0; i < test_count; i++) tests[i](); + for (int i = 0; i < test_count; i++) { + int res = tests[i](); + } } diff --git a/test/registry.h b/test/registry.h index c964751..bb5d299 100644 --- a/test/registry.h +++ b/test/registry.h @@ -2,12 +2,12 @@ #define REGISTRY_H #include +#include "unity/unity.h" -// Test functions neither consume nor return anything. -typedef void (*Test)(void); +typedef int (*Test)(void); // Register a new test function. -void register_test(Test t); +void register_test(Test); // Run all registered tests. void run_all_tests(); diff --git a/test/token.c b/test/token.c index 4938614..a83466f 100644 --- a/test/token.c +++ b/test/token.c @@ -1,31 +1,19 @@ -#include #include "unity/unity.h" +#include -#include "registry.h" -#include "unity/unity_internals.h" #include "../src/include/token.h" +#include "registry.h" -void test_token_init() { +int test_token_init() { + UNITY_BEGIN(); char* s = malloc(sizeof("Hello, world!")); s = "Hello, world!"; Token* t = token_init(TOKEN_TYPE_CALL, s); TEST_ASSERT_EQUAL(TOKEN_TYPE_CALL, t->type); TEST_ASSERT_EQUAL_STRING("Hello, world!", t->val); -} - -void test_token_destroy() { - char* s = malloc(1 * sizeof(char)); - *s = 'h'; - Token* t = token_init(TOKEN_TYPE_CALL, s); - token_destroy(t); -} - -void token_test() { - UNITY_BEGIN(); - RUN_TEST(test_token_init); - UNITY_END(); + return UNITY_END(); } __attribute__((constructor)) void register_tests_token() { - register_test(token_test); + register_test(test_token_init); } diff --git a/test/util.c b/test/util.c index 5eef12e..737c660 100644 --- a/test/util.c +++ b/test/util.c @@ -1,19 +1,14 @@ -#include "unity/unity.h" -#include "registry.h" -#include "unity/unity_internals.h" #include "../src/include/util.h" +#include "registry.h" +#include "unity/unity.h" -void test_is_even() { +int test_is_even() { + UNITY_BEGIN(); TEST_ASSERT_EQUAL(0, is_even(1)); TEST_ASSERT_EQUAL(1, is_even(2)); -} - -void util_test() { - UNITY_BEGIN(); - RUN_TEST(test_is_even); - UNITY_END(); + return UNITY_END(); } __attribute__((constructor)) void register_tests_util() { - register_test(util_test); + register_test(test_is_even); }