-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
146 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
#ifndef clox_compiler_h | ||
#define clox_compiler_h | ||
|
||
#include "object.h" | ||
#include "vm.h" | ||
|
||
bool compile(const char* source, Chunk* chunk); | ||
|
||
#endif | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include <stdio.h> | ||
#include <string.h> | ||
|
||
#include "memory.h" | ||
#include "object.h" | ||
#include "value.h" | ||
#include "vm.h" | ||
|
||
#define ALLOCATE_OBJ(type, objectType) \ | ||
(type*)allocateObject(sizeof(type), objectType) | ||
|
||
static Obj* allocateObject(size_t size, ObjType type) { | ||
Obj* object = (Obj*)reallocate(NULL, 0, size); | ||
object->type = type; | ||
return object; | ||
} | ||
|
||
static ObjString* allocateString(char* chars, int length) { | ||
ObjString* string = ALLOCATE_OBJ(ObjString, OBJ_STRING); | ||
string->length = length; | ||
string->chars = chars; | ||
return string; | ||
} | ||
|
||
ObjString* copyString(const char* chars, int length) { | ||
char* heapChars = ALLOCATE(char, length+1); | ||
memcpy(heapChars, chars, length); | ||
heapChars[length] = '\0'; | ||
return allocateString(heapChars, length); | ||
} | ||
|
||
ObjString* takeString(char* chars, int length) { | ||
return allocateString(chars, length); | ||
} | ||
|
||
void printObject(Value value) { | ||
switch (OBJ_TYPE(value)) { | ||
case OBJ_STRING: | ||
printf("%s", AS_CSTRING(value)); | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#ifndef clox_object_h | ||
#define clox_object_h | ||
|
||
#include "common.h" | ||
#include "value.h" | ||
|
||
#define OBJ_TYPE(value) (AS_OBJ(value)->type) | ||
|
||
#define IS_STRING(value) isObjType(value, OBJ_STRING) | ||
|
||
#define AS_STRING(value) ((ObjString*)AS_OBJ(value)) | ||
#define AS_CSTRING(value) (((ObjString*)AS_OBJ(value))->chars) | ||
|
||
typedef enum { | ||
OBJ_STRING, | ||
} ObjType; | ||
|
||
struct Obj { | ||
ObjType type; | ||
}; | ||
|
||
struct ObjString { | ||
Obj obj; | ||
int length; | ||
char* chars; | ||
}; | ||
|
||
static inline bool isObjType(Value value, ObjType type) { | ||
return IS_OBJ(value) && AS_OBJ(value)->type == type; | ||
} | ||
|
||
ObjString* takeString(char* chars, int length); | ||
ObjString* copyString(const char* chars, int length); | ||
void printObject(Value value); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters