diff --git a/src/lexer/SolsToken.h b/src/lexer/SolsToken.h index 21abee1..c306fb3 100644 --- a/src/lexer/SolsToken.h +++ b/src/lexer/SolsToken.h @@ -15,6 +15,14 @@ typedef enum SolsTokenType { 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. // Most token types exclusively use the .type field, however some tokens require storing // more data, inside the .as union. @@ -31,10 +39,7 @@ typedef struct SolsToken { char* idName; char* inlineGround; } as; - struct { - size_t num; - char* content; - } line; + LineInfo line; } SolsToken; Result(SolsToken, charptr); diff --git a/src/parser/SolsNode.h b/src/parser/SolsNode.h index 3d6eb52..3cd3282 100644 --- a/src/parser/SolsNode.h +++ b/src/parser/SolsNode.h @@ -2,11 +2,13 @@ #define SOLSNODE_H #include +#include #include "../include/error.h" #include "../lexer/SolsType.h" #include "../lexer/SolsLiteral.h" +#include "../lexer/SolsToken.h" 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 @@ -35,6 +37,10 @@ typedef struct SolsNode { size_t capacity; struct SolsNode* at; } children; + LineInfo line; + + // Used by the code generator, do not use in the parser! + GroundArg accessArg; } SolsNode; Result(SolsNode, charptr); diff --git a/src/parser/parser.c b/src/parser/parser.c index a7434d8..6544b94 100644 --- a/src/parser/parser.c +++ b/src/parser/parser.c @@ -138,6 +138,7 @@ static inline ResultType(Nothing, charptr) parseIdentifier(SolsParser* parser) { APPEND_ESTR(err, " (in parseLiteral() function)"); return Error(Nothing, charptr, err.str); } + node.as.success.line = peek.as.success.line; addChildToSolsNode(parser->currentParent, node.as.success); return Success(Nothing, charptr, {}); } @@ -153,6 +154,7 @@ static inline ResultType(Nothing, charptr) parseLiteral(SolsParser* parser) { APPEND_ESTR(err, " (in parseLiteral() function)"); return Error(Nothing, charptr, err.str); } + node.as.success.line = peek.as.success.line; addChildToSolsNode(parser->currentParent, node.as.success); return Success(Nothing, charptr, {}); } @@ -165,6 +167,9 @@ static inline ResultType(Nothing, charptr) parsePuts(SolsParser* parser) { return Error(Nothing, charptr, tokens.as.error); } + ResultType(SolsToken, Nothing) peek = parserPeek(parser, 0); + if (peek.error) return Error(Nothing, charptr, "ruh roh"); + for (;;) { ResultType(SolsToken, Nothing) peek = parserPeek(parser, 1); if (peek.error) break; @@ -178,6 +183,7 @@ static inline ResultType(Nothing, charptr) parsePuts(SolsParser* parser) { // Create node ResultType(SolsNode, charptr) node = createSolsNode(SNT_PUTS); if (node.error) return Error(Nothing, charptr, node.as.error); + node.as.success.line = peek.as.success.line; // Parse selected tokens ResultType(SolsParser, charptr) putsParser = createSolsParser(&tokens.as.success);