uniform-team / Uniform-Validation-Language

A logic language to simplify and improve online form validation.
Apache License 2.0
1 stars 0 forks source link

Add evaluator #25

Closed dgp1130 closed 8 years ago

dgp1130 commented 8 years ago

Next step in reimplementing the language is to add the evaluator. It performs all the actual operations necessary to compute the script given. The structure of it worked surprisingly well last time, with all evaluator functions working as bindings. Instead of taking in inputs, operating on them, and returning the result, it takes functions which return the inputs and returns a function that performs the operation.

Instead of:

function add(left, right) {
    return left + right;
}

Evaluator does:

function add(leftExpr, rightExpr) {
    return function () {
        return leftExpr() + rightExpr();
    };
}

This architecture works very well for chaining operations together, as one function's result can easily be plugged in to another's input based on the given Uniform script. Where this really got complicated was in type coercion and arrays. Coercion should be pulled out into its own module so it can be logically separated from evaluator. Arrays will come later, and don't need to be worried about yet.

dgp1130 commented 8 years ago

Added evaluator.js and coercion.js as specified. Coercion is currently just a placeholder, it doesn't actually coerce any types, it just throws an error if it receives the wrong type.

I also got a little distracted and ended up reworking the build process, which admittedly should have been in a separate commit. I swapped out Browserify for Webpack because it played nicer with WebStorm's debugger. I also added ES6 features to the test suite, and configured the debugger to work with it, so we can use breakpoints within the test suite and production code. Note: When a test fails, the stack trace it generates has invalid line numbers. This is a little annoying, and hopefully I can come back to that eventually. Lastly I removed Grunt, as it was overcomplicating the build process without giving any real benefit. I simply moved the test, build, and run scripts into package.json, so you just use $ npm run build, $ npm test, and $ npm start. This is much simpler and easier to maintain.

sawyernovak commented 8 years ago

All the functions currently do as expected. It seems most of the code was just taken from the old parser file and updated with the new grammar, object oriented structure, and es6. Works good and looks fine for now.

Coerce still needs to be worked on as like you mentioned, only throws an error if it isn't what it wants it to be, but that can be a new ticket.

Parser will need to be revisited for the array implementations, but looks good as a start.