1 commit xd
feat: tokenizer #6
@ -50,0 +17,4 @@
TOKEN_ASSIGNMENT_WORD, // assigned during parsing
TOKEN_NAME, //
TOKEN_IO_NUMBER, //
TOKEN_KEYWORD //
either remove all of these comments or actually explain TOKEN_ASSIGNMENT_WORD and TOKEN_IO_NUMBER (I'm for removing)
@ -62,0 +34,4 @@
#define SINGLE_QUOTE '\''
#define NEWLINE '\n'
#define DOLLAR '$'
#define BACKQUOTE '`'
I've always seen this called a backtick but if it's backquote in the docs then let's keep that
@ -66,0 +45,4 @@
do { \
if(!is_end()) { \
tk_state.prev_char = c; \
c = *(++tk_state.source); \
this macro assumes that there's a c, provide it as an argument
same with the macro below
@ -66,0 +47,4 @@
tk_state.prev_char = c; \
c = *(++tk_state.source); \
} \
} while(0)
this might be thinking about optimizations too early but do the compilers optimize these away?
we could check in some disassembler I think?
@ -66,2 +50,3 @@
} while(0)
NOT_IMPLEMENTED;
#define TK_ADD_TO_VALUE(c) \
both of these can be functions, please do that
@ -68,0 +61,4 @@
static struct {
char *source;
I'm guessing source means the current string, we're parsing, maybe rename to something like input or input_string? it doesn't make much sense when you're thinking about interactive mode
@ -68,0 +66,4 @@
token_type_t next_type;
} tk_state = {0};
minor thing but please remove one of the empty lines here and above on 61-62
@ -68,0 +72,4 @@
}
static bool is_operator_first_char(char c) {
for(size_t i = 0; i < sizeof(operators)/sizeof(operators[0]); i++)
sizeof(arr)/sizeof(arr[0]) is probably gonna be used a lot over the place, maybe make it a function / macro in util.h?
@ -68,0 +79,4 @@
}
static bool can_operator_be_continued(char *cur_op, char c) {
size_t i, j, sc = strlen(cur_op);
please put i and j declarations inside the loops, also maybe rename sc to cur_op_len?
@ -68,0 +81,4 @@
static bool can_operator_be_continued(char *cur_op, char c) {
size_t i, j, sc = strlen(cur_op);
char *newop;
malloc_safe(newop, sc + 1 /* c */ + 1 /* nul */);
small thing but these at first glance look like you did sc + 1 + c + 1 + nul earlier but uncommented these for debugging or something and forgot to remove them
maybe put these in a comment a line above lined up vertically with the ones?
@ -68,0 +85,4 @@
for(i = 0; i < sizeof(operators)/sizeof(operators[0]); i++) {
const char *op = operators[i];
size_t so = strlen(op);
same here, op_len instead of so
@ -68,0 +98,4 @@
for(j = 0; j < so && j < sc; j++)
if(newop[j] != op[j])
goto skip_op; // move onto the next operator
why not just continue here?????
nvm I'm blind and didn't see that you wanna continue the outer loop
@ -68,0 +121,4 @@
static token_t _next_token(void) {
token_t t = {0};
char quote_char = '0円', c;
bool quoted = false, request_more_input = false;
maybe split the declarations into one per line?
no
why not
i am saving disk space (few less bytes)
at least swap them, I missed the c the first time
@ -68,0 +189,4 @@
continue;
}
if(c == DOLLAR && !quoted) {
envvars can also be inside double quotes
@ -68,0 +257,4 @@
continue;
}
if(c == HASH) {
maybe we should parse the comment as a token too? IDK if it'll be of any use though
no
it wont
@ -68,0 +271,4 @@
TK_STATE_ADVANCE();
}
if(t.type == TOKEN_UNKNOWN && (t.value == NULL || !t.value[0]) && is_end())
maybe t.value[0] == '0円'?
@ -68,0 +306,4 @@
============================================================ */
static void remove_escaped_newlines(char **command) {
size_t len = strlen(*command), i;
put the i back into the loop where it belongs
@ -5,3 +7,4 @@
#define NOT_IMPLEMENTED assert("Not implemented" && 0)
#define malloc_safe(p, sz) \
make these 2 functions as well
@ -6,2 +6,4 @@
command_t cmd;
// ignore empty command (NULL or "")
if(!command || !command[0])
will this catch blank commands? (all whitespaces)
no but those dont segfault
ok let's leave it for now, in the future maybe we can trim the whole string before doing anything on it
No due date set.
No dependencies set.
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?