Skip to content

Commit

Permalink
iterator goo
Browse files Browse the repository at this point in the history
  • Loading branch information
borisbat committed Aug 28, 2019
1 parent df9d27a commit 0ef5965
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 7 deletions.
43 changes: 37 additions & 6 deletions examples/test/hello_world.das
Original file line number Diff line number Diff line change
@@ -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<int&> = _builtin_make_iterator(arr,typeinfo(sizeof type int))
var ikey : int?
var pkey : void? = reinterpret<void?>(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<int const&> = keys(t)
var ikey : int?
var pkey : void? = reinterpret<void?>(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<string&> = values(t)
var sval : string?
var pval : void? = reinterpret<void?>(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
6 changes: 6 additions & 0 deletions include/daScript/simulate/aot_builtin.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
}
1 change: 1 addition & 0 deletions include/daScript/simulate/heap.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ namespace das {
case Type::tURange: return makeNode<NodeType<urange>>(args...);
case Type::tString: return makeNode<NodeType<char *>>(args...);
case Type::tPointer: return makeNode<NodeType<void *>>(args...);
case Type::tIterator: return makeNode<NodeType<void *>>(args...);
case Type::tFunction: return makeNode<NodeType<Func>>(args...);
case Type::tLambda: return makeNode<NodeType<Lambda>>(args...);
case Type::tDouble: return makeNode<NodeType<double>>(args...);
Expand Down
25 changes: 25 additions & 0 deletions src/builtin/module_builtin_runtime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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<CommentAnnotation>());
Expand All @@ -201,6 +220,12 @@ namespace das
addAnnotation(make_shared<NoAotFunctionAnnotation>());
addAnnotation(make_shared<InitFunctionAnnotation>());
addAnnotation(make_shared<HybridFunctionAnnotation>());
// iterator functions
addExtern<DAS_BIND_FUN(builtin_iterator_first)>(*this, lib, "_builtin_iterator_first", SideEffects::modifyExternal, "builtin_iterator_first");
addExtern<DAS_BIND_FUN(builtin_iterator_next)>(*this, lib, "_builtin_iterator_next", SideEffects::modifyExternal, "builtin_iterator_next");
addExtern<DAS_BIND_FUN(builtin_iterator_close)>(*this, lib, "_builtin_iterator_close", SideEffects::modifyExternal, "builtin_iterator_close");
// make-iterator functions
addExtern<DAS_BIND_FUN(builtin_make_iterator)>(*this, lib, "_builtin_make_iterator", SideEffects::modifyExternal, "builtin_make_iterator");
// functions
addExtern<DAS_BIND_FUN(builtin_throw)> (*this, lib, "panic", SideEffects::modifyExternal, "builtin_throw");
addExtern<DAS_BIND_FUN(builtin_print)> (*this, lib, "print", SideEffects::modifyExternal, "builtin_print");
Expand Down
1 change: 1 addition & 0 deletions src/simulate/data_walker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/simulate/debug_info.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 0ef5965

Please sign in to comment.