Skip to content

Instantly share code, notes, and snippets.

@yumitsu
Created November 7, 2014 19:51
Show Gist options
  • Save yumitsu/3704887a92738453a8a0 to your computer and use it in GitHub Desktop.
Save yumitsu/3704887a92738453a8a0 to your computer and use it in GitHub Desktop.
Interfaces in JavaScript with Sweet.js
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