Skip to content

Commit 45c4b70

Browse files
Fix an integer width error in compare_objects which was breaking tables
1 parent 4e9ac62 commit 45c4b70

File tree

3 files changed

+15
-10
lines changed

3 files changed

+15
-10
lines changed

src/runtime.h

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -910,17 +910,23 @@ static ptrdiff_t compare_numbers(NUMBER n1, NUMBER n2)
910910

911911
static ptrdiff_t compare_objects(ANY ob1, ANY ob2)
912912
{
913-
if (ob1.type != ob2.type) return ob1.type - ob2.type;
914-
switch (ob1.type)
913+
if (ob1.type != ob2.type) return (ptrdiff_t)ob1.type - (ptrdiff_t)ob2.type;
914+
else switch (ob1.type)
915915
{
916916
case number: return compare_numbers(unbox_NUMBER(ob1), unbox_NUMBER(ob2));
917-
case boolean: return unbox_BOOLEAN(ob1) - unbox_BOOLEAN(ob2);
918-
case string: return strcmp(unbox_STRING(ob1), unbox_STRING(ob2));
917+
case boolean: return (ptrdiff_t)unbox_BOOLEAN(ob1) - (ptrdiff_t)unbox_BOOLEAN(ob2);
918+
case string: return (ptrdiff_t)strcmp(unbox_STRING(ob1), unbox_STRING(ob2));
919919
case symbol: return unbox_SYMBOL(ob1) - unbox_SYMBOL(ob2);
920920
case list: return compare_lists(unbox_LIST(ob1), unbox_LIST(ob2));
921921
case block: return compare_blocks(unbox_BLOCK(ob1), unbox_BLOCK(ob2));
922922
case box: return (char*)unbox_BOX(ob1) - (char*)unbox_BOX(ob2);
923923
default: return 0; // really shouldn't happen
924+
/* NOTE
925+
* The garbage collector *will* reorder objects in memory,
926+
* which means that the relative addresses of blocks and boxes
927+
* *will* change - which is why these can't be used to index tables
928+
* for example.
929+
*/
924930
}
925931
}
926932

@@ -2133,7 +2139,7 @@ static TABLE ___table (BLOCK expr)
21332139
stack.start = stack.top;
21342140
// Eval expr
21352141
call_block(expr);
2136-
// Move to a list.
2142+
// Move to a table.
21372143
TABLE d = NULL;
21382144
size_t len = stack_length();
21392145
if unlikely(len & 1) throw_error("Table initialiser must be key-value pairs");
@@ -2179,7 +2185,7 @@ static TABLE ___insert(ANY key, ANY value, TABLE d)
21792185
D->right = d->right;
21802186
D->left = ___insert(key, value, d->left);
21812187
}
2182-
else if (diff > 0)
2188+
else //if (diff > 0)
21832189
{
21842190
D->key = d->key;
21852191
D->value = d->value;

tests/prime2.cog

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Def Primes (
1010
)
1111
);
1212

13-
Let P be Primes up to 1000;
13+
Let P be Primes up to 100;
1414

1515
Print If And Not . 50 P . 37 P
1616
"PASS: Prime numbers using a table"

tests/table.cog

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ Let T be Table (
22
\foo is "bar"
33
"bar" is \foo;
44
12 is 13;
5+
\key is \value;
56
);
67

78
Print If == "bar" . \foo T
89
"PASS: Table lookup 1"
910
else
1011
"FAIL: Table lookup 1";
1112

12-
Print T;
13-
1413
Print If == \foo . "bar" T
1514
"PASS: Table lookup 2"
1615
else
@@ -28,7 +27,7 @@ Print If == "baz" . \foo T2
2827
else
2928
"FAIL: Table insertion";
3029

31-
Print If == "{ foo:\"bar\" \"foo\":bar }" Show Table ( \foo "bar" and "foo" \bar )
30+
Print If == "{ \"foo\":bar foo:\"bar\" }" Show Table ( \foo "bar" and "foo" \bar )
3231
"PASS: Printing a table"
3332
else
3433
"FAIL: Printing a table";

0 commit comments

Comments
 (0)