Created
November 7, 2014 19:51
-
-
Save yumitsu/3704887a92738453a8a0 to your computer and use it in GitHub Desktop.
Interfaces in JavaScript with Sweet.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
macro if_t { | |
case { _ $v } => { | |
letstx $vv = [makeValue(#{$v}.map(unwrapSyntax).join(''), #{here})] | |
return #{$vv} | |
} | |
} | |
macroclass iclass { | |
pattern { | |
rule { $type:if_t $vn:if_t } | |
} | |
} | |
macro interface { | |
rule { | |
$iname $body:iclass ... | |
} => { | |
var $iname = { | |
included: function() { | |
var validations = [], e; | |
$(validations.push({'name': $body$vn, 'type': $body$type});) ... | |
for(e in validations) { | |
var check = validations[e]; | |
var cname = check['name'], ctype = check['type']; | |
if(this.prototype[cname] !== void(0)) { | |
if(ctype != 'var') { | |
if(typeof(this.prototype[cname]) != ctype) { | |
throw new Error('Class doesn\'t implements '+if_t $iname+' interface'); | |
} | |
} | |
} else { | |
throw new Error('Class doesn\'t implements '+if_t $iname+' interface'); | |
} | |
} | |
} | |
}; | |
} | |
} | |
interface Test | |
var foz | |
var baz | |
object obj | |
function test |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment