Giter VIP home page Giter VIP logo

evaluator2's Introduction

Expression evaluator in C++17

Build and run

mkdir build
cd build

Windows MSVC

cmake ..
cmake --build . --config [Debug|Release]
..\bin\eval.exe

Windows MinGW

cmake -G"MinGW Makefiles" -DCMAKE_BUILD_TYPE=[Debug|Release] ..
mingw32-make
..\bin\eval.exe

Linux GCC

cmake -DCMAKE_BUILD_TYPE=[Debug|Release] ..
make 
../bin/eval

Examples

Arithmetic Expressions

sin(pi / 2) + sqrt(2) ^ gamma(3) - 4 * 5 / sin(-6 - 7 / ln(8)) - 9

List

[]
[1, 2, 3, 4, 5]
[1 + 2, (3 - 4) / 5, 6]

Lambda

@(x){x^2}
@(x, y){x + y}
@(x, y, z){[x + y, y + z, z + x]}
@(a){@(x){x + a}}

List Arithmetic Expressions

1 + [2, 3]
[1, 2] + [3, 4]
[3, 4] * 5
[3, 4] / [5, 6]
([1, 2] - 1.5)^2
sin([1, 2, 3, 4])[1]
len([1, 2, 3, 4])

Mixed

f(x, y) = sqrt(x^2 + y^2)
f(3, 4)
fib(n) = if_else(gt(n, 1), fib(n - 1) + fib(n - 2), 1)

sum_list(l, n) = if_else(gt(n, 0), sum_list(l, n - 1) + l[n - 1], 0)
sum = @(l){sum_list(l, len(l))}
sum([1, 2, 3])
mean(l) = sum(l) / len(l)

Y = @(f){@(g){f(g(g))}(@(g){f(@(y){g(g)(y)})})}
fact_gen = @(f){@(n){if_else(n, n * f(n - 1), 1)}}
fact = Y(fact_gen)
fact(5)

map_n(list, f, n) = if_else(gt(n, 0), assign(map_n(list, f, n - 1), n - 1, f(list[n - 1])), list)
map(list, f) = map_n(list, f, len(list))
map([1, 2, 3, 4, 5], sin)

construct(f, n) = if_else(gt(n, 0), append(construct(f, n - 1), f(n - 1)), [])
construct(fib, 10)

Commands

!init: initialize internal function list
!list: display current identifiers
!exit: exit
!ast: print expression AST

Specification

EBNF

ASSIGN -> ident '=' EXPR
ASSIGN_LABMDA -> ident '(' PARAM_LIST ')' '=' EXPR
EXPR -> EXPR_L1

TERM -> decimal | ident | '(' EXPR ')' | LIST | LAMBDA | INDEX | CALL 
LIST -> '[' EXPR_LIST ']'
LAMBDA -> '@' '(' PARAM_LIST ')' '{' EXPR '}'
INDEX -> TERM '[' EXPR ']'
CALL -> TERM '(' EXPR_LIST ')'
PARAM_LIST -> empty | ident {',' ident}
EXPR_LIST -> empty | EXPR {',' EXPR}
EXPR_UNARY -> ['-'] EXPR_L2
EXPR_L1 -> EXPR_UNARY {OPTR_L1 EXPR_UNARY}
EXPR_L2 -> EXPR_L3 {OPTR_L2 EXPR_L3}
EXPR_L3 -> TERM {'^' TERM}
OPTR_L1 -> '+' | '-'
OPTR_L2 -> '*' | '/'

Internal Variables

pi
e
ans

Internal Functions

sqrt(x)
sin(x)
cos(x)
tan(x)
asin(x)
acos(x)
atan(x)
exp(x)
ln(x)
abs(x)
round(x)
floor(x)
ceil(x)
gamma(x)
erf(x)

len(list)
assign(list, idx, val)
append(list, val)
slice(list, st, ed)
reverse(list)

not(x)
and(x, y)
or(x, y)

eq(x, y)
neq(x, y)
gt(x, y)
lt(x, y)
geq(x, y)
leq(x, y)

if_else(cond, true, false)

evaluator2's People

Contributors

ljghy avatar

Stargazers

 avatar

Watchers

 avatar

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.