implemented for
This commit is contained in:
parent
749d5b4185
commit
122e066ca7
|
|
@ -55,6 +55,7 @@ Boolean builtin_floor(InterpreterState *interpreter_state);
|
||||||
// Boolean builtin_fn(InterpreterState *interpreter_state);
|
// Boolean builtin_fn(InterpreterState *interpreter_state);
|
||||||
// Boolean builtin_foldl(InterpreterState *interpreter_state);
|
// Boolean builtin_foldl(InterpreterState *interpreter_state);
|
||||||
// Boolean builtin_foldr(InterpreterState *interpreter_state);
|
// Boolean builtin_foldr(InterpreterState *interpreter_state);
|
||||||
|
Boolean builtin_for(InterpreterState *interpreter_state);
|
||||||
// Boolean builtin_format(InterpreterState *interpreter_state);
|
// Boolean builtin_format(InterpreterState *interpreter_state);
|
||||||
// Boolean builtin_get(InterpreterState *interpreter_state);
|
// Boolean builtin_get(InterpreterState *interpreter_state);
|
||||||
Boolean builtin_if(InterpreterState *interpreter_state);
|
Boolean builtin_if(InterpreterState *interpreter_state);
|
||||||
|
|
@ -298,6 +299,12 @@ Boolean load_builtins(InterpreterState *interpreter_state) {
|
||||||
if (!hash_table_put_funcs(interpreter_state->functions, SLS_STR("floor"), func))
|
if (!hash_table_put_funcs(interpreter_state->functions, SLS_STR("floor"), func))
|
||||||
return FALSE;
|
return FALSE;
|
||||||
|
|
||||||
|
func = (FunctionItem *)malloc(sizeof(FunctionItem));
|
||||||
|
if (func == NULL) return FALSE;
|
||||||
|
*func = (FunctionItem){ .type = FUNCTION_BUILTIN, .builtin = *builtin_for};
|
||||||
|
if (!hash_table_put_funcs(interpreter_state->functions, SLS_STR("for"), func))
|
||||||
|
return FALSE;
|
||||||
|
|
||||||
func = (FunctionItem *)malloc(sizeof(FunctionItem));
|
func = (FunctionItem *)malloc(sizeof(FunctionItem));
|
||||||
if (func == NULL) return FALSE;
|
if (func == NULL) return FALSE;
|
||||||
*func = (FunctionItem){ .type = FUNCTION_BUILTIN, .builtin = *builtin_if};
|
*func = (FunctionItem){ .type = FUNCTION_BUILTIN, .builtin = *builtin_if};
|
||||||
|
|
@ -419,7 +426,6 @@ Boolean load_builtins(InterpreterState *interpreter_state) {
|
||||||
"// SLS Defined Builtin Operators\n"
|
"// SLS Defined Builtin Operators\n"
|
||||||
"// November 2025\n"
|
"// November 2025\n"
|
||||||
"\n"
|
"\n"
|
||||||
"// { } lambda ::for const\n"
|
|
||||||
"{ ln swap ln swap / } lambda ::logb const\n"
|
"{ ln swap ln swap / } lambda ::logb const\n"
|
||||||
"{ 1 pick 1 pick < { swap drop } { drop } if } lambda ::max const\n"
|
"{ 1 pick 1 pick < { swap drop } { drop } if } lambda ::max const\n"
|
||||||
"{ 1 pick 1 pick < { drop } { swap drop } if } lambda ::min const\n"
|
"{ 1 pick 1 pick < { drop } { swap drop } if } lambda ::min const\n"
|
||||||
|
|
@ -815,6 +821,53 @@ Boolean load_builtins(InterpreterState *interpreter_state) {
|
||||||
break; \
|
break; \
|
||||||
} \
|
} \
|
||||||
|
|
||||||
|
#define SCALAR(item, value) \
|
||||||
|
switch (item->type) { \
|
||||||
|
case STACK_IDENTIFIER: \
|
||||||
|
return FALSE; \
|
||||||
|
case STACK_I64: \
|
||||||
|
if (item->i64 < 0) return FALSE; \
|
||||||
|
value = item->i64; \
|
||||||
|
break; \
|
||||||
|
case STACK_I32: \
|
||||||
|
if (item->i32 < 0) return FALSE; \
|
||||||
|
value = item->i32; \
|
||||||
|
break; \
|
||||||
|
case STACK_I16: \
|
||||||
|
if (item->i16 < 0) return FALSE; \
|
||||||
|
value = item->i16; \
|
||||||
|
break; \
|
||||||
|
case STACK_I8: \
|
||||||
|
if (item->i8 < 0) return FALSE; \
|
||||||
|
value = item->i8; \
|
||||||
|
break; \
|
||||||
|
case STACK_U64: \
|
||||||
|
value = item->u64; \
|
||||||
|
break; \
|
||||||
|
case STACK_U32: \
|
||||||
|
value = item->u32; \
|
||||||
|
break; \
|
||||||
|
case STACK_U16: \
|
||||||
|
value = item->u16; \
|
||||||
|
break; \
|
||||||
|
case STACK_U8: \
|
||||||
|
value = item->u8; \
|
||||||
|
break; \
|
||||||
|
case STACK_FLOAT: \
|
||||||
|
return FALSE; \
|
||||||
|
case STACK_DOUBLE: \
|
||||||
|
return FALSE; \
|
||||||
|
case STACK_CHARACTER: \
|
||||||
|
value = item->character; \
|
||||||
|
break; \
|
||||||
|
case STACK_BOOLEAN: \
|
||||||
|
value = item->boolean; \
|
||||||
|
break; \
|
||||||
|
case STACK_TOKEN_STRING: \
|
||||||
|
case STACK_CALLABLE: \
|
||||||
|
return FALSE; \
|
||||||
|
} \
|
||||||
|
|
||||||
Boolean builtin_addition(InterpreterState *interpreter_state) {
|
Boolean builtin_addition(InterpreterState *interpreter_state) {
|
||||||
NUMERIC_TYPES;
|
NUMERIC_TYPES;
|
||||||
ARITHMETIC(+);
|
ARITHMETIC(+);
|
||||||
|
|
@ -1615,6 +1668,50 @@ Boolean builtin_floor(InterpreterState *interpreter_state) {
|
||||||
FLOAT_FUNCTION(floor);
|
FLOAT_FUNCTION(floor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Boolean builtin_for(InterpreterState *interpreter_state) {
|
||||||
|
if (interpreter_state->stack == NULL) return FALSE;
|
||||||
|
StackItem *for_block = interpreter_state->stack;
|
||||||
|
if (for_block->next == NULL) return FALSE;
|
||||||
|
StackItem *end_item = for_block->next;
|
||||||
|
if (end_item->next == NULL) return FALSE;
|
||||||
|
StackItem *start_item = end_item->next;
|
||||||
|
|
||||||
|
if (!(
|
||||||
|
for_block->type == STACK_TOKEN_STRING ||
|
||||||
|
for_block->type == STACK_CALLABLE ||
|
||||||
|
for_block->type == STACK_IDENTIFIER
|
||||||
|
)) return FALSE;
|
||||||
|
|
||||||
|
interpreter_state->stack = start_item->next;
|
||||||
|
start_item->next = NULL;
|
||||||
|
|
||||||
|
size_t start_value;
|
||||||
|
SCALAR(start_item, start_value);
|
||||||
|
size_t end_value;
|
||||||
|
SCALAR(end_item, end_value);
|
||||||
|
|
||||||
|
Boolean return_value = FALSE;
|
||||||
|
for (size_t i = start_value; i < end_value; i++) {
|
||||||
|
return_value = push_token(interpreter_state, (Token){
|
||||||
|
.type = TOKEN_INTEGER,
|
||||||
|
.integer_literal = (IntegerLiteral){
|
||||||
|
.type = INTEGER_I64,
|
||||||
|
.value = (uint64_t)i,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
if (!return_value) break;
|
||||||
|
|
||||||
|
if (for_block->type == STACK_TOKEN_STRING || for_block->type == STACK_CALLABLE)
|
||||||
|
return_value = execute_token_string(interpreter_state, for_block->token_string);
|
||||||
|
else if (for_block->type == STACK_IDENTIFIER)
|
||||||
|
return_value = execute_func(interpreter_state, for_block->identifier.name);
|
||||||
|
if (!return_value) break;
|
||||||
|
}
|
||||||
|
|
||||||
|
clean_stack(for_block);
|
||||||
|
return return_value;
|
||||||
|
}
|
||||||
|
|
||||||
Boolean builtin_if(InterpreterState *interpreter_state) {
|
Boolean builtin_if(InterpreterState *interpreter_state) {
|
||||||
if (interpreter_state->stack == NULL) return FALSE;
|
if (interpreter_state->stack == NULL) return FALSE;
|
||||||
StackItem *else_block = interpreter_state->stack;
|
StackItem *else_block = interpreter_state->stack;
|
||||||
|
|
@ -1809,98 +1906,10 @@ Boolean builtin_roll(InterpreterState *interpreter_state) {
|
||||||
StackItem *count_item = times_item->next;
|
StackItem *count_item = times_item->next;
|
||||||
|
|
||||||
size_t times;
|
size_t times;
|
||||||
switch (times_item->type) {
|
SCALAR(times_item, times);
|
||||||
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;
|
size_t count;
|
||||||
switch (count_item->type) {
|
SCALAR(count_item, count);
|
||||||
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;
|
StackItem *head = NULL, *tail = NULL, *prev = NULL;
|
||||||
if (count > 0) {
|
if (count > 0) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue