Skip to content

Commit 402a2b3

Browse files
committed
fix int := enum as a type mismatch error
1 parent 6e9abc6 commit 402a2b3

3 files changed

Lines changed: 28 additions & 21 deletions

File tree

examples/450-enum.pas

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818

1919
Procedure TestLowHighPredSucc();
2020
Begin
21-
// should not work! (and does not with FPC) because enumeration values are not implicitly convertible to integer/unsigned
22-
I := Female;
23-
WriteLn('I = ', I, ' (SHOULD NOT WORK!');
21+
// does not work! (and does not with FPC) because enumeration values are not implicitly convertible to integer/unsigned
22+
// I := Female;
23+
// WriteLn('I = ', I, ' (SHOULD NOT WORK!');
2424
// should work! and does with FPC because Ord() is used to explicitly convert the enumeration value to an integer/unsigned
25+
I := Ord(Female);
26+
WriteLn('I = ', I, ' (=', 1, ')');
2527
I := Ord('A');
2628
WriteLn('I = ', I, ' (=', 65, ')');
2729
I := Ord(Succ('A'));

src/ps_array.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ ps_error ps_array_get_value(const ps_symbol *array_var, const ps_value *index, p
110110
ps_symbol_debug(stderr, "ps_array_get_value, array: ", array_var);
111111
ps_value_debug(stderr, "ps_array_get_value, index: ", index);
112112
}
113-
const ps_type_definition *type_def = // ps_array_get_type_def(array->value->type);
114-
array_var->value->type->value->data.t;
113+
const ps_type_definition *type_def = array_var->value->type->value->data.t;
115114
if (ps_array_debug)
116115
ps_type_definition_debug(stderr, "*** ps_array_get_value, type_def: ", type_def);
117116
ps_unsigned offset = ps_type_definition_get_subrange_offset(type_def->def.a.subrange->value->data.t, index);

src/ps_value.c

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,22 @@ ps_value *ps_value_free(ps_value *value)
3535
if (value == NULL || !value->allocated)
3636
return NULL;
3737
ps_value_type type = ps_value_get_type(value);
38-
switch(type)
38+
switch (type)
3939
{
40-
// case PS_TYPE_STRING:
41-
// ps_string_free(value->data.s);
42-
// break;
43-
case PS_TYPE_ARRAY:
44-
ps_array_free(value->data.a);
45-
break;
46-
case PS_TYPE_EXECUTABLE:
47-
ps_executable_free(value->data.x);
48-
break;
49-
case PS_TYPE_DEFINITION:
50-
ps_type_definition_free(value->data.t);
51-
break;
52-
default:
53-
break;
40+
// case PS_TYPE_STRING:
41+
// ps_string_free(value->data.s);
42+
// break;
43+
case PS_TYPE_ARRAY:
44+
ps_array_free(value->data.a);
45+
break;
46+
case PS_TYPE_EXECUTABLE:
47+
ps_executable_free(value->data.x);
48+
break;
49+
case PS_TYPE_DEFINITION:
50+
ps_type_definition_free(value->data.t);
51+
break;
52+
default:
53+
break;
5454
}
5555
ps_memory_free(PS_MEMORY_VALUE, value);
5656
return NULL;
@@ -155,12 +155,18 @@ ps_error ps_value_copy(const ps_value *from, ps_value *to, bool range_check)
155155
// If destination type is NONE, set it to source type
156156
if (to->type == &ps_system_none || to_base == PS_TYPE_NONE)
157157
to->type = from->type;
158-
// Same type, just copy value
158+
// Same value type, just copy value
159159
if (from->type == to->type)
160160
{
161+
// TODO copy array?
161162
to->data = from->data;
162163
return PS_ERROR_NONE;
163164
}
165+
// Enum can only be copied to same enum type
166+
if (ps_value_get_type(from) == PS_TYPE_ENUM)
167+
{
168+
return PS_ERROR_TYPE_MISMATCH;
169+
}
164170
// Char => Char? (subrange)
165171
if (from_base == PS_TYPE_CHAR && to_base == PS_TYPE_CHAR)
166172
{

0 commit comments

Comments
 (0)