All Projects → cparse → Cparse

cparse / Cparse

Licence: mit
A C++ configurable Expression Parser. Useful as a Calculator or for helping you write your own Programming Language

Projects that are alternatives of or similar to Cparse

Accrue.js
A loan and interest calculation plugin for jQuery. Create calculators that allow you to calculate interest on loans, compare multiple loans (with different interest rates), and create amortization charts.
Stars: ✭ 90 (-53.61%)
Mutual labels:  calculator
Calc
C-style arbitrary precision calculator
Stars: ✭ 127 (-34.54%)
Mutual labels:  calculator
Xopcodecalc
Opcode calculator
Stars: ✭ 147 (-24.23%)
Mutual labels:  calculator
Kairos
A non date-based time calculator
Stars: ✭ 100 (-48.45%)
Mutual labels:  calculator
Kalk
kalk is a powerful command line calculator app for developers.
Stars: ✭ 118 (-39.18%)
Mutual labels:  calculator
Wabbitemu
Wabbitemu is a Z80 TI Calculator emulator
Stars: ✭ 131 (-32.47%)
Mutual labels:  calculator
Simply Calculator Vuejs
A simple calculator made by vuejs 用vue.js实现简易计算器
Stars: ✭ 76 (-60.82%)
Mutual labels:  calculator
Amadia
Astus' Mathematical Display Application : A GUI for Mathematics (Calculator, LaTeX Converter, Plotter, ... )
Stars: ✭ 172 (-11.34%)
Mutual labels:  calculator
Calculatorapp
scientific calculator basic calculator and unit converter android app
Stars: ✭ 119 (-38.66%)
Mutual labels:  calculator
Mediapipe
Cross-platform, customizable ML solutions for live and streaming media.
Stars: ✭ 15,338 (+7806.19%)
Mutual labels:  calculator
Calc.plugin.zsh
zsh calculator - with support for basic math
Stars: ✭ 105 (-45.88%)
Mutual labels:  calculator
Calculator
Calculator app created with Java Swing, It is simple with an easy code to help novices learn how to operate a calculator.
Stars: ✭ 107 (-44.85%)
Mutual labels:  calculator
Kalk
A calculator/expression evaluator written in rust that supports variables and functions.
Stars: ✭ 134 (-30.93%)
Mutual labels:  calculator
Alfred Calculate Anything
Alfred Workflow to calculate anything with natural language
Stars: ✭ 92 (-52.58%)
Mutual labels:  calculator
Xk Time
xk-time 是时间转换,时间计算,时间格式化,时间解析,日历,时间cron表达式和时间NLP等的工具,使用Java8,线程安全,简单易用,多达70几种常用日期格式化模板,支持Java8时间类和Date,轻量级,无第三方依赖。
Stars: ✭ 162 (-16.49%)
Mutual labels:  calculator
Ac Nh Turnip Prices
Price calculator/predictor for Turnip prices
Stars: ✭ 1,298 (+569.07%)
Mutual labels:  calculator
Simple Java Calculator
🔢 Simple calculator written in Java with Eclipse. This calculator is simple with an easy code to help novices learn how to operate a calculator.
Stars: ✭ 130 (-32.99%)
Mutual labels:  calculator
Computator.net
Computator.NET is a special kind of numerical software that is fast and easy to use but not worse than others feature-wise. It's features include: - Real and complex functions charts - Real and complex calculator - Real functions numerical calculations including different methods - Over 107 Elementary functions - Over 141 Special functions - Over 21 Matrix functions and operations - Scripting language with power to easy computations including matrices - You can declare your own custom functions with scripting language
Stars: ✭ 174 (-10.31%)
Mutual labels:  calculator
Pegparser
💡 Build your own programming language! A C++17 PEG parser generator supporting parser combination, memoization, left-recursion and context-dependent grammars.
Stars: ✭ 164 (-15.46%)
Mutual labels:  calculator
Programmer Calculator
Terminal calculator made for programmers working with multiple number representations, sizes, and overall close to the bits
Stars: ✭ 135 (-30.41%)
Mutual labels:  calculator

