diff --git a/lib/generator.js b/lib/generator.js index 159786f..3cef2af 100644 --- a/lib/generator.js +++ b/lib/generator.js @@ -176,8 +176,15 @@ class Visitor { visitFor(ast, level) { assert.equal(ast.type, 'for'); + if (ast.list.inferred.type === 'iterator') { + this.emit(`ArrayList<`, level); + this.visitType(ast.list.inferred.valueType); + this.emit(`> _result = new ArrayList<`); + this.visitType(ast.list.inferred.valueType); + this.emit(`>();\n`); + } this.emit(`for (`, level); - this.visitType(ast.list.inferred.itemType); + this.visitType(ast.list.inferred.itemType || ast.list.inferred.valueType); this.emit(` ${_name(ast.id)} : `); this.visitExpr(ast.list, level + 1); this.emit(') {\n'); @@ -524,6 +531,8 @@ class Visitor { } else if (ast.type === 'module_instance') { let className = this.imports[_name(ast)].className || 'Client'; this.emit(`${this.imports[_name(ast)].package}.${className}`); + } else if (ast.type === 'iterator') { + this.emit(`Iterable<${_type(ast.valueType.lexeme || ast.valueType.name)}>`); } else { if (isSubType) { this.emit(collectionType(_type(ast.lexeme || ast.name))); @@ -601,6 +610,10 @@ class Visitor { this.visitFor(ast, level); } else if (ast.type === 'try') { this.visitTry(ast, level); + } else if (ast.type === 'yield') { + this.emit(`_result.add(`, level); + this.visitExpr(ast.expr, level); + this.emit(`);\n`); } else { this.emit(``, level); this.visitExpr(ast, level); diff --git a/test/fixtures/iterator/Client.java b/test/fixtures/iterator/Client.java new file mode 100644 index 0000000..35b72d0 --- /dev/null +++ b/test/fixtures/iterator/Client.java @@ -0,0 +1,40 @@ +// This file is auto-generated, don't edit it. Thanks. +package com.aliyun.main; + +import com.aliyun.tea.*; + +public class Client { + + public Client() throws Exception { + } + + + public Iterable test1() throws Exception { + } + + public Iterable test2() throws Exception { + Iterable it = this.test1(); + ArrayList _result = new ArrayList(); + for (String test : it) { + _result.add(test); + } + } + + public String test3(Iterable iter) throws Exception { + Iterable it = iter; + String str = ""; + ArrayList _result = new ArrayList(); + for (String i : it) { + str = "" + str + ", " + i + ""; + } + return str; + } + + public void test4(Object test) throws Exception { + } + + public void test5(Iterable iter) throws Exception { + test3(iter); + this.test4(iter); + } +} diff --git a/test/fixtures/iterator/Darafile b/test/fixtures/iterator/Darafile new file mode 100644 index 0000000..37cd623 --- /dev/null +++ b/test/fixtures/iterator/Darafile @@ -0,0 +1,13 @@ +{ + "scope": "darabonba", + "name": "main", + "version": "0.0.1", + "main": "./main.dara", + "libraries": {}, + "java": { + "package": "com.aliyun.main", + "className": "Client" + }, + "releases": { + } +} diff --git a/test/fixtures/iterator/main.dara b/test/fixtures/iterator/main.dara new file mode 100644 index 0000000..5ef14a4 --- /dev/null +++ b/test/fixtures/iterator/main.dara @@ -0,0 +1,26 @@ +init(){} + +async function test1(): iterator[string]; +async function test2(): iterator[string]{ + var it:iterator[string] = test1(); + for(var test : it) { + yield test; + } +} + +async function test3(iter: iterator[string]): string{ + var it = iter; + var str : string = ''; + for (var i : it) { + str = `${str}, ${i}`; + } + return str; +} + +async function test4(test: any): void{ +} + +async function test5(iter: iterator[string]): void{ + // test3(iter); + test4(iter); +} \ No newline at end of file diff --git a/test/main.test.js b/test/main.test.js index 81d8695..f60ba74 100644 --- a/test/main.test.js +++ b/test/main.test.js @@ -232,4 +232,15 @@ describe('new Generator', function () { await msleep(500); assert.deepStrictEqual(fs.readFileSync(clientPath, 'utf8'), expected); }); + + it('iterator should ok', function () { + const outputDir = path.join(__dirname, 'output/iterator'); + const mainFilePath = path.join(__dirname, 'fixtures/iterator/main.dara'); + const pkgContent = fs.readFileSync(path.join(__dirname, 'fixtures/iterator/Darafile'), 'utf8'); + const pkg = JSON.parse(pkgContent); + check(mainFilePath, outputDir, path.join(__dirname, 'fixtures/iterator/Client.java'), 'src/main/java/com/aliyun/test/Client.java', { + pkgDir: path.join(__dirname, 'fixtures/iterator'), + ...pkg + }); + }); }); \ No newline at end of file