SolsNode stores line info

This commit is contained in:
2026-02-24 19:33:41 +11:00
parent d4017b7c18
commit 2266990cb4
3 changed files with 21 additions and 4 deletions

View File

@@ -15,6 +15,14 @@ typedef enum SolsTokenType {
typedef char* charptr; typedef char* charptr;
// Stores information about the line that the token/node is on, for printing if an error
// occurs.
// .num is the line number, .content is the line's contents.
typedef struct LineInfo {
size_t num;
char* content;
} LineInfo;
// Represents a token lexed by the lex() function. // Represents a token lexed by the lex() function.
// Most token types exclusively use the .type field, however some tokens require storing // Most token types exclusively use the .type field, however some tokens require storing
// more data, inside the .as union. // more data, inside the .as union.
@@ -31,10 +39,7 @@ typedef struct SolsToken {
char* idName; char* idName;
char* inlineGround; char* inlineGround;
} as; } as;
struct { LineInfo line;
size_t num;
char* content;
} line;
} SolsToken; } SolsToken;
Result(SolsToken, charptr); Result(SolsToken, charptr);

View File

@@ -2,11 +2,13 @@
#define SOLSNODE_H #define SOLSNODE_H
#include <stdarg.h> #include <stdarg.h>
#include <groundvm.h>
#include "../include/error.h" #include "../include/error.h"
#include "../lexer/SolsType.h" #include "../lexer/SolsType.h"
#include "../lexer/SolsLiteral.h" #include "../lexer/SolsLiteral.h"
#include "../lexer/SolsToken.h"
typedef enum SolsNodeType { typedef enum SolsNodeType {
SNT_IDENTIFIER, SNT_LITERAL, SNT_TYPE, SNT_CODE_BLOCK, SNT_OP_ADD, SNT_OP_SUB, SNT_OP_MUL, SNT_OP_DIV, SNT_OP_ADDTO, SNT_OP_SUBTO, SNT_OP_MULTO, SNT_OP_DIVTO, SNT_OP_INCREMENT, SNT_OP_DECREMENT, SNT_OP_SET, SNT_OP_GREATER, SNT_OP_LESSER, SNT_OP_EQUAL, SNT_OP_INEQUAL, SNT_OP_EQGREATER, SNT_OP_EQLESSER, SNT_DEF, SNT_STRUCT, SNT_PUTS, SNT_IF, SNT_WHILE, SNT_NEW, SNT_GROUND, SNT_ROOT SNT_IDENTIFIER, SNT_LITERAL, SNT_TYPE, SNT_CODE_BLOCK, SNT_OP_ADD, SNT_OP_SUB, SNT_OP_MUL, SNT_OP_DIV, SNT_OP_ADDTO, SNT_OP_SUBTO, SNT_OP_MULTO, SNT_OP_DIVTO, SNT_OP_INCREMENT, SNT_OP_DECREMENT, SNT_OP_SET, SNT_OP_GREATER, SNT_OP_LESSER, SNT_OP_EQUAL, SNT_OP_INEQUAL, SNT_OP_EQGREATER, SNT_OP_EQLESSER, SNT_DEF, SNT_STRUCT, SNT_PUTS, SNT_IF, SNT_WHILE, SNT_NEW, SNT_GROUND, SNT_ROOT
@@ -35,6 +37,10 @@ typedef struct SolsNode {
size_t capacity; size_t capacity;
struct SolsNode* at; struct SolsNode* at;
} children; } children;
LineInfo line;
// Used by the code generator, do not use in the parser!
GroundArg accessArg;
} SolsNode; } SolsNode;
Result(SolsNode, charptr); Result(SolsNode, charptr);

View File

@@ -138,6 +138,7 @@ static inline ResultType(Nothing, charptr) parseIdentifier(SolsParser* parser) {
APPEND_ESTR(err, " (in parseLiteral() function)"); APPEND_ESTR(err, " (in parseLiteral() function)");
return Error(Nothing, charptr, err.str); return Error(Nothing, charptr, err.str);
} }
node.as.success.line = peek.as.success.line;
addChildToSolsNode(parser->currentParent, node.as.success); addChildToSolsNode(parser->currentParent, node.as.success);
return Success(Nothing, charptr, {}); return Success(Nothing, charptr, {});
} }
@@ -153,6 +154,7 @@ static inline ResultType(Nothing, charptr) parseLiteral(SolsParser* parser) {
APPEND_ESTR(err, " (in parseLiteral() function)"); APPEND_ESTR(err, " (in parseLiteral() function)");
return Error(Nothing, charptr, err.str); return Error(Nothing, charptr, err.str);
} }
node.as.success.line = peek.as.success.line;
addChildToSolsNode(parser->currentParent, node.as.success); addChildToSolsNode(parser->currentParent, node.as.success);
return Success(Nothing, charptr, {}); return Success(Nothing, charptr, {});
} }
@@ -165,6 +167,9 @@ static inline ResultType(Nothing, charptr) parsePuts(SolsParser* parser) {
return Error(Nothing, charptr, tokens.as.error); return Error(Nothing, charptr, tokens.as.error);
} }
ResultType(SolsToken, Nothing) peek = parserPeek(parser, 0);
if (peek.error) return Error(Nothing, charptr, "ruh roh");
for (;;) { for (;;) {
ResultType(SolsToken, Nothing) peek = parserPeek(parser, 1); ResultType(SolsToken, Nothing) peek = parserPeek(parser, 1);
if (peek.error) break; if (peek.error) break;
@@ -178,6 +183,7 @@ static inline ResultType(Nothing, charptr) parsePuts(SolsParser* parser) {
// Create node // Create node
ResultType(SolsNode, charptr) node = createSolsNode(SNT_PUTS); ResultType(SolsNode, charptr) node = createSolsNode(SNT_PUTS);
if (node.error) return Error(Nothing, charptr, node.as.error); if (node.error) return Error(Nothing, charptr, node.as.error);
node.as.success.line = peek.as.success.line;
// Parse selected tokens // Parse selected tokens
ResultType(SolsParser, charptr) putsParser = createSolsParser(&tokens.as.success); ResultType(SolsParser, charptr) putsParser = createSolsParser(&tokens.as.success);