Compare commits

..

No commits in common. "47a314375212e16c3e528943c61c710face3cef2" and "5ecdf2d89a7f0c3f3d8e34f8a1a7d72aa0173e6b" have entirely different histories.

18 changed files with 50 additions and 358 deletions

View File

@ -30,7 +30,7 @@ UNITY_OBJ = $(TEST_BUILD_DIR)/unity.o
TEST_SRC_FILES = $(wildcard $(TEST_DIR)/*.c)
TEST_OBJ_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_OBJ_DIR)/%.o, $(TEST_SRC_FILES))
TEST_BIN_FILES = $(patsubst $(TEST_DIR)/%.c, $(TEST_BUILD_DIR)/%.out, $(TEST_SRC_FILES))
TEST_VAL_DIR = $(TEST_DIR)/val
TEST_VAL_DIR = $(TEST_DIR)/validation
RESETCOLOR = \033[0m
WHITE = $(RESETCOLOR)\033[37m

View File

@ -1,3 +0,0 @@
f(n) = 2 * n
f(5)

View File

@ -9,9 +9,7 @@ extern AST* root;
static char* asttype_names[] = {
[AST_TYPE_CALL] = "FUNC CALL",
[AST_TYPE_NUM] = "NUMBER",
[AST_TYPE_VREF] = "VAR REFERENCE",
[AST_TYPE_VDEF] = "VAR DEFINITION",
[AST_TYPE_BLOCK] = "BLOCK",
[AST_TYPE_VREF] = "VAR REFERENCE"
};
AST* ast_init(ASTType type, void* data) {
@ -30,7 +28,6 @@ void ast_destroy(AST* ast) {
case AST_TYPE_NUM: ast_num_data_destroy(ast->data); break;
case AST_TYPE_CALL: ast_call_data_destroy(ast->data); break;
case AST_TYPE_VREF: ast_vref_data_destroy(ast->data); break;
case AST_TYPE_VDEF: ast_vdef_data_destroy(ast->data); break;
default:
log_dbgf("Unknown ast type %d (max: %d)", ast->type, AST_TYPE_MAX);
}
@ -52,7 +49,6 @@ void ast_print_i(AST* ast, int i) {
break;
case AST_TYPE_CALL: ast_call_print(ast->data, i + 2); break;
case AST_TYPE_VREF: ast_vref_print(ast->data, i + 2); break;
case AST_TYPE_VDEF: ast_vdef_print(ast->data, i + 2); break;
default: exit(1);
}
INDENT_FIELD_NONL_END;
@ -108,33 +104,6 @@ void ast_call_print(ASTCallData* data, int i) {
INDENT_END;
}
ASTVDefData* ast_vdef_data_init(char* name, AST* val) {
talloc(ASTVDefData, vdef);
vdef->name = name;
vdef->val = val;
return vdef;
}
void ast_vdef_data_destroy(ASTVDefData* vdef) {
ast_destroy(vdef->val);
free(vdef->name);
free(vdef);
}
void ast_vdef_print(ASTVDefData* vdef, int depth) {
INDENT_BEGIN(depth);
INDENT_TITLE("ASTVDefData", vdef);
INDENT_FIELD("name", "%s", vdef->name);
INDENT_FIELD_EXT_NONL_START("val");
ast_print_i(vdef->val, depth + 2); // 2 because already indented.
INDENT_FIELD_NONL_END;
INDENT_END;
}
ASTVrefData* ast_vref_data_init(char* to) {
talloc(ASTVrefData, vref);
@ -156,31 +125,3 @@ void ast_vref_print(ASTVrefData* data, int i) {
INDENT_END;
}
ASTBlockData* ast_block_data_init(AST** inside, size_t ln) {
ASTBlockData* block = malloc(sizeof(ASTBlockData));
block->inside = calloc(ln, sizeof(AST));
block->ln = ln;
return block;
}
void ast_block_data_destroy(ASTBlockData* block) {
for (size_t i = 0; i < block->ln; i++) {
ast_destroy(block->inside[i]);
}
free(block->inside);
free(block);
}
void ast_block_data_print(ASTBlockData* data, int depth) {
INDENT_BEGIN(depth);
INDENT_TITLE("BLOCK", data);
INDENT_FIELD("ln", "%ld", data->ln);
INDENT_FIELD_LIST("inside", data->inside, data->ln, ast_print_i);
INDENT_END;
}

View File

@ -1,41 +0,0 @@
#include "include/dlist.h"
#include "include/util.h"
#include <stddef.h>
#include <stdio.h>
DList* dlist_init(void) {
DList* dlist = malloc(sizeof(DList));
dlist->sz = DLIST_INITSZ;
dlist->buf = malloc(DLIST_INITSZ);
dlist->ln = 0;
return dlist;
}
void dlist_destroy(DList* dlist) {
free(dlist->buf);
free(dlist);
}
void dlist_destroypsv(DList* dlist) { free(dlist); }
// Check whether the buffer is overflowing and resize it if necessary.
void dlist_check_resz(DList* dlist, size_t ln) {
while (dlist->ln + ln + 1 > dlist->sz) {
// Double the buffer size when overflown.
dlist->sz *= 2;
dlist->buf = realloc(dlist->buf, dlist->sz);
log_dbgf(
"dlist @ %p doubled from %ld to %ld", dlist, dlist->sz / 2, dlist->sz
);
}
}
void dlist_append(DList* dest, void* src) {
dlist_check_resz(dest, 1);
dest->buf[dest->ln] = src;
dest->ln += 1;
}

View File

@ -24,7 +24,7 @@ void dstr_destroy(Dstr* dstr) {
void dstr_destroypsv(Dstr* dstr) { free(dstr); }
// Check whether the buffer is overflowing and resize it if necessary.
void dstr_check_resz(Dstr* dstr, size_t ln) {
void check_resz(Dstr* dstr, size_t ln) {
while (dstr->ln + ln + 1 > dstr->sz) {
// Double the buffer size when overflown.
dstr->sz *= 2;
@ -36,7 +36,7 @@ void dstr_check_resz(Dstr* dstr, size_t ln) {
}
void dstr_append(Dstr* dest, char* src, size_t ln) {
dstr_check_resz(dest, ln);
check_resz(dest, ln);
// Overwrites the \0 at the end of the string, keeps the null from the given
// string.
@ -45,7 +45,7 @@ void dstr_append(Dstr* dest, char* src, size_t ln) {
}
void dstr_appendch(Dstr* dest, char ch) {
dstr_check_resz(dest, 1);
check_resz(dest, 1);
// Overwrites the preexisting null terminator, and adds one of its own.
dest->buf[dest->ln] = ch;

View File

@ -14,7 +14,6 @@ ASTNumData exec_exp(AST* ast) {
case AST_TYPE_CALL: return exec_call(ast);
case AST_TYPE_NUM: return *(ASTNumData*)ast->data;
case AST_TYPE_VREF: return exec_vref(ast);
case AST_TYPE_VDEF: return exec_vdef(ast);
default: printf("what\n");
exit(1);
}
@ -69,12 +68,6 @@ ASTNumData exec_call(AST* ast) {
return -1000;
}
ASTNumData exec_vdef(AST* ast) {
ASTVDefData* data = (ASTVDefData*) ast->data;
AST* val = data->val;
return exec_exp(val);
}
ASTNumData exec_vref(AST* ast) {
return *ast_num_data_init(42.42);
}

View File

@ -1,14 +0,0 @@
#include "include/fnv1a.h"
#include <stdlib.h>
uint64_t fnv1a_hash(char *key, size_t ln) {
uint64_t hash = FNV1A_BASIS_64;
for (size_t i = 0; i < ln; i++) {
hash ^= (unsigned char)key[i];
hash *= FNV1A_PRIME_64;
}
return hash;
}

View File

@ -3,7 +3,6 @@
#include <stdio.h>
#include "../../src/include/ast.h"
#include "../../src/include/lexer.h"
#include "../../src/include/dlist.h"
int yylex(void);
void yyerror(char const*);
@ -13,7 +12,6 @@
%code requires {
#include "../../src/include/ast.h"
#include "../../src/include/dlist.h"
}
%union {
@ -21,41 +19,33 @@
char* strval;
AST* ast;
ArgArr* argarr;
DList* exps;
}
%define parse.error verbose
%token BLOCKS // Block start {.
%token BLOCKE // Block end }.
%token LGROUP
%token RGROUP
%token SEP
%token GROUPS // Group start (.
%token GROUPE // Group end ).
%token SEP // Seperator ,.
%token EXPSEP
%token EQ // Equals =.
%token<strval> WORD
%token<fval> NUM
%token EXPSEP // Expression seperator ;.
%token SUB
%token PLUS
%token MULT
%token DIV
%token<strval> WORD // Word, i.e. keyword.
%token<fval> NUM // Number.
%token NL
%token SUB // Subtract -.
%token ADD // Addition *.
%token MUL // Multiplication *.
%token DIV // Division /.
%token NL // Newline.
%left ADD SUB
%left MUL DIV
%left PLUS SUB
%left MULT DIV
%precedence NEG
%type<ast> exp;
%type<argarr> arg;
%type<argarr> argstart;
%type<exps> blockstart;
%type<exps> block;
%%
@ -65,6 +55,7 @@ input:
| input EXPSEP exp { root = $3; }
;
argstart:
exp {
ArgArr* argarr = argarr_init();
@ -81,32 +72,9 @@ arg:
}
;
blockstart:
exp {
DList* exps = dlist_init(); // List of expressions.
dlist_append(exps, $1);
$$ = exps;
}
;
block:
blockstart { $$ = $1; }
| block EXPSEP exp {
dlist_append($1, $3);
$$ = $1;
}
;
exp:
NUM { $$ = ast_init(AST_TYPE_NUM, ast_num_data_init($1)); }
| BLOCKS exp BLOCKE { $$ = $2; }
| BLOCKS block BLOCKE {
size_t i = $2->ln - 1;
$$ = $2->buf[i];
}
| SUB exp {
AST** argv = calloc(2, sizeof(AST*));
argv[0] = ast_init(AST_TYPE_NUM, ast_num_data_init(-1));
@ -116,26 +84,21 @@ exp:
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
}
| GROUPS exp GROUPE { $$ = $2; }
// Variable definition.
| WORD EQ exp {
$$ = ast_init(AST_TYPE_VDEF, ast_vdef_data_init($1, $3));
}
| LGROUP exp RGROUP { $$ = $2; }
// Variable reference.
//| WORD {
// $$ = ast_init(AST_TYPE_VREF, ast_vref_data_init($1));
//}
| WORD {
$$ = ast_init(AST_TYPE_VREF, ast_vref_data_init($1));
}
| WORD GROUPS arg GROUPE {
| WORD LGROUP arg RGROUP {
size_t argc = $3->ln;
AST** argv = $3->buf;
argarr_destroypsv($3);
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init($1, argc, argv));
}
| exp ADD exp {
| exp PLUS exp {
AST** argv = calloc(2, sizeof(AST*));
argv[0] = $1;
argv[1] = $3;
@ -153,7 +116,7 @@ exp:
$$ = ast_init(AST_TYPE_CALL, ast_call_data_init(to, 2, argv));
}
| exp MUL exp {
| exp MULT exp {
AST** argv = calloc(2, sizeof(AST*));
argv[0] = $1;
argv[1] = $3;

View File

@ -1,38 +0,0 @@
#include "include/htab.h"
#include "include/fnv1a.h"
#include "include/util.h"
#include "include/util.h"
#include <assert.h>
#include <string.h>
#include <stdio.h>
HTab* htab_init() {
HTab* htab = calloc(1, sizeof(HTab));
return htab;
}
void htab_destroy(HTab *htab) {
free(htab);
}
// Get the index of a key.
size_t geti(char* key) {
uint64_t hash = fnv1a_hash(key, strlen(key));
size_t i = hash & (HTAB_SPACE - 1);
return i;
}
void* htab_get(HTab* htab, char* key) {
size_t i = geti(key);
log_dbgf("Getting something from hash table @ index %lu", i);
return (*htab)[i];
}
void htab_ins(HTab* htab, char* key, void* data) {
size_t i = geti(key);
assert((*htab)[i] == NULL);
(*htab)[i] = data;
log_dbgf("Inserted something to hash table @ index %lu", i);
}

View File

@ -6,10 +6,8 @@
typedef enum {
AST_TYPE_NUM, // A number.
AST_TYPE_CALL, // A function call.
AST_TYPE_VDEF, // A variable definition.
AST_TYPE_VREF, // A variable reference.
AST_TYPE_BLOCK, // A block of code (scope).
AST_TYPE_MAX = AST_TYPE_BLOCK,
AST_TYPE_MAX = AST_TYPE_CALL
} ASTType;
typedef struct {
@ -38,18 +36,6 @@ ASTCallData* ast_call_data_init(char* to, size_t argc, AST** argv);
void ast_call_data_destroy(ASTCallData* call);
void ast_call_print(ASTCallData*, int i);
// A variable definition's data.
typedef struct {
char* name;
AST* val;
} ASTVDefData;
ASTVDefData* ast_vdef_data_init(char* name, AST* val);
// Destroys the vdef, its name, and its ->val.
void ast_vdef_data_destroy(ASTVDefData* vdef);
void ast_vdef_print(ASTVDefData*, int depth);
// A variable reference's data.
typedef struct {
char* to; // What the reference's to.
} ASTVrefData;
@ -58,14 +44,4 @@ ASTVrefData* ast_vref_data_init(char* to);
void ast_vref_data_destroy(ASTVrefData* call);
void ast_vref_print(ASTVrefData*, int i);
typedef struct {
AST** inside; // What's inside the block.
size_t ln; // How many ASTs are in the block.
} ASTBlockData;
ASTBlockData* ast_block_data_init(AST** inside, size_t ln);
// Destroy a block. Also destroy all ASTs inside.
void ast_block_data_destroy(ASTBlockData* block);
void ast_block_data_print(ASTBlockData*, int i);
#endif

View File

@ -1,22 +0,0 @@
#ifndef DLIST_H
#define DLIST_H
#include <stdlib.h>
#define DLIST_INITSZ 128
typedef struct {
void** buf; // The buffer containing the list.
size_t sz; // The size of the buffer.
size_t ln; // The number of elements in the list.
} DList;
DList* dlist_init(void);
void dlist_destroy(DList* dstr);
// Destroy DList structure but preserve ->buf.
void dlist_destroypsv(DList* dstr);
// Append src to dest.
void dlist_append(DList* dest, void* src);
#endif

View File

@ -6,7 +6,6 @@
ASTNumData exec_exp(AST* ast);
ASTNumData exec_call(AST* ast);
ASTNumData exec_vref(AST* ast);
ASTNumData exec_vdef(AST* ast);
void exec_print(double n);
#endif

View File

@ -1,17 +0,0 @@
#ifndef FNV1A_H
#define FNV1A_H
// Implements the FNV-1a hash algorithm.
#include <stdint.h>
#include <stdlib.h>
// FNV prime.
#define FNV1A_PRIME_64 0x00000100000001b3u
// Offset basis.
#define FNV1A_BASIS_64 0xcbf29ce484222325u
uint64_t fnv1a_hash(char* str, size_t ln);
#endif

View File

@ -2,27 +2,25 @@
#define HTAB_H
#include <stddef.h>
#include <stdint.h>
#define HTAB_FN fnv1a_hash // Function used for hashing.
#define HTAB_SPACE 1024 // Number of entries possible in the hash table; must be
// power of 2.
#define HTAB_SPACE 128
// Hash Table.
typedef void* HTab[HTAB_SPACE];
typedef struct {} HTab;
// Create a new hash table.
HTab* htab_init();
// Destroy a hash table, but not its elements.
// Destroy a hash table.
void htab_destroy(HTab* htab);
// Get element at `hash`. Return its contents, or NULL if nothing found.
void* htab_get(HTab* htab, char* str);
void* htab_get(HTab* htab, int hash);
// Insert `data` at index `hash`.
void htab_ins(HTab* htab, char* key, void* data);
void htab_ins(HTab* htab, int key, void* data);
// Gets the length of the hash table.
size_t htab_ln(HTab* htab);
#endif

View File

@ -72,10 +72,10 @@ double acc_float(int c) {
char* acc_word(int c) {
Dstr* val = dstr_init();
while (isalpha(*inp)) {
do {
dstr_appendch(val, *(inp - 1));
inp++;
}
} while (isalpha(*inp));
dstr_appendch(val, *(inp - 1));
char* ret = val->buf;
@ -104,18 +104,15 @@ int yylex() {
}
switch (c) {
case '+': return ADD;
case '+': return PLUS;
case '\n': return NL;
case '-': return SUB;
case '*': return MUL;
case '*': return MULT;
case '/': return DIV;
case '(': return GROUPS;
case ')': return GROUPE;
case '(': return LGROUP;
case ')': return RGROUP;
case ',': return SEP;
case ';': return EXPSEP;
case '{': return BLOCKS;
case '}': return BLOCKE;
case '=': return EQ;
default: fprintf(stderr, "Unexpected character: %c\n", c);
}

View File

@ -53,7 +53,7 @@ void test_ast_vref() {
int main() {
UNITY_BEGIN();
RUN_TEST(test_ast_num);
//RUN_TEST(test_ast_call);
RUN_TEST(test_ast_call);
//RUN_TEST(test_ast_vref);
return UNITY_END();
}

View File

@ -1,27 +0,0 @@
#include "../src/include/htab.h"
#include "Unity/src/unity.h"
#include "Unity/src/unity_internals.h"
#include <string.h>
void setUp() {}
void tearDown() {}
void test_htab() {
char* key = "hello";
char* data = "world";
HTab* htab = htab_init();
htab_ins(htab, key, data);
TEST_ASSERT_EQUAL_STRING(data, htab_get(htab, key));
TEST_ASSERT_NOT_EQUAL(data, htab_get(htab, "h"));
}
int main() {
UNITY_BEGIN();
RUN_TEST(test_htab);
return UNITY_END();
}

View File

@ -7,7 +7,6 @@ bin() { ./scl.out $1 | tail -n1; }
[ "$output" = "= 2.000000" ]
run bin "-1+1"
echo $output
[ "$output" = "= 0.000000" ]
run bin "1+-1"
@ -84,27 +83,15 @@ bin() { ./scl.out $1 | tail -n1; }
[ "$output" = "= 9.000000" ]
}
@test "basic blocks" {
run bin "{1}"
[ "$output" = "= 1.000000" ]
run bin "2+{3;4}"
[ "$output" = "= 6.000000" ]
run bin "5*{1+1;5*2}"
echo $output
[ "$output" = "= 50.000000" ]
@test "multiple expressions per line" {
run bin "1+1;2"
[ "$output" = "= 2.000000" ]
}
@test "variable definition" {
run bin "x = 1"
[ "$output" = "= 1.000000" ]
# run bin "x = 1; x + 1"
# [ "$output" = "= 2.000000" ]
run bin "x = 1; x + 1"
[ "$output" = "= 2.000000" ]
}
#@test "function definition" {
# run bin "f(n)=2*n; f(2)"
# [ "$output" = "= 4.000000" ]
#}