Skip to content

Commit

Permalink
Overload . to do implicit self calls to the frame
Browse files Browse the repository at this point in the history
  • Loading branch information
doublec committed Jul 30, 2009
1 parent d42d427 commit 01ef261
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
17 changes: 17 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,23 @@ There is an 'object' primitive that returns the prototypical object. The
object ( -- object )
copy ( object -- object' )

The get and unquote primitives have been overloaded to work with the object
system. If ';' is called on a symbol, and that symbol is not found in the
environment, then it will be looked up in an object on the stack using
'lookup':

object copy 25 foo add-data-slot foo;.
=> 25

If '.' is called on a symbol, and that symbol is not found in the
environment, then it will be looked up in the frame object using
'lookup'. This makes '.' similar to Self's implicit self sends:

object copy 25 foo add-data-slot
[ foo. println ] print-foo add-method-slot
foo;.
=> 25

Troubleshooting
===============
Error handling started of as being C++ assertions so a lot of
Expand Down
19 changes: 16 additions & 3 deletions cf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1227,9 +1227,22 @@ static void primitive_unquote(XY* xy) {
primitive_unquote(xy);
}
else {
// If the symbol doesn't exist in the environment, just
// return the symbol itself.
xy->mX.push_back(symbol);
// If the symbol doesn't exist in the environment, look
// it up in the current frame.
assert(xy->mFrame);
set<XYObject*> circular;
XYSlot* slot = xy->mFrame->lookup(symbol->mValue, circular, 0);
if (slot) {
xy_assert(slot->mMethod, XYError::INVALID_SLOT_TYPE);
xy->mX.push_back(xy->mFrame);
xy->mX.push_back(slot->mMethod);
primitive_unquote(xy);
}
else {
// Return the symbol itself if not in the environment
// or the frame.
xy->mX.push_back(symbol);
}
}
}
else
Expand Down
10 changes: 5 additions & 5 deletions factorial.lcf
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ Compare overhead of object factorial computation vs non-object version
object copy
20000 n add-data-slot
[
frame n;. 1 = [
frame self;.
n. 1 = [
self.
] [
frame n;.
dup. 1 - frame self;. copy n:;.
factorial;. n;. * frame self;. copy n:;.
n.
dup. 1 - self. copy n:;.
factorial;. n;. * self. copy n:;.
] if
] factorial add-method-slot

Expand Down

0 comments on commit 01ef261

Please sign in to comment.