Giter Site home page Giter Site logo

calc's Introduction

calc

A very simple calculator based on a recursive descent parser (not using python eval)

Supports exponentiation, add, sub, mul, div, modulo/formatting, shift, bitwise and/or/xor, comparison operators, logical and/or (short-circuited), unary negation, parenthesized subexpressions and assignment (an operator like in C, not a statement like in Python).

Atoms can be integers, floats, strings or variables.

It's also possible to store an expression in a variable (like a spreadsheet formula) using the := operator. No check about circular formulas is present.

The core parser/evaluator is about 40 lines. The whole program is less than 100 lines in total.

Works in both Python 2.x and 3.x

Example session:

> 1 + 1
2
> 1 + 2*3
7
> 1 + 2*(3 + 4)
15
> pi = 3.141592654
3.141592654
> r = 10
10
> r * r * pi
314.1592654
> area := r * r * pi
314.1592654
> r = 100
100
> area
31415.92654
> r =42
42
> area
5541.76944166

calc's People

Stargazers

 avatar  avatar

Watchers

Andrea Griffini avatar James Cloos avatar

Forkers

wassekaran

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.