compiler

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 4e242074ef7528c0a89cb899bda6c9f93d1c504a
parent c3ccd6b83f3e363bda5c3a917f8ad3b779ff9966
Author: Gabriel G. de Brito <me@gboncoffee.dev.br>
Date:   Mon,  9 Nov 2026 21:06:39 -0300

Almost parsing types.

Diffstat:
Msrc/compile.c | 18+++++++++++-------
Msrc/compiler.h | 11++++++-----
Msrc/parser.c | 223++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
Msrc/parser.h | 78+++++++++++++++++++++++++++++++-----------------------------------------------
Msrc/string.h | 3+++
Msrc/tokenizer.c | 26++++++++++++++++++++------
Msrc/tokenizer.h | 2++
7 files changed, 248 insertions(+), 113 deletions(-)

diff --git a/src/compile.c b/src/compile.c @@ -3,6 +3,7 @@ #include "compiler.h" #include "parser.h" #include "semantics.h" +#include "string.h" #include "tokenizer.h" #include <assert.h> @@ -12,28 +13,31 @@ bool ErrorOk(Error error) { return error.location.column == 0 && error.location.line == 0 && - error.message == nullptr && error.dynamic == nullptr; + StringEqCstr(error.message, "") && StringEqCstr(error.dynamic, ""); } void PrintError(Error error, const char* file) { - if (error.dynamic != nullptr) { + if (!StringEqCstr(error.dynamic, "")) { printf( - "%s:%d:%d: %s: %s.\n", + "%s:%d:%d: %.*s: %.*s.\n", file, (int) error.location.line, (int) error.location.column, - error.message, - error.dynamic + (int) error.message.size, + error.message.ptr, + (int) error.dynamic.size, + error.dynamic.ptr ); return; } printf( - "%s:%d:%d: %s.\n", + "%s:%d:%d: %.*s.\n", file, (int) error.location.line, (int) error.location.column, - error.message + (int) error.message.size, + error.message.ptr ); } diff --git a/src/compiler.h b/src/compiler.h @@ -2,6 +2,7 @@ #define COMPILER_COMPILER_H_ #include "arena.h" +#include "string.h" #include <stdio.h> @@ -11,15 +12,15 @@ typedef struct { } Location; typedef struct { - Location location; - const char* message; - char* dynamic; + Location location; + String message; + String dynamic; } Error; constexpr Error ZERO_ERROR = { {0, 0}, - nullptr, - nullptr + STR(""), + STR("") }; bool ErrorOk(Error error); diff --git a/src/parser.c b/src/parser.c @@ -21,8 +21,8 @@ Location MakeLocation(const char* program, Tokens* token) Tokens* SkipComments(Tokens* tokens) { - if (tokens == nullptr) - return nullptr; + if (tokens->this.kind == TokenEOF) + return tokens; if (tokens->this.kind == TokenComment) return SkipComments(tokens); tokens->next = SkipComments(tokens->next); @@ -31,41 +31,41 @@ Tokens* SkipComments(Tokens* tokens) Tokens* SkipIODeclaration(Tokens* tokens, const char* program, Error* error) { - if (tokens == nullptr || tokens->this.kind != TokenOpenParen) + if (tokens->this.kind != TokenOpenParen) return tokens; tokens = tokens->next; - if (tokens == nullptr || tokens->this.kind != TokenIdent) { + if (tokens->this.kind != TokenIdent) { *error = (Error) { .location = MakeLocation(program, tokens), - .message = "expected IO declaration", - .dynamic = nullptr, + .message = STR("expected IO declaration, got"), + .dynamic = tokens->this.lex, }; return nullptr; } tokens = tokens->next; - if (tokens == nullptr || tokens->this.kind != TokenComma) { + if (tokens->this.kind != TokenComma) { *error = (Error) { .location = MakeLocation(program, tokens), - .message = "expected comma", - .dynamic = nullptr, + .message = STR("expected comma, got"), + .dynamic = tokens->this.lex, }; return nullptr; } tokens = tokens->next; - if (tokens == nullptr || tokens->this.kind != TokenIdent) { + if (tokens->this.kind != TokenIdent) { *error = (Error) { .location = MakeLocation(program, tokens), - .message = "expected IO declaration", - .dynamic = nullptr, + .message = STR("expected IO declaration, got"), + .dynamic = tokens->this.lex, }; return nullptr; } tokens = tokens->next; - if (tokens == nullptr || tokens->this.kind != TokenCloseParen) { + if (tokens->this.kind != TokenCloseParen) { *error = (Error) { .location = MakeLocation(program, tokens), - .message = "expected closing parenthesis", - .dynamic = nullptr, + .message = STR("expected closing parenthesis, got"), + .dynamic = tokens->this.lex, }; return nullptr; } @@ -73,7 +73,36 @@ Tokens* SkipIODeclaration(Tokens* tokens, const char* program, Error* error) return tokens->next; } -Declarations* ParseVariableDeclarations( +Names* ParseNameList( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + if (tokens->this.kind != TokenIdent) { + *error = (Error) { + .location = MakeLocation(program, tokens), + .message = STR("expected identifier, got"), + .dynamic = tokens->this.lex, + }; + return nullptr; + } + + auto name = (Names*) Calloc(arena, sizeof(Names)); + name->this = tokens->this.lex; + if (tokens->next == nullptr || tokens->next->this.kind != TokenComma) { + *tokensAfter = tokens->next; + name->next = nullptr; + return name; + } + name->next = + ParseNameList(arena, program, tokens->next->next, error, tokensAfter); + return name; +} + +Array ParseArraySpecifier( Arena* arena, const char* program, Tokens* tokens, @@ -82,10 +111,119 @@ Declarations* ParseVariableDeclarations( ) { (void) arena; - (void) program; - (void) tokens; - (void) error; (void) tokensAfter; + if (tokens->this.kind != TokenOpenSq) { + *error = (Error) { + .location = MakeLocation(program, tokens), + .message = STR("expected array size specifier, got"), + .dynamic = tokens->this.lex, + }; + return (Array) {0}; + } + tokens = tokens->next; + if (tokens->this.kind != TokenDigit) { + *error = (Error) { + .location = MakeLocation(program, tokens), + .message = STR("expected number for array size specifier, got"), + .dynamic = tokens->this.lex, + }; + return (Array) {0}; + } + // TODO. +} + +Type ParseType( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + switch (tokens->this.kind) { + case TokenReal: + return (Type) { + .kind = TypeReal, + }; + case TokenInteger: + return (Type) { + .kind = TypeInteger, + }; + case TokenArray: + return (Type) { + .kind = TypeArray, + .array = ParseArraySpecifier( + arena, program, tokens->next, error, tokensAfter + ), + }; + break; + default: + *error = (Error) { + .location = MakeLocation(program, tokens), + .message = STR("expected type, got"), + .dynamic = tokens->this.lex, + }; + return (Type) {0}; + } +} + +Type ParseTypeSpecifier( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + if (tokens->this.kind != TokenColon) { + *error = (Error) { + .location = MakeLocation(program, tokens), + .message = STR("expected colon after identifiers, got"), + .dynamic = tokens->this.lex, + }; + return (Type) {0}; + } + + return ParseType(arena, program, tokens->next, error, tokensAfter); +} + +Declaration ParseDeclaration( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + Declaration this; + this.names = ParseNameList(arena, program, tokens, error, tokensAfter); + if (!ErrorOk(*error)) + return this; + this.type = + ParseTypeSpecifier(arena, program, *tokensAfter, error, tokensAfter); + return this; +} + +Declarations* ParseVariableDeclarations( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + if (tokens->this.kind != TokenVar) + return nullptr; + + auto decls = (Declarations*) Calloc(arena, sizeof(Declarations)); + decls->this = + ParseDeclaration(arena, program, tokens->next, error, tokensAfter); + if (!ErrorOk(*error)) + return nullptr; + decls->next = ParseVariableDeclarations( + arena, program, *tokensAfter, error, tokensAfter + ); + return decls; } Subprograms* ParseSubprograms( @@ -104,7 +242,7 @@ Subprograms* ParseSubprograms( return nullptr; } -Statement ParseStatement( +Expression ParseExpression( Arena* arena, const char* program, Tokens* tokens, @@ -117,7 +255,7 @@ Statement ParseStatement( (void) tokens; (void) error; *tokensAfter = tokens; - return (Statement) {0}; + return (Expression) {0}; } void ParseProgramBody( @@ -128,15 +266,6 @@ void ParseProgramBody( Error* error ) { - if (tokens == nullptr) { - *error = (Error) { - .location = MakeLocation(program, tokens), - .message = "expected at least main program declaration", - .dynamic = nullptr, - }; - return; - } - Tokens* tokensAfterDecl = nullptr; ast->declarations = ParseVariableDeclarations( arena, program, tokens, error, &tokensAfterDecl @@ -152,27 +281,25 @@ void ParseProgramBody( return; Tokens* tokensAfterMainPrg = nullptr; - ast->main = ParseStatement( + ast->main = ParseExpression( arena, program, tokensAfterSubprgs, error, &tokensAfterMainPrg ); if (!ErrorOk(*error)) return; - if (tokensAfterMainPrg == nullptr || - tokensAfterMainPrg->this.kind != TokenPeriod) { + if (tokensAfterMainPrg->this.kind != TokenPeriod) { *error = (Error) { .location = MakeLocation(program, nullptr), - .message = "expected . after main program declaration", - .dynamic = nullptr, + .message = STR("expected . after main program declaration, got"), + .dynamic = tokens->this.lex, }; return; } - if (tokensAfterMainPrg->next == nullptr || - tokensAfterMainPrg->next->this.kind != TokenEOF) { + if (tokensAfterMainPrg->next->this.kind != TokenEOF) { *error = (Error) { .location = MakeLocation(program, nullptr), - .message = "expected nothing, got something", - .dynamic = nullptr, + .message = STR("expected nothing, got"), + .dynamic = tokens->this.lex, }; return; } @@ -185,20 +312,20 @@ Ast* ParseProgram( Error* error ) { - if (tokens == nullptr || tokens->this.kind != TokenProgram) { + if (tokens->this.kind != TokenProgram) { *error = (Error) { .location = MakeLocation(program, tokens), - .message = "expected program declaration", - .dynamic = nullptr, + .message = STR("expected program declaration, got"), + .dynamic = tokens->this.lex, }; return nullptr; } tokens = tokens->next; - if (tokens == nullptr || tokens->this.kind != TokenIdent) { + if (tokens->this.kind != TokenIdent) { *error = (Error) { .location = MakeLocation(program, tokens), - .message = "expected program name", - .dynamic = nullptr, + .message = STR("expected program name, got"), + .dynamic = tokens->this.lex, }; } @@ -210,11 +337,11 @@ Ast* ParseProgram( tokens = SkipIODeclaration(tokens->next, program, error); if (!ErrorOk(*error)) return nullptr; - if (tokens == nullptr || tokens->this.kind != TokenSemicolon) { + if (tokens->this.kind != TokenSemicolon) { *error = (Error) { .location = MakeLocation(program, tokens), - .message = "expected semicolon", - .dynamic = nullptr, + .message = STR("expected semicolon, got"), + .dynamic = tokens->this.lex, }; return nullptr; } diff --git a/src/parser.h b/src/parser.h @@ -20,10 +20,7 @@ typedef struct { } Array; typedef struct { - union { - Array array; - } this; - + Array array; TypeKind kind; } Type; @@ -40,10 +37,29 @@ typedef struct { String content; } Literal; +typedef struct Compound { + struct Expression* this; + struct Compound* next; +} Compound; + +typedef struct { + struct Expression* condition; + struct Expression* body; + struct Expression* elsee; +} If; + +typedef struct { + struct Expression* condition; + struct Expression* body; +} While; + typedef enum { ExpressionLiteral, ExpressionName, ExpressionOperation, + ExpressionIf, + ExpressionWhile, + ExpressionCompound, } ExpressionKind; typedef struct { @@ -51,58 +67,26 @@ typedef struct { Literal literal; String name; Operation operation; + If iff; + While whilee; + Compound compound; } 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 Names { + String this; + struct Names* next; +} Names; 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 { - Assignment assignment; - If iff; - While whilee; - CompoundStatement compound; - } this; - - StatementKind kind; -} Statement; - -typedef struct { - String name; + Names* names; Type type; } Declaration; typedef struct Declarations { - Declaration* this; + Declaration this; struct Declarations* next; } Declarations; @@ -119,7 +103,7 @@ typedef struct { typedef struct { Parameters parameters; - Statement body; + Expression body; } Subprogram; typedef struct Subprograms { @@ -131,7 +115,7 @@ typedef struct Ast { String name; Declarations* declarations; Subprograms* subprograms; - Statement main; + Expression main; } Ast; Ast* Parse(Arena* arena, const char* program, Tokens* tokens, Error* error); diff --git a/src/string.h b/src/string.h @@ -6,6 +6,9 @@ typedef struct { unsigned long size; } String; +#define FROM_STR(str) ((String) {.ptr = str, .size = strlen(str)}) +#define STR(str) ((String) {.ptr = str, .size = sizeof(str) - 1}) + bool StringEq(String a, String b); bool StringEqCstr(String a, const char* cstr); diff --git a/src/tokenizer.c b/src/tokenizer.c @@ -12,6 +12,7 @@ typedef enum { StateClosingComment, StatePeriod, StateOperator, + StateColon, } State; typedef struct { @@ -34,6 +35,8 @@ static const Keyword keywords[] = { { .keyword = "else", .kind = TokenElse}, { .keyword = "function", .kind = TokenFunction}, {.keyword = "procedure", .kind = TokenProcedure}, + { .keyword = "real", .kind = TokenReal}, + { .keyword = "integer", .kind = TokenInteger}, }; constexpr unsigned long keywordsLen = sizeof(keywords) / sizeof(keywords[0]); @@ -43,8 +46,8 @@ constexpr unsigned long keywordsLen = sizeof(keywords) / sizeof(keywords[0]); Error MakeTokenError( const char* program, Token* this, - const char* message, - char* dynamic + String message, + String dynamic ) { int i = 0; @@ -133,9 +136,7 @@ Tokens* Tokenize(Arena* arena, char* program, Error* error) } else if (cchar == ':') { token->this.lex.ptr = &program[idx]; token->this.lex.size = 1; - token->this.kind = TokenColon; - token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); - token = token->next; + state = StateColon; idx += 1; } else if (cchar == '(') { token->this.lex.ptr = &program[idx]; @@ -219,7 +220,7 @@ Tokens* Tokenize(Arena* arena, char* program, Error* error) } else if (cchar == '\0') { *error = MakeTokenError( - program, &token->this, "unclosed comment", nullptr + program, &token->this, STR("unclosed comment"), STR("") ); token->next = nullptr; token->this.kind = TokenComment; @@ -269,6 +270,19 @@ Tokens* Tokenize(Arena* arena, char* program, Error* error) state = StateInitial; } break; + case StateColon: + if (cchar == '=') { + token->this.lex.size += 1; + idx += 1; + state = StateOperator; + } else { + token->this.lex.ptr = &program[idx]; + token->this.lex.size = 1; + token->this.kind = TokenColon; + token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); + token = token->next; + } + break; } } } diff --git a/src/tokenizer.h b/src/tokenizer.h @@ -11,6 +11,8 @@ typedef enum { TokenIdent, TokenDigit, TokenArray, + TokenInteger, + TokenReal, TokenOf, TokenOpenSq, TokenCloseSq,