From 0ef5965f97c9d9ae76c78666544934692c124cea Mon Sep 17 00:00:00 2001 From: Boris Batkin Date: Wed, 28 Aug 2019 14:11:18 -0700 Subject: [PATCH] iterator goo --- examples/test/hello_world.das | 43 +++++++++++++++++++++---- include/daScript/simulate/aot_builtin.h | 6 ++++ include/daScript/simulate/heap.h | 1 + src/builtin/module_builtin_runtime.cpp | 25 ++++++++++++++ src/simulate/data_walker.cpp | 1 + src/simulate/debug_info.cpp | 2 +- 6 files changed, 71 insertions(+), 7 deletions(-) diff --git a/examples/test/hello_world.das b/examples/test/hello_world.das index 909595d15..4b2e77e84 100644 --- a/examples/test/hello_world.das +++ b/examples/test/hello_world.das @@ -1,12 +1,43 @@ -require math +options log=true, logStack=true, logNodes=true -options logCpp=true +[unsafe,sideeffects] +def test_good_array + let arr <- toarray([[int 1;2;3;4]]) + let ait : iterator = _builtin_make_iterator(arr,typeinfo(sizeof type int)) + var ikey : int? + var pkey : void? = reinterpret(addr(ikey)) + var loop = _builtin_iterator_first(ait,pkey) + while loop + print("*ait = {deref(ikey)}\n") + loop = _builtin_iterator_next(ait,pkey) + _builtin_iterator_close(ait,pkey) -[sideeffects] -def cf(a) - return ceili(a) +[unsafe,sideeffects] +def test_keys_values + let t <- totable([[auto 1=>"one"; 2=>"two"; 3=>"three"]]) + for k,v in keys(t),values(t) + print("{k} => {v}\n") + if true + let itk : iterator = keys(t) + var ikey : int? + var pkey : void? = reinterpret(addr(ikey)) + var loop = _builtin_iterator_first(itk,pkey) + while loop + print("*ikey = {deref(ikey)}\n") + loop = _builtin_iterator_next(itk,pkey) + _builtin_iterator_close(itk,pkey) + if true + let itv : iterator = values(t) + var sval : string? + var pval : void? = reinterpret(addr(sval)) + var loop = _builtin_iterator_first(itv,pval) + while loop + print("itv = {deref(sval)}\n") + loop = _builtin_iterator_next(itv,pval) + _builtin_iterator_close(itv,pval) [export] def test - print("ceili(1.2)={cf(1.2)}") + test_good_array() + test_keys_values() return true diff --git a/include/daScript/simulate/aot_builtin.h b/include/daScript/simulate/aot_builtin.h index ee29a0fd8..076c26b0b 100644 --- a/include/daScript/simulate/aot_builtin.h +++ b/include/daScript/simulate/aot_builtin.h @@ -22,4 +22,10 @@ namespace das { int builtin_array_push ( Array & pArray, int index, int stride, Context * context ); void builtin_array_erase ( Array & pArray, int index, int stride, Context * context ) ; void builtin_array_clear ( Array & pArray, Context * context ); + + bool builtin_iterator_first ( Iterator * it, void * data, Context * context ); + bool builtin_iterator_next ( Iterator * it, void * data, Context * context ); + void builtin_iterator_close ( Iterator * it, void * data, Context * context ); + + Iterator * builtin_make_iterator ( const Array & arr, int stride, Context * context ); } diff --git a/include/daScript/simulate/heap.h b/include/daScript/simulate/heap.h index 21bdea0ad..f3dc62d36 100644 --- a/include/daScript/simulate/heap.h +++ b/include/daScript/simulate/heap.h @@ -170,6 +170,7 @@ namespace das { case Type::tURange: return makeNode>(args...); case Type::tString: return makeNode>(args...); case Type::tPointer: return makeNode>(args...); + case Type::tIterator: return makeNode>(args...); case Type::tFunction: return makeNode>(args...); case Type::tLambda: return makeNode>(args...); case Type::tDouble: return makeNode>(args...); diff --git a/src/builtin/module_builtin_runtime.cpp b/src/builtin/module_builtin_runtime.cpp index b7e17e1e6..64a4ce623 100644 --- a/src/builtin/module_builtin_runtime.cpp +++ b/src/builtin/module_builtin_runtime.cpp @@ -7,6 +7,7 @@ #include "daScript/simulate/runtime_profile.h" #include "daScript/simulate/hash.h" #include "daScript/simulate/bin_serializer.h" +#include "daScript/simulate/runtime_array.h" namespace das { @@ -189,6 +190,24 @@ namespace das table_unlock(*context, arr); } + bool builtin_iterator_first ( Iterator * it, void * data, Context * context ) { + return it->first(*context, (char *)data); + } + + bool builtin_iterator_next ( Iterator * it, void * data, Context * context ) { + return it->next(*context, (char *)data); + } + + void builtin_iterator_close ( Iterator * it, void * data, Context * context ) { + it->close(*context, (char *)&data); + } + + Iterator * builtin_make_iterator ( const Array & arr, int stride, Context * context ) { + char * iter = context->heap.allocate(sizeof(GoodArrayIterator)); + new (iter) GoodArrayIterator((Array *)&arr, stride); + return (Iterator *) iter; + } + void Module_BuiltIn::addRuntime(ModuleLibrary & lib) { // function annotations addAnnotation(make_shared()); @@ -201,6 +220,12 @@ namespace das addAnnotation(make_shared()); addAnnotation(make_shared()); addAnnotation(make_shared()); + // iterator functions + addExtern(*this, lib, "_builtin_iterator_first", SideEffects::modifyExternal, "builtin_iterator_first"); + addExtern(*this, lib, "_builtin_iterator_next", SideEffects::modifyExternal, "builtin_iterator_next"); + addExtern(*this, lib, "_builtin_iterator_close", SideEffects::modifyExternal, "builtin_iterator_close"); + // make-iterator functions + addExtern(*this, lib, "_builtin_make_iterator", SideEffects::modifyExternal, "builtin_make_iterator"); // functions addExtern (*this, lib, "panic", SideEffects::modifyExternal, "builtin_throw"); addExtern (*this, lib, "print", SideEffects::modifyExternal, "builtin_print"); diff --git a/src/simulate/data_walker.cpp b/src/simulate/data_walker.cpp index e86696eaa..f1dcfd84e 100644 --- a/src/simulate/data_walker.cpp +++ b/src/simulate/data_walker.cpp @@ -195,6 +195,7 @@ namespace das { if ( cancel ) return; afterHandle(pa, info); break; + case Type::tVoid: break; // skip void default: DAS_ASSERTF(0, "unsupported print type"); break; } } diff --git a/src/simulate/debug_info.cpp b/src/simulate/debug_info.cpp index 59e3005df..faf8b3270 100644 --- a/src/simulate/debug_info.cpp +++ b/src/simulate/debug_info.cpp @@ -358,7 +358,7 @@ namespace das } else if ( info->type==Type::tTable ) { stream << "Table<" << debug_type(info->firstType) << "," << debug_type(info->secondType) << ">"; } else if ( info->type==Type::tIterator ) { - stream << "Iterator<" << debug_type(info->firstType) << ">"; + stream << "Iterator<" << (info->firstType ? debug_type(info->firstType) : "") << ">"; } else { stream << das_to_string(info->type); }