Skip to content

Commit 339007c

Browse files
committed
multi dimensional arrays WIP
1 parent 985e7bf commit 339007c

File tree

4 files changed

+38
-14
lines changed

4 files changed

+38
-14
lines changed

include/ps_error.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ extern "C"
7575
PS_ERROR_OUT_OF_RANGE,
7676
PS_ERROR_INVALID_PARAMETERS,
7777
PS_ERROR_TOO_MANY_VARIABLES,
78+
PS_ERROR_TOO_MANY_DIMENSIONS,
7879
PS_ERROR_PARAMETER_COUNT_MISMATCH,
7980
PS_ERROR_MATH_NAN_INF,
8081
// ...

include/ps_type_definition.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,8 +107,9 @@ extern "C"
107107
/** @brief *FUTURE* => index goes from range->value->g.min to range->value->g.max */
108108
typedef struct s_ps_type_definition_array
109109
{
110-
ps_symbol *subrange; /** @brief index range as subrange */
111-
ps_symbol *item_type; /** @brief type of elements, may be another array definition */
110+
ps_symbol **subranges; /** @brief index ranges as subrange */
111+
ps_symbol *item_type; /** @brief type of elements, may be another array definition (?) */
112+
int dimensions;/** @brief dimensions count */
112113
} __attribute__((__packed__)) ps_type_definition_array;
113114

114115
typedef struct s_ps_type_definition_record_field

src/ps_error.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ static const ps_error_message ps_error_messages[] = {
7575
{PS_ERROR_OUT_OF_RANGE, "Out of range"},
7676
{PS_ERROR_INVALID_PARAMETERS, "Invalid parameters"},
7777
{PS_ERROR_TOO_MANY_VARIABLES, "Too many variables in declaration"},
78+
{PS_ERROR_TOO_MANY_DIMENSIONS, "Too many dimensions in array definition"},
7879
{PS_ERROR_PARAMETER_COUNT_MISMATCH, "Parameter count mismatch"},
7980
{PS_ERROR_MATH_NAN_INF, "Math result is NaN or Infinity"},
8081
{PS_ERROR_MAX, "MAX error?"},

src/ps_visit_type.c

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -598,29 +598,43 @@ bool ps_visit_type_reference_subrange(ps_interpreter *interpreter, ps_interprete
598598
bool ps_visit_type_reference_array(ps_interpreter *interpreter, ps_interpreter_mode mode, ps_symbol **type_symbol,
599599
const char *type_name)
600600
{
601-
// interpreter->debug = DEBUG_VERBOSE;
602601
VISIT_BEGIN("TYPE_REFERENCE_ARRAY", "");
603602

604-
ps_symbol *subrange = NULL;
603+
ps_symbol *subranges[8] = {0};
604+
int dimensions = 0;
605605
ps_symbol *item_type = NULL;
606+
ps_symbol *subrange = NULL;
606607

607-
// ARRAY
608+
// 'ARRAY'
608609
if (lexer->current_token.type != PS_TOKEN_ARRAY)
609610
RETURN_ERROR(PS_ERROR_UNEXPECTED_TOKEN)
610611
READ_NEXT_TOKEN
611612
// '['
612613
if (lexer->current_token.type != PS_TOKEN_LEFT_BRACKET)
613614
RETURN_ERROR(PS_ERROR_UNEXPECTED_TOKEN)
614615
READ_NEXT_TOKEN
615-
// SUBRANGE (LOW '..' HIGH)
616-
if (!ps_visit_type_reference(interpreter, mode, &subrange, NULL))
617-
TRACE_ERROR("DIMENSION")
618-
// Dimension *must* be a subrange
619-
if (subrange->kind != PS_SYMBOL_KIND_TYPE_DEFINITION || subrange->value->data.t->type != PS_TYPE_SUBRANGE)
620-
RETURN_ERROR(PS_ERROR_EXPECTED_SUBRANGE)
621-
// ']'
622-
if (lexer->current_token.type != PS_TOKEN_RIGHT_BRACKET)
616+
do
617+
{
618+
// SUBRANGE (LOW '..' HIGH)
619+
if (!ps_visit_type_reference(interpreter, mode, &subrange, NULL))
620+
TRACE_ERROR("DIMENSION")
621+
// Dimension *must* be a subrange
622+
if (subrange->kind != PS_SYMBOL_KIND_TYPE_DEFINITION || subrange->value->data.t->type != PS_TYPE_SUBRANGE)
623+
RETURN_ERROR(PS_ERROR_EXPECTED_SUBRANGE)
624+
subranges[dimensions] = subrange;
625+
dimensions += 1;
626+
// ',' ?
627+
if (lexer->current_token.type == PS_TOKEN_COMMA)
628+
{
629+
if (dimensions == 8)
630+
RETURN_ERROR(PS_ERROR_TOO_MANY_DIMENSIONS)
631+
continue;
632+
}
633+
// ']'
634+
if (lexer->current_token.type == PS_TOKEN_RIGHT_BRACKET)
635+
break;
623636
RETURN_ERROR(PS_ERROR_UNEXPECTED_TOKEN)
637+
} while (true);
624638
READ_NEXT_TOKEN
625639
// 'OF'
626640
if (lexer->current_token.type != PS_TOKEN_OF)
@@ -641,8 +655,15 @@ bool ps_visit_type_reference_array(ps_interpreter *interpreter, ps_interpreter_m
641655
type_def = ps_type_definition_alloc(PS_TYPE_ARRAY, PS_TYPE_ARRAY);
642656
if (type_def == NULL)
643657
RETURN_ERROR(PS_ERROR_OUT_OF_MEMORY)
644-
type_def->def.a.subrange = subrange;
658+
type_def->def.a.subranges = ps_memory_calloc(PS_MEMORY_TYPE, dimensions, sizeof(ps_symbol *));
659+
if (type_def->def.a.subranges == NULL)
660+
{
661+
ps_type_definition_free(type_def);
662+
RETURN_ERROR(PS_ERROR_OUT_OF_MEMORY);
663+
}
664+
memccpy(type_def->def.a.subranges, subranges, dimensions * sizeof(ps_symbol *));
645665
type_def->def.a.item_type = item_type;
666+
type_def->def.a.dimensions = dimensions;
646667
// Register new type definition in symbol table
647668
if (!ps_type_definition_register(interpreter, mode, name, type_def, type_symbol))
648669
RETURN_ERROR(interpreter->error)

0 commit comments

Comments
 (0)