Skip to content

Commit

Permalink
fix the magic type in complex type
Browse files Browse the repository at this point in the history
  • Loading branch information
peze authored and JacksonTian committed Jun 26, 2024
1 parent a72d925 commit c9bbeda
Show file tree
Hide file tree
Showing 3 changed files with 142 additions and 8 deletions.
64 changes: 60 additions & 4 deletions lib/semantic.js
Original file line number Diff line number Diff line change
Expand Up @@ -1158,7 +1158,53 @@ class TypeChecker {
return paramMap;
}

checkMagicType(node) {
if (node.type === 'array') {
return this.checkMagicType(node.subType);
}

if (node.type === 'map') {
return this.checkMagicType(node.valueType);
}

if(node.type === 'entry') {
return this.checkMagicType(node.valueType);
}

if (node.tag === Tag.TYPE && node.lexeme === '$type') {
return true;
}

return false;
}

getMagicType(node, realType) {
if (node.type === 'array') {
return {
type: 'array',
itemType: this.getMagicType(node.subType, realType)
};
}

if (node.type === 'map') {
return {
type: 'map',
keyType: _type(node.keyType),
valueType: this.getMagicType(node.valueType, realType)
};
}

if(node.type === 'entry') {
return {
type: 'entry',
valueType: this.getMagicType(node.valueType, realType)
};
}

if (node.tag === Tag.TYPE && node.lexeme === '$type') {
return realType;
}
}

checkType(node) {
if (node.type === 'array') {
Expand All @@ -1171,6 +1217,11 @@ class TypeChecker {
return;
}

if (node.type === 'entry') {
this.checkType(node.valueType);
return;
}

if (node.tag === Tag.TYPE) {
this.usedTypes.set(node.lexeme, true);
return;
Expand Down Expand Up @@ -2277,9 +2328,9 @@ class TypeChecker {
}



if(definedApi.returnType.tag === Tag.TYPE && definedApi.returnType.lexeme === '$type') {
ast.inferred = this.getInstanceType(ast.left.id, env);
if(this.checkMagicType(definedApi.returnType)) {
const magicType = this.getInstanceType(ast.left.id, env);
ast.inferred = this.getMagicType(definedApi.returnType, magicType);
} else {
ast.inferred = this.getType(definedApi.returnType, moduleName);
}
Expand Down Expand Up @@ -2794,6 +2845,7 @@ class TypeChecker {
if (t.type === 'entry') {
if (t.valueType.type === 'subModel_or_moduleModel') {
this.checkType(t.valueType);

return {
type: 'entry',
valueType: t.valueType
Expand Down Expand Up @@ -3064,7 +3116,7 @@ class TypeChecker {
} else if (arrayField.tag === Tag.TYPE) {
// TODO
} else if (arrayField.type === 'map') {
// TODO
this.checkType(arrayField.valueType);
} else if (arrayField.type === 'subModel_or_moduleModel') {
this.checkType(arrayField);
} else if (arrayField.fieldType === 'array') {
Expand Down Expand Up @@ -3113,6 +3165,10 @@ class TypeChecker {
this.checkType(fieldValue.valueType);
}

if (fieldValue.fieldType === 'entry') {
this.checkType(fieldValue.valueType);
}

if (fieldValue.fieldType.type === 'subModel_or_moduleModel') {
this.checkType(fieldValue.fieldType);
}
Expand Down
6 changes: 4 additions & 2 deletions test/fixtures/map_model_assign/main.dara
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ model M{
}

model N{
n?: map[string]M
n?: map[string]M,
ns?: [map[string]M]
}

model A{
Expand All @@ -25,7 +26,8 @@ init(){
key = m2
};
var n = new N{
n = map2
n = map2,
ns = [map2],
};
var a = new A{
a = [
Expand Down
80 changes: 78 additions & 2 deletions test/semantic.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2400,6 +2400,16 @@ describe('semantic', function () {
}`, '__filename');
}).to.not.throwException();

expect(function () {
parse(`static function callOSS(): string {
var m = ['a', 'b', 'c'];
var arr: [ string ] = m.sort('acs');
return arr[0];
}`, '__filename');
}).to.not.throwException();

expect(function () {
parse(`static function callOSS(): map[string]string {
var m = {
Expand Down Expand Up @@ -2498,8 +2508,8 @@ describe('semantic', function () {
var m2 = {
key = new M2{ en = entries[0] }
};
entries = m2.entries();
return entries[0];
var entries2 = m2.entries();
return entries2[0];
}`, '__filename');
}).to.not.throwException();

Expand Down Expand Up @@ -6419,6 +6429,72 @@ describe('semantic', function () {
}).to.not.throwException();
});

it('assign when map value type which in array is model should ok', function () {
let ast = parse(`
model N {
config: string,
}
model M {
configs: [map[string]N]
}
static function main(m: M): void {
var config = m.configs;
}`, '__filename');
const value = ast.moduleBody.nodes[1].modelBody.nodes[0].fieldValue;
expect(value).to.eql({
'type': 'fieldType',
'fieldType': 'array',
'fieldItemType': {
'loc': {
'start': {
'line': 7,
'column': 19
},
'end': {
'line': 7,
'column': 31
}
},
'type': 'map',
'keyType': {
'tag': 8,
'loc': {
'start': {
'line': 7,
'column': 23
},
'end': {
'line': 7,
'column': 29
}
},
'lexeme': 'string',
'index': 17
},
'valueType': {
'tag': 2,
'loc': {
'start': {
'line': 7,
'column': 30
},
'end': {
'line': 7,
'column': 31
}
},
'lexeme': 'N',
'index': 19,
'idType': 'model'
}
}
});
});

it('map value type is model should ok', function () {
let ast = parse(`
model M {
Expand Down

0 comments on commit c9bbeda

Please sign in to comment.