compiler

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

commit fcd570e4c7b55f0e82b1d23ef7cf41b78afdb6fa
parent 9e9951e7587a4004b818d9b94dbc71686038a8f5
Author: Gabriel de Brito <me@gboncoffee.dev.br>
Date:   Mon, 23 Nov 2026 11:50:09 -0300

Almost parsing expressions.

Diffstat:
Msrc/codegen.c | 6+++---
Msrc/parser.c | 308+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------
Msrc/parser.h | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------------
Msrc/tokenizer.c | 120++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------
Msrc/tokenizer.h | 17++++++++++++++++-
5 files changed, 457 insertions(+), 68 deletions(-)

diff --git a/src/codegen.c b/src/codegen.c @@ -2,7 +2,7 @@ void Codegen(Arena* arena, Ast* ast, FILE* output) { - (void)arena; - (void)output; - printf("Program: %.*s\n", (int)ast->name.size, ast->name.ptr); + (void) arena; + (void) output; + printf("Program: %.*s\n", (int) ast->name.size, ast->name.ptr); } diff --git a/src/parser.c b/src/parser.c @@ -28,7 +28,7 @@ Tokens* SkipComments(Tokens* tokens) if (tokens->this.kind == TokenEOF) return tokens; if (tokens->this.kind == TokenComment) - return SkipComments(tokens); + return SkipComments(tokens->next); tokens->next = SkipComments(tokens->next); return tokens; } @@ -172,6 +172,15 @@ Array ParseArraySpecifier( }; return array; } + tokens = tokens->next; + if (tokens->this.kind != TokenOf) { + *error = (Error) { + .location = MakeLocation(program, tokens), + .message = STR("expected of, got"), + .dynamic = tokens->this.lex, + }; + return array; + } *tokensAfter = tokens->next; @@ -190,6 +199,7 @@ Type ParseType( Tokens** tokensAfter ) { + *tokensAfter = tokens->next; switch (tokens->this.kind) { case TokenReal: return (Type) { @@ -262,21 +272,31 @@ Declarations* ParseVariableDeclarations( Tokens** tokensAfter ) { - if (tokens->this.kind != TokenVar) + if (tokens->this.kind != TokenVar) { + *tokensAfter = tokens; return nullptr; + } auto decls = (Declarations*) Calloc(arena, sizeof(Declarations)); decls->this = ParseDeclaration(arena, program, tokens->next, error, tokensAfter); if (!ErrorOk(*error)) return nullptr; + if ((*tokensAfter)->this.kind != TokenSemicolon) { + *error = (Error) { + .location = MakeLocation(program, *tokensAfter), + .message = STR("expected semicolon, got"), + .dynamic = (*tokensAfter)->this.lex, + }; + return nullptr; + } decls->next = ParseVariableDeclarations( - arena, program, *tokensAfter, error, tokensAfter + arena, program, (*tokensAfter)->next, error, tokensAfter ); return decls; } -Expression ParseExpression( +Expression ParseIf( Arena* arena, const char* program, Tokens* tokens, @@ -292,6 +312,224 @@ Expression ParseExpression( return (Expression) {0}; } +Expression ParseWhile( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + (void) arena; + (void) program; + (void) tokens; + (void) error; + (void) tokensAfter; + return (Expression) {0}; +} + +Expression ParseCompound( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + (void) arena; + (void) program; + (void) tokens; + (void) error; + (void) tokensAfter; + return (Expression) {0}; +} + +Expression ParseIdentOrCallOrIndex( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + (void) arena; + (void) program; + (void) tokens; + (void) error; + (void) tokensAfter; + return (Expression) {0}; +} + +Expression ParsePrimary( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + Expression expression; + + switch (tokens->this.kind) { + case TokenDigit: + expression.kind = ExpressionLiteral; + expression.literal.type = TypeInteger; + expression.literal.content = tokens->this.lex; + return expression; + case TokenIdent: + return ParseIdentOrCallOrIndex( + arena, program, tokens, error, tokensAfter + ); + case TokenPlus: + expression.kind = ExpressionUnaryOperation; + expression.unary = (UnaryOperation) { + .operator= UnaryPlus, + .operand = (Expression*) Calloc(arena, sizeof(Expression)), + }; + *expression.unary.operand = + ParsePrimary(arena, program, tokens->next, error, tokensAfter); + return expression; + case TokenMinus: + expression.kind = ExpressionUnaryOperation; + expression.unary = (UnaryOperation) { + .operator= UnaryMinus, + .operand = (Expression*) Calloc(arena, sizeof(Expression)), + }; + *expression.unary.operand = + ParsePrimary(arena, program, tokens->next, error, tokensAfter); + return expression; + case TokenNot: + expression.kind = ExpressionUnaryOperation; + expression.unary = (UnaryOperation) { + .operator= UnaryNot, + .operand = (Expression*) Calloc(arena, sizeof(Expression)), + }; + *expression.unary.operand = + ParsePrimary(arena, program, tokens->next, error, tokensAfter); + return expression; + case TokenOpenParen: + expression = + ParseExpression(arena, program, tokens->next, error, tokensAfter); + if ((*tokensAfter)->this.kind != TokenCloseParen) { + *error = (Error) { + .message = STR("expected ), got "), + .dynamic = (*tokensAfter)->this.lex, + }; + } + *tokensAfter = (*tokensAfter)->next; + return expression; + default: + break; + } +} + +int Token2BinaryOperator(TokenKind token, BinaryOperator* ret) +{ + switch (token) { + case TokenPlus: + *ret = BinaryPlus; + return 0; + case TokenMinus: + *ret = BinaryMinus; + return 0; + case TokenMul: + *ret = BinaryMul; + return 0; + case TokenDiv: + *ret = BinaryDiv; + return 0; + case TokenMod: + *ret = BinaryMod; + return 0; + case TokenAnd: + *ret = BinaryAnd; + return 0; + case TokenOr: + *ret = BinaryOr; + return 0; + case TokenEq: + *ret = BinaryEq; + return 0; + case TokenGe: + *ret = BinaryGe; + return 0; + case TokenGt: + *ret = BinaryGt; + return 0; + case TokenLe: + *ret = BinaryLe; + return 0; + case TokenLt: + *ret = BinaryLt; + return 0; + case TokenAssign: + *ret = BinaryAssign; + return 0; + + default: + return 1; + } +} + +int Precedence(BinaryOperator operator) +{ + switch (operator) { + case BinaryAssign: + return 0; + case BinaryEq: + case BinaryGe: + case BinaryGt: + case BinaryLe: + case BinaryLt: + return 1; + case BinaryPlus: + case BinaryMinus: + case BinaryOr: + return 2; + case BinaryMul: + case BinaryDiv: + case BinaryMod: + case BinaryAnd: + return 3; + default: + return -1; + } +} + +Expression ParseBinary( + int minPrec, + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + auto left = ParsePrimary(arena, program, tokens, error, tokensAfter); + + // TODO. +} + +Expression ParseExpression( + Arena* arena, + const char* program, + Tokens* tokens, + Error* error, + Tokens** tokensAfter +) +{ + switch (tokens->this.kind) { + case TokenIf: + return ParseIf(arena, program, tokens, error, tokensAfter); + case TokenWhile: + return ParseWhile(arena, program, tokens, error, tokensAfter); + case TokenBegin: + return ParseCompound(arena, program, tokens, error, tokensAfter); + default: + return ParseBinary(0, arena, program, tokens, error, tokensAfter); + } +} + Parameters* ParseParameterDeclarations( Arena* arena, const char* program, @@ -305,8 +543,8 @@ Parameters* ParseParameterDeclarations( return nullptr; } - auto parameters = (Parameters*) Calloc(sizeof(Parameters)); - parameters->next = nullptr; + auto parameters = (Parameters*) Calloc(arena, sizeof(Parameters)); + parameters->next = nullptr; parameters->this.reference = tokens->this.kind == TokenVar; if (tokens->this.kind == TokenVar) @@ -314,9 +552,11 @@ Parameters* ParseParameterDeclarations( parameters->this.this = ParseDeclaration(arena, program, tokens, error, tokensAfter); + if (!ErrorOk(*error)) + return parameters; if ((*tokensAfter)->this.kind == TokenSemicolon) parameters->next = ParseParameterDeclarations( - arena, program, (*tokensAfter)->next, tokensAfter + arena, program, (*tokensAfter)->next, error, tokensAfter ); return parameters; } @@ -371,20 +611,12 @@ Subprogram ParseFunction( prg.name = tokens->this.lex; prg.parameters = - ParseParameters(arena, program, tokens, error, tokensAfter); + ParseParameterList(arena, program, tokens->next, 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); + ParseTypeSpecifier(arena, program, *tokensAfter, error, tokensAfter); if (!ErrorOk(*error)) return prg; @@ -392,17 +624,24 @@ Subprogram ParseFunction( *error = (Error) { .location = MakeLocation(program, *tokensAfter), .message = STR("expected semicolon, got"), - .dynamic = tokens->this.lex, + .dynamic = (*tokensAfter)->this.lex, }; return prg; } + prg.locals = ParseVariableDeclarations( + arena, program, (*tokensAfter)->next, error, tokensAfter + ); + if (!ErrorOk(*error)) + return prg; prg.body = ParseExpression(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, + .dynamic = (*tokensAfter)->this.lex, }; } *tokensAfter = (*tokensAfter)->next; @@ -418,7 +657,7 @@ Subprogram ParseProcedure( ) { auto prg = (Subprogram) { - .returnType = TypeVoid, + .returnType = (Type) {.kind = TypeVoid}, }; if (tokens->this.kind != TokenIdent) { *error = (Error) { @@ -431,7 +670,7 @@ Subprogram ParseProcedure( prg.name = tokens->this.lex; prg.parameters = - ParseParameters(arena, program, tokens, error, tokensAfter); + ParseParameterList(arena, program, tokens->next, error, tokensAfter); if (!ErrorOk(*error)) return prg; @@ -439,17 +678,24 @@ Subprogram ParseProcedure( *error = (Error) { .location = MakeLocation(program, *tokensAfter), .message = STR("expected semicolon, got"), - .dynamic = tokens->this.lex, + .dynamic = (*tokensAfter)->this.lex, }; return prg; } + prg.locals = ParseVariableDeclarations( + arena, program, (*tokensAfter)->next, error, tokensAfter + ); + if (!ErrorOk(*error)) + return prg; prg.body = ParseExpression(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, + .dynamic = (*tokensAfter)->this.lex, }; } *tokensAfter = (*tokensAfter)->next; @@ -478,10 +724,13 @@ Subprograms* ParseSubprograms( ) { if (tokens->this.kind != TokenProcedure && - tokens->this.kind != TokenFunction) + tokens->this.kind != TokenFunction) { + *tokensAfter = tokens; return nullptr; + } auto prgs = (Subprograms*) Calloc(arena, sizeof(Subprograms)); + prgs->this = (Subprogram*) Calloc(arena, sizeof(Subprogram)); *prgs->this = ParseSubprogram(arena, program, tokens, error, tokensAfter); if (!ErrorOk(*error)) return nullptr; @@ -522,16 +771,16 @@ void ParseProgramBody( if (tokensAfterMainPrg->this.kind != TokenPeriod) { *error = (Error) { .location = MakeLocation(program, nullptr), - .message = STR("expected . after main program declaration, got"), - .dynamic = tokens->this.lex, + .message = STR("expected . after main program declaration"), + .dynamic = STR(""), }; return; } if (tokensAfterMainPrg->next->this.kind != TokenEOF) { *error = (Error) { - .location = MakeLocation(program, nullptr), + .location = MakeLocation(program, tokensAfterMainPrg->next), .message = STR("expected nothing, got"), - .dynamic = tokens->this.lex, + .dynamic = tokensAfterMainPrg->next->this.lex, }; return; } @@ -559,6 +808,7 @@ Ast* ParseProgram( .message = STR("expected program name, got"), .dynamic = tokens->this.lex, }; + return nullptr; } auto ast = (Ast*) Calloc(arena, sizeof(Ast)); @@ -578,7 +828,7 @@ Ast* ParseProgram( return nullptr; } - ParseProgramBody(arena, ast, program, tokens, error); + ParseProgramBody(arena, ast, program, tokens->next, error); return ast; } diff --git a/src/parser.h b/src/parser.h @@ -20,18 +20,50 @@ typedef struct { long end; } Array; -typedef struct { +typedef struct Type { Array array; TypeKind kind; } Type; struct Expression; +typedef enum { + BinaryPlus, + BinaryMinus, + BinaryMul, + BinaryDiv, + BinaryMod, + BinaryAnd, + BinaryOr, + BinaryEq, + BinaryGe, + BinaryGt, + BinaryLe, + BinaryLt, + BinaryAssign, +} BinaryOperator; + +typedef enum { + UnaryPlus, + UnaryMinus, + UnaryNot, +} UnaryOperator; + +typedef struct { + UnaryOperator operator; + struct Expression* operand; +} UnaryOperation; + typedef struct { - String operator; + BinaryOperator operator; struct Expression* left; struct Expression* right; -} Operation; +} BinaryOperation; + +typedef struct { + String name; + struct Expression* index; +} Index; typedef struct { Type type; @@ -44,6 +76,11 @@ typedef struct Compound { } Compound; typedef struct { + String name; + Compound* params; +} Call; + +typedef struct { struct Expression* condition; struct Expression* body; struct Expression* elsee; @@ -57,20 +94,24 @@ typedef struct { typedef enum { ExpressionLiteral, ExpressionName, - ExpressionOperation, ExpressionIf, ExpressionWhile, ExpressionCompound, + ExpressionBinaryOperation, + ExpressionUnaryOperation, + ExpressionIndex, + ExpressionCall, } ExpressionKind; -typedef struct { +typedef struct Expression { union { - Literal literal; - String name; - Operation operation; - If iff; - While whilee; - Compound compound; + Literal literal; + String name; + UnaryOperation unary; + BinaryOperation binary; + If iff; + While whilee; + Compound compound; } this; ExpressionKind kind; @@ -96,16 +137,17 @@ typedef struct { bool reference; } ParameterDeclaration; -typedef struct { +typedef struct Parameters { ParameterDeclaration this; struct Parameters* next; } Parameters; typedef struct { - Parameters* parameters; - Expression body; - Type returnType; - String name; + Parameters* parameters; + Declarations* locals; + Expression body; + Type returnType; + String name; } Subprogram; typedef struct Subprograms { diff --git a/src/tokenizer.c b/src/tokenizer.c @@ -11,8 +11,9 @@ typedef enum { StateComment, StateClosingComment, StatePeriod, - StateOperator, StateColon, + StateGt, + StateLt, } State; typedef struct { @@ -32,11 +33,15 @@ static const Keyword keywords[] = { { .keyword = "end", .kind = TokenEnd}, { .keyword = "if", .kind = TokenIf}, { .keyword = "else", .kind = TokenElse}, - { .keyword = "else", .kind = TokenElse}, { .keyword = "function", .kind = TokenFunction}, {.keyword = "procedure", .kind = TokenProcedure}, { .keyword = "real", .kind = TokenReal}, { .keyword = "integer", .kind = TokenInteger}, + { .keyword = "div", .kind = TokenDiv}, + { .keyword = "mod", .kind = TokenMod}, + { .keyword = "and", .kind = TokenAnd}, + { .keyword = "or", .kind = TokenOr}, + { .keyword = "not", .kind = TokenNot}, }; constexpr unsigned long keywordsLen = sizeof(keywords) / sizeof(keywords[0]); @@ -45,15 +50,15 @@ constexpr unsigned long keywordsLen = sizeof(keywords) / sizeof(keywords[0]); Error MakeTokenError( const char* program, - Token* this, - String message, - String dynamic + Token* this, + String message, + String dynamic ) { int i = 0; int line = 1; int column = 1; - while (&program[i] <= this->lex.ptr) { + while (&program[i] < this->lex.ptr) { if (program[i] == '\n') { column = 0; line += 1; @@ -169,13 +174,64 @@ Tokens* Tokenize(Arena* arena, char* program, Error* error) token->this.lex.size = 1; state = StatePeriod; idx += 1; - } else if (ispunct(cint)) { + } else if (cchar == '+') { token->this.lex.ptr = &program[idx]; token->this.lex.size = 1; - state = StateOperator; + token->this.kind = TokenPlus; + token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); + token = token->next; idx += 1; - } else { + } else if (cchar == '-') { + token->this.lex.ptr = &program[idx]; + token->this.lex.size = 1; + token->this.kind = TokenMinus; + token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); + token = token->next; idx += 1; + } else if (cchar == '*') { + token->this.lex.ptr = &program[idx]; + token->this.lex.size = 1; + token->this.kind = TokenMul; + token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); + token = token->next; + idx += 1; + } else if (cchar == '=') { + token->this.lex.ptr = &program[idx]; + token->this.lex.size = 1; + token->this.kind = TokenEq; + token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); + token = token->next; + idx += 1; + } else if (cchar == '>') { + token->this.lex.ptr = &program[idx]; + token->this.lex.size = 1; + state = StateGt; + idx += 1; + } else if (cchar == '<') { + token->this.lex.ptr = &program[idx]; + token->this.lex.size = 1; + state = StateLt; + idx += 1; + } else if (cchar == '/') { + token->this.lex.ptr = &program[idx]; + token->this.lex.size = 1; + token->this.kind = TokenSlash; + token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); + token = token->next; + idx += 1; + } else { + *error = MakeTokenError( + program, + &token->this, + STR("unexpected symbol"), + (String){ + .ptr = &program[idx], + .size = 1, + } + ); + token->next = nullptr; + token->this.kind = TokenEOF; + return list; } break; case StateAlpha: @@ -239,8 +295,6 @@ Tokens* Tokenize(Arena* arena, char* program, Error* error) state = StateInitial; idx += 1; } else { - idx += 1; - token->this.lex.size += 1; state = StateComment; } break; @@ -259,28 +313,56 @@ Tokens* Tokenize(Arena* arena, char* program, Error* error) state = StateInitial; } break; - case StateOperator: - if (ispunct(cint)) { + case StateColon: + if (cchar == '=') { + token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); token->this.lex.size += 1; + token->this.kind = TokenAssign; + token = token->next; + state = StateInitial; idx += 1; } else { + token->this.kind = TokenColon; token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); - token->this.kind = TokenOperator; + token = token->next; + state = StateInitial; + } + break; + case StateGt: + if (cchar == '=') { + token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); + token->this.lex.size += 1; + token->this.kind = TokenGe; token = token->next; state = StateInitial; + idx += 1; + } else { + token->this.kind = TokenGt; + token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); + token = token->next; + state = StateInitial; } break; - case StateColon: + case StateLt: if (cchar == '=') { + token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); + token->this.lex.size += 1; + token->this.kind = TokenLe; + token = token->next; + state = StateInitial; + idx += 1; + } else if (cchar == '>') { + token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); token->this.lex.size += 1; + token->this.kind = TokenNeq; + token = token->next; + state = StateInitial; idx += 1; - state = StateOperator; } else { - token->this.lex.ptr = &program[idx]; - token->this.lex.size = 1; - token->this.kind = TokenColon; + token->this.kind = TokenLt; token->next = (Tokens*) Calloc(arena, sizeof(*token->next)); token = token->next; + state = StateInitial; } break; } diff --git a/src/tokenizer.h b/src/tokenizer.h @@ -23,7 +23,22 @@ typedef enum { TokenSemicolon, TokenPeriod, TokenTwoPeriods, - TokenOperator, + TokenPlus, + TokenMinus, + TokenMul, + TokenDiv, + TokenMod, + TokenEq, + TokenNeq, + TokenLt, + TokenGt, + TokenLe, + TokenGe, + TokenAssign, + TokenSlash, + TokenAnd, + TokenOr, + TokenNot, TokenIf, TokenElse, TokenThen,