for f in runtime/*.c; do
printf "CC %s\n" "$f"
- gcc -c -I. -O2 -o "${f%.c}.o" "$f"
+ gcc -c -I. -O2 -o "${f%.c}.o" "$f" &
done
+wait
printf "LIB %s\n" libruntime.a
ar rcs libruntime.a runtime/*.o
./cerise-c.rb > cerise-c.c \
&& gcc -I. -O2 -o cerise-c cerise-c.c libruntime.a \
&& ./cerise-c
-
end
elsif v.value.is_a? Hash
- putln state, "Value #{var} = MakeHash(#{v.value.length});"
+ putln state, "Value #{var} = MakeDict(#{v.value.length});"
v.value.each do |k, v|
val = emit(state, v)
- putln state, "Hash_Set(#{var}, MakeString(#{k.value}), #{val});"
+ putln state, "Dict_Set(#{var}, MakeString(#{k.value}), #{val});"
end
else
raise "code emitting constants of this type not implemented"
//#define VALUE_TRUE 0x3llu
//#define NAN_TAG_RECORD 0x0000000000000000llu
-//#define NAN_TAG_ARRAY 0x0001000000000000llu
+#define NAN_TAG_ARRAY 0x0001000000000000llu
#define NAN_TAG_STRING 0x0002000000000000llu
//#define NAN_TAG_BLOCK 0x0003000000000000llu
//#define NAN_TAG_NATIVE 0x0004000000000000llu
typedef struct {
uint64_t length;
- char bytes[];
-} String;
+ Value values[];
+} Array;
/* Value Tests
return (val.as_uint64 & HIGH16_TAG) == NAN_TAG_STRING;
}
+static inline bool IsArray(Value val) {
+ return (val.as_uint64 & HIGH16_TAG) == NAN_TAG_STRING;
+}
+
+static inline bool IsDict(Value val) {
+ return (val.as_uint64 & HIGH16_TAG) == NAN_TAG_STRING;
+}
+
/* C Type Conversions
*************************************************/
return val;
}
-static inline Value MakeString(char* s) {
- size_t sz = strlen(s);
- String* str = calloc(sizeof(String) + sz + 1, 1);
- str->length = sz;
- strncpy(str->bytes, s, sz+1);
- return (Value){
- .as_uint64 = (NAN_TAG_STRING | (uint64_t)str)
- };
-}
-
-static inline Value MakeArray(int32_t nslots) {
-// Value* record = calloc(2,sizeof(Value));
-// record[0] = MakeInt(1);
-// record[1] = MakeInt(nslots); //
-// record[2] = MakeInt(nslots); //
-// return (Value){ .as_uint64 = (NAN_TAG_ARRAY | (uintptr_t)record) };
- return MakeNil();
-}
-
-
-static inline Value MakeHash(int32_t nslots) {
-// Value* record = calloc(2,sizeof(Value));
-// record[0] = MakeInt(1);
-// record[1] = MakeInt(nslots); //
-// record[2] = MakeInt(nslots); //
-// return (Value){ .as_uint64 = (NAN_TAG_ARRAY | (uintptr_t)record) };
- return MakeNil();
-}
+Value MakeString(char* s);
+Value MakeArray(int32_t nslots);
+Value MakeDict(int32_t nslots);
/* Value Type Conversions
/* Arrays And Hashes
*************************************************/
+void String_Set(Value array, int index, Value value);
+Value String_Get(Value array, int index);
+
void Array_Set(Value array, int index, Value value);
Value Array_Get(Value array, int index);
-void Hash_Set(Value hash, Value key, Value value);
-Value Hash_Get(Value hash, Value key);
+void Dict_Set(Value hash, Value key, Value value);
+Value Dict_Get(Value hash, Value key);
Value Object_Get(Value object, Value key);
-Value Object_Set(Value object, Value key, Value value);
+void Object_Set(Value object, Value key, Value value);
#include "runtime.h"
+Value MakeArray(int32_t nslots) {
+// Value* record = calloc(2,sizeof(Value));
+// record[0] = MakeInt(1);
+// record[1] = MakeInt(nslots); //
+// record[2] = MakeInt(nslots); //
+// return (Value){ .as_uint64 = (NAN_TAG_ARRAY | (uintptr_t)record) };
+ return MakeNil();
+}
+
void Array_Set(Value array, int index, Value value)
{
}
#include "runtime.h"
-void Hash_Set(Value hash, Value key, Value value)
+Value MakeDict(int32_t nslots) {
+// Value* record = calloc(2,sizeof(Value));
+// record[0] = MakeInt(1);
+// record[1] = MakeInt(nslots); //
+// record[2] = MakeInt(nslots); //
+// return (Value){ .as_uint64 = (NAN_TAG_ARRAY | (uintptr_t)record) };
+ return MakeNil();
+}
+
+void Dict_Set(Value hash, Value key, Value value)
{
}
-Value Hash_Get(Value hash, Value key)
+Value Dict_Get(Value hash, Value key)
{
return MakeNil();
}
Value Object_Get(Value object, Value key)
{
- return MakeNil();
+ Value result = MakeNil();
+ if (IsDict(object)) {
+ result = Dict_Get(object, key);
+ } else if (IsArray(object)) {
+ result = Array_Get(object, ValueAsInt(key));
+ } else if (IsString(object)) {
+ result = String_Get(object, ValueAsInt(key));
+ } else {
+ Error(MakeString("object is not indexable"));
+ }
+ return result;
}
-Value Object_Set(Value object, Value key, Value value)
+void Object_Set(Value object, Value key, Value value)
{
- return MakeNil();
+ if (IsDict(object)) {
+ Dict_Set(object, key, value);
+ } else if (IsArray(object)) {
+ Array_Set(object, ValueAsInt(key), value);
+ } else {
+ Error(MakeString("object is not assignable"));
+ }
}
--- /dev/null
+#include "runtime.h"
+
+Value MakeString(char* s) {
+ size_t sz = strlen(s);
+ String* str = calloc(sizeof(String) + sz + 1, 1);
+ str->length = sz;
+ strncpy(str->bytes, s, sz+1);
+ return (Value){
+ .as_uint64 = (NAN_TAG_STRING | (uint64_t)str)
+ };
+}
+
+Value String_Get(Value hash, int index)
+{
+ String* str = ValueAsString(hash);
+ if (index >= str->length) {
+ Error(MakeString("string index out of bounds"));
+ }
+ return MakeInt( str->bytes[index] );
+}