compiler

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

commit 9e9951e7587a4004b818d9b94dbc71686038a8f5
parent 4e242074ef7528c0a89cb899bda6c9f93d1c504a
Author: Gabriel G. de Brito <me@gboncoffee.dev.br>
Date:   Sat, 14 Nov 2026 18:15:25 -0300

Ok we can parse everything except actual code.

Diffstat:
Msrc/parser.c | 264++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
Msrc/parser.h | 22++++++++++++----------
Msrc/string.c | 32++++++++++++++++++++++++++++++--
Msrc/string.h | 5+++--
4 files changed, 293 insertions(+), 30 deletions(-)

diff --git a/src/parser.c b/src/parser.c @@ -1,6 +1,10 @@ +#include "arena.h" +#include "compiler.h" #include "parser.h" #include "tokenizer.h" +#include <string.h> + Location MakeLocation(const char* program, Tokens* token) { auto location = (Location) {1, 1}; @@ -102,6 +106,14 @@ Names* ParseNameList( return name; } +Type ParseType( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +); + Array ParseArraySpecifier( Arena* arena, const char* program, @@ -110,26 +122,64 @@ Array ParseArraySpecifier( Tokens** tokensAfter ) { - (void) arena; - (void) tokensAfter; + auto array = (Array) {0}; 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}; + return array; + } + tokens = tokens->next; + if (tokens->this.kind != TokenDigit || + String2Long(tokens->this.lex, &array.begin)) { + *error = (Error) { + .location = MakeLocation(program, tokens), + .message = STR("expected number for array size specifier, got"), + .dynamic = tokens->this.lex, + }; + return array; + } + tokens = tokens->next; + if (tokens->this.kind != TokenTwoPeriods) { + *error = (Error) { + .location = MakeLocation(program, tokens), + .message = + STR("expected two periods for array size specifier, got"), + .dynamic = tokens->this.lex, + }; + return array; } tokens = tokens->next; - if (tokens->this.kind != TokenDigit) { + if (tokens->this.kind != TokenDigit || + String2Long(tokens->this.lex, &array.end)) { *error = (Error) { .location = MakeLocation(program, tokens), .message = STR("expected number for array size specifier, got"), .dynamic = tokens->this.lex, }; - return (Array) {0}; + return array; + } + tokens = tokens->next; + if (tokens->this.kind != TokenCloseSq) { + *error = (Error) { + .location = MakeLocation(program, tokens), + .message = STR( + "expected closing square bracket for array size specifier, got" + ), + .dynamic = tokens->this.lex, + }; + return array; } - // TODO. + + *tokensAfter = tokens->next; + + array.content = (struct Type*) Calloc(arena, sizeof(Type)); + auto type = ParseType(arena, program, *tokensAfter, error, tokensAfter); + memcpy(array.content, &type, sizeof(type)); + + return array; } Type ParseType( @@ -226,7 +276,7 @@ Declarations* ParseVariableDeclarations( return decls; } -Subprograms* ParseSubprograms( +Expression ParseExpression( Arena* arena, const char* program, Tokens* tokens, @@ -238,11 +288,11 @@ Subprograms* ParseSubprograms( (void) program; (void) tokens; (void) error; - *tokensAfter = tokens; - return nullptr; + (void) tokensAfter; + return (Expression) {0}; } -Expression ParseExpression( +Parameters* ParseParameterDeclarations( Arena* arena, const char* program, Tokens* tokens, @@ -250,12 +300,194 @@ Expression ParseExpression( Tokens** tokensAfter ) { - (void) arena; - (void) program; - (void) tokens; - (void) error; - *tokensAfter = tokens; - return (Expression) {0}; + if (tokens->this.kind != TokenIdent && tokens->this.kind != TokenVar) { + *tokensAfter = tokens; + return nullptr; + } + + auto parameters = (Parameters*) Calloc(sizeof(Parameters)); + parameters->next = nullptr; + parameters->this.reference = tokens->this.kind == TokenVar; + + if (tokens->this.kind == TokenVar) + tokens = tokens->next; + + parameters->this.this = + ParseDeclaration(arena, program, tokens, error, tokensAfter); + if ((*tokensAfter)->this.kind == TokenSemicolon) + parameters->next = ParseParameterDeclarations( + arena, program, (*tokensAfter)->next, tokensAfter + ); + return parameters; +} + +Parameters* ParseParameterList( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + if (tokens->this.kind != TokenOpenParen) { + *tokensAfter = tokens; + return nullptr; + } + + auto parameters = ParseParameterDeclarations( + arena, program, tokens->next, error, tokensAfter + ); + + if ((*tokensAfter)->this.kind != TokenCloseParen) { + *error = (Error) { + .location = MakeLocation(program, *tokensAfter), + .message = STR("expected ), got"), + .dynamic = (*tokensAfter)->this.lex, + }; + return parameters; + } + *tokensAfter = (*tokensAfter)->next; + + return parameters; +} + +Subprogram ParseFunction( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + auto prg = (Subprogram) {0}; + if (tokens->this.kind != TokenIdent) { + *error = (Error) { + .location = MakeLocation(program, tokens), + .message = STR("expected function name, got"), + .dynamic = tokens->this.lex, + }; + return prg; + } + + prg.name = tokens->this.lex; + prg.parameters = + ParseParameters(arena, program, tokens, error, tokensAfter); + if (!ErrorOk(*error)) + return prg; + + if ((*tokensAfter)->this.kind != TokenColon) { + *error = (Error) { + .location = MakeLocation(program, *tokensAfter), + .message = STR("expected return type, got"), + .dynamic = tokens->this.lex, + }; + return prg; + } + prg.returnType = + ParseType(arena, program, *tokensAfter, error, tokensAfter); + if (!ErrorOk(*error)) + return prg; + + if ((*tokensAfter)->this.kind != TokenSemicolon) { + *error = (Error) { + .location = MakeLocation(program, *tokensAfter), + .message = STR("expected semicolon, got"), + .dynamic = tokens->this.lex, + }; + return prg; + } + prg.body = + ParseExpression(arena, program, *tokensAfter, error, tokensAfter); + if ((*tokensAfter)->this.kind != TokenSemicolon) { + *error = (Error) { + .location = MakeLocation(program, *tokensAfter), + .message = STR("expected semicolon, got"), + .dynamic = tokens->this.lex, + }; + } + *tokensAfter = (*tokensAfter)->next; + return prg; +} + +Subprogram ParseProcedure( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + auto prg = (Subprogram) { + .returnType = TypeVoid, + }; + if (tokens->this.kind != TokenIdent) { + *error = (Error) { + .location = MakeLocation(program, tokens), + .message = STR("expected procedure name, got"), + .dynamic = tokens->this.lex, + }; + return prg; + } + + prg.name = tokens->this.lex; + prg.parameters = + ParseParameters(arena, program, tokens, error, tokensAfter); + if (!ErrorOk(*error)) + return prg; + + if ((*tokensAfter)->this.kind != TokenSemicolon) { + *error = (Error) { + .location = MakeLocation(program, *tokensAfter), + .message = STR("expected semicolon, got"), + .dynamic = tokens->this.lex, + }; + return prg; + } + prg.body = + ParseExpression(arena, program, *tokensAfter, error, tokensAfter); + if ((*tokensAfter)->this.kind != TokenSemicolon) { + *error = (Error) { + .location = MakeLocation(program, *tokensAfter), + .message = STR("expected semicolon, got"), + .dynamic = tokens->this.lex, + }; + } + *tokensAfter = (*tokensAfter)->next; + return prg; +} + +Subprogram ParseSubprogram( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + if (tokens->this.kind == TokenFunction) + return ParseFunction(arena, program, tokens->next, error, tokensAfter); + return ParseProcedure(arena, program, tokens->next, error, tokensAfter); +} + +Subprograms* ParseSubprograms( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + if (tokens->this.kind != TokenProcedure && + tokens->this.kind != TokenFunction) + return nullptr; + + auto prgs = (Subprograms*) Calloc(arena, sizeof(Subprograms)); + *prgs->this = ParseSubprogram(arena, program, tokens, error, tokensAfter); + if (!ErrorOk(*error)) + return nullptr; + prgs->next = + ParseSubprograms(arena, program, *tokensAfter, error, tokensAfter); + return prgs; } void ParseProgramBody( diff --git a/src/parser.h b/src/parser.h @@ -6,6 +6,7 @@ #include "tokenizer.h" typedef enum { + TypeVoid, TypeInteger, TypeReal, TypeArray, @@ -14,9 +15,9 @@ typedef enum { struct Type; typedef struct { - struct Type* content; - unsigned long begin; - unsigned long end; + struct Type* content; + long begin; + long end; } Array; typedef struct { @@ -91,19 +92,20 @@ typedef struct Declarations { } Declarations; typedef struct { - String name; - Type type; - bool reference; -} Parameter; + Declaration this; + bool reference; +} ParameterDeclaration; typedef struct { - Parameter this; + ParameterDeclaration this; struct Parameters* next; } Parameters; typedef struct { - Parameters parameters; - Expression body; + Parameters* parameters; + Expression body; + Type returnType; + String name; } Subprogram; typedef struct Subprograms { diff --git a/src/string.c b/src/string.c @@ -1,13 +1,41 @@ #include "string.h" +#include <stdckdint.h> #include <string.h> -bool StringEqCstr(String a, const char* b) +bool StringEqCstr(const String a, const char* b) { return ((strncmp(a.ptr, b, a.size) == 0) && strlen(b) == a.size); } -bool StringEq(String a, String b) +bool StringEq(const String a, const String b) { return a.size == b.size && memcmp(a.ptr, b.ptr, a.size) == 0; } + +int String2Long(const String a, long* ret) +{ + unsigned long i = 0; + bool neg = false; + long r = 0; + + if (a.size == 0) + return 1; + if (a.ptr[0] == '-' || a.ptr[0] == '+') { + neg = a.ptr[0] == '-'; + i = 1; + if (a.size == 1) + return 1; + } + + for (; i < a.size; i++) { + if (a.ptr[i] < '0' || a.ptr[i] > '9') + return 1; + long d = a.ptr[i] - '0'; + if (ckd_mul(&r, r, 10) || (neg ? ckd_sub(&r, r, d) : ckd_add(&r, r, d))) + return 1; + } + + *ret = r; + return 0; +} diff --git a/src/string.h b/src/string.h @@ -9,7 +9,8 @@ typedef struct { #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); +bool StringEq(const String a, const String b); +bool StringEqCstr(const String a, const char* cstr); +int String2Long(const String a, long* ret); #endif // COMPILER_STRING_H_