Code Monkey home page Code Monkey logo

simplecalculatorforarduino's Introduction

SimpleCalculatorForArduino

A simple calculator for Arduino and PC

This is our proposed project for CMPUT 274, based on recursive descent parser

Author: Yancheng Ou, Yun Cao

Grammar:

<stat> ::= IDENTIFIER = <expr>
         | <expr>

<expr> ::= <term> <expr_tail>

<identifier_expr> ::= IDENTIFIER
                    | IDENTIFIER ( )
                    | IDENTIFIER ( <expr> )
                    | IDENTIFIER ( <expr> , <expr> )

// <identifier_expr> ::= IDENTIFIER
//                     | IDENTIFIER ( )
//                     | IDENTIFIER ( <expr>,* <expr> )


<expr_tail> ::= + <term> <expr_tail>
              | - <term> <expr_tail>
              | * <term> <expr_tail>
              | / <term> <expr_tail>
              | EMPTY

<term> ::= <positive_term>
         | - <positive_term>
         | + <positive_term> 

<positive_term> ::= <identifier_expr>
                  | NUM
                  | ( <expr> )


IDENTIFIER: {VARIABLE_NAME, sin, cos, tan, pow, abs, sqrt, max, min, log, log2, log10}
VARIABLE_NAME: [A-Za-z][A-Za-z0-9]*
NUM: [0-9.]+

Usage:

Running on PC: simply run g++ main.cpp -Wall and ./a.out

Uploading to Arduino: use Arduino IDE to upload the code to Arduino platform and open the serial monitor.

When running on PC, type exit or quit will quit the program.

On both platform, type clear will clear the symbol table.

This program supports variable assignment and some common functions.

Limitations:

  • The maximum length of identifier name is 16 bytes (ascii characters)
  • The maximum length of input string is 128 bytes (ascii characters)
  • The maximum number of different variables is 16
  • Passing incorrect number of parameters to a function may cause unexpected result.

Adding New Functions:

It's very simple to add new functions. Just open ASTWalker.hpp and scroll down to the end, you will see a bunch of else-if statements. And we can write a new else-if block between the last else-if block and the else block.

For example: We want to add a single-parameter function exp. Then we can write:

else if ...
else if (MATCH_FUNCTION_NAME("exp")) {
    SINGLE_ARG_FUNCTION(va);
    s.push(exp(va));
}
else ...

If we want to add a double-parameter function, for example an add function which returns the sum of its two arguments, we can write:

else if ...
else if (MATCH_FUNCTION_NAME("add")) {
    DOUBLE_ARGS_FUNCTION(va, vb);
    s.push(va + vb);
}
else ...

The macro MATCH_FUNCTION_NAME helps you to define the function name.

The macro SINGLE_ARG_FUNCTION and DOUBLE_ARGS_FUNCTION helps you to specify the number of arguments.

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.