Skip to content

Commit

Permalink
add instance moduleType
Browse files Browse the repository at this point in the history
  • Loading branch information
peze authored and yndu13 committed Jan 6, 2025
1 parent 15fb6ff commit 2496d04
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 32 deletions.
54 changes: 35 additions & 19 deletions lib/semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -2369,27 +2369,43 @@ class TypeChecker {

visitInstanceCall(ast, env) {
assert.equal(ast.left.type, 'instance_call');
let moduleName;
if (ast.left.id.type === Tag.ID) {
const moduleType = env.local.get(ast.left.id.lexeme);
moduleName = moduleType.name;
} else {
const type = this.getVariableType(ast.left.id, env);
const type = this.getVariableType(ast.left.id, env);
let moduleName = type.name;
if(isBasicType(type.type)) {
moduleName = this.getBuiltinModule(type.type);
ast.builtinModule = moduleName;
ast.left.id.moduleType = {
type: 'basic',
name: type.type,
};
} else if(type.type === 'basic' && isBasicType(type.name)) {
moduleName = this.getBuiltinModule(type.name);
ast.builtinModule = moduleName;
ast.left.id.moduleType = {
type: 'basic',
name: type.name,
};
} else if(type.type === 'model') {
moduleName = '$ModelInstance';
ast.builtinModule = moduleName;
ast.left.id.moduleType = {
type: 'model',
name: type.name,
};
} else if(builtin.has(moduleName)) {
ast.builtinModule = moduleName;
ast.left.id.moduleType = {
type: 'builtin_module',
name: moduleName,
};
} else if (ast.left.id.tag === Tag.ID) {
moduleName = type.name;
if(isBasicType(type.type)) {
moduleName = this.getBuiltinModule(type.type);
ast.builtinModule = moduleName;
} else if(type.type === 'basic' && isBasicType(type.name)) {
moduleName = this.getBuiltinModule(type.name);
ast.builtinModule = moduleName;
} else if(type.type === 'model') {
moduleName = '$ModelInstance';
ast.builtinModule = moduleName;
} else if(builtin.has(moduleName)) {
ast.builtinModule = moduleName;
}
ast.left.id.moduleType = {
type: 'module',
name: moduleName
};
}

const checker = this.getChecker(moduleName);
const method = ast.left.propertyPath[0];
const name = method.lexeme;
Expand Down
4 changes: 3 additions & 1 deletion test/fixtures/module_instance/main.dara
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import OSS

static function call(): OSS {
return new OSS();
var oss = new OSS();
oss.putObject();
return oss;
}
36 changes: 24 additions & 12 deletions test/import.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,29 +355,41 @@ function callOSS(): string {

it('module instance should ok', function () {
const ast = readAndParse('fixtures/module_instance/main.dara');
console.log('%j', ast);
const [f1] = ast.moduleBody.nodes;
const [s1] = f1.functionBody.stmts.stmts;
expect(s1).to.be.eql({
'expectedType': undefined,
'type': 'declare',
'id': {
'tag': 2,
'loc': loc(4, 7, 4 ,10),
'lexeme': 'oss',
'index': 12
},
'expr': {
'type': 'construct',
'aliasId': {
'tag': 2,
'loc': loc(4, 17, 4 ,20),
'lexeme': 'OSS',
'index': 13,
'loc': loc(4, 14, 4, 17),
'tag': 2
'index': 15
},
'args': [],
'tokenRange': [
14,
18
],
'inferred': {
'type': 'module_instance',
'name': 'OSS',
'parentModuleIds': [],
'type': 'module_instance'
},
'tokenRange': [12, 16],
'type': 'construct'
'parentModuleIds': []
}
},
'loc': loc(4, 3, 5, 1),
'needCast': false,
'tokenRange': [11, 16],
'type': 'return'
'tokenRange': [
11,
18
]
});
});
});
26 changes: 26 additions & 0 deletions test/semantic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7617,4 +7617,30 @@ describe('semantic', function () {
readAndParse('fixtures/multi_catch/main.dara');
}).to.not.throwException();
});

it('module instance_call left type shoule be ok', function(){
let ast = readAndParse('fixtures/module_instance/main.dara');
const [f1] = ast.moduleBody.nodes;
const [, s2] = f1.functionBody.stmts.stmts;
expect(s2.left.id).to.be.eql({
'tag': 2,
'loc': {
'start': {
'line': 5,
'column': 3
},
'end': {
'line': 5,
'column': 6
}
},
'lexeme': 'oss',
'index': 19,
'type': 'variable',
'moduleType': {
'type': 'module',
'name': 'OSS'
}
});
});
});

0 comments on commit 2496d04

Please sign in to comment.