Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/yasulab/lp-calculator

A calculator as a language processer
https://github.com/yasulab/lp-calculator

Last synced: 1 day ago
JSON representation

A calculator as a language processer

Awesome Lists containing this project

README

        

Description
-----------
This program is a calculator which can run along this specification below.

1. can use operands: +, -, *, /, %
2. can use Gauss's integer using branckets
3. can change priority of operation using parenthsis
4. can assign value to a valuable structured by alphabets

The specification is implemented by this syntax rule below.

expression = ['-'] term {('+'|'-') term }
term = primary {('*'|'/'|'%') primary }
primary = character | figure | '(' expression ')' | '[' expression ']'
character = ID | ID '=' expression
figure = {number} ['.' {number}]
number = 0|1|2|3|4|5|6|7|8|9
ID = a|b|...|y|z|A|B|...|Y|Z

Sample Output
-------------
yohei@YOHEI-note ~/wl/calc
$ gcc calc.c -o calc

yohei@YOHEI-note ~/wl/calc
$ ./calc

/* General Operations */
# +2-1
>> 1.000000

# -2-1
>> -3.000000

# 2+5*10
>> 52.000000

# 2*5+10
>> 20.000000

# 2*(5+10)
>> 30.000000

# 3/2
>> 1.500000

# 5.5/2.2
>> 2.500000

# [5.5/2.2]
>> 2.000000

# [5/(1+1)]
>> 2.000000

# 2/1+6%3
>> 2.000000

# 4.9%0.3
>> 0.100000

# two=2
two holds 2.000000
>> 2.000000

# five=5
five holds 5.000000
>> 5.000000

# ten=10
ten holds 10.000000
>> 10.000000

# hundred=100
hundred holds 100.000000
>> 100.000000

# -(ten+five/two)*two*hundred
>> -2500.000000

#
shut down this program...
yohei@YOHEI-note ~/wl/calc
$

/* Exception Handler*/

# 1+ 2++ 4
error: incorrect position of symbol.
1+ 2++ 4
^
1 error was found.

# (1+3*4
error: missing ')'
(1+3*4
^
1 error was found.

# 1++[2*3**1
error: incorrect position of symbol.
1++[2*3**1
^
error: incorrect position of symbol.
1++[2*3**1
^
error: missing ']'
1++[2*3**1
^
3 errors were found.

# two+five*second+ten
error: not registered word is used.
two+five*second+ten
^
1 error was found.

# 1.3.4.5.6
unexpected error