syntax and operator are built into the compiler
I pasted this:
function Monad(z = 'default', ID = 'tempMonad') {
this.x = z;
this.id = ID;
};
Monad.prototype.bnd = function (func, ...args) {
var _this = this;
if (func instanceof Promise) {
_this.getVal(func,this.x,args);
}
var m = func(this.x, ...args)
var ID;
if (m instanceof Monad) {
ID = testPrefix(args, this.id);
_this[ID] = new Monad(m.x, ID);
return _this[ID];
}
else return m;
};
Monad.prototype.ret = function (a) {
return window[this.id] = new Monad(a, this.id);
};
var mM1 = new Monad (1, 'mM1');
var mM2 = new Monad (2, 'mM2');
var mM3 = new Monad (3, 'mM3');
var cube = x => xxx;
var square = x => x*x;
var add = a => b => a+b;
console.log(mM1.bnd(mM2.ret))
operator >>= left 1 = (left, right) => {
return #${left}.bnd(${right});
};
mM3 >>= cube >>= add(3) >>= square
I pasted this:
function Monad(z) {
this.x = z;
this.bnd = function (func, ...args) {
return func(this.x, ...args)
}
this.ret = function (val = 0) {
return new Monad(val);
}
};
function ret (v) {
return new Monad(v);
}
var cube = x => ret(xxx);
var square = x => ret(x*x);
var add = a => b => ret(a+b);
operator >>= left 1 = (left, right) => {
return #${left}.bnd(${right});
};
ret(3) >>= cube >>= add(3) >>= square
and I got this:
function Monad(z_62) {
this.x = z_62;
this.bnd = function (func_63, ...args_64) {
return func_63(this.x, ...args_64);
};
this.ret = function (val_65 = 0) {
return new Monad(val_65);
};
}
function ret(v_66) {
return new Monad(v_66);
}
var cube_55 = (x_67) => ret(x_67 x_67 x_67);
var square_56 = (x_68) => ret(x_68 * x_68);
var add_57 = (a_69) => (b_70) => ret(a_69 + b_70);
ret(3).bnd(cube_55).bnd(add_57(3)).bnd(square_56);
This is exactly what I wanted. ret(3) >>= cube >>= add(3) >>= square
compiled to ret(3).bnd(cube_55).bnd(add_57(3)).bnd(square_56). This should
evaluate to an instance of Monad with value 900.
enable // this is the macro
// your macro finds an expression (log) and looks for $
// the you can transform the following tokens (1 and 2)
log $ 1 2
// a guard identifier used to end consumption of the token tree
// or you could let the macro work to the end of the enclosing block
// if not at the top level
disable
enable {
// only handle the code between the delimiters
}
{
enable
// ...
}
Hi all...
I was trying to get the following syntax to work:
function hello(a:integer:>10, b:string:not-empty) {
const myString = a + B
return:string:not-empty myString
}to convert to
function hello(a, b) {
if (!Number.isInteger(a)) throw new Error('a must be an integer')
if (!(a > 10)) throw new Error('a must be > 10')
if (typeof b !== 'string') throw new Error('b must be a string')
const myString = a + b
const __returnValue = myString
if (typeof __returnValue !== 'string') throw new Error('return value must be a string')
if (__returnValue === '') throw new Error('return value can not be an empty string')
return:string:not-empty myString
}I've tried for days I just can't get it to work
Very noobie to SweetJS and having issues with doing something that I think should be fairly simple. Tutorial is not being very helpful because I'm getting odd errors about things not being defined.
Here's what I'm trying to do:
syntax req = function(ctx) {
const moduleName = ctx.next().value
return #`const ${moduleName} = require('${moduleName}')`
}
req someModule
// output:
const someModule_4 = require("${moduleName}");
return #`const ${moduleName} = require(${fromStringLiteral(moduleName)})`
Output:
evalmachine.<anonymous>:6
return syntaxTemplate(1, moduleName_2, fromStringLiteral(moduleName_2));
^
ReferenceError: fromStringLiteral is not defined
at evalmachine.<anonymous>:6:30
import { fromStringLiteral } from '@sweet-js/helpers' for syntax