From 5ec187979649be16212d8d39665846095bfe2ac0 Mon Sep 17 00:00:00 2001 From: Richard Thier Date: Thu, 26 Sep 2024 13:25:58 +0200 Subject: [PATCH] handle indentation + minor cleanup, words should know their indentation for pythonlikes.. --- engine/slc.h | 32 +++++++++++++++++++++++++------- main.c | 4 ++-- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/engine/slc.h b/engine/slc.h index d69d7e6..d0a4d3e 100644 --- a/engine/slc.h +++ b/engine/slc.h @@ -38,11 +38,17 @@ enum SLC_WORDTYP { }; typedef enum SLC_WORDTYP SLC_WORDTYP; +/** Gets padding bytes for a memory address to be padded to alignment */ static inline int get_padding(uint8_t *ptr, int alignment) { // return (alignment - (ptr % alignment)) % alignment; return (ptrdiff_t)(ptr + alignment - 1) / alignment * alignment; } +/** Tells if c ends a line (that is either \n or \r) */ +static inline char endsline(char c) { + return (c == '\n') || (c == '\r'); +} + /** * A word definition starts right after this. After processing it, we inline overwrite random parts of it in memory... * @@ -375,7 +381,9 @@ static inline void slc( char last_is_endl = 0; int line = 0; - int col_plus_one = -1; + int col = -1; + char is_indenting = 1; + int indent = 0; // TODO: Handle/count indentation for better error messages @@ -389,18 +397,28 @@ static inline void slc( char c = 0; while(((c = code_src()) != 0)) { - /* Handle lines and columns */ - if(c == '\n' || c == '\r') { + /* Handle lines and columns, parts of indenting */ + if(endsline(c)) { if(!last_is_endl) { ++line; - col_plus_one = 0; + col = 0; + /* Indent part */ + is_indenting = 1; + indent = 0; } last_is_endl = 1; - } else { last_is_endl = 0; ++col_plus_one; } + } else { last_is_endl = 0; ++col; } + + /* Handle indenting */ + if((c == ' ') || (c == ' ')) { + indent += is_indenting; + } else { + /* Defends against state-changer endline */ + is_indenting = endsline(c); + } process_char: - int col = col_plus_one; #ifdef SLC_DEBUG - printf("%c state:%c @ line:%d col:%d\n", c, state, line, col); + fprintf(stderr, "%c state:%c @ line:%d col:%d indent:%d\n", c, state, line, col, indent); #endif switch(state) { case SLC_START: diff --git a/main.c b/main.c index ec07b0d..46dd68b 100644 --- a/main.c +++ b/main.c @@ -33,8 +33,8 @@ char testcoderead() { "/*#push(21)\n" "#push(21)\n" "#add*/\n" - "#push(42)\n" - "#print\n"; + " #push(42)\n" + " #print\n"; return code[i++]; }