{"id":21064032,"url":"https://github.com/luochen1990/ast-reducer","last_synced_at":"2025-03-14T01:24:52.843Z","repository":{"id":57185266,"uuid":"117646352","full_name":"luochen1990/ast-reducer","owner":"luochen1990","description":"A Tool to process AST or any other recursive data structure in JavaScript.","archived":false,"fork":false,"pushed_at":"2018-11-06T09:29:32.000Z","size":10,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-02-21T07:06:05.551Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"CoffeeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/luochen1990.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-01-16T07:11:29.000Z","updated_at":"2024-02-21T12:27:53.000Z","dependencies_parsed_at":"2022-09-17T07:22:39.639Z","dependency_job_id":null,"html_url":"https://github.com/luochen1990/ast-reducer","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luochen1990%2Fast-reducer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luochen1990%2Fast-reducer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luochen1990%2Fast-reducer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/luochen1990%2Fast-reducer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/luochen1990","download_url":"https://codeload.github.com/luochen1990/ast-reducer/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243506810,"owners_count":20301757,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-19T17:48:05.077Z","updated_at":"2025-03-14T01:24:52.532Z","avatar_url":"https://github.com/luochen1990.png","language":"CoffeeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"AST Reducer\n===========\n\nA Tool to process AST or any other recursive data structure in JavaScript.\n\nFeatures\n--------\n\n- An EDSL to define reduce rules\n- Pure functional:\n  - state is managed via simulating a state monad (the `state` field in context)\n  - global constants is managed via simulating a reader monad (the `env` field in context)\n- Derivable Reducer\n- Pretty and helpful error message\n- Configuable node parsing logic (the `parseNode` field in opts)\n- Configuable default rule (the `defaultRule` field in opts)\n\nInstall\n-------\n\n```\nnpm install ast-reducer\n```\n\nUsage\n-----\n\nIn CoffeeScript\n\n#### Simple Usage\n\n```coffeescript\n{defineReducer} = require('ast-reducer')\n\nevalExpr = defineReducer({name: 'ArithCalc'}) (def) -\u003e\n  def('+') ([a, b]) -\u003e @(a) + @(b)\n  def('-') ([a, b]) -\u003e @(a) - @(b)\n  def('const') ([a]) -\u003e parseFloat(a)\n\nast = ['+', ['-', ['const', 1], ['const', 2]], ['+', ['const', '4'], ['const', '8']]]\nconsole.log evalExpr.eval(ast)\n\n# output: 11\n```\n\n#### Use State\n\n```coffeescript\nevalExpr = defineReducer({name: 'ArithCalc0'}) (def) -\u003e\n  def('+') ([a, b], {env, state}) -\u003e state.cnt += 1; @(a) + @(b)\n  def('-') ([a, b], {env, state}) -\u003e state.cnt += 1; @(a) - @(b)\n  def('const') ([a]) -\u003e parseFloat(a)\n\ncontext = {env: {}, initState: (-\u003e {cnt: 0})}\nconsole.log evalExpr.runState(ast, context)\n\n# output: { result: 11, state: { cnt: 3 } }\n```\n\n#### Derive Reducer\n\n```coffeescript\nevalExpr2 = evalExpr.derive({name: 'ArithCalc1'}) (def) -\u003e\n  def('*') ([a, b], {env, state}) -\u003e state.cnt += 1; @(a) * @(b)\n\nast2 = ['+', ['-', ['const', 1], ['const', 2]], ['*', ['const', '4'], ['const', '8']]]\nconsole.log evalExpr2.runState(ast2, context)\n\n# output: { result: 31, state: { cnt: 3 } }\n```\n\n#### Error Report\n\n```coffeescript\nconsole.log evalExpr.runState(ast2, context)\n\n### error message:\n\nReduceError:\n  [reducer]: \"ArithCalc0\"\n  [path]: [\"+\",\"*\"]\n  [state]: {\"cnt\":2}\n  [current]: [\"*\",[\"const\",\"4\"],[\"const\",\"8\"]]\n  [input]: [\"+\",[\"-\",[\"const\",1],[\"const\",2]],[\"*\",[\"const\",\"4\"],[\"const\",\"8\"]]]\n\n  Caused By: UnimplementedRule: *\n      at Function.defaultRule (the/long/long/path/to/reducer.coffee:34:13)\n\n    at reduce (the/long/long/path/to/reducer.coffee:42:11)\n    at Object.runState (the/long/long/path/to/reducer.coffee:47:31)\n    ...\n###\n```\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluochen1990%2Fast-reducer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fluochen1990%2Fast-reducer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fluochen1990%2Fast-reducer/lists"}