Differences From Artifact [b35cfd66e862f7bf]:
- File
sample/type.pmy
- 2010-11-21 16:05:20 - part of checkin [da7559b744] on branch trunk - fizzbuzz sample changed (user: kinaba) [annotate]
To Artifact [f80a12af3228ea33]:
- File
sample/type.pmy
- 2010-11-24 12:14:00 - part of checkin [3ae09b8cbf] on branch trunk - changed if-then-else syntax (user: kinaba) [annotate]
1 @@type = fun(x){ 1 @@type = fun(x){
2 if( _isint(x) ) { "int" } | 2 if _isint(x): "int"
3 else { if( _isstr(x) ) { "str" } | 3 else if _isstr(x): "str"
4 else { if( _isundefined(x) ) { "undefined" } | 4 else if _isundefined(x): "undefined"
5 else { "any" }}} | 5 else: "any"
6 }; 6 };
7 7
8 def binop(a,b,c) { 8 def binop(a,b,c) {
9 fun(x,y){@value( 9 fun(x,y){@value(
10 if( @type(x)=="undefined" || @type(y)=="undefined" ) { "undefined" } else { | 10 if( @type(x)=="undefined" || @type(y)=="undefined" ) then "undefined" else
11 if( @type(x)==a && @type(y)==b ) { c } else { "error" } | 11 if( @type(x)==a && @type(y)==b ) then c else "error"
12 } <
13 )} 12 )}
14 }; 13 };
15 14
16 @type "+" = binop("int", "int", "int"); 15 @type "+" = binop("int", "int", "int");
17 @type "-" = binop("int", "int", "int"); 16 @type "-" = binop("int", "int", "int");
18 @type "<" = binop("int", "int", "int"); 17 @type "<" = binop("int", "int", "int");
19 @type ">" = binop("int", "int", "int"); 18 @type ">" = binop("int", "int", "int");
20 19
21 def mergeType(a,b) { 20 def mergeType(a,b) {
22 if( a == "undefined" ) { if(b=="undefined"){"error"}else{b} } else { a } | 21 if( a == "undefined" ): ( if(b=="undefined"):"error" else b ) else ( a )
23 }; 22 };
24 23
25 @type "if" = fun(c,t,e) {@value( 24 @type "if" = fun(c,t,e) {@value(
26 if(@type(c)=="int" ) { mergeType(@type(t()), @type(e())) } else { "error" } | 25 if(@type(c)=="int" ): mergeType(@type(t()), @type(e())) else : "error"
27 )}; 26 )};