Skip to content

Commit

Permalink
Minor refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
bayerf42 committed Sep 29, 2022
1 parent 174bf6d commit 5df9d99
Show file tree
Hide file tree
Showing 5 changed files with 5,275 additions and 5,295 deletions.
26 changes: 12 additions & 14 deletions compiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -316,26 +316,24 @@ static void slice(bool canAssign) {
}
}

static void slice_right(bool canAssign) {
if (match(TOKEN_RIGHT_BRACKET)) {
emitConstant(NUMBER_VAL(INT32_MAX>>1));
} else {
expression();
consume(TOKEN_RIGHT_BRACKET, "Expect ']' after slice.");
}
slice(canAssign);
}

static void index_(bool canAssign) {
if (match(TOKEN_COLON)) {
emitConstant(NUMBER_VAL(0));
if (match(TOKEN_RIGHT_BRACKET)) {
emitConstant(NUMBER_VAL(INT32_MAX>>1));
} else {
expression();
consume(TOKEN_RIGHT_BRACKET, "Expect ']' after slice.");
}
slice(canAssign);
slice_right(canAssign);
} else {
expression();
if (match(TOKEN_COLON)) {
if (match(TOKEN_RIGHT_BRACKET)) {
emitConstant(NUMBER_VAL(INT32_MAX>>1));
} else {
expression();
consume(TOKEN_RIGHT_BRACKET, "Expect ']' after slice.");
}
slice(canAssign);
slice_right(canAssign);
} else {
consume(TOKEN_RIGHT_BRACKET, "Expect ']' after index.");

Expand Down
49 changes: 18 additions & 31 deletions object.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,20 +40,23 @@ typedef enum {
OBJ_UPVALUE
} ObjType;

struct Obj {
struct Obj* nextObj;
uint8_t type;

#define OBJ_HEADER \
struct Obj* nextObj; \
uint8_t type; \
uint8_t isMarked;


struct Obj {
OBJ_HEADER
};

// The IDE68K C compiler doesn't seem to like including struct Obj in the following structures
// and generates wrong code when casting, so we expand struct Obj manually.

typedef struct {
struct Obj* nextObj;
uint8_t type;
uint8_t isMarked;

OBJ_HEADER

uint8_t arity;
uint8_t upvalueCount;
Chunk chunk;
Expand All @@ -64,75 +67,59 @@ typedef bool (*NativeFn)(int argCount, Value* args);
typedef char Signature[8];

typedef struct {
struct Obj* nextObj;
uint8_t type;
uint8_t isMarked;
OBJ_HEADER

Signature signature;
NativeFn function;
} ObjNative;

struct ObjString {
struct Obj* nextObj;
uint8_t type;
uint8_t isMarked;
OBJ_HEADER

int16_t length;
uint32_t hash;
char chars[]; // chap 19, single alloc for strings, as embedded char array
};

typedef struct ObjUpvalue {
struct Obj* nextObj;
uint8_t type;
uint8_t isMarked;
OBJ_HEADER

Value* location;
Value closed;
struct ObjUpvalue* nextUpvalue;
} ObjUpvalue;

typedef struct {
struct Obj* nextObj;
uint8_t type;
uint8_t isMarked;
OBJ_HEADER

ObjFunction* function;
int16_t upvalueCount; // too big, but keep alignment
ObjUpvalue* upvalues[]; // like ObjString, array embedded in structure
} ObjClosure;

typedef struct {
struct Obj* nextObj;
uint8_t type;
uint8_t isMarked;
OBJ_HEADER

ObjString* name;
Table methods;
} ObjClass;

typedef struct {
struct Obj* nextObj;
uint8_t type;
uint8_t isMarked;
OBJ_HEADER

ObjClass* klass;
Table fields;
} ObjInstance;

typedef struct {
struct Obj* nextObj;
uint8_t type;
uint8_t isMarked;
OBJ_HEADER

Value receiver;
ObjClosure* method;
} ObjBoundMethod;

typedef struct {
struct Obj* nextObj;
uint8_t type;
uint8_t isMarked;
OBJ_HEADER

int16_t count;
int16_t capacity;
Expand Down
Loading

0 comments on commit 5df9d99

Please sign in to comment.