Skip to content

Commit

Permalink
support iterator type
Browse files Browse the repository at this point in the history
  • Loading branch information
yndu13 committed Nov 16, 2023
1 parent 8b5117b commit 4ea533c
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 1 deletion.
15 changes: 14 additions & 1 deletion lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -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)));
Expand Down Expand Up @@ -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);
Expand Down
40 changes: 40 additions & 0 deletions test/fixtures/iterator/Client.java
Original file line number Diff line number Diff line change
@@ -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<String> test1() throws Exception {
}

public Iterable<String> test2() throws Exception {
Iterable<String> it = this.test1();
ArrayList<String> _result = new ArrayList<String>();
for (String test : it) {
_result.add(test);
}
}

public String test3(Iterable<String> iter) throws Exception {
Iterable<String> it = iter;
String str = "";
ArrayList<String> _result = new ArrayList<String>();
for (String i : it) {
str = "" + str + ", " + i + "";
}
return str;
}

public void test4(Object test) throws Exception {
}

public void test5(Iterable<String> iter) throws Exception {
test3(iter);
this.test4(iter);
}
}
13 changes: 13 additions & 0 deletions test/fixtures/iterator/Darafile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"scope": "darabonba",
"name": "main",
"version": "0.0.1",
"main": "./main.dara",
"libraries": {},
"java": {
"package": "com.aliyun.main",
"className": "Client"
},
"releases": {
}
}
26 changes: 26 additions & 0 deletions test/fixtures/iterator/main.dara
Original file line number Diff line number Diff line change
@@ -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);
}
11 changes: 11 additions & 0 deletions test/main.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
});
});
});

0 comments on commit 4ea533c

Please sign in to comment.