Skip to content

Commit 4da3130

Browse files
committed
array access & allocation WIP
1 parent b25d936 commit 4da3130

1 file changed

Lines changed: 74 additions & 3 deletions

File tree

src/ps_visit_expression.c

Lines changed: 74 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,12 @@ bool ps_visit_relational_expression(ps_interpreter *interpreter, ps_interpreter_
134134

135135
static ps_token_type relational_operators[] = {
136136
// < <= > >= = <>
137-
PS_TOKEN_LT, PS_TOKEN_LE, PS_TOKEN_GT, PS_TOKEN_GE, PS_TOKEN_EQ, PS_TOKEN_NE,
137+
PS_TOKEN_LT,
138+
PS_TOKEN_LE,
139+
PS_TOKEN_GT,
140+
PS_TOKEN_GE,
141+
PS_TOKEN_EQ,
142+
PS_TOKEN_NE,
138143
};
139144
ps_value left = {.type = &ps_system_none, .data.v = NULL};
140145
ps_value right = {.type = &ps_system_none, .data.v = NULL};
@@ -395,8 +400,74 @@ bool ps_visit_factor(ps_interpreter *interpreter, ps_interpreter_mode mode, ps_v
395400
// *** Identifier: variable, constant, function ***
396401
case PS_TOKEN_IDENTIFIER:
397402
COPY_IDENTIFIER(identifier)
398-
if (!ps_visit_factor_identifier(interpreter, mode, identifier, result))
399-
TRACE_ERROR("IDENTIFIER")
403+
symbol = ps_interpreter_find_symbol(interpreter, identifier, false);
404+
if (symbol == NULL)
405+
RETURN_ERROR(PS_ERROR_SYMBOL_NOT_FOUND);
406+
switch (symbol->kind)
407+
{
408+
case PS_SYMBOL_KIND_AUTO:
409+
case PS_SYMBOL_KIND_CONSTANT:
410+
case PS_SYMBOL_KIND_VARIABLE:
411+
if (ps_value_is_array(symbol->value))
412+
{
413+
// interpreter->debug = DEBUG_VERBOSE;
414+
// if (interpreter->debug >= DEBUG_VERBOSE)
415+
fprintf(stderr, " INFO\tFACTOR: identifier '%s' is a '%s' of type '%s'\n", symbol->name,
416+
ps_symbol_get_kind_name(symbol->kind),
417+
ps_type_definition_get_name(symbol->value->type->value->data.t));
418+
ps_type_definition *type_def = ps_array_get_type_def(symbol);
419+
if (type_def == NULL)
420+
RETURN_ERROR(PS_ERROR_TYPE_MISMATCH)
421+
READ_NEXT_TOKEN
422+
if (lexer->current_token.type == PS_TOKEN_LEFT_BRACKET)
423+
{
424+
ps_value_type base = type_def->def.a.subrange->value->data.t->base;
425+
ps_symbol *index_type = base == PS_TYPE_CHAR ? &ps_system_char : base == PS_TYPE_UNSIGNED ? &ps_system_unsigned
426+
: base == PS_TYPE_INTEGER ? &ps_system_integer
427+
: NULL; // PS_TYPE_ENUM
428+
ps_value index = {.type = index_type, .allocated = false, .data.v = NULL};
429+
// clang-format off
430+
ps_symbol_debug (stderr, "ARRAY ", symbol);
431+
ps_value_debug (stderr, "INDEX1 ", &index);
432+
ps_symbol_debug (stderr, "ITEM_TYPE ", type_def->def.a.item_type);
433+
ps_symbol_debug (stderr, "SUBRANGE1 ", type_def->def.a.subrange);
434+
ps_type_definition_debug(stderr, "SUBRANGE2 ", type_def->def.a.subrange->value->data.t);
435+
// clang-format on
436+
READ_NEXT_TOKEN
437+
if (!ps_visit_expression(interpreter, mode, &index))
438+
{
439+
ps_interpreter_set_message(interpreter, "Index is invalid");
440+
TRACE_ERROR("INDEX")
441+
}
442+
ps_value_debug(stderr, "INDEX2 ", &index);
443+
EXPECT_TOKEN(PS_TOKEN_RIGHT_BRACKET)
444+
READ_NEXT_TOKEN
445+
if (mode == MODE_EXEC && !ps_array_get_value(symbol, &index, result))
446+
{
447+
ps_interpreter_set_message(interpreter, "Can't get array %s value for index %s",
448+
symbol->name,
449+
ps_value_get_debug_string(&index));
450+
RETURN_ERROR(PS_ERROR_TYPE_MISMATCH)
451+
}
452+
}
453+
}
454+
else
455+
{
456+
result->type = symbol->value->type;
457+
if (mode == MODE_EXEC && !ps_interpreter_copy_value(interpreter, symbol->value, result))
458+
TRACE_ERROR("COPY");
459+
READ_NEXT_TOKEN
460+
}
461+
break;
462+
case PS_SYMBOL_KIND_FUNCTION:
463+
// interpreter->debug = DEBUG_VERBOSE;
464+
// fprintf(stderr, " INFO\tFACTOR: identifier '%s' is a FUNCTION\n", symbol->name);
465+
if (!ps_visit_function_call(interpreter, mode, symbol, result))
466+
TRACE_ERROR("FUNCTION");
467+
break;
468+
default:
469+
RETURN_ERROR(PS_ERROR_UNEXPECTED_TOKEN)
470+
}
400471
break;
401472
// ***Literal values ***
402473
case PS_TOKEN_CHAR_VALUE:

0 commit comments

Comments
 (0)