Skip to content
This repository has been archived by the owner on Feb 3, 2021. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
Implement P6int/P6num/P6str reprs so far as they are in the .Net vers…
…ion of 6model and add them to the build.
  • Loading branch information
jnthn committed Sep 25, 2010
1 parent 0beab0f commit 51b35b7
Show file tree
Hide file tree
Showing 5 changed files with 468 additions and 2 deletions.
11 changes: 9 additions & 2 deletions build/Makefile.in
Expand Up @@ -143,11 +143,15 @@ METAMODEL_SOURCE = src/metamodel/rakudoobject.h src/metamodel/rakudoobject.c \
src/metamodel/repr_registry.h src/metamodel/repr_registry.c \
src/metamodel/knowhow_bootstrapper.h src/metamodel/knowhow_bootstrapper.c \
src/metamodel/reprs/KnowHOWREPR.h src/metamodel/reprs/KnowHOWREPR.c \
src/metamodel/reprs/P6opaque.h src/metamodel/reprs/P6opaque.c
src/metamodel/reprs/P6opaque.h src/metamodel/reprs/P6opaque.c \
src/metamodel/reprs/P6int.h src/metamodel/reprs/P6int.c \
src/metamodel/reprs/P6str.h src/metamodel/reprs/P6str.c \
src/metamodel/reprs/P6num.h src/metamodel/reprs/P6num.c

METAMODEL_OBJS = ../metamodel/rakudoobject$(O) ../metamodel/repr_registry$(O) \
../metamodel/knowhow_bootstrapper$(O) ../metamodel/reprs/KnowHOWREPR$(O) \
../metamodel/reprs/P6opaque$(O)
../metamodel/reprs/P6opaque$(O) ../metamodel/reprs/P6int$(O) \
../metamodel/reprs/P6str$(O) ../metamodel/reprs/P6num$(O)

CLEANUPS = \
*.manifest \
Expand Down Expand Up @@ -326,6 +330,9 @@ $(OPS_DIR)/$(OPS)$(LOAD_EXT): $(OPS_DIR)/$(OPS_SOURCE) $(DYNPMC)
cd src/metamodel && $(CC) -c @cc_o_out@knowhow_bootstrapper$(O) -I../../$(PMC_DIR) $(CINCLUDES) $(CFLAGS) knowhow_bootstrapper.c
cd src/metamodel/reprs && $(CC) -c @cc_o_out@KnowHOWREPR$(O) -I../../../$(PMC_DIR) $(CINCLUDES) $(CFLAGS) KnowHOWREPR.c
cd src/metamodel/reprs && $(CC) -c @cc_o_out@P6opaque$(O) -I../../../$(PMC_DIR) $(CINCLUDES) $(CFLAGS) P6opaque.c
cd src/metamodel/reprs && $(CC) -c @cc_o_out@P6int$(O) -I../../../$(PMC_DIR) $(CINCLUDES) $(CFLAGS) P6int.c
cd src/metamodel/reprs && $(CC) -c @cc_o_out@P6str$(O) -I../../../$(PMC_DIR) $(CINCLUDES) $(CFLAGS) P6str.c
cd src/metamodel/reprs && $(CC) -c @cc_o_out@P6num$(O) -I../../../$(PMC_DIR) $(CINCLUDES) $(CFLAGS) P6num.c
cd $(OPS_DIR) && $(LD) @ld_out@$(OPS)$(LOAD_EXT) $(OPS)$(O) $(METAMODEL_OBJS) $(LINKARGS)

bootstrap-files: $(STAGE2_PBCS) $(SETTING_NQP)
Expand Down
9 changes: 9 additions & 0 deletions src/metamodel/repr_registry.c
Expand Up @@ -8,6 +8,9 @@
#include "rakudoobject.h"
#include "reprs/KnowHOWREPR.h"
#include "reprs/P6opaque.h"
#include "reprs/P6int.h"
#include "reprs/P6num.h"
#include "reprs/P6str.h"

