commit 8583075830aedbdd77e93ccbf38b91708c8e28dd
parent 3de0de13e88e769bd932c4015d2cc56c859d0258
Author: citbl <citbl@citbl.org>
Date: Sun, 17 May 2026 22:07:01 +1000
wip
Diffstat:
7 files changed, 47 insertions(+), 12 deletions(-)
diff --git a/mtcc/makefile b/mtcc/makefile
@@ -1,7 +1,7 @@
MAKEFLAGS += --silent
default:
- clang -O1 -Wall -Wextra -Wpedantic -std=c99 \
+ clang -O1 -Wall -Wextra -Wpedantic -std=c23 \
-Werror=declaration-after-statement \
-o mtcc **/*.c
./mtcc target.mty
diff --git a/mtcc/mtcc b/mtcc/mtcc
Binary files differ.
diff --git a/mtcc/src/lexer.c b/mtcc/src/lexer.c
@@ -1 +1,16 @@
#include "lexer.h"
+#include "string.h"
+
+void lexer_lex(lexer_t *lexer) {
+ size_t len = strnlen(lexer->source, lexer->source_len);
+ while (lexer->pos < len) {
+ char c = lexer->source[lexer->pos++];
+ switch (c) {
+ case '\n':
+ case '\t':
+ case '\r':
+ case ' ':
+ continue;
+ }
+ }
+}
diff --git a/mtcc/src/lexer.h b/mtcc/src/lexer.h
@@ -1,9 +1,14 @@
#pragma once
#include "token.h"
-typedef struct {
- char* filename;
- token_t* tokens;
+typedef struct lexer_t {
+ const char *filename;
+ const char *source;
+ size_t source_len;
+ token_t *tokens;
size_t tok_len;
size_t tok_cap;
+ size_t pos;
} lexer_t;
+
+void lexer_lex(lexer_t *lexer);
diff --git a/mtcc/src/main.c b/mtcc/src/main.c
@@ -1,10 +1,11 @@
#include <stdio.h>
#include "utils.h"
-#include "token.h"
+#include "lexer.h"
int main(int argc, char **argv) {
const char *filename;
- const char *contents;
+ file_t file;
+ lexer_t lexer;
if (argc < 2) {
printf("usage: ./mtcc <filename.mty>\n");
@@ -12,7 +13,14 @@ int main(int argc, char **argv) {
}
filename = argv[1];
- contents = read_file(filename);
+ file = read_file(filename);
+ lexer = (lexer_t){
+ .filename = filename,
+ .source = file.contents,
+ .source_len = file.len,
+ };
- printf("%s\n", contents);
+ lexer_lex(&lexer);
+
+ printf("%s\n", file.contents);
}
diff --git a/mtcc/src/utils.c b/mtcc/src/utils.c
@@ -3,13 +3,14 @@
#include <stdlib.h>
#include <string.h>
-const char *read_file(const char *filename) {
+file_t read_file(const char *filename) {
long fsize;
char *source;
FILE *fp = fopen(filename, "rb");
if (fp == NULL) {
fprintf(stderr, "file not found\n");
- return NULL;
+ fclose(fp);
+ return (file_t){.contents = NULL, .len = 0};
}
if (fseek(fp, 0, SEEK_END) != 0) {
@@ -35,7 +36,7 @@ const char *read_file(const char *filename) {
fclose(fp);
source[fsize] = '\0';
- return source;
+ return (file_t){.contents = source, .len = fsize};
}
void separate_file_from_path(const char *fullpath, char **out_path, char **out_filename) {
diff --git a/mtcc/src/utils.h b/mtcc/src/utils.h
@@ -1,7 +1,13 @@
#pragma once
#include <stdbool.h>
+#include <stddef.h>
-const char *read_file(const char *filename);
+typedef struct {
+ const char *contents;
+ size_t len;
+} file_t;
+
+file_t read_file(const char *filename);
void separate_file_from_path(const char *fullpath, char **out_path, char **out_filename);