cc4f29afcb2d — Leonard Ritter 1 year, 2 months ago
* added new stub inspect module and test
* `ValueKind` enum type is now exposed to the global scope; `'kind` no longer returns a simple integer
* added `'kind?` method to `Value`, which allows to check for value kind before `CEnum` is bootstrapped
M lib/scopes/core.sc +13 -10
@@ 1049,6 1049,9 @@ run-stage; # 3
     constant? = sc_value_is_constant
     pure? = sc_value_is_pure
     kind = sc_value_kind
+    kind? =
+        inline (self kind)
+            icmp== (sc_value_kind self) kind
     tag =
         inline (self anchor)
             sc_valueref_tag anchor self

          
@@ 2816,7 2819,7 @@ let report =
                 view args
 
 fn extract-integer (value)
-    if (== ('kind value) value-kind-const-int)
+    if ('kind? value value-kind-const-int)
         return (sc_const_int_extract value)
     error@ ('anchor value) "while extracting integer" str"integer constant expected"
 

          
@@ 4482,7 4485,7 @@ let import =
                     repeat (add i 1:usize) start
             let sxname rest = (decons args)
             let name namestr bind? =
-                if (('kind sxname) == value-kind-const-string)
+                if ('kind? sxname value-kind-const-string)
                     let name = (sxname as string)
                     _ (Symbol name) name false
                 else

          
@@ 5182,8 5185,8 @@ let packedtupleof = (gen-tupleof sc_pack
                     let other = ('getarg args 1)
                     if
                         &
-                            ('kind self) == value-kind-const-string
-                            ('kind self) == value-kind-const-string
+                            'kind? self value-kind-const-string
+                            'kind? other value-kind-const-string
                         return ('tag `[(self == other)] ('anchor args))
                     loop (i result = ('element-count ('typeof self)) `true)
                         if (i == 0)

          
@@ 5206,8 5209,8 @@ let packedtupleof = (gen-tupleof sc_pack
                                 let rhs = ('getarg args 1)
                                 if
                                     &
-                                        ('kind lhs) == value-kind-const-string
-                                        ('kind rhs) == value-kind-const-string
+                                        'kind? lhs value-kind-const-string
+                                        'kind? rhs value-kind-const-string
                                     return
                                         sc_const_string_new
                                             sc_string_join

          
@@ 5973,12 5976,12 @@ fn gen-sugar-matcher (failfunc expr scop
                 sc_expression_append outexpr arg
                 let scope = ('bind scope param arg)
                 repeat (i + 1) rest next (| varargs variadic?) scope
-            elseif (('kind paramv) == value-kind-const-string)
+            elseif ('kind? paramv value-kind-const-string)
                 let str = (unbox-string paramv)
                 sc_expression_append outexpr
                     spice-quote
                         let arg next = (sc_list_decons next)
-                        if (('kind arg) != value-kind-const-string)
+                        if (not ('kind? arg value-kind-const-string))
                             failfunc;
                         if ((unbox-string arg) != paramv)
                             failfunc;

          
@@ 6015,7 6018,7 @@ fn gen-sugar-matcher (failfunc expr scop
                                 sc_list_decons next
                     let check =
                         if ((('typeof exprT) == type) and (exprT == string))
-                            `(('kind arg) == value-kind-const-string)
+                            `('kind? arg value-kind-const-string)
                         else
                             `(('constant? arg) and (('typeof arg) == exprT))
                     let arg =

          
@@ 6623,7 6626,7 @@ spice overloaded-fn-append (T args...)
                 # separator for (using ...)
                 let fT = ('typeof f)
                 if ('function-pointer? fT)
-                    if ((('kind f) != value-kind-function)
+                    if ((not ('kind? f value-kind-function))
                         and (not ('constant? f)))
                         error "argument must be constant or function"
                     let fT = ('element@ fT 0)

          
A => lib/scopes/inspect.sc +7 -0
@@ 0,0 1,7 @@ 
+
+
+
+
+
+do
+    locals;

          
M src/globals.cpp +3 -3
@@ 2493,12 2493,12 @@ void init_globals(int argc, char *argv[]
     DEFINE_EXTERN_C_FUNCTION(sc_value_is_constant, TYPE_Bool, TYPE_ValueRef);
     DEFINE_EXTERN_C_FUNCTION(sc_value_is_pure, TYPE_Bool, TYPE_ValueRef);
     DEFINE_EXTERN_C_FUNCTION(sc_value_compare, TYPE_Bool, TYPE_ValueRef, TYPE_ValueRef);
-    DEFINE_EXTERN_C_FUNCTION(sc_value_kind, TYPE_I32, TYPE_ValueRef);
+    DEFINE_EXTERN_C_FUNCTION(sc_value_kind, TYPE_ValueKind, TYPE_ValueRef);
     DEFINE_EXTERN_C_FUNCTION(sc_value_block_depth, TYPE_I32, TYPE_ValueRef);
     DEFINE_EXTERN_C_FUNCTION(sc_identity, TYPE_ValueRef, TYPE_ValueRef);
     DEFINE_EXTERN_C_FUNCTION(sc_value_wrap, TYPE_ValueRef, TYPE_Type, TYPE_ValueRef);
     DEFINE_EXTERN_C_FUNCTION(sc_value_unwrap, TYPE_ValueRef, TYPE_Type, TYPE_ValueRef);
-    DEFINE_EXTERN_C_FUNCTION(sc_value_kind_string, TYPE_String, TYPE_I32);
+    DEFINE_EXTERN_C_FUNCTION(sc_value_kind_string, TYPE_String, TYPE_ValueKind);
 
     DEFINE_EXTERN_C_FUNCTION(sc_keyed_new, TYPE_ValueRef, TYPE_Symbol, TYPE_ValueRef);
     DEFINE_EXTERN_C_FUNCTION(sc_empty_argument_list, TYPE_ValueRef);

          
@@ 2814,7 2814,7 @@ B_TYPES()
 #undef T
 
 #define T(NAME, BNAME, CLASS) \
-    bind_new_value(Symbol(BNAME), ConstInt::from(TYPE_I32, (int32_t)NAME));
+    bind_new_value(Symbol(BNAME), ConstInt::from(TYPE_ValueKind, (int32_t)NAME));
     SCOPES_VALUE_KIND()
 #undef T
 

          
M src/type.cpp +1 -0
@@ 554,6 554,7 @@ void init_types() {
 
     DEFINE_BASIC_TYPE("Symbol", Symbol, TYPE_Symbol, TYPE_Immutable, TYPE_U64);
     DEFINE_BASIC_TYPE("Builtin", Builtin, TYPE_Builtin, nullptr, TYPE_U64);
+    DEFINE_BASIC_TYPE("ValueKind", ValueKind, TYPE_ValueKind, TYPE_CEnum, TYPE_I32);
 
     DEFINE_OPAQUE_HANDLE_TYPE("_Value", Value, TYPE__Value, nullptr);
 

          
M src/type.hpp +2 -0
@@ 116,6 116,8 @@ typedef std::vector<const Type *> Types;
     T(TYPE__Value, "_Value") \
     T(TYPE_ValueRef, "Value") \
     \
+    T(TYPE_ValueKind, "ValueKind") \
+    \
     T(TYPE_Bool, "bool") \
     \
     T(TYPE_I8, "i8") \

          
M testing/test_all.sc +1 -0
@@ 50,6 50,7 @@ test-modules
     .test_import
     .test_inline
     .test_inplace_arithmetic
+    .test_inspect
     .test_intrinsics
     .test_iter2
     .test_itertools

          
A => testing/test_inspect.sc +19 -0
@@ 0,0 1,19 @@ 
+
+using import testing
+import inspect
+
+# test introspection
+
+fn somefunc (a b)
+    if a
+        return 1
+    else
+        return b
+
+somefunc := `[(static-typify somefunc bool i32)]
+
+print ('kind somefunc)
+#print (('kind somefunc) == value-kind-const-string)
+
+
+;
  No newline at end of file