Fixed some things, broke others.

This commit is contained in:
Jacob Signorovitch 2024-10-13 23:46:03 -04:00
parent 8e8b6233d6
commit 63f5064ba9
9 changed files with 115 additions and 37 deletions

View File

@ -32,6 +32,10 @@ release: clean
release: CFLAGS = -Wall -O2 release: CFLAGS = -Wall -O2
release: $(TARGET) release: $(TARGET)
run: $(TARGET)
@ echo -e "$(WHITE_BOLD)Running... $(RESETCOLOR)./$(TARGET)"
@ ./$(TARGET)
# Link to final binary. # Link to final binary.
$(TARGET): $(OBJ_FILES) $(TARGET): $(OBJ_FILES)
@ echo -e "$(WHITE_BOLD)Linking $(WHITE)$(TARGET)$(WHITE_BOLD)...$(RESETCOLOR) $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)" @ echo -e "$(WHITE_BOLD)Linking $(WHITE)$(TARGET)$(WHITE_BOLD)...$(RESETCOLOR) $(CC) -o $(TARGET) $(OBJ_FILES) $(LDFLAGS)"
@ -60,8 +64,5 @@ clean:
@ echo -e "$(WHITE_BOLD)Cleaning up...$(WHITE) $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET)$(RESETCOLOR)" @ echo -e "$(WHITE_BOLD)Cleaning up...$(WHITE) $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET)$(RESETCOLOR)"
@ rm -rf $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET) @ rm -rf $(OBJ_DIR)/*.o $(TEST_OBJ_DIR)/*.o $(TEST_BUILD_DIR)/test.out $(TARGET)
run: .PHONY: all clean test nocolor release run
./$(TARGET)
.PHONY: all clean test nocolor release

View File

@ -1,11 +1,15 @@
#include "include/dstr.h" #include "include/dstr.h"
#include "include/util.h"
#include <string.h> #include <string.h>
#include <stdio.h>
Dstr* dstr_init(void) { Dstr* dstr_init(void) {
Dstr* dstr = malloc(sizeof(Dstr)); Dstr* dstr = malloc(sizeof(Dstr));
dstr->bufsz = DSTR_INITSZ; dstr->bufsz = DSTR_INITSZ;
dstr->buf = malloc(DSTR_INITSZ); dstr->buf = malloc(DSTR_INITSZ);
*dstr->buf = '\0';
dstr->ln = 0; dstr->ln = 0;
return dstr; return dstr;
@ -16,12 +20,30 @@ void dstr_destroy(Dstr* dstr) {
free(dstr); free(dstr);
} }
void dstr_append(Dstr* dest, size_t ln, char* src) { void dstr_append(Dstr* dest, char* src, size_t ln) {
if (dest->ln + ln > dest->bufsz) { while (dest->ln + ln + 1 > dest->bufsz) {
// Double the buffer size when overflown. // Double the buffer size when overflown.
dest->bufsz *= dest->bufsz; dest->bufsz *= 2;
dest->buf = realloc(dest->buf, dest->bufsz); dest->buf = realloc(dest->buf, dest->bufsz);
log_dbgf("dstr @ %p doubled from %ld to %ld", dest, dest->bufsz/2, dest->bufsz);
} }
strcat(dest->buf + ln, src); // Overwrites the \0 at the end of the string, keeps the null from the given
// string.
memcpy(dest->buf + dest->ln, src, ln + 1);
dest->ln += ln;
}
void dstr_appendch(Dstr *dest, char ch) {
if (dest->ln + 1 + 1 > dest->bufsz) {
// Double the buffer size when overflown.
dest->bufsz *= 2;
dest->buf = realloc(dest->buf, dest->bufsz);
log_dbgf("dstr @ %p doubled from %ld to %ld", dest, dest->bufsz/2, dest->bufsz);
}
// Overwrites the preexisting null terminator, and adds one of its own.
dest->buf[dest->ln] = ch;
dest->buf[dest->ln+1] = '\0';
dest->ln += 1;
} }

View File

@ -2,7 +2,6 @@
#define DSTR_H #define DSTR_H
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#define DSTR_INITSZ 128 #define DSTR_INITSZ 128
@ -16,6 +15,9 @@ Dstr* dstr_init(void);
void dstr_destroy(Dstr* dstr); void dstr_destroy(Dstr* dstr);
// Append ln characters of src to dest. // Append ln characters of src to dest.
void dstr_append(Dstr* dest, size_t ln, char* src); void dstr_append(Dstr* dest, char* src, size_t ln);
// Append ch to dest.
void dstr_appendch(Dstr* dest, char ch);
#endif #endif

View File

@ -3,6 +3,9 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <ctype.h>
#include "dstr.h"
typedef enum { typedef enum {
TOKEN_TYPE_CALL, TOKEN_TYPE_CALL,
@ -12,15 +15,16 @@ typedef enum {
// Token. // Token.
typedef struct { typedef struct {
TokenType type; // The type of the Token. TokenType type; // The type of the Token.
size_t valn; // The length of val.
char* val; // The text of the Token. char* val; // The text of the Token.
size_t len; // Length of the text of the Token. size_t len; // Length of the text of the Token.
} Token; } Token;
Token* token_init(TokenType type, char* val); Token* token_init(TokenType type, char* val, size_t valn);
void token_destroy(Token* token); void token_destroy(Token* token);
// Returns a string representation of the Token. // Returns a string representation of the Token.
char* token_to_str(Token* token, unsigned int indent); Dstr* token_to_dstr(Token* token);
// Returns a string representation of the TokenType. // Returns a string representation of the TokenType.
char* token_type_to_str(TokenType t); char* token_type_to_str(TokenType t);

View File

@ -2,9 +2,17 @@
#define UTIL_H #define UTIL_H
#ifdef DBG #ifdef DBG
#define log_dbg(msg) \ #define log_dbg(msg) \
printf("[dbg:%s:%s:%d] %s\n", __FILE__, __func__, __LINE__, msg); \ printf("\033[37;1mdbg\033[0m:\033[37;5m%s\033[0m:\033[32m " msg \
fflush(stdout); "\033[0m\n", \
__func__);
#define log_dbgf(msg, ...) \
printf("\033[37;1mdbg\033[0m:\033[37;5m%s\033[0m:\033[32m " msg \
"\033[0m\n", \
__func__, __VA_ARGS__);
#else #else
#define log_dbg(msg) #define log_dbg(msg)
#endif #endif

View File

@ -1,3 +1,6 @@
#include <ctype.h>
#include <string.h>
#include "include/lexer.h" #include "include/lexer.h"
#include "include/util.h" #include "include/util.h"
@ -12,6 +15,8 @@ Lexer* lexer_init(char* src) {
lexer->ntokens = 0; lexer->ntokens = 0;
lexer->state = LEXER_STATE_CONFUSED; lexer->state = LEXER_STATE_CONFUSED;
log_dbgf("created new lexer @ %p", lexer);
return lexer; return lexer;
} }
@ -33,13 +38,17 @@ void lexer_lex(Lexer* lexer) {
} }
void lexer_do_confused(Lexer* lexer) { void lexer_do_confused(Lexer* lexer) {
log_dbg("entered confused mode"); log_dbgf("lexer @ %p entered confused mode @ char '%c' (%d)", lexer, *lexer->cchar, (int)*lexer->cchar);
lexer->state = LEXER_STATE_CONFUSED;
if (isdigit(*lexer->cchar)) lexer_do_number(lexer); if (isdigit(*lexer->cchar)) lexer_do_number(lexer);
else lexer_do_call(lexer); else lexer_do_call(lexer);
} }
void lexer_do_number(Lexer* lexer) { void lexer_do_number(Lexer* lexer) {
log_dbg("entered number mode"); log_dbgf("lexer @ %p entered number mode @ char '%c' (%d)", lexer, *lexer->cchar, (int)*lexer->cchar);
lexer->state = LEXER_STATE_NUM;
// Size of the number string. // Size of the number string.
size_t numsz; size_t numsz;
@ -53,17 +62,21 @@ void lexer_do_number(Lexer* lexer) {
memcpy(num, start, numsz); memcpy(num, start, numsz);
num[numsz] = '\0'; num[numsz] = '\0';
lexer_add_token(lexer, token_init(TOKEN_TYPE_NUMBER, num)); lexer_add_token(lexer, token_init(TOKEN_TYPE_NUMBER, num, 1));
} }
void lexer_do_call(Lexer* lexer) { void lexer_do_call(Lexer* lexer) {
log_dbg("entered call mode"); log_dbgf("lexer @ %p entered call mode @ char '%c' (%d)", lexer, *lexer->cchar, (int)*lexer->cchar);
lexer->state = LEXER_STATE_CALL;
// Size of the call string. // Size of the call string.
size_t callsz; size_t callsz;
// Where the call string starts. // Where the call string starts.
char* start = lexer->cchar; char* start = lexer->cchar;
for (; *lexer->cchar && (isblank(lexer->cchar) || *lexer->cchar == '\n'); lexer_inc(lexer));
for (callsz = 0; *lexer->cchar && isalpha(*lexer->cchar); callsz++) for (callsz = 0; *lexer->cchar && isalpha(*lexer->cchar); callsz++)
lexer_inc(lexer); lexer_inc(lexer);
@ -71,7 +84,7 @@ void lexer_do_call(Lexer* lexer) {
memcpy(call, start, callsz); memcpy(call, start, callsz);
call[callsz] = '\0'; call[callsz] = '\0';
lexer_add_token(lexer, token_init(TOKEN_TYPE_CALL, call)); lexer_add_token(lexer, token_init(TOKEN_TYPE_CALL, call, 1));
} }
void lexer_inc(Lexer* lexer) { void lexer_inc(Lexer* lexer) {

View File

@ -1,17 +1,24 @@
#include <stdio.h> #include <stdio.h>
#include "include/dstr.h"
#include "include/token.h" #include "include/token.h"
#include "include/util.h" #include "include/util.h"
#include "include/lexer.h" #include "include/lexer.h"
int main(int argc, char** argv) { int main(int argc, char** argv) {
char* text = malloc(5); while(1) {
text = "aa11"; Dstr* cline = dstr_init(); // The current line.
printf("> ");
Lexer* lexer = lexer_init(text); fflush(stdout);
lexer_lex(lexer); for (char cch; (cch = getchar() != EOF);) {
dstr_appendch(cline, fgetc(stdin));
printf("%s\n", token_to_str(lexer->tokens[0], 0)); }
lexer_destroy(lexer); if (cline->ln > 0) {
Lexer* lexer = lexer_init(cline->buf);
lexer_lex(lexer);
printf("\n=%s\n", token_to_dstr(lexer->tokens[0])->buf);
}
}
} }

View File

@ -1,11 +1,13 @@
#include "include/token.h" #include "include/token.h"
#include <stdio.h> #include "include/dstr.h"
#include <stdlib.h>
Token* token_init(TokenType type, char* val) { Token* token_init(TokenType type, char* val, size_t valn) {
Token* t = malloc(sizeof(Token)); Token* t = malloc(sizeof(Token));
t->type = type; t->type = type;
t->val = val; t->val = val;
t->valn = valn;
return t; return t;
} }
@ -15,8 +17,25 @@ void token_destroy(Token* t) {
free(t); free(t);
} }
char* token_to_str(Token* token, unsigned int indent) { Dstr* token_to_dstr(Token* token) {
char* title = malloc(sizeof("Token @ 0x000000000000\n")); Dstr* str = dstr_init();
sprintf(title, "Token @%p\n", token);
return title; size_t titlesz = sizeof("Token @ 0x00000000");
char title[titlesz];
sprintf(title, "Token @ %p", token);
dstr_append(str, title, titlesz - 1);
dstr_append(str, "\n", 1);
size_t typesz = sizeof("type: 1");
char type[typesz];
// If token_to_dstr starts breaking, it might be because there're more than
// 10 types. FIXME.
sprintf(type, "type: %d", token->type);
dstr_append(str, type, typesz - 1);
dstr_append(str, "\n", 1);
dstr_append(str, "val: ", 5);
dstr_append(str, token->val, token->valn);
return str;
} }

View File

@ -12,14 +12,16 @@ void test_dstr_init() {
void test_dstr_append() { void test_dstr_append() {
char* str1 = malloc(2); char* str1 = malloc(2);
str1 = "h"; str1[0] = 'h';
str1[1] = '\0';
char* str2 = malloc(DSTR_INITSZ); char* str2 = malloc(DSTR_INITSZ);
str2 = "h"; str2[0] = 'h';
str2[1] = '\0';
Dstr* dstr = dstr_init(); Dstr* dstr = dstr_init();
dstr_append(dstr, 1, str1); dstr_append(dstr, str1, 1);
TEST_ASSERT_EQUAL_STRING(str2, dstr->buf); TEST_ASSERT_EQUAL_STRING(str2, dstr->buf);
} }