/* An array of representations. */
static PMC *repr_registry = NULL;
Expand Down Expand Up @@ -38,6 +41,12 @@ void REPR_initialize_registry(PARROT_INTERP) {
KnowHOWREPR_initialize(interp));
register_repr(interp, Parrot_str_new_constant(interp, "P6opaque"),
P6opaque_initialize(interp));
register_repr(interp, Parrot_str_new_constant(interp, "P6int"),
P6int_initialize(interp));
register_repr(interp, Parrot_str_new_constant(interp, "P6num"),
P6num_initialize(interp));
register_repr(interp, Parrot_str_new_constant(interp, "P6str"),
P6str_initialize(interp));
}

/* Get a representation's ID from its name. Note that the IDs may change so
Expand Down
150 changes: 150 additions & 0 deletions src/metamodel/reprs/P6int.c
@@ -0,0 +1,150 @@
/* This is the implementation of the P6int representation, which holds a native
* integer. */

#define PARROT_IN_EXTENSION
#include "parrot/parrot.h"
#include "parrot/extend.h"
#include "../rakudoobject.h"
#include "P6int.h"

/* Creates a new type object of this representation, and associates it with
* the given HOW. */
static PMC * type_object_for(PARROT_INTERP, PMC *self, PMC *HOW) {
/* Create new object instance. */
P6intInstance *obj = mem_allocate_zeroed_typed(P6intInstance);

/* Build an STable. */
PMC *st_pmc = create_stable(interp, self, HOW);
STable *st = STABLE_STRUCT(st_pmc);

/* Create type object and point it back at the STable. */
st->WHAT = wrap_object(interp, obj);
obj->common.stable = st_pmc;

return st->WHAT;
}

/* Creates a new instance based on the type object. */
static PMC * instance_of(PARROT_INTERP, PMC *self, PMC *WHAT) {
P6intInstance *obj = mem_allocate_zeroed_typed(P6intInstance);
obj->common.stable = STABLE_PMC(WHAT);
obj->value = 0;
return wrap_object(interp, obj);
}

/* Checks if a given object is defined (from the point of view of the
* representation). */
static INTVAL defined(PARROT_INTERP, PMC *self, PMC *obj) {
/* Native types cannot be undefined. */
return 1;
}

/* Gets the current value for an attribute. */
static PMC * get_attribute(PARROT_INTERP, PMC *self, PMC *obj, PMC *class_handle, STRING *name) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6int does not support attribute storage");
}

/* Gets the current value for an attribute, obtained using the given hint.*/
static PMC * get_attribute_with_hint(PARROT_INTERP, PMC *self, PMC *obj, PMC *class_handle, STRING *name, INTVAL hint) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6int does not support attribute storage");
}

/* Binds the given value to the specified attribute. */
static void bind_attribute(PARROT_INTERP, PMC *self, PMC *obj, PMC *class_handle, STRING *name, PMC *value) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6int does not support attribute storage");
}

/* Binds the given value to the specified attribute, using the given hint. */
static void bind_attribute_with_hint(PARROT_INTERP, PMC *self, PMC *obj, PMC *class_handle, STRING *name, INTVAL hint, PMC *value) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6int does not support attribute storage");
}

/* Gets the hint for the given attribute ID. */
static INTVAL hint_for(PARROT_INTERP, PMC *self, PMC *class_handle, STRING *name) {
return NO_HINT;
}

/* Used with boxing. Sets an integer value, for representations that can hold
* one. */
static void set_int(PARROT_INTERP, PMC *self, PMC *obj, INTVAL value) {
((P6intInstance *)PMC_data(obj))->value = value;
}

/* Used with boxing. Gets an integer value, for representations that can
* hold one. */
static INTVAL get_int(PARROT_INTERP, PMC *self, PMC *obj) {
return ((P6intInstance *)PMC_data(obj))->value;
}

