Compare commits

..

2 Commits

Author SHA1 Message Date
d244cfbfe1 Tests are slightly not. 2024-10-05 09:24:12 -04:00
6f283c1d12 Fixed some things. 2024-10-02 21:06:54 -04:00
12 changed files with 56 additions and 99 deletions

View File

@ -1,16 +1,3 @@
# SCL: Simple Calculator Language # SCL: Simple Calculator Language
Project structure: A rewrite of halk.
```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.
```

View File

@ -21,7 +21,7 @@ typedef struct {
char* cchar; // The current character. char* cchar; // The current character.
Token** tokens; // The tokens produced. Token** tokens; // The tokens produced.
size_t ntokens; // The number of tokens. size_t ntokens; // The number of tokens.
LexerState state; // What the lexxer is looking at. LexerState state; // What the lexer is looking at.
} Lexer; } Lexer;
// Create a lexer. // Create a lexer.
@ -42,4 +42,7 @@ void lexer_do_call(Lexer* lexer);
// Convert text to tokens. // Convert text to tokens.
void lexer_lex(Lexer* lexer); void lexer_lex(Lexer* lexer);
// Add a token to the lexer.
void lexer_add_token(Lexer* lexer, Token* token);
#endif #endif

View File

@ -10,21 +10,4 @@
// - Expression 1 // - Expression 1
// - Expression 2 // - Expression 2
typedef enum OpType {
OPTYPE_PLUS,
OPTYPE_MINUS
} optype_t;
typedef union Exp {
typedef struct Op {
optype_t type;
Exp* exp1;
Exp* exp2;
} op_t;
int n;
} exp_t;
#endif #endif

View File

@ -1,12 +0,0 @@
#ifndef UTIL_H
#define UTIL_H
// Utilies.
#include <stdlib.h>
#include <stdio.h>
// Exit with an error. Returns int for ease of use, but should be treated as void.
int die(char* msg);
#endif

View File

@ -1,4 +1,6 @@
#include "include/lexer.h" #include "include/lexer.h"
#include "include/token.h"
#include <stdlib.h>
Lexer* lexer_init(char* src) { Lexer* lexer_init(char* src) {
Lexer* lexer = malloc(sizeof(Lexer)); 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++])); 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) { void lexer_lex(Lexer* lexer) {
while (*lexer->cchar) { 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;
}

View File

@ -1,6 +1,3 @@
#include "include/util.h" #include "include/util.h"
int is_even(int n) { int is_even(int n) { return !(n % 2); }
return !(n%2);
}

View File

@ -1,5 +1,5 @@
#include "unity/unity.h"
#include "registry.h" #include "registry.h"
#include "unity/unity.h"
void (*tests[30])(); void (*tests[30])();

View File

@ -1,5 +1,5 @@
#include "unity/unity_internals.h"
#include "registry.h" #include "registry.h"
#include "unity/unity_internals.h"
#define TESTS_MAX 128 #define TESTS_MAX 128
@ -10,10 +10,11 @@ void register_test(Test t) {
if (test_count < TESTS_MAX) { if (test_count < TESTS_MAX) {
tests[test_count] = t; tests[test_count] = t;
test_count++; test_count++;
} else } else printf("Maximum number of tests (%d) exceeded.\n", TESTS_MAX);
printf("Maximum number of tests (%d) exceeded.\n", TESTS_MAX);
} }
void run_all_tests() { 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]();
}
} }

View File

@ -2,12 +2,12 @@
#define REGISTRY_H #define REGISTRY_H
#include <stdio.h> #include <stdio.h>
#include "unity/unity.h"
// Test functions neither consume nor return anything. typedef int (*Test)(void);
typedef void (*Test)(void);
// Register a new test function. // Register a new test function.
void register_test(Test t); void register_test(Test);
// Run all registered tests. // Run all registered tests.
void run_all_tests(); void run_all_tests();

View File

@ -1,31 +1,19 @@
#include <stdlib.h>
#include "unity/unity.h" #include "unity/unity.h"
#include <stdlib.h>
#include "registry.h"
#include "unity/unity_internals.h"
#include "../src/include/token.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!")); char* s = malloc(sizeof("Hello, world!"));
s = "Hello, world!"; s = "Hello, world!";
Token* t = token_init(TOKEN_TYPE_CALL, s); Token* t = token_init(TOKEN_TYPE_CALL, s);
TEST_ASSERT_EQUAL(TOKEN_TYPE_CALL, t->type); TEST_ASSERT_EQUAL(TOKEN_TYPE_CALL, t->type);
TEST_ASSERT_EQUAL_STRING("Hello, world!", t->val); TEST_ASSERT_EQUAL_STRING("Hello, world!", t->val);
} return UNITY_END();
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();
} }
__attribute__((constructor)) void register_tests_token() { __attribute__((constructor)) void register_tests_token() {
register_test(token_test); register_test(test_token_init);
} }

View File

@ -1,19 +1,14 @@
#include "unity/unity.h"
#include "registry.h"
#include "unity/unity_internals.h"
#include "../src/include/util.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(0, is_even(1));
TEST_ASSERT_EQUAL(1, is_even(2)); TEST_ASSERT_EQUAL(1, is_even(2));
} return UNITY_END();
void util_test() {
UNITY_BEGIN();
RUN_TEST(test_is_even);
UNITY_END();
} }
__attribute__((constructor)) void register_tests_util() { __attribute__((constructor)) void register_tests_util() {
register_test(util_test); register_test(test_is_even);
} }