Writing a Scheme interpreter with FPC: Recursive data structures

对着背影说爱祢 提交于 2019-12-03 17:33:53

The MakePair function is defined like this:

function MakePair(car, cdr: PScmObject): TScmObject;

Note that it receives two pointers of type PScmObject. You then call it like this:

MakePair(Test1, Test2);

But Test1 and Test2 are of type TScmObject. So the actual parameters passed are not compatible, just as the compiler says.

You need to pass pointers to these records instead:

MakePair(@Test1, @Test2);

In the longer term you are going to need to be careful about the lifetime of these records. You'll need to allocate on the heap and without garbage collection I suspect that you'll enter a world of pain trying to keep track of who owns the records. Perhaps you could consider using interface reference counting to manage lifetime.

The procedure is expecting a pointer to the record, and not the record itself.

You can use the @ (at) operator, at the call point, to create a pointer on the fly to the record, and thus satisfy the compiler type check:

begin
   Test1 := MakeFixnum(7);
   writeln('Test1, Tag: ', Test1.ScmObjectTag,
           ', Content: ', Test1.ScmObjectFixnum);
   Test2 := MakeFixnum(9);
   writeln('Test2, Tag: ', Test2.ScmObjectTag,
           ', Content: ', Test2.ScmObjectFixnum);
   Test3 := MakePair(@Test1, @Test2);
end.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!