implemented type_of

This commit is contained in:
Kyler Olsen 2025-11-30 21:11:55 -07:00
parent ae3483c612
commit 50f90dcf84
4 changed files with 44 additions and 18 deletions

View File

@ -27,10 +27,11 @@ typedef enum {
STACK_CHARACTER,
STACK_BOOLEAN,
STACK_TOKEN_STRING,
STACK_CODE_BLOCK,
STACK_CALLABLE,
} StackType;
extern const char *STACK_TYPES_NAMES[];
extern const char *STACK_TYPES_TYPES[];
extern const size_t STACK_TYPE_COUNT;
typedef struct StackItem {

View File

@ -508,7 +508,7 @@ Boolean load_builtins(InterpreterState *interpreter_state) {
af = interpreter_state->stack->boolean; \
break; \
case STACK_TOKEN_STRING: \
case STACK_CODE_BLOCK: \
case STACK_CALLABLE: \
return FALSE; \
} \
@ -585,7 +585,7 @@ Boolean load_builtins(InterpreterState *interpreter_state) {
af = interpreter_state->stack->boolean; \
break; \
case STACK_TOKEN_STRING: \
case STACK_CODE_BLOCK: \
case STACK_CALLABLE: \
return FALSE; \
} \
\
@ -655,7 +655,7 @@ Boolean load_builtins(InterpreterState *interpreter_state) {
bf = interpreter_state->stack->next->boolean; \
break; \
case STACK_TOKEN_STRING: \
case STACK_CODE_BLOCK: \
case STACK_CALLABLE: \
return FALSE; \
} \
@ -1490,7 +1490,7 @@ Boolean builtin_dup(InterpreterState *interpreter_state) {
item->boolean = interpreter_state->stack->boolean;
break;
case STACK_TOKEN_STRING:
case STACK_CODE_BLOCK:
case STACK_CALLABLE:
item->token_string = copy_token_string(interpreter_state->stack->token_string);
break;
}
@ -1507,7 +1507,7 @@ Boolean builtin_eval(InterpreterState *interpreter_state) {
code_target->next = NULL;
Boolean return_value = FALSE;
if (code_target->type == STACK_TOKEN_STRING || code_target->type == STACK_CODE_BLOCK)
if (code_target->type == STACK_TOKEN_STRING || code_target->type == STACK_CALLABLE)
return_value = execute_token_string(interpreter_state, code_target->token_string);
else if (code_target->type == STACK_IDENTIFIER)
return_value = execute_func(interpreter_state, code_target->identifier.name);
@ -1536,13 +1536,13 @@ Boolean builtin_if(InterpreterState *interpreter_state) {
if (!(
else_block->type == STACK_TOKEN_STRING ||
else_block->type == STACK_CODE_BLOCK ||
else_block->type == STACK_CALLABLE ||
else_block->type == STACK_IDENTIFIER
)) return FALSE;
if (!(
then_block->type == STACK_TOKEN_STRING ||
then_block->type == STACK_CODE_BLOCK ||
then_block->type == STACK_CALLABLE ||
then_block->type == STACK_IDENTIFIER
)) return FALSE;
@ -1591,19 +1591,19 @@ Boolean builtin_if(InterpreterState *interpreter_state) {
value = conditional->boolean;
break;
case STACK_TOKEN_STRING:
case STACK_CODE_BLOCK:
case STACK_CALLABLE:
value = (conditional->token_string.length && conditional->token_string.tokens[0].type != TOKEN_EOF) ? TRUE : FALSE;
break;
}
Boolean return_value = FALSE;
if (value) {
if (then_block->type == STACK_TOKEN_STRING || then_block->type == STACK_CODE_BLOCK)
if (then_block->type == STACK_TOKEN_STRING || then_block->type == STACK_CALLABLE)
return_value = execute_token_string(interpreter_state, then_block->token_string);
else if (then_block->type == STACK_IDENTIFIER)
return_value = execute_func(interpreter_state, then_block->identifier.name);
} else {
if (else_block->type == STACK_TOKEN_STRING || else_block->type == STACK_CODE_BLOCK)
if (else_block->type == STACK_TOKEN_STRING || else_block->type == STACK_CALLABLE)
return_value = execute_token_string(interpreter_state, else_block->token_string);
else if (else_block->type == STACK_IDENTIFIER)
return_value = execute_func(interpreter_state, else_block->identifier.name);
@ -1616,7 +1616,7 @@ Boolean builtin_if(InterpreterState *interpreter_state) {
Boolean builtin_lambda(InterpreterState *interpreter_state) {
if (interpreter_state->stack == NULL) return FALSE;
if (interpreter_state->stack->type != STACK_TOKEN_STRING) return FALSE;
interpreter_state->stack->type = STACK_CODE_BLOCK;
interpreter_state->stack->type = STACK_CALLABLE;
return TRUE;
}
@ -1756,7 +1756,7 @@ Boolean builtin_pick(InterpreterState *interpreter_state) {
item->boolean = picked->boolean;
break;
case STACK_TOKEN_STRING:
case STACK_CODE_BLOCK:
case STACK_CALLABLE:
item->token_string = copy_token_string(picked->token_string);
break;
}
@ -1995,8 +1995,14 @@ Boolean builtin_tan(InterpreterState *interpreter_state) {
}
Boolean builtin_type_of(InterpreterState *interpreter_state) {
(void)interpreter_state;
return FALSE;
if (interpreter_state->stack == NULL) return FALSE;
return push_token(interpreter_state, (Token){
.type = TOKEN_IDENTIFIER,
.identifier = (Identifier){
.is_literal = TRUE,
.name = sls_str_malloc(STACK_TYPES_TYPES[interpreter_state->stack->type], TYPE_NAMES_SAFE_LENGTH),
},
});
}
Boolean builtin_while(InterpreterState *interpreter_state) {

View File

@ -28,6 +28,25 @@ const char *STACK_TYPES_NAMES[] = {
"Character",
"Boolean",
"Token String",
"Callable",
};
const char *STACK_TYPES_TYPES[] = {
"Identifier",
"i64",
"i32",
"i16",
"i8",
"u64",
"u32",
"u16",
"u8",
"f32",
"f64",
"char",
"bool",
"TokenString",
"Callable",
};
const size_t STACK_TYPE_COUNT = sizeof(STACK_TYPES_NAMES) / sizeof(*STACK_TYPES_NAMES);
@ -146,7 +165,7 @@ Boolean push_token(InterpreterState *interpreter_state, Token token) {
item->boolean = token.boolean_literal;
break;
case STACK_TOKEN_STRING:
case STACK_CODE_BLOCK:
case STACK_CALLABLE:
item->token_string = copy_token_string(token.token_string);
break;
}

View File

@ -65,8 +65,8 @@ void print_top_of_stack(InterpreterState *interpreter_state) {
case STACK_TOKEN_STRING:
printf("#0: <TOKEN STRING>\n");
break;
case STACK_CODE_BLOCK:
printf("#0: <CODE BLOCK>\n");
case STACK_CALLABLE:
printf("#0: <CALLABLE>\n");
break;
}
}