handle indentation + minor cleanup, words should know their indentation for pythonlikes..

This commit is contained in:
Richard Thier 2024-09-26 13:25:58 +02:00
parent 5d2f0d187b
commit 5ec1879796
2 changed files with 27 additions and 9 deletions

View File

@ -38,11 +38,17 @@ enum SLC_WORDTYP {
}; };
typedef enum SLC_WORDTYP 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) { static inline int get_padding(uint8_t *ptr, int alignment) {
// return (alignment - (ptr % alignment)) % alignment; // return (alignment - (ptr % alignment)) % alignment;
return (ptrdiff_t)(ptr + alignment - 1) / 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... * 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; char last_is_endl = 0;
int line = 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 // TODO: Handle/count indentation for better error messages
@ -389,18 +397,28 @@ static inline void slc(
char c = 0; char c = 0;
while(((c = code_src()) != 0)) { while(((c = code_src()) != 0)) {
/* Handle lines and columns */ /* Handle lines and columns, parts of indenting */
if(c == '\n' || c == '\r') { if(endsline(c)) {
if(!last_is_endl) { if(!last_is_endl) {
++line; ++line;
col_plus_one = 0; col = 0;
/* Indent part */
is_indenting = 1;
indent = 0;
} }
last_is_endl = 1; 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: process_char:
int col = col_plus_one;
#ifdef SLC_DEBUG #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 #endif
switch(state) { switch(state) {
case SLC_START: case SLC_START:

4
main.c
View File

@ -33,8 +33,8 @@ char testcoderead() {
"/*#push(21)\n" "/*#push(21)\n"
"#push(21)\n" "#push(21)\n"
"#add*/\n" "#add*/\n"
"#push(42)\n" " #push(42)\n"
"#print\n"; " #print\n";
return code[i++]; return code[i++];
} }