/* Used with boxing. Sets a floating point value, for representations that can
* hold one. */
static void set_num(PARROT_INTERP, PMC *self, PMC *obj, FLOATVAL value) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6int cannot box a native num");
}

/* Used with boxing. Gets a floating point value, for representations that can
* hold one. */
static FLOATVAL get_num(PARROT_INTERP, PMC *self, PMC *obj) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6int cannot unbox to a native num");
}

/* Used with boxing. Sets a string value, for representations that can hold
* one. */
static void set_str(PARROT_INTERP, PMC *self, PMC *obj, STRING *value) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6int cannot box a native string");
}

/* Used with boxing. Gets a string value, for representations that can hold
* one. */
static STRING * get_str(PARROT_INTERP, PMC *self, PMC *obj) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6int cannot unbox to a native string");
}

/* This Parrot-specific addition to the API is used to mark an object. */
static void gc_mark(PARROT_INTERP, PMC *self, PMC *obj) {
P6intInstance *instance = (P6intInstance *)PMC_data(obj);
if (!PMC_IS_NULL(instance->common.stable))
Parrot_gc_mark_PMC_alive(interp, instance->common.stable);
}

/* This Parrot-specific addition to the API is used to free an object. */
static void gc_free(PARROT_INTERP, PMC *self, PMC *obj) {
mem_sys_free(PMC_data(obj));
PMC_data(obj) = NULL;
}

/* Initializes the P6int representation. */
PMC * P6int_initialize(PARROT_INTERP) {
REPRCommonalities *repr;
PMC *repr_pmc;

/* Allocate and populate the representation function table. */
repr = mem_allocate_typed(REPRCommonalities);
repr->type_object_for = type_object_for;
repr->instance_of = instance_of;
repr->defined = defined;
repr->get_attribute = get_attribute;
repr->get_attribute_with_hint = get_attribute_with_hint;
repr->bind_attribute = bind_attribute;
repr->bind_attribute_with_hint = bind_attribute_with_hint;
repr->hint_for = hint_for;
repr->set_int = set_int;
repr->get_int = get_int;
repr->set_num = set_num;
repr->get_num = get_num;
repr->set_str = set_str;
repr->get_str = get_str;
repr->gc_mark = gc_mark;
repr->gc_free = gc_free;

/* Wrap it in a PMC. */
return wrap_repr(interp, repr);
}
150 changes: 150 additions & 0 deletions src/metamodel/reprs/P6num.c
@@ -0,0 +1,150 @@
/* This is the implementation of the P6num representation, which holds a native
* floating point number. */

#define PARROT_IN_EXTENSION
#include "parrot/parrot.h"
#include "parrot/extend.h"
#include "../rakudoobject.h"
#include "P6num.h"

/* Creates a new type object of this representation, and associates it with
* the given HOW. */
static PMC * type_object_for(PARROT_INTERP, PMC *self, PMC *HOW) {
/* Create new object instance. */
P6numInstance *obj = mem_allocate_zeroed_typed(P6numInstance);

/* Build an STable. */
PMC *st_pmc = create_stable(interp, self, HOW);
STable *st = STABLE_STRUCT(st_pmc);

/* Create type object and point it back at the STable. */
st->WHAT = wrap_object(interp, obj);
obj->common.stable = st_pmc;

return st->WHAT;
}

/* Creates a new instance based on the type object. */
static PMC * instance_of(PARROT_INTERP, PMC *self, PMC *WHAT) {
P6numInstance *obj = mem_allocate_zeroed_typed(P6numInstance);
obj->common.stable = STABLE_PMC(WHAT);
obj->value = 0.0;
return wrap_object(interp, obj);
}

/* Checks if a given object is defined (from the point of view of the
* representation). */
static INTVAL defined(PARROT_INTERP, PMC *self, PMC *obj) {
/* Native types cannot be undefined. */
return 1;
}

/* Gets the current value for an attribute. */
static PMC * get_attribute(PARROT_INTERP, PMC *self, PMC *obj, PMC *class_handle, STRING *name) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6num does not support attribute storage");
}

