diff --git a/src/stdlib/ns/modular.es6 b/src/stdlib/ns/modular.es6 new file mode 100644 index 00000000..773be50e --- /dev/null +++ b/src/stdlib/ns/modular.es6 @@ -0,0 +1,10 @@ +// modular library written by Conor O'Bruie + +export default function(cheddar){ + // let modular = new cheddar.func(); + // modular.scope = new Map([ + + return cheddar.namespace([ + ["of", cheddar.from(require("./modular/of"))] + ]); +} diff --git a/src/stdlib/ns/modular/helpers/ModularClass.es6 b/src/stdlib/ns/modular/helpers/ModularClass.es6 new file mode 100644 index 00000000..ea05a386 --- /dev/null +++ b/src/stdlib/ns/modular/helpers/ModularClass.es6 @@ -0,0 +1,35 @@ +let importInstance; +export default function(cheddar){ + console.log("from ModularClass.es6") + console.log(cheddar); + if(importInstance) return importInstance; + + return importInstance = class ModularSystem extends cheddar.class { + init(min, max){ + this.setter("min", min); + this.accessor("min"); + this.setter("max", max); + this.accessor("max"); + } + + // MUST be capitalized, otherwise bork + Scope = new Map([ + ["fit", cheddar.var( + new cheddar.func([ + ["entry", { type: cheddar.number }] + ], function(scope, input){ + let self = input("self"), + value = input("entry"), + min = self.accessor("min"), + max = self.accessor("max"); + + while(value < this.min) value += this.max; + while(value >= this.max) value -= this.max; + return value; + }) + )] + ]); + } + + return ModularSystem; +}; diff --git a/src/stdlib/ns/modular/of.es6 b/src/stdlib/ns/modular/of.es6 new file mode 100644 index 00000000..04793864 --- /dev/null +++ b/src/stdlib/ns/modular/of.es6 @@ -0,0 +1,16 @@ +import Modular from "./helpers/ModularClass"; + +export default function(cheddar){ + return new cheddar.func( + [ + ["min", { type: cheddar.number }], + ["max", { type: cheddar.number }] + ], + function(cheddar, input){ + return new (Modular(cheddar))( + input("min"), + input("max") + ); + } + ); +} diff --git a/src/stdlib/stdlib.es6 b/src/stdlib/stdlib.es6 index ababd50e..eb1b904d 100644 --- a/src/stdlib/stdlib.es6 +++ b/src/stdlib/stdlib.es6 @@ -18,6 +18,7 @@ STDLIB.Item("cheddar"); STDLIB.Item("Math"); STDLIB.Item("Rational"); +STDLIB.Item("modular"); // Interface Libraries STDLIB.Item("Encoding");