Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
goccy committed Aug 5, 2013
1 parent 6a7aa15 commit 6971204
Show file tree
Hide file tree
Showing 7 changed files with 675 additions and 4,070 deletions.
3 changes: 3 additions & 0 deletions example/operator.pl
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
say 1 + 2 == 3;
say 1.2 + 2.3 == 3.5;
say 2.1 + 2 == 4.1;
=hoge
say 1 - 2 == -1;
say 1.3 - 2 == -0.7;
say 1.3 * 2 == 2.6;
Expand All @@ -41,3 +43,4 @@
say 2 + $b;
say $a + 2.1;
say 2.1 + $b;
=cut
50 changes: 19 additions & 31 deletions gen/runtime_api.c
Original file line number Diff line number Diff line change
@@ -1,24 +1,13 @@
#include "runtime_api.h"

typedef union {
int ivalue;
double dvalue;
char *svalue;
void *ovalue;
} UNION;

UNION v;

void print_object(Object *o)
void print_object(Value o)
{
v.svalue = "hello world";
fprintf(stderr, "o->type = [%d]\n", o->type);
switch (o->type) {
switch (TYPE(o)) {
case Int:
printf("%ld", to_Int(o));
printf("%d", to_Int(o));
break;
case Double:
printf("%lf", to_Double(o));
printf("%f", to_Double(o));
break;
case String:
printf("%s", to_String(o));
Expand Down Expand Up @@ -55,7 +44,7 @@ Object *shift(ArrayObject *args)
size_t size = args->size;
if (size > 1) return NULL;
if (size == 1) {
Object *o = args->list[0];
Value o = args->list[0];
TYPE_CHECK(o, Array);
ArrayObject *array = to_Array(o);
ret = array->list[0];
Expand All @@ -67,28 +56,26 @@ Object *shift(ArrayObject *args)
return ret;
}

Object *push(ArrayObject *args)
Value push(ArrayObject *args)
{
size_t size = args->size;
Object *ret = NULL;
Value ret = NULL;
if (size != 2) {
fprintf(stderr, "Type Error!: near by push\n");
} else {
Object *array = args->list[0];
Object *value = args->list[1];
Value array = args->list[0];
Value value = args->list[1];
TYPE_CHECK(array, Array);
ArrayObject *base = to_Array(array);
void *tmp;
if (!(tmp = malloc(sizeof(Object) * (base->size + 1)))) {
if (!(tmp = malloc(sizeof(Value) * (base->size + 1)))) {
fprintf(stderr, "ERROR!!: cannot allocated memory\n");
} else {
memcpy(tmp, base->list, sizeof(Object) * base->size);
base->list = (Object **)tmp;
memcpy(tmp, base->list, sizeof(Value) * base->size);
base->list = (Value *)tmp;
base->list[base->size] = value;
base->size++;
ret = (Object *)malloc(sizeof(Object));
ret->type = Int;
ret->v.ivalue = base->size;
ret = INT_init(base->size);
}
}
return ret;
Expand Down Expand Up @@ -116,15 +103,15 @@ Object *new_Object(void)
return (Object *)malloc(sizeof(Object));
}

Object *Object_addObject(Object *_a, Object *_b)
Value Object_addObject(Value _a, Value _b)
{
Object *ret = (Object *)malloc(sizeof(Object));
Object *a = (_a->type == ObjectType) ? to_Object(_a) : _a;
Object *b = (_b->type == ObjectType) ? to_Object(_b) : _b;
Object *a = to_Object(_a);
Object *b = to_Object(_b);
Value ret;
setResultByObjectObject(ret, a, b, +);
return ret;
}

/*
Object *Object_subObject(Object *_a, Object *_b)
{
Object *ret = (Object *)malloc(sizeof(Object));
Expand Down Expand Up @@ -459,3 +446,4 @@ int Object_isTrue(Object *a)
}
return ret;
}
*/
78 changes: 47 additions & 31 deletions gen/runtime_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,20 @@
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <stdint.h>

typedef enum {
Int,
Double,
Int,
String,
Array,
Hash,
BlessedObject,
ObjectType,
BlessedObject,
Unknown
} Type;

typedef struct _Value {
long ivalue;
double dvalue;
char *svalue;
void *ovalue;
} Value;
typedef void * Value;

typedef struct _Object {
int type;
Expand All @@ -28,46 +24,66 @@ typedef struct _Object {

typedef struct _Array {
int type;
Object **list;
void **list;
size_t size;
} ArrayObject;

#define to_Int(o) o->v.ivalue
#define to_Double(o) o->v.dvalue
#define to_String(o) o->v.svalue
#define to_Object(o) (Object *)o->v.ovalue
#define to_Array(o) (ArrayObject *)o->v.ovalue
#define NaN (0xFFF0000000000000)
#define MASK (0x00000000FFFFFFFF)
#define _TYPE (0x000F000000000000)
#define INT_TAG (uint64_t)(0x0001000000000000)
#define STRING_TAG (uint64_t)(0x0002000000000000)
#define ARRAY_TAG (uint64_t)(0x0003000000000000)
#define HASH_TAG (uint64_t)(0x0004000000000000)
#define OBJECT_TAG (uint64_t)(0x0005000000000000)

#define TYPE(data) ((((uint64_t)data & NaN) == NaN) * (((uint64_t)data & _TYPE) >> 48))

#define INT_init(data) (void *)(uint64_t)((data & MASK) | NaN | INT_TAG)
#define DOUBLE_init(data) (void *)&data
#define STRING_init(data) (void *)((uint64_t)data | NaN | STRING_TAG)
#define ARRAY_init(data) (void *)((uint64_t)data | NaN | ARRAY_TAG)
#define HASH_init(data) (void *)((uint64_t)data | NaN | HASH_TAG)
#define OBJECT_init(data) (void *)((uint64_t)data | NaN | OBJECT_TAG)

#define to_Int(o) ((intptr_t)o)
#define to_Double(o) (*(double *)o)
#define to_String(o) (char *)((uint64_t)o ^ (NaN | STRING_TAG))
#define to_Object(o) (Object *)((uint64_t)o ^ (NaN | OBJECT_TAG))
#define to_Array(o) (ArrayObject *)((uint64_t)o ^ (NaN | ARRAY_TAG))
#define TYPE_CHECK(o, T) do { \
if (o->type != T) { \
if (TYPE(o) != T) { \
assert(0 && "Type Error!\n"); \
} \
} while (0)


void print(ArrayObject *array);

#define SET(param, target, a, b, op) \
switch (b->type) { \
case Int: \
target->type = Int; \
target->v.param = a op to_Int(b); \
break; \
case Double: \
target->type = Double; \
target->v.dvalue = a op to_Double(b); \
break; \
default: \
break; \
}
#define SET(ret, a, b, op) do { \
switch (b->type) { \
case Int: { \
int i = a op to_Int(b); \
ret = INT_init(i); \
break; \
} \
case Double: { \
double d = a op to_Double(b); \
ret = DOUBLE_init(d); \
break; \
} \
default: \
break; \
} \
} while (0)

#define setResultByObjectObject(ret, a, b, op) do { \
switch (a->type) { \
case Int: \
SET(ivalue, ret, to_Int(a), b, op); \
SET(ret, to_Int(a), b, op); \
break; \
case Double: \
ret->type = Double; \
SET(dvalue, ret, to_Double(a), b, op); \
SET(ret, to_Double(a), b, op); \
break; \
default: \
break; \
Expand Down
Loading

0 comments on commit 6971204

Please sign in to comment.