Git Product home page Git Product logo

calculator's Introduction

Requirements

  • Unix
$ node -v
v14.2.0

$ npm -v
6.14.4

// not a requirement, only info
$ docker -v
Docker version 19.03.9, build 9d988398e7

Install

git clone https://github.com/dohjon/calculator.git
npm install

Test

npm test

Usage

Docker support

// Step 1. change code
...
// Step 2. build
docker build -t calculator:latest .
// Step 3. run

// arguments
docker run \
    --rm \
    -it \
    --workdir /usr/src/app \
    --volume $(pwd):/usr/src/app \
    --init \
    calculator:latest x add 3 print x

// interactive
docker run \
    --rm \
    -it \
    --workdir /usr/src/app \
    --volume $(pwd):/usr/src/app \
    --init \
    calculator:latest

// file
docker run \
    --rm \
    -it \
    --workdir /usr/src/app \
    --volume $(pwd):/usr/src/app \
    --init \
    calculator:latest test.txt
// link bin to path 
sudo npm link

// or use path
$ lib/calculator.js

// arguments
$ calculator x add 3 print x

// interactive
$ calculator

// file
$ calculator text.txt

Assumptions

  • Assumption: Cross platform support is not needed.

  • Assumption: data does not need to be saved after application exits

  • Assumption: no advanced error handling

  • Assumption: only synchronous code

  • Assumption: no need to handle signals (SIGINT, SIGTERM, etc) in interactive mode

  • Assumption: no need to optimize by using steams (read file content into memory)

  • Assumption: process should exit with 0 even when invalid tokens.

  • Assumption: no special characters needs to be supported

  • Assumption: operations words cannot be valid register word

  • Assumption: quit, print cannot be valid register word

  • Assumption: Beacuse this is a test I avoided using external libraries except for linting, formatting and testing. In the real world I would use existing libraries for everything.

  • Assumption: Circular dependency is not implemenented. Input using below examples will cause endless recursion.

    x => y

    y => x

    And

    x => y

    y => y

Overview

Scan => Parse => Evaluate => Calculate

Scan

Converts a string to tokens.

'x add 3'

To

['x', 'add', '3']

Parse

Converts tokens to AST (abstract syntax tree)

['x', 'add', '3', 'print', 'x']

To

[
    {
        val: 'x',
        type: REGISTER,
        children: [
            {
                val: 'add',
                type: OPERATION,
                children: [
                    {
                        val: 3,
                        type: VALUE,
                        children: [],
                    },
                ],
            },
        ],
    },
    {
        val: 'print',
        type: PRINT,
        children: [
            {
                val: 'x',
                type: REGISTER,
                children: [],
            },
        ],
    }
]

Add new operation

Add new operation handler in operations.js

function division(a, b) {
  return a / b;
}

Add handler to operations in operations.js

const operations = {
  division: division,
  ...
};

Add name of handler to operation in parser/rules.js

const operation = `^division$`;

Built with

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.