commit 8b75d3c7453d06dc5d38b6bd06c04830ce296e4a
parent 7340e14b35aabe79fa4fdf8ff2a197dab1e5635c
Author: Gabriel G. de Brito <me@gboncoffee.dev.br>
Date: Thu, 5 Nov 2026 10:42:19 -0300
Started parser.
Diffstat:
11 files changed, 301 insertions(+), 157 deletions(-)
diff --git a/src/compile.c b/src/compile.c
@@ -7,35 +7,43 @@
#include <assert.h>
#include <stdio.h>
+#include <string.h>
+
+bool ErrorOk(Error error)
+{
+ return error.location.column == 0 && error.location.line == 0 &&
+ error.message == nullptr && error.dynamic == nullptr;
+}
void PrintError(Error error, const char* file)
{
printf(
"%s:%d:%d: %s: %s.\n",
file,
- (int) error.line,
- (int) error.column,
+ (int) error.location.line,
+ (int) error.location.column,
error.message,
error.dynamic
);
}
-Errors* Compile(Arena* arena, char* program, FILE* output)
+Error Compile(Arena* arena, char* program, FILE* output)
{
- Errors* errors = nullptr;
- auto tokens = Tokenize(arena, program, &errors);
+ auto error = ZERO_ERROR;
+
+ auto tokens = Tokenize(arena, program, &error);
+ if (!ErrorOk(error))
+ return error;
- auto tree = Parse(arena, tokens, &errors);
- if (errors != nullptr) {
- return errors;
- }
+ auto tree = Parse(arena, program, tokens, &error);
+ if (!ErrorOk(error))
+ return error;
- errors = SemanticAnalysis(arena, tree);
- if (errors != nullptr) {
- return errors;
- }
+ error = SemanticAnalysis(arena, tree);
+ if (!ErrorOk(error))
+ return error;
Codegen(arena, tree, output);
- return nullptr;
+ return error;
}
diff --git a/src/compiler.h b/src/compiler.h
@@ -8,17 +8,23 @@
typedef struct {
unsigned long line;
unsigned long column;
- const char* message;
- char* dynamic;
+} Location;
+
+typedef struct {
+ Location location;
+ const char* message;
+ char* dynamic;
} Error;
-typedef struct Errors {
- Error this;
- struct Errors* next;
-} Errors;
+constexpr Error ZERO_ERROR = {
+ {0, 0},
+ nullptr,
+ nullptr
+};
+bool ErrorOk(Error error);
void PrintError(Error error, const char* file);
-Errors* Compile(Arena* arena, char* program, FILE* output);
+Error Compile(Arena* arena, char* program, FILE* output);
#endif // COMPILER_COMPILER_H_
diff --git a/src/main.c b/src/main.c
@@ -40,9 +40,9 @@ int main(int argc, char* argv[])
FILE* output = fopen("a.out", "w");
- auto errors = Compile(arena, input, output);
- for (auto error = errors; error != nullptr; error = error->next)
- PrintError(errors->this, argv[1]);
+ auto error = Compile(arena, input, output);
+ if (!ErrorOk(error))
+ PrintError(error, argv[1]);
DropArena(arena);
diff --git a/src/parser.c b/src/parser.c
@@ -1,106 +1,100 @@
#include "parser.h"
+#include "tokenizer.h"
-Ast* Parse(Arena* arena, Tokens* tokens, Errors** errors)
+Location MakeLocation(const char* program, Tokens* token)
{
- for (auto token = tokens; token != nullptr; token = token->next) {
- switch (token->this.kind) {
- case TokenProgram:
- printf("TokenProgram\n");
- break;
- case TokenVar:
- printf("TokenVar\n");
- break;
- case TokenIdent:
- printf(
- "TokenIdent %.*s\n",
- (int) token->this.lex.size,
- token->this.lex.ptr
- );
- break;
- case TokenDigit:
- printf(
- "TokenDigit %.*s\n",
- (int) token->this.lex.size,
- token->this.lex.ptr
- );
- break;
- case TokenArray:
- printf("TokenArray\n");
- break;
- case TokenOf:
- printf("TokenOf\n");
- break;
- case TokenOpenSq:
- printf("TokenOpenSq\n");
- break;
- case TokenCloseSq:
- printf("TokenCloseSq\n");
- break;
- case TokenOpenParen:
- printf("TokenOpenParen\n");
- break;
- case TokenCloseParen:
- printf("TokenCloseParen\n");
- break;
- case TokenComma:
- printf("TokenComma\n");
- break;
- case TokenColon:
- printf("TokenColon\n");
- break;
- case TokenSemicolon:
- printf("TokenSemicolon\n");
- break;
- case TokenPeriod:
- printf("TokenPeriod\n");
- break;
- case TokenTwoPeriods:
- printf("TokenTwoPeriods\n");
- break;
- case TokenOperator:
- printf(
- "TokenOperator %.*s\n",
- (int) token->this.lex.size,
- token->this.lex.ptr
- );
- break;
- case TokenIf:
- printf("TokenIf\n");
- break;
- case TokenElse:
- printf("TokenElse\n");
- break;
- case TokenThen:
- printf("TokenThen\n");
- break;
- case TokenWhile:
- printf("TokenWhile\n");
- break;
- case TokenDo:
- printf("TokenDo\n");
- break;
- case TokenBegin:
- printf("TokenBegin\n");
- break;
- case TokenEnd:
- printf("TokenEnd\n");
- break;
- case TokenFunction:
- printf("TokenFunction\n");
- break;
- case TokenProcedure:
- printf("TokenProcedure\n");
- break;
- case TokenComment:
- printf("TokenComment\n");
- break;
- case TokenEOF:
- printf("TokenEOF\n");
- break;
+ Location location;
+
+ for (auto i = 0; program[i] != '\0'; i += 1) {
+ if (token != nullptr && token->this.lex.ptr == &program[i])
+ break;
+ if (program[i] == '\n') {
+ location.line += 1;
+ location.column = 1;
+ } else {
+ location.column += 1;
}
}
- (void) arena;
- (void) errors;
+ return location;
+}
+
+Tokens* SkipComments(Tokens* tokens)
+{
+ if (tokens == nullptr)
+ return nullptr;
+ if (tokens->this.kind == TokenComment)
+ return SkipComments(tokens);
+ tokens->next = SkipComments(tokens->next);
+ return tokens;
+}
+
+Tokens* SkipIODeclaration(Tokens* tokens)
+{
+ if (tokens == nullptr)
+ return nullptr;
+ if (tokens->this.kind != TokenOpenParen)
+ return tokens;
+ tokens = tokens->next;
return nullptr;
}
+
+Ast* ParseRec(
+ Arena* arena,
+ Ast* ast,
+ const char* program,
+ Tokens* tokens,
+ Error* error
+)
+{
+ if (tokens == nullptr) {}
+}
+
+Ast* ParseProgram(
+ Arena* arena,
+ const char* program,
+ Tokens* tokens,
+ Error* error
+)
+{
+ if (tokens == nullptr || tokens->this.kind != TokenProgram) {
+ *error = (Error) {
+ .location = MakeLocation(program, tokens),
+ .message = "expected program declaration",
+ .dynamic = nullptr,
+ };
+ return nullptr;
+ }
+ tokens = tokens->next;
+ if (tokens == nullptr || tokens->this.kind != TokenIdent) {
+ *error = (Error) {
+ .location = MakeLocation(program, tokens),
+ .message = "expected program name",
+ .dynamic = nullptr,
+ };
+ }
+
+ auto ast = (Ast*) Calloc(arena, sizeof(Ast));
+ ast->name = tokens->this.lex;
+ ast->declarations = nullptr;
+ ast->subprograms = nullptr;
+
+ tokens = SkipIODeclaration(tokens);
+ if (tokens == nullptr || tokens->this.kind != TokenSemicolon) {
+ *error = (Error) {
+ .location = MakeLocation(program, tokens),
+ .message = "expected semicolon",
+ .dynamic = nullptr,
+ };
+ return nullptr;
+ }
+
+ return ParseRec(arena, ast, program, tokens, error);
+}
+
+Ast* Parse(Arena* arena, const char* program, Tokens* tokens, Error* error)
+{
+ tokens = SkipComments(tokens);
+
+ return ParseProgram(arena, program, tokens, error);
+}
diff --git a/src/parser.h b/src/parser.h
@@ -2,25 +2,138 @@
#define COMPILER_PARSER_H_
#include "compiler.h"
+#include "string.h"
#include "tokenizer.h"
typedef enum {
- A,
-} NodeKind;
+ TypeInteger,
+ TypeReal,
+ TypeArray,
+} TypeKind;
+
+struct Type;
+
+typedef struct {
+ struct Type* content;
+ unsigned long begin;
+ unsigned long end;
+} Array;
+
+typedef struct {
+ union {
+ Array array;
+ } this;
+
+ TypeKind kind;
+} Type;
+
+struct Expression;
+
+typedef struct {
+ String operator;
+ struct Expression* left;
+ struct Expression* right;
+} Operation;
+
+typedef struct {
+ Type type;
+ String content;
+} Literal;
+
+typedef enum {
+ ExpressionLiteral,
+ ExpressionName,
+ ExpressionOperation,
+} ExpressionKind;
+
+typedef struct {
+ union {
+ Literal literal;
+ String name;
+ Operation operation;
+ } this;
+
+ ExpressionKind kind;
+} Expression;
+
+struct Statement;
+
+typedef struct CompoundStatement {
+ struct Statement* this;
+ struct CompoundStatement* next;
+} CompoundStatement;
+
+typedef enum {
+ StatementAssignment,
+ StatementIf,
+ StatementWhile,
+ StatementCompound,
+} StatementKind;
+
+typedef struct {
+ String lvalue;
+ Expression rvalue;
+} Assignment;
+
+typedef struct {
+ Expression condition;
+ struct Statement* body;
+ struct Statement* elsee;
+} If;
+
+typedef struct {
+ Expression condition;
+} While;
typedef struct {
union {
- int a;
+ Assignment assignment;
+ If iff;
+ While whilee;
+ CompoundStatement compound;
} this;
- NodeKind kind;
-} Node;
+ StatementKind kind;
+} Statement;
+
+typedef struct {
+ String name;
+ Type type;
+} Declaration;
+
+typedef struct Declarations {
+ Declaration* this;
+ struct Declarations* next;
+} Declarations;
+
+typedef struct {
+ String name;
+ Type type;
+ bool reference;
+} Parameter;
+
+typedef struct {
+ Parameter this;
+ struct Parameters* next;
+} Parameters;
+
+typedef struct {
+ Parameters parameters;
+ Statement body;
+} Subprogram;
+
+typedef struct Subprograms {
+ Subprogram* this;
+ struct Subprograms* next;
+} Subprograms;
typedef struct Ast {
- Node this;
- struct Ast* next;
+ String name;
+ Declarations* declarations;
+ Subprograms* subprograms;
+ Statement main;
} Ast;
-Ast* Parse(Arena* arena, Tokens* tokens, Errors** errors);
+Ast* Parse(Arena* arena, const char* program, Tokens* tokens, Error* error);
#endif // COMPILER_PARSER_H_
diff --git a/src/semantics.c b/src/semantics.c
@@ -1,8 +1,12 @@
#include "semantics.h"
-Errors* SemanticAnalysis(Arena* arena, Ast* tree)
+#include <string.h>
+
+Error SemanticAnalysis(Arena* arena, Ast* tree)
{
+ Error error;
+ memset(&error, 0, sizeof(error));
(void) arena;
(void) tree;
- return nullptr;
+ return error;
}
diff --git a/src/semantics.h b/src/semantics.h
@@ -5,6 +5,6 @@
#include "compiler.h"
#include "parser.h"
-Errors* SemanticAnalysis(Arena* arena, Ast* tree);
+Error SemanticAnalysis(Arena* arena, Ast* tree);
#endif // COMPILER_SEMANTICS_H_
diff --git a/src/string.c b/src/string.c
@@ -0,0 +1,13 @@
+#include "string.h"
+
+#include <string.h>
+
+bool StringEqCstr(String a, const char* b)
+{
+ return ((strncmp(a.ptr, b, a.size) == 0) && strlen(b) == a.size);
+}
+
+bool StringEq(String a, String b)
+{
+ return a.size == b.size && memcmp(a.ptr, b.ptr, a.size) == 0;
+}
diff --git a/src/string.h b/src/string.h
@@ -0,0 +1,12 @@
+#ifndef COMPILER_STRING_H_
+#define COMPILER_STRING_H_
+
+typedef struct {
+ const char* ptr;
+ unsigned long size;
+} String;
+
+bool StringEq(String a, String b);
+bool StringEqCstr(String a, const char* cstr);
+
+#endif // COMPILER_STRING_H_
diff --git a/src/tokenizer.c b/src/tokenizer.c
@@ -40,8 +40,7 @@ constexpr unsigned long keywordsLen = sizeof(keywords) / sizeof(keywords[0]);
#include <stdio.h>
-Errors* MakeTokenError(
- Arena* arena,
+Error MakeTokenError(
const char* program,
Token* this,
const char* message,
@@ -60,22 +59,23 @@ Errors* MakeTokenError(
column += 1;
}
- auto errors = (Errors*) Calloc(arena, sizeof(Errors));
- errors->next = nullptr;
- errors->this.line = line;
- errors->this.column = column;
- errors->this.message = message;
- errors->this.dynamic = dynamic;
+ Error error = {
+ .location =
+ {
+ .line = line,
+ .column = column,
+ },
+ .message = message,
+ .dynamic = dynamic,
+ };
- return errors;
+ return error;
}
void MakeKeyword(Token* this)
{
for (auto i = 0; i < (int) keywordsLen; i += 1) {
- if ((strncmp(this->lex.ptr, keywords[i].keyword, this->lex.size) ==
- 0) &&
- strlen(keywords[i].keyword) == this->lex.size) {
+ if (StringEqCstr(this->lex, keywords[i].keyword)) {
this->kind = keywords[i].kind;
return;
}
@@ -84,15 +84,13 @@ void MakeKeyword(Token* this)
this->kind = TokenIdent;
}
-Tokens* Tokenize(Arena* arena, char* program, Errors** errors)
+Tokens* Tokenize(Arena* arena, char* program, Error* error)
{
- (void) errors;
auto list = (Tokens*) Calloc(arena, sizeof(Tokens));
auto token = list;
- for (int i = 0; program[i] != '\0'; i += 1) {
+ for (int i = 0; program[i] != '\0'; i += 1)
program[i] = tolower(program[i]);
- }
auto idx = 0;
auto state = StateInitial;
@@ -220,8 +218,8 @@ Tokens* Tokenize(Arena* arena, char* program, Errors** errors)
idx += 1;
} else if (cchar == '\0') {
- *errors = MakeTokenError(
- arena, program, &token->this, "unclosed comment", nullptr
+ *error = MakeTokenError(
+ program, &token->this, "unclosed comment", nullptr
);
token->next = nullptr;
token->this.kind = TokenComment;
diff --git a/src/tokenizer.h b/src/tokenizer.h
@@ -3,11 +3,7 @@
#include "arena.h"
#include "compiler.h"
-
-typedef struct {
- const char* ptr;
- unsigned long size;
-} String;
+#include "string.h"
typedef enum {
TokenProgram,
@@ -49,6 +45,6 @@ typedef struct Tokens {
struct Tokens* next;
} Tokens;
-Tokens* Tokenize(Arena* arena, char* program, Errors** errors);
+Tokens* Tokenize(Arena* arena, char* program, Error* errors);
#endif // COMPILER_TOKENIZER_H_