/* Gets the current value for an attribute, obtained using the given hint.*/
static PMC * get_attribute_with_hint(PARROT_INTERP, PMC *self, PMC *obj, PMC *class_handle, STRING *name, INTVAL hint) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6num does not support attribute storage");
}

/* Binds the given value to the specified attribute. */
static void bind_attribute(PARROT_INTERP, PMC *self, PMC *obj, PMC *class_handle, STRING *name, PMC *value) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6num does not support attribute storage");
}

/* Binds the given value to the specified attribute, using the given hint. */
static void bind_attribute_with_hint(PARROT_INTERP, PMC *self, PMC *obj, PMC *class_handle, STRING *name, INTVAL hint, PMC *value) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6num does not support attribute storage");
}

/* Gets the hint for the given attribute ID. */
static INTVAL hint_for(PARROT_INTERP, PMC *self, PMC *class_handle, STRING *name) {
return NO_HINT;
}

/* Used with boxing. Sets an integer value, for representations that can hold
* one. */
static void set_int(PARROT_INTERP, PMC *self, PMC *obj, INTVAL value) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6num cannot box a native int");
}

/* Used with boxing. Gets an integer value, for representations that can
* hold one. */
static INTVAL get_int(PARROT_INTERP, PMC *self, PMC *obj) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6num cannot unbox to a native int");
}

/* Used with boxing. Sets a floating point value, for representations that can
* hold one. */
static void set_num(PARROT_INTERP, PMC *self, PMC *obj, FLOATVAL value) {
((P6numInstance *)PMC_data(obj))->value = value;
}

/* Used with boxing. Gets a floating point value, for representations that can
* hold one. */
static FLOATVAL get_num(PARROT_INTERP, PMC *self, PMC *obj) {
return ((P6numInstance *)PMC_data(obj))->value;
}

/* Used with boxing. Sets a string value, for representations that can hold
* one. */
static void set_str(PARROT_INTERP, PMC *self, PMC *obj, STRING *value) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6num cannot box a native string");
}

/* Used with boxing. Gets a string value, for representations that can hold
* one. */
static STRING * get_str(PARROT_INTERP, PMC *self, PMC *obj) {
Parrot_ex_throw_from_c_args(interp, NULL, EXCEPTION_INVALID_OPERATION,
"P6num cannot unbox to a native string");
}

/* This Parrot-specific addition to the API is used to mark an object. */
static void gc_mark(PARROT_INTERP, PMC *self, PMC *obj) {
P6numInstance *instance = (P6numInstance *)PMC_data(obj);
if (!PMC_IS_NULL(instance->common.stable))
Parrot_gc_mark_PMC_alive(interp, instance->common.stable);
}

/* This Parrot-specific addition to the API is used to free an object. */
static void gc_free(PARROT_INTERP, PMC *self, PMC *obj) {
mem_sys_free(PMC_data(obj));
PMC_data(obj) = NULL;
}

/* Initializes the P6num representation. */
PMC * P6num_initialize(PARROT_INTERP) {
REPRCommonalities *repr;
PMC *repr_pmc;

/* Allocate and populate the representation function table. */
repr = mem_allocate_typed(REPRCommonalities);
repr->type_object_for = type_object_for;
repr->instance_of = instance_of;
repr->defined = defined;
repr->get_attribute = get_attribute;
repr->get_attribute_with_hint = get_attribute_with_hint;
repr->bind_attribute = bind_attribute;
repr->bind_attribute_with_hint = bind_attribute_with_hint;
repr->hint_for = hint_for;
repr->set_int = set_int;
repr->get_int = get_int;
repr->set_num = set_num;
repr->get_num = get_num;
repr->set_str = set_str;
repr->get_str = get_str;
repr->gc_mark = gc_mark;
repr->gc_free = gc_free;

/* Wrap it in a PMC. */
return wrap_repr(interp, repr);
}

0 comments on commit 51b35b7

Please sign in to comment.