CParser • Build Status License

This project provides a C++ library to parse a character sequence as an expression using Dijkstra's Shunting-yard algorithm, which modifies Jesse Brown's original code.

This project was developed by Brandon Amos and Vinícius Garcia.

Getting Started

If you want to use this library in your project please take a look at our Wiki

Builtin Features

  • Unary operators. +, -
  • Binary operators. +, -, /, *, %, <<, >>, ^
  • Boolean operators. <, >, <=, >=, ==, !=, &&, ||
  • Functions. sin, cos, tan, abs, print
  • Support for an hierarchy of scopes with local scope, global scope etc.
  • Easy to add new operators, operations, functions and even new types
  • Easy to implement object-to-object inheritance (with the prototype concept)
  • Built-in garbage collector (does not handle cyclic references yet)

Setup

Download and Compile

cd 'my/project/dir'
git clone https://github.com/cparse/cparse.git
make release -C cparse

Link with your project:

g++ -I cparse -std=c++11 cparse/builtin-features.o cparse/core-shunting-yard.o main.cpp -o main

Customizing your Library

To customize your calculator:

  1. Copy the builtin-features.cpp file and builtin-features/ directory to your project.
  2. Edit the builtin-features/*.inc files as you like.
  3. Then build the project:
    1. Compile the library: make release -C cparse/
    2. Compile your modified features: g++ -I cparse -std=c++11 -c builtin-features.cpp -o my-features.o
    3. Link your project: g++ -I cparse -std=c++11 my-features.o cparse/core-shunting-yard.o main.cpp -o main

For a more detailed guide read our Wiki advanced concepts' section:

Minimal examples

As a simple calculator

#include <iostream>
#include "shunting-yard.h"

int main() {
  TokenMap vars;
  vars["pi"] = 3.14;
  std::cout << calculator::calculate("-pi+1", &vars) << std::endl;

  // Or if you want to evaluate an expression
  // several times efficiently:
  calculator c1("pi-b");
  vars["b"] = 0.14;
  std::cout << c1.eval(vars) << std::endl; // 3
  vars["b"] = 2.14;
  std::cout << c1.eval(vars) << std::endl; // 1

  return 0;
}

As a sub-parser for a programming language

Here we implement an interpreter for multiple expressions, the delimiter used will be ; or \n just like Javascript or Python and the code must start and end on curly brackets.

A similar architecture can be used for interpreting other common programming language statements like for loops and if statements. If you're interested take a look on the jSpy programming language that uses this project as the core parsing system.

#include <iostream>
#include "shunting-yard.h"
#include "shunting-yard-exceptions.h"

struct codeBlock {
  static void interpret(const char* start, const char** end, TokenMap vars) {
    // Remove white spaces:
    while (isspace(*start)) ++start;

    if (*start != '{') {
      throw syntax_error("Expected '{'");
    } else {
      ++start;
    }

    while (*start != '}') {
      calculator::calculate(start, vars, ";\n}", &start);

      // Alternatively you could write above:
      // - calculator(start, ";\n}", &start).eval(vars);

      // Find the beginning of the next expression:
      while(isspace(*start) || *start == ';') ++start;
    }

    if (*start == '}') {
      *end = start+1;
    } else {
      throw syntax_error("Expected '}'");
    }
  }
};

int main() {
  GlobalScope vars;
  const char* code =
    "{"
    "  a = 10;"
    "  b = 20\n"
    "  c = a + b }";

  codeBlock::interpret(code, &code, vars);

  std::cout << vars["c"] << std::endl; // 30
  return 0;
}

Please note that a calculator can compile an expression so that it can efficiently be executed several times at a later moment.

More examples

  • For more examples and a comprehensible guide please read our Wiki

Contributing

  • I would like to keep this library minimal so new features should be very useful to be accepted.
  • If proposed change is not a common use case, I will probably not accept it.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].