@@ -62,6 +62,7 @@ heavy::ExternFunction parent_op;
6262heavy::ExternFunction op_next;
6363heavy::ExternFunction verify;
6464heavy::ExternFunction module_lookup;
65+ heavy::ExternFunction is_value;
6566}
6667
6768using namespace heavy ::mlir_helper;
@@ -533,7 +534,7 @@ void type(Context& C, ValueRefs Args) {
533534 mlir::MLIRContext* MLIRContext = getCurrentContext (C);
534535 llvm::StringRef TypeStr = Args[0 ].getStringRef ();
535536 if (TypeStr.empty ())
536- return C.RaiseError (" expecting string" );
537+ return C.RaiseError (" expecting string with type " );
537538
538539 mlir::Type Type = mlir::parseType (TypeStr, MLIRContext,
539540 nullptr , heavy::String::IsNullTerminated);
@@ -652,7 +653,7 @@ void string_attr(Context& C, ValueRefs Args) {
652653 return C.RaiseError (" invalid arity" );
653654 if (!isa<heavy::String, heavy::Symbol>(Args[0 ]))
654655 return C.RaiseError (" expecting string-like object" );
655- llvm::StringRef Str = Args[0 ].getStringRef ();
656+ llvm::StringRef Str = Args[0 ].getStringRef ();
656657 mlir::MLIRContext* MLIRContext = getCurrentContext (C);
657658 mlir::Attribute Attr = AttrTy::get (MLIRContext, Str);
658659 C.Cont (C.CreateAny (Attr));
@@ -801,6 +802,35 @@ void module_lookup(Context& C, heavy::ValueRefs Args) {
801802 C.Cont (Result);
802803}
803804
805+ // (value? obj)
806+ // (value? _typestr_ obj)
807+ // (value? mlir.type obj)
808+ void is_value (Context& C, ValueRefs Args) {
809+ if (Args.size () < 1 || Args.size () > 2 )
810+ return C.RaiseError (" invalid arity" );
811+
812+ mlir::MLIRContext* MLIRContext = getCurrentContext (C);
813+ mlir::Type Type;
814+
815+ if (Args.size () == 2 ) {
816+ if (isa<String, Symbol>(Args[0 ])) {
817+ llvm::StringRef TypeStr = Args[0 ].getStringRef ();
818+ Type = mlir::parseType (TypeStr, MLIRContext,
819+ nullptr , heavy::String::IsNullTerminated);
820+ } else {
821+ Type = any_cast<mlir::Type>(Args[0 ]);
822+ }
823+
824+ if (!Type)
825+ return C.RaiseError (" expecting a mlir.type" );
826+ Args = Args.drop_front ();
827+ }
828+
829+ mlir::Value Value = any_cast<mlir::Value>(Args[0 ]);
830+ bool Result = Value && (!Type || Value.getType () == Type);
831+ C.Cont (heavy::Bool (Result));
832+ }
833+
804834} // namespace heavy::mlir_bind
805835
806836extern " C" {
@@ -839,6 +869,7 @@ void HEAVY_MLIR_INIT(heavy::Context& C) {
839869 HEAVY_MLIR_VAR (with_new_context) = heavy::mlir_bind::with_new_context;
840870 HEAVY_MLIR_VAR (verify) = heavy::mlir_bind::verify;
841871 HEAVY_MLIR_VAR (module_lookup) = heavy::mlir_bind::module_lookup;
872+ HEAVY_MLIR_VAR (is_value) = heavy::mlir_bind::is_value;
842873}
843874
844875void HEAVY_MLIR_LOAD_MODULE (heavy::Context& C) {
@@ -871,6 +902,7 @@ void HEAVY_MLIR_LOAD_MODULE(heavy::Context& C) {
871902 {" load-dialect" , HEAVY_MLIR_VAR (load_dialect)},
872903 {" verify" , HEAVY_MLIR_VAR (verify)},
873904 {" module-lookup" , HEAVY_MLIR_VAR (module_lookup)},
905+ {" value?" , HEAVY_MLIR_VAR (is_value)},
874906 });
875907}
876908}
0 commit comments