Skip to content

Commit

Permalink
Merge branch 'master' into parser
Browse files Browse the repository at this point in the history
  • Loading branch information
somethingelseentirely committed Feb 3, 2012
2 parents c4a6ef0 + 3bd8039 commit 27cb440
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 10 deletions.
1 change: 1 addition & 0 deletions wee/CMakeLists.txt
Expand Up @@ -9,6 +9,7 @@ set(SRCS
"string.cpp"
"nil.cpp"
"object_space.cpp"
"block.cpp"
)

add_library(wee SHARED ${SRCS})
33 changes: 24 additions & 9 deletions wee/block.cpp
Expand Up @@ -28,32 +28,47 @@

namespace Acute
{
Block::Block(Message* body, ArgNames argNames, Object* ctx) : message(body), argumentNames(argNames), scope(ctx)
template<class T>
Block<T>::Block(Message* body, ArgNames argNames, Object* ctx) : message(body), argument_names(argNames), scope(ctx)
{
locals = new Object();
}

Object* Block::activate(Object* target, Object* locals, Message* body, Object* slot_context)
template<class T>
Object* Block<T>::activate(Object* target, Object* locals, Message* body, Object* slot_context)
{
Object* s = scope;
Object* ctx = new Object();
my_locals = new Object();

if(!s)
s = target;

for(int i = 0; i < argument_names.size(); i++)
{
Object* arg = message->object_at_arg(i);
ctx->add_slot(argument_names.at(i));
Object* arg = message->object_at_arg(i, locals);
my_locals->add_slot(argument_names.at(i));
}
my_locals->add_slot("self", s);

return message->perform_on(ctx, ctx);
return message->perform_on(my_locals, my_locals);
}

void Block::walk()
template<class T>
Object* Block<T>::call(void)
{
if(this->builtin)
{
Object* locals = new Object(); // TODO: Need a real locals object
return builtin(locals, new Message());
}
// TODO: call activate.
return nullptr;
}

template<class T>
void Block<T>::walk()
{
generic_object_walk();
collector->shade(locals);
collector->shade(my_locals);
if(scope)
collector->shade(scope);
collector->shade(message);
Expand Down
6 changes: 5 additions & 1 deletion wee/block.hpp
Expand Up @@ -33,20 +33,24 @@ namespace Acute
{
typedef std::vector<std::string> ArgNames;

template<class T = Object>
class Block : public Object
{
private:
Message* message;
Object* scope;
ArgNames argument_names;
Object* locals;
Object* my_locals;
typedef Object* (T::*builtin)(Object*, Message*);

public:
Block(Message*, ArgNames, Object*);
~Block();

Object* activate(Object*, Object*, Message*, Object*);

virtual Object* call(void);

virtual const std::string object_name();
virtual void walk();
};
Expand Down
1 change: 1 addition & 0 deletions wee/gc.hpp
Expand Up @@ -26,6 +26,7 @@

#include <vector>
#include <stdint.h>
#include <climits>
#include "address.hpp"

#define GC_MAX_FREE_OBJECTS 8192
Expand Down
1 change: 1 addition & 0 deletions wee/message.hpp
Expand Up @@ -43,6 +43,7 @@ namespace Acute
std::string get_name() { return name; }
std::vector<Message*> get_arguments() { return arguments; }

Message* message_at_arg(int n) { return arguments.at(n); }
Object* object_at_arg(int, Object*);

private:
Expand Down
57 changes: 57 additions & 0 deletions wee/object.cpp
Expand Up @@ -27,6 +27,9 @@
#include "object.hpp"
#include "mailbox.hpp"
#include "integer.hpp"
#include "string.hpp"
#include "boolean.hpp"
#include "block.hpp"

namespace Acute
{
Expand Down Expand Up @@ -181,4 +184,58 @@ namespace Acute
{
return "Object";
}

void Object::register_primitives()
{
// TODO: Implement.
}

Object* Object::clone(Object* locals, Message* m)
{
Object* obj = new Object();
Object* ctx = nullptr;
Block<Object>* func = dynamic_cast<Block<Object>*>(obj->lookup("init", ctx));
return func->call();
}

Object* Object::setSlot(Object* locals, Message* m)
{
String* str = dynamic_cast<String*>(m->object_at_arg(0, locals));
Object* val = m->object_at_arg(1, locals);
add_slot(str->stringValue(), val);
return this;
}

Object* Object::addTrait(Object* locals, Message* m)
{
Object* trait = m->object_at_arg(0, locals);
add_trait(trait);
return this;
}

Object* Object::doPrim(Object* locals, Message* m)
{
if(m->get_arguments().size() > 0)
{
Message* msg = m->message_at_arg(0);
msg->perform_on(this, this);
}
return this;
}

Object* Object::hasSlot(Object* locals, Message* m)
{
Boolean* b = nullptr;
String* name = dynamic_cast<String*>(m->object_at_arg(0, locals));
Object* tmp1 = nullptr;
Object* tmp2 = nullptr;
return dynamic_cast<Object*>(new Boolean(local_lookup(name->stringValue(), tmp1, tmp2)));
}

Object* Object::getSlot(Object* locals, Message* m)
{
String* str = dynamic_cast<String*>(m->object_at_arg(0, locals));
Object* slot_context = nullptr;
return lookup(str->stringValue(), slot_context);
}
}
13 changes: 13 additions & 0 deletions wee/object.hpp
Expand Up @@ -94,6 +94,19 @@ namespace Acute
// Our object name.
virtual const std::string object_name();

//
// Exposed primitives
//

virtual void register_primitives();

virtual Object* clone(Object*, Message*);
virtual Object* setSlot(Object*, Message*);
virtual Object* addTrait(Object*, Message*);
virtual Object* doPrim(Object*, Message*);
virtual Object* hasSlot(Object*, Message*);
virtual Object* getSlot(Object*, Message*);

// The mailbox is where messages come into. This allows us to decouple
// message sending and message receiving.
Mailbox* mailbox;
Expand Down

0 comments on commit 27cb440

Please sign in to comment.