Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Apr 30, 2018. It is now read-only.

Commit3115643

Browse files
committed
Add automatic -ish normalization; avoid double-annotation.
1 parentb89238a commit3115643

File tree

4 files changed

+45
-25
lines changed

4 files changed

+45
-25
lines changed

‎src/func.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import{assert,typeError,validateType,wrap,flowToAsm}from'./util';
1+
import{assert,typeError,validateType,getWrap,replaceWrap,flowToAsm,unish}from'./util';
22
import{Fixnum,Unsigned,Double,Arrow,Overloaded,Int,Intish,Str}from'./types';
33
import{UNOPS,BINOPS}from'./tables';
44

@@ -23,6 +23,7 @@ var funcVisitor = {
2323
for(lettypeofopTypes){
2424
if(argType.subtype(type.params[0])){
2525
this.setData('asmType',type.result);
26+
this::unish();
2627
return;
2728
}
2829
}
@@ -38,6 +39,7 @@ var funcVisitor = {
3839
for(lettypeofopTypes){
3940
if(leftType.subtype(type.params[0])&&rightType.subtype(type.params[1])){
4041
this.setData('asmType',type.result);
42+
this::unish();
4143
return;
4244
}
4345
}
@@ -98,7 +100,7 @@ var funcVisitor = {
98100
node
99101
];
100102
}
101-
node.argument=this.get('argument')::wrap(state.returnType,true);
103+
this.get('argument')::replaceWrap(state.returnType,true);
102104
}
103105
},
104106

@@ -140,8 +142,7 @@ var funcVisitor = {
140142
AssignmentExpression:{
141143
exit:functionAssignmentExpression(node,parent,scope,state){
142144
varasmType=scope.getBinding(node.left.name).path.getData('asmType');
143-
varright=this.get('right');
144-
right.replaceWith(right::wrap(asmType));
145+
this.get('right')::replaceWrap(asmType);
145146
}
146147
},
147148

@@ -150,16 +151,15 @@ var funcVisitor = {
150151
varcallee=this.get('callee');
151152
callee::assert(callee.node.type==='Identifier','only calls to direct identifiers are possible');
152153
varresultType=callee.getData('asmType').result;
153-
this.setData('asmType',resultType);
154-
this.replaceWith(this::wrap(resultType,true));
154+
this::replaceWrap(resultType,true)::unish();
155155
}
156156
},
157157

158158
TypeCastExpression:{
159159
exit:functionTypeCastExpression(node){
160160
varasmType=this.get('typeAnnotation')::flowToAsm();
161161
this.setData('asmType',asmType);
162-
returnthis.get('expression')::wrap(asmType);
162+
returnthis.get('expression')::getWrap(asmType);
163163
}
164164
}
165165
};
@@ -183,7 +183,7 @@ export default function visit(programState) {
183183
returnprogramState.t.expressionStatement(programState.t.assignmentExpression(
184184
'=',
185185
node,
186-
param::wrap(asmType,true)
186+
param::getWrap(asmType)
187187
));
188188
});
189189
varreturnType=this.get('returnType')::flowToAsm();

‎src/util.js

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import{STDLIB_TYPES,HEAP_VIEW_TYPES}from'./tables';
2-
import{Int,Double,Float,Extern,Str}from'./types';
2+
import{Int,Double,Float,Extern,Str,Intish,Floatish}from'./types';
33

44
exportconstGLOBALS=newMap([...STDLIB_TYPES.entries(), ...HEAP_VIEW_TYPES.entries()]);
55

@@ -35,44 +35,64 @@ export function flowToAsm() {
3535
returnflowToAsmMappings[type];
3636
}
3737

38-
exportfunctionwrap(type,force){
38+
exportfunctiongetWrap(type){
3939
var{node}=this;
40-
if(!force){
41-
letasmType=this.getData('asmType');
42-
if(asmType.subtype(type)){
43-
returnnode;
44-
}
45-
}
4640
if(type.subtype(Int)){
47-
return{
41+
returnnode._wrapFor===Int ?node :{
4842
type:'BinaryExpression',
4943
left:node,
5044
operator:'|',
51-
right:{type:'Literal',value:0}
45+
right:{type:'Literal',value:0},
46+
_wrapFor:Int
5247
};
5348
}
5449
if(type.subtype(Double)){
55-
return{
50+
returnnode._wrapFor===Double ?node :{
5651
type:'UnaryExpression',
5752
operator:'+',
58-
argument:node
53+
argument:node,
54+
_wrapFor:Double
5955
};
6056
}
6157
if(type.subtype(Float)){
62-
return{
58+
returnnode._wrapFor===Float ?node :{
6359
type:'CallExpression',
6460
callee:{
6561
type:'MemberExpression',
6662
object:{type:'Identifier',name:'Math'},
6763
property:{type:'Identifier',name:'fround'}
6864
},
69-
arguments:[node]
65+
arguments:[node],
66+
_wrapFor:Float
7067
};
7168
}
7269
if(type.subtype(Extern)){
7370
returnnode;
7471
}
75-
this::typeError(`can\'t wrap into type${type}`);
72+
this::typeError(`cannot wrap into type${type}`);
73+
}
74+
75+
exportfunctionreplaceWrap(type,force){
76+
if(!force){
77+
letasmType=this.getData('asmType');
78+
if(asmType&&asmType.subtype(type)){
79+
returnthis;
80+
}
81+
}
82+
this.replaceWith(this::getWrap(type));
83+
this.setData('asmType',type);
84+
returnthis;
85+
}
86+
87+
exportfunctionunish(){
88+
varasmType=this.getData('asmType');
89+
if(asmType.equals(Intish)){
90+
returnthis::replaceWrap(Int);
91+
}
92+
if(asmType.equals(Floatish)){
93+
returnthis::replaceWrap(Float);
94+
}
95+
returnthis;
7696
}
7797

7898
exportfunctionvalidateType(path,expectedType){

‎test/fixtures/actual.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ function logSum(start: int, end: int): double {
2222

2323
exportfunctiongeometricMean(start:int,end:int):double{
2424
console.log(start,end);
25-
returnMath.exp(logSum(start,end)/((end-start:int):double));
25+
returnMath.exp(logSum(start,end)/(end-start:double));
2626
}

‎test/fixtures/expected.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ function asm(stdlib, foreign, heap) {
5353
end=end|0;
5454

5555
_console$log_func(start,end);
56-
return++_Math$exp(+logSum(start,end)/+(end-start|0));
56+
return+_Math$exp(+logSum(start,end)/+(end-start|0));
5757
}
5858
return{
5959
geometricMean:geometricMean

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp