Compare commits
2 Commits
024af6a778
...
8067b93e62
| Author | SHA1 | Date |
|---|---|---|
|
|
8067b93e62 | |
|
|
5ea2d8fe2a |
|
|
@ -1776,8 +1776,129 @@ Boolean builtin_rand(InterpreterState *interpreter_state) {
|
|||
}
|
||||
|
||||
Boolean builtin_roll(InterpreterState *interpreter_state) {
|
||||
(void)interpreter_state;
|
||||
if (interpreter_state->stack == NULL) return FALSE;
|
||||
if (interpreter_state->stack->next == NULL) return FALSE;
|
||||
|
||||
StackItem *times_item = interpreter_state->stack;
|
||||
StackItem *count_item = times_item->next;
|
||||
|
||||
size_t times;
|
||||
switch (times_item->type) {
|
||||
case STACK_IDENTIFIER:
|
||||
return FALSE;
|
||||
case STACK_I64:
|
||||
if (times_item->i64 < 0) return FALSE;
|
||||
times = times_item->i64;
|
||||
break;
|
||||
case STACK_I32:
|
||||
if (times_item->i32 < 0) return FALSE;
|
||||
times = times_item->i32;
|
||||
break;
|
||||
case STACK_I16:
|
||||
if (times_item->i16 < 0) return FALSE;
|
||||
times = times_item->i16;
|
||||
break;
|
||||
case STACK_I8:
|
||||
if (times_item->i8 < 0) return FALSE;
|
||||
times = times_item->i8;
|
||||
break;
|
||||
case STACK_U64:
|
||||
times = times_item->u64;
|
||||
break;
|
||||
case STACK_U32:
|
||||
times = times_item->u32;
|
||||
break;
|
||||
case STACK_U16:
|
||||
times = times_item->u16;
|
||||
break;
|
||||
case STACK_U8:
|
||||
times = times_item->u8;
|
||||
break;
|
||||
case STACK_FLOAT:
|
||||
return FALSE;
|
||||
case STACK_DOUBLE:
|
||||
return FALSE;
|
||||
case STACK_CHARACTER:
|
||||
times = times_item->character;
|
||||
break;
|
||||
case STACK_BOOLEAN:
|
||||
times = times_item->boolean;
|
||||
break;
|
||||
case STACK_TOKEN_STRING:
|
||||
case STACK_CALLABLE:
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
size_t count;
|
||||
switch (count_item->type) {
|
||||
case STACK_IDENTIFIER:
|
||||
return FALSE;
|
||||
case STACK_I64:
|
||||
if (count_item->i64 < 0) return FALSE;
|
||||
count = count_item->i64;
|
||||
break;
|
||||
case STACK_I32:
|
||||
if (count_item->i32 < 0) return FALSE;
|
||||
count = count_item->i32;
|
||||
break;
|
||||
case STACK_I16:
|
||||
if (count_item->i16 < 0) return FALSE;
|
||||
count = count_item->i16;
|
||||
break;
|
||||
case STACK_I8:
|
||||
if (count_item->i8 < 0) return FALSE;
|
||||
count = count_item->i8;
|
||||
break;
|
||||
case STACK_U64:
|
||||
count = count_item->u64;
|
||||
break;
|
||||
case STACK_U32:
|
||||
count = count_item->u32;
|
||||
break;
|
||||
case STACK_U16:
|
||||
count = count_item->u16;
|
||||
break;
|
||||
case STACK_U8:
|
||||
count = count_item->u8;
|
||||
break;
|
||||
case STACK_FLOAT:
|
||||
return FALSE;
|
||||
case STACK_DOUBLE:
|
||||
return FALSE;
|
||||
case STACK_CHARACTER:
|
||||
count = count_item->character;
|
||||
break;
|
||||
case STACK_BOOLEAN:
|
||||
count = count_item->boolean;
|
||||
break;
|
||||
case STACK_TOKEN_STRING:
|
||||
case STACK_CALLABLE:
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
StackItem *head = NULL, *tail = NULL, *prev = NULL;
|
||||
if (count > 0) {
|
||||
StackItem *node = count_item->next;
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
if (node == NULL) return FALSE;
|
||||
if (i == (count - times) - 1) {
|
||||
tail = node;
|
||||
head = node->next;
|
||||
}
|
||||
prev = node;
|
||||
node = node->next;
|
||||
}
|
||||
}
|
||||
|
||||
if (head == NULL || tail == NULL) return FALSE;
|
||||
|
||||
interpreter_state->stack = head;
|
||||
tail->next = prev->next;
|
||||
prev->next = count_item->next;
|
||||
count_item->next = NULL;
|
||||
clean_stack(times_item);
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
Boolean builtin_rot(InterpreterState *interpreter_state) {
|
||||
|
|
@ -2009,6 +2130,49 @@ Boolean builtin_type_of(InterpreterState *interpreter_state) {
|
|||
}
|
||||
|
||||
Boolean builtin_while(InterpreterState *interpreter_state) {
|
||||
(void)interpreter_state;
|
||||
return FALSE;
|
||||
if (interpreter_state->stack == NULL) return FALSE;
|
||||
StackItem *while_block = interpreter_state->stack;
|
||||
if (while_block->next == NULL) return FALSE;
|
||||
StackItem *conditional_block = while_block->next;
|
||||
|
||||
if (!(
|
||||
while_block->type == STACK_TOKEN_STRING ||
|
||||
while_block->type == STACK_CALLABLE ||
|
||||
while_block->type == STACK_IDENTIFIER
|
||||
)) return FALSE;
|
||||
|
||||
if (!(
|
||||
conditional_block->type == STACK_TOKEN_STRING ||
|
||||
conditional_block->type == STACK_CALLABLE ||
|
||||
conditional_block->type == STACK_IDENTIFIER
|
||||
)) return FALSE;
|
||||
|
||||
interpreter_state->stack = conditional_block->next;
|
||||
conditional_block->next = NULL;
|
||||
|
||||
Boolean return_value = FALSE;
|
||||
while (TRUE) {
|
||||
if (conditional_block->type == STACK_TOKEN_STRING || conditional_block->type == STACK_CALLABLE)
|
||||
return_value = execute_token_string(interpreter_state, conditional_block->token_string);
|
||||
else if (conditional_block->type == STACK_IDENTIFIER)
|
||||
return_value = execute_func(interpreter_state, conditional_block->identifier.name);
|
||||
if (!return_value) break;
|
||||
if (interpreter_state->stack == NULL) return FALSE;
|
||||
|
||||
StackItem *conditional = interpreter_state->stack;
|
||||
interpreter_state->stack = conditional->next;
|
||||
conditional->next = NULL;
|
||||
TRUTHY;
|
||||
clean_stack(conditional);
|
||||
if (!value) break;
|
||||
|
||||
if (while_block->type == STACK_TOKEN_STRING || while_block->type == STACK_CALLABLE)
|
||||
return_value = execute_token_string(interpreter_state, while_block->token_string);
|
||||
else if (while_block->type == STACK_IDENTIFIER)
|
||||
return_value = execute_func(interpreter_state, while_block->identifier.name);
|
||||
if (!return_value) break;
|
||||
}
|
||||
|
||||
clean_stack(while_block);
|
||||
return return_value;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -186,13 +186,15 @@ Boolean execute_func(InterpreterState *interpreter_state, SlsStr key) {
|
|||
}
|
||||
|
||||
Boolean execute_token_string(InterpreterState *interpreter_state, TokenString token_string) {
|
||||
Boolean return_value = FALSE;
|
||||
for (size_t i = 0; i < token_string.length; i++) {
|
||||
if (token_string.tokens[i].type == TOKEN_IDENTIFIER && !token_string.tokens[i].identifier.is_literal)
|
||||
return execute_func(interpreter_state, token_string.tokens[i].identifier.name);
|
||||
return_value = execute_func(interpreter_state, token_string.tokens[i].identifier.name);
|
||||
else
|
||||
return push_token(interpreter_state, token_string.tokens[i]);
|
||||
return_value = push_token(interpreter_state, token_string.tokens[i]);
|
||||
if (!return_value) break;
|
||||
}
|
||||
return FALSE;
|
||||
return return_value;
|
||||
}
|
||||
|
||||
Boolean execute(InterpreterState *interpreter_state, LexerTokenResult *token) {
|
||||
|
|
|
|||
Loading…
Reference in New Issue