GithubHelp home page GithubHelp logo

smaep's Introduction

| pipeline status | | code coverage |

smaep

smaep is a SMAll Expression Parser. It can parse simple mathematical expressions into an abstract syntax tree and evaluate or print that tree.

smaep can be used as a command line calculator. But it is also possible to reuse a parsed expression with different sets of data.

I use gitlab for CI, so have a look at https://gitlab.com/waltronix/smaep if you want to have a closer look at the build results and some sample calls.

Usage

smaep provides a basic command line interface that takes expressions as arguments.

> smaep_cli -e "1 + 1"

    1 + 1

  1 + 1 = 2

Printing the syntax tree can be activated by using -t. At the moment, the address of the tree items is printed as identifier.

> smaep_cli -t -e "(3 + 4)/2"

    (3 + 4)/2


  ast
     └ /                             - 0x0x55d181024b90
       └ +                           - 0x0x55d181024c60
         └ 3.000000                  - 0x0x55d181023030
         └ 4.000000                  - 0x0x55d181024250
       └ 2.000000                    - 0x0x55d181025260

  (3 + 4)/2 = 3.5

In addition, it is possible to add a data source that can be queried during evaluation of the expression. The command line tool is able to query data from json files via json path.

> smaep_cli -t -e "(data[\$.A.x] + data[\$.B.x])/2" -f smaep_cli/samples/temp_0*

    (data[$.A.x] + data[$.B.x])/2


  ast
     └ /                             - 0x0x55eda6c57c00
       └ +                           - 0x0x55eda6c57e30
         └ "$.A.x"                   - 0x0x55eda6c57f00
         └ "$.B.x"                   - 0x0x55eda6c57ac0
       └ 2.000000                    - 0x0x55eda6c57060

  smaep_cli/samples/temp_01.json : 2
  smaep_cli/samples/temp_02.json : 6

Thanks to

My special thanks go to the following projects:

How to build

smaep is build using cmake.

There are some options to control what to build.

SMAEP_BUILD_CLI_TOOL
Build the smaep cli calculator.
SMAEP_BUILD_JSON_SOURCE
Build json data source.
SMAEP_BUILD_TESTS
Build unit tests for all components.

By default these options are activated.

How it works

The following diagram give a rough overview, on what happens during parsing.

The single values, operators and functions are extracted by PEGTL_ and pushed into the tree_nursery. In there, they are converted into a tree by using the Shunting Yard Algorithm. And thats it.

.. plantuml::

  boundary smaep
  participant tree_nursery
  collections nodes
  boundary pegtl

  [-> smaep: parse(\n  expression, \n  operators);
  activate smaep
      smaep -> tree_nursery **
      activate tree_nursery #LightGreen
          smaep -> pegtl: parse\n  <grammar, actions>\n  (expr, ops, tr);
          activate pegtl
              group during expression parsing
                  opt value
                      pegtl -> pegtl: make node
                      pegtl -> nodes**
                      activate nodes #LightSalmon
                      pegtl -> tree_nursery: push(node)
                  else operation or function
                      pegtl -> tree_nursery: push(&function)
                  end
              end
          pegtl --> smaep
          deactivate pegtl

          smaep -> tree_nursery: get_ast()
          activate tree_nursery
              tree_nursery -> nodes: transfer\n ownership\n of nodes
          smaep <-- tree_nursery
          deactivate tree_nursery

          [<- smaep: ast
      destroy tree_nursery
  deactivate smaep

smaep's People

Contributors

waltronix avatar

Stargazers

 avatar

Watchers

 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.