]> git.mdlowis.com Git - proto/cerise-c.git/commitdiff
array literals working again
authorMichael D. Lowis <mike@mdlowis.com>
Wed, 23 Oct 2024 03:12:44 +0000 (23:12 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Wed, 23 Oct 2024 03:12:44 +0000 (23:12 -0400)
cerise-c.m
lib/codegen.rb
runtime.h

index 733051cbb77610c23dd589ba4ef4aab8665745f1..666f8ae25ecf0d11c1548e0863ac8b17e33a7aba 100644 (file)
@@ -96,8 +96,8 @@ TestStringOps()
 
 TestArrayOps()
 {
-#    def array = [1,2,3]
-#    def item = 42
+    def array = [1,2,3]
+    def item = 42
 #    set array[0] = item
 #    assert array[0] == 42
 #    assert array[1] == 2
index bc64970c8c1eb0d37ba96e5320665ab376902607..c81a83a4007ad47435088782ca0e735d4c3e7524 100644 (file)
@@ -18,9 +18,9 @@ class Codegen
     if type.is_a? Symbol
       type
     elsif type.form == :array
-      "Value"
+      "_array"
     elsif type.form == :hash
-      "Value"
+      "_dict"
     else
       raise "unconvertable type: #{type}"
     end
@@ -218,7 +218,7 @@ class Codegen
     @syms.add_sym(v.name.name, v.loc, :local, v.type, v.value)
     temp = emit(v.value)
     error v, "invalid local definition: #{v.name.name}" if not @locals.index(v.name.name)
-    putln "Value #{v.name.name} = #{temp};"
+    putln "#{type_to_s(v.value.type)} #{v.name.name} = #{temp};"
   end
 
   def emit_set(v)
index e0a8663b254672f3e29123681572d51a2baceaa7..d5e3f8d581741ea43dcae83f7ed24714e6a1ee1b 100644 (file)
--- a/runtime.h
+++ b/runtime.h
 #include <assert.h>
 #include <string.h>
 
-#if (UINTPTR_MAX == 0xFFFFFFFFu)
-#error "32-bit machines are not supported at this time"
-#endif
-
-//# Nil         X
-//# Bool        X
-//# Int         X
-//# Real        X
-//# Record      .
-//# Array
-//# Hashmap
-//# String
-
-//#define VALUE_NIL       0x0llu
-//#define VALUE_UNDEFINED 0x1llu
-//#define VALUE_FALSE     0x2llu
-//#define VALUE_TRUE      0x3llu
-
-#define NAN_TAG_DICT         0x0000000000000000llu
-#define NAN_TAG_ARRAY        0x0001000000000000llu
-#define NAN_TAG_STRING       0x0002000000000000llu
-//#define NAN_TAG_BLOCK        0x0003000000000000llu
-//#define NAN_TAG_NATIVE       0x0004000000000000llu
-//#define NAN_TAG_SYMBOL       0x0005000000000000llu
-#define NAN_TAG_INTEGER      0x0006000000000000llu
-#define NAN_TAG_DOUBLE       0x0007000000000000llu
-
-#define MASK_POINTER         0x0000fffffffffffcllu
-#define HIGH16_TAG           0xffff000000000000llu
-//#define IMM_TAG              0x0000000000000003llu
-//#define TAG_BITS             (HIGH16_TAG | IMM_TAG)
-
-
-#define VALUE_NIL    0x0llu
-#define VALUE_UNUSED 0x1llu
-#define VALUE_FALSE  0x2llu
-#define VALUE_TRUE   0x3llu
-
-typedef union {
-    uint64_t as_uint64;
-    double as_double;
-} Value;
+typedef intptr_t _value;
 
 typedef struct string {
     uint64_t length;
     uint8_t bytes[];
 }* string;
 
-typedef struct {
+typedef struct array {
     uint64_t length;
     uint64_t capacity;
-    Value* values;
-} Array;
-
-typedef struct DictNode {
-    struct DictNode* left;
-    struct DictNode* right;
-    Value key;
-    Value value;
+    _value* values;
+}* _array;
+
+typedef struct dict_node {
+    struct dict_node* left;
+    struct dict_node* right;
+    _value key;
+    _value value;
     uint32_t hash;
-} DictNode;
+}* dict_node;
 
-typedef struct {
+typedef struct dict {
     uint64_t length;
-    DictNode* root;
-} Dict;
-
-
-/* Value Tests
- *************************************************/
-
-static inline bool IsNil(Value val) {
-    return val.as_uint64 == VALUE_NIL;
-}
-
-static inline bool IsFalse(Value val) {
-    return val.as_uint64 == VALUE_FALSE;
-}
-
-static inline bool IsTrue(Value val) {
-    return val.as_uint64 == VALUE_TRUE;
-}
-
-static inline bool IsBool(Value val) {
-    return (IsTrue(val) || IsFalse(val));
-}
-
-static inline bool IsNumber(Value val) {
-    return val.as_uint64 >= NAN_TAG_INTEGER;
-}
-
-static inline bool IsInt(Value val) {
-    return (val.as_uint64 & HIGH16_TAG) == NAN_TAG_INTEGER;
-}
-
-static inline bool IsReal(Value val) {
-    return IsNumber(val) && !IsInt(val);
-}
-
-static inline bool IsString(Value val) {
-    return (val.as_uint64 & HIGH16_TAG) == NAN_TAG_STRING;
-}
-
-static inline bool IsArray(Value val) {
-    return (val.as_uint64 & HIGH16_TAG) == NAN_TAG_ARRAY;
-}
-
-static inline bool IsDict(Value val) {
-    return (val.as_uint64 & HIGH16_TAG) == NAN_TAG_DICT;
-}
-
-
-/* C Type Conversions
- *************************************************/
-
-static inline bool ValueAsBool(Value val) {
-    assert(IsBool(val));
-    return (bool)(val.as_uint64 == VALUE_TRUE);
-}
-
-static inline int32_t ValueAsInt(Value val) {
-    assert(IsInt(val));
-    return (int32_t)val.as_uint64;
-}
-
-static inline double ValueAsReal(Value val) {
-    assert(IsReal(val));
-    val.as_uint64 -= NAN_TAG_DOUBLE;
-    return val.as_double;
-}
-
-//static String* ValueAsString(Value val) {
-//    assert(IsString(val));
-//    return (String*)(val.as_uint64 & MASK_POINTER);
-//}
-//
-static Array* ValueAsArray(Value val) {
-    assert(IsArray(val));
-    return (Array*)(val.as_uint64 & MASK_POINTER);
-}
-
-static Dict* ValueAsDict(Value val) {
-    assert(IsDict(val));
-    return (Dict*)(val.as_uint64 & MASK_POINTER);
-}
-
-
-//static Array* to_array(Value val) {
-//    assert(is_array(val));
-//    return (Array*)(val.as_pointer);
-//}
-//
-//
-//static Block* to_block(Value val) {
-//    assert(is_block(val));
-//    return (Block*)(val.as_pointer);
-//}
-
-//static NativeBlock* to_native(Value val) {
-//    assert(is_native(val));
-//    return (NativeBlock*)(val.as_pointer);
-//}
-//
-//static Symbol* to_symbol(Value val) {
-//    assert(is_symbol(val));
-//    return (Symbol*)(val.as_pointer);
-//}
-
-
-/* Value Constructors
- *************************************************/
-
-static inline Value MakeNil(void) {
-    return (Value){ .as_uint64 = VALUE_NIL };
-}
-
-static inline Value MakeBool(bool b) {
-    return (Value){ .as_uint64 = (b ? VALUE_TRUE : VALUE_FALSE) };
-}
-
-static inline Value MakeInt(int32_t i) {
-    return (Value){ .as_uint64 = NAN_TAG_INTEGER | (uint32_t)i };
-}
-
-static inline Value MakeReal(double d) {
-    Value val = { .as_double = d };
-    val.as_uint64 += NAN_TAG_DOUBLE;
-    assert(IsReal(val));
-    return val;
-}
+    dict_node root;
+}* _dict;
 
 string MakeString(char* s);
-Value MakeArray(int32_t nslots);
-Value MakeDict(int32_t nslots);
-
-
-/* Value Type Conversions
- *************************************************/
-
-Value ToString(Value val);
-
-
-/* Builtin Functions
- *************************************************/
-
-Value Error(Value val);
-void RuntimeError(char* s);
+_array MakeArray(int32_t nslots);
+_dict MakeDict(int32_t nslots);
 void Assert(char* file, int lineno, bool val);
-Value Length(Value val);
-
 
-/* Binary Operators
- *************************************************/
-
-Value OpAdd(Value left, Value right);
-Value OpSub(Value left, Value right);
-Value OpMul(Value left, Value right);
-Value OpDiv(Value left, Value right);
-Value OpMod(Value left, Value right);
-Value OpLt(Value left, Value right);
-Value OpLtEq(Value left, Value right);
-Value OpGt(Value left, Value right);
-Value OpGtEq(Value left, Value right);
-Value OpEq(Value left, Value right);
-Value OpNeq(Value left, Value right);
-
-/* 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 Dict_Set(Value hash, Value key, Value value);
-Value Dict_Get(Value hash, Value key);
-
-Value Object_Get(Value object, Value key);
-void Object_Set(Value object, Value key, Value value);
+static inline void Array_Set(_array ary, int index, _value val)
+{
+}
 
 /* Garbage Collection
  *************************************************/
@@ -256,7 +50,7 @@ void* GC_Allocate(size_t nbytes);
 
 #define ENTRYPOINT(func) \
     int main(int argc, char** argv) { \
-        extern Value func(); \
-        (void)func(); \
+        extern void func(); \
+        func(); \
         return 0; \
     }