Git Product home page Git Product logo

ast-reducer's Introduction

AST Reducer

A Tool to process AST or any other recursive data structure in JavaScript.

Features

  • An EDSL to define reduce rules
  • Pure functional:
    • state is managed via simulating a state monad (the state field in context)
    • global constants is managed via simulating a reader monad (the env field in context)
  • Derivable Reducer
  • Pretty and helpful error message
  • Configuable node parsing logic (the parseNode field in opts)
  • Configuable default rule (the defaultRule field in opts)

Install

npm install ast-reducer

Usage

In CoffeeScript

Simple Usage

{defineReducer} = require('ast-reducer')

evalExpr = defineReducer({name: 'ArithCalc'}) (def) ->
  def('+') ([a, b]) -> @(a) + @(b)
  def('-') ([a, b]) -> @(a) - @(b)
  def('const') ([a]) -> parseFloat(a)

ast = ['+', ['-', ['const', 1], ['const', 2]], ['+', ['const', '4'], ['const', '8']]]
console.log evalExpr.eval(ast)

# output: 11

Use State

evalExpr = defineReducer({name: 'ArithCalc0'}) (def) ->
  def('+') ([a, b], {env, state}) -> state.cnt += 1; @(a) + @(b)
  def('-') ([a, b], {env, state}) -> state.cnt += 1; @(a) - @(b)
  def('const') ([a]) -> parseFloat(a)

context = {env: {}, initState: (-> {cnt: 0})}
console.log evalExpr.runState(ast, context)

# output: { result: 11, state: { cnt: 3 } }

Derive Reducer

evalExpr2 = evalExpr.derive({name: 'ArithCalc1'}) (def) ->
  def('*') ([a, b], {env, state}) -> state.cnt += 1; @(a) * @(b)

ast2 = ['+', ['-', ['const', 1], ['const', 2]], ['*', ['const', '4'], ['const', '8']]]
console.log evalExpr2.runState(ast2, context)

# output: { result: 31, state: { cnt: 3 } }

Error Report

console.log evalExpr.runState(ast2, context)

### error message:

ReduceError:
  [reducer]: "ArithCalc0"
  [path]: ["+","*"]
  [state]: {"cnt":2}
  [current]: ["*",["const","4"],["const","8"]]
  [input]: ["+",["-",["const",1],["const",2]],["*",["const","4"],["const","8"]]]

  Caused By: UnimplementedRule: *
      at Function.defaultRule (the/long/long/path/to/reducer.coffee:34:13)

    at reduce (the/long/long/path/to/reducer.coffee:42:11)
    at Object.runState (the/long/long/path/to/reducer.coffee:47:31)
    ...
###

ast-reducer's People

Contributors

luochen1990 avatar

Stargazers

shuangyu Lu avatar

Watchers

James Cloos avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.