Expr is a simple, lightweight and performant programming toolkit for evaluating basic mathematical expression and boolean expression. The resulting value is one of these following primitive types: string, boolean, numerical (complex, float, integer).
- Binary (base-2) : 0b1011
- Octal (base-8) : 0o13 or 013
- Decimal (base-10) : 11
- Hexadecimal (base-16) : 0xB
- Scientific : 11e0
"1 + 1" -> 2
"1.0 / 2" -> 0.5
"2 < 2 + 2" -> true
"true && !false" -> true
"4 << 10" -> 4096
"0b0100 << 0b1010" -> 4096 (0b1000000000000)
"0x4 << 0xA" -> 4096 (0x1000)
"0o4 << 0o12" -> 4096 (0o10000)
"0b1000 | 0b1001" -> 9 (0b1001)
"(2+1i) + (2+2i)" -> (4+3i)
"0x4 << 0xA > 1024" -> true
For binding variables into expr string, see Bind
For explaining step-by-step operations, see Explain
- Any parses the given expr string into any type it returns as a result. e.g:
- "1 < 2" -> true
- "true || false" -> true
- "2 + 2" -> 4
- "4 << 10" -> 4906
- "2.2 + 2" -> 4.2
- "(2+1i) + (2+2i)" -> (4+3i)
- ""abc" == "abc"" -> true
- ""abc"" -> "abc"
- Supported operators:
- Comparison: [==, !=, <, <=, >, >=]
- Logical: [&&, ||, !]
- Arithmetic: [+, -, *, /, %] (% operator does not work for complex number)
- Bitwise: [&, |, ^, &^, <<, >>] (only work for integer values)
str := "(2+1i) + (2+2i)"
v, err := expr.Any(str)
if err != nil {
panic(err)
}
fmt.Printf("%v", v) // (4+3i)
- Bool parses the given expr string into boolean as a result. e.g:
- "1 < 2" -> true
- "1 > 2" -> false
- "true || false" -> true
- "true && !false" -> true
- Arithmetic operation are supported. e.g:
- "1 + 2 > 1" -> true
- "(1 * 10) > -2" -> true
- Supported operators:
- Comparison: [==, !=, <, <=, >, >=]
- Logical: [&&, ||, !]
- Arithmetic: [+, -, *, /, %] (% operator does not work for complex number)
- Bitwise: [&, |, ^, &^, <<, >>] (only work for integer values)
str := "((1 < 2 && 3 > 4) || 1 == 1) && 4 < 5"
v, err := expr.Bool(str)
if err != nil {
panic(err)
}
fmt.Printf("%t", v) // true
- Complex128 parses the given expr string into complex128 as a result. e.g:
- "(2+1i) + (2+2i)" -> (4+3i)
- "(2.2+1i) + 2" -> (4.2+1i)
- "2 + 2" -> (4+0i)
- Supported operators:
- Arithmetic: [+, -, *, /]
str := "(2+1i) + (2+2i)"
v, err := expr.Complex128(str)
if err != nil {
panic(err)
}
fmt.Printf("%f", v) // (4+3i)
- Float64 parses the given expr string into float64 as a result. e.g:
- "2 + 2" -> 4
- "2.2 + 2" -> 4.2
- "10 * -5 + (-5.5)" -> -55.5
- "10.0 % 2.6" -> 2.2
- Supported operators:
- Arithmetic: [+, -, *, /, %]
str := "((2 * 2) * (8 + 2) * 2) + 2.56789"
v, err := expr.Float64(str)
if err != nil {
panic(err)
}
fmt.Printf("%f", v) // 82.56789
- Int64 parses the given expr string into int64 as a result. e.g:
- "2 + 2" -> 4
- "2.2 + 2" -> 4
- "10 + ((-5 * -10) / -10) - 2" -> 3
- Supported operators:
- Arithmetic: [+, -, *, /, %]
- Bitwise: [&, |, ^, &^, <<, >>]
str := "((2 * 2) * (8 + 2) * 2) + 2.56789"
v, err := expr.Int64(str)
if err != nil {
panic(err)
}
fmt.Printf("%d", v) // 82
- Int64Strict is shorthand for Int64(str) but when x / y and y == 0, it will return ErrIntegerDividedByZero
str := "12 + 24 - 10/0"
v, err := expr.Int64Strict(str)
if err != nil {
// err == ErrIntegerDividedByZero
}
fmt.Printf("%d", v) // 0
- Int is shorthand for Int64(str) with its result will be converted into int.
str := "1 + 10"
v, err := expr.Int(str)
if err != nil {
panic(err)
}
fmt.Printf("%d", v) // 11
Expr is released under Apache Licence 2.0