From 5d2f0d187bc27298b7a233f842dd4cb5dafd01a2 Mon Sep 17 00:00:00 2001 From: Richard Thier Date: Thu, 26 Sep 2024 13:05:38 +0200 Subject: [PATCH] line and column handling + better debug log --- engine/slc.h | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/engine/slc.h b/engine/slc.h index cf1e615..d69d7e6 100644 --- a/engine/slc.h +++ b/engine/slc.h @@ -246,23 +246,24 @@ typedef iores (*ioconn)(SLC_IO_OP op, const char *param); */ typedef char (*coderead)(); -enum slc_state : uint32_t { +/** States the main state-engine can pick up - use characters for debugging better */ +enum slc_state : uint8_t { /** Before things */ - SLC_START = 0, + SLC_START = 's', /** In a comment */ - SLC_COMMENT = 1, + SLC_COMMENT = 'c', /** In multi-line comment */ - SLC_MULTILINE_COMMENT = 2, + SLC_MULTILINE_COMMENT = 'm', /** Name part of word-definition (after ':') - whitespace ends it */ - SLC_DEF_NAME = 3, + SLC_DEF_NAME = 'd', /** Variable-listing part of word-definition - endline, '(', '[' or '{' ends it */ - SLC_DEF_VAR = 4, + SLC_DEF_VAR = 'D', /** Raw body part of the word definition - these can contain local variable accesses + words, depth counted by vars */ - SLC_DEF_BODY = 5, + SLC_DEF_BODY = 'b', /** Name part of a word "call" (non-definition). Ends by whitespace, '@' (in case of variable) or various parentheses */ - SLC_WORD_NAME = 6, + SLC_WORD_NAME = 'w', /** Variable call (MYWORD@MYVAR) - we get to be here from SLC_WORD_NAME or from START */ - SLC_WORD_VAR = 7, + SLC_WORD_VAR = 'W', }; typedef enum slc_state slc_state; @@ -372,11 +373,14 @@ static inline void slc( const char *ender, const char *varprefix) { - slc_state state = SLC_START; + char last_is_endl = 0; + int line = 0; + int col_plus_one = -1; - // TODO: Count line numbers // TODO: Handle/count indentation for better error messages + slc_state state = SLC_START; + int comment_i = 0; int multiline_i = 0; int prefix_i = 0; @@ -385,9 +389,18 @@ static inline void slc( char c = 0; while(((c = code_src()) != 0)) { + /* Handle lines and columns */ + if(c == '\n' || c == '\r') { + if(!last_is_endl) { + ++line; + col_plus_one = 0; + } + last_is_endl = 1; + } else { last_is_endl = 0; ++col_plus_one; } process_char: + int col = col_plus_one; #ifdef SLC_DEBUG - printf("%c @ %d\n", c, state); + printf("%c state:%c @ line:%d col:%d\n", c, state, line, col); #endif switch(state) { case SLC_START: