Browse Source

NimKalc 0.2.5: Exported all API for easier use

main
nocturn9x 1 year ago
parent
commit
d1f96da9d1
  1. 8
      README.md
  2. 10
      examples/repl.nim
  3. 2
      nimkalc.nimble
  4. 28
      src/nimkalc.nim
  5. 1
      src/nimkalc/objects/error.nim

8
README.md

@ -15,7 +15,6 @@ Features:
- `inf` (Infinity)
- `nan` (Not a number)
- Support for the following of nim's [math library](https://nim-lang.org/docs/math.html#log10%2Cfloat32) functions:
- `binom`
- `sin`
- `cos`
- `tan`
@ -105,12 +104,7 @@ where `name` is the function name and `args` is a list of arguments
Here is an example of a REPL using all of NimKalc's functionality to evaluate expressions from stdin (can be found at `examples/repl.nim`)
```nim
import nimkalc/objects/ast
import nimkalc/objects/token
import nimkalc/parsing/parser
import nimkalc/parsing/lexer
import nimkalc/objects/error
import nimkalc
import strformat
import strutils

10
examples/repl.nim

@ -12,14 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.
# A simple library to parse and evaluate mathematical expressions
# A simple REPL to parse and evaluate mathematical expressions
import nimkalc/objects/ast
import nimkalc/objects/token
import nimkalc/parsing/parser
import nimkalc/parsing/lexer
import nimkalc/objects/error
import nimkalc
import strformat
import strutils
@ -66,4 +62,4 @@ proc repl() =
when isMainModule:
repl()
repl()

2
nimkalc.nimble

@ -1,6 +1,6 @@
# Package
version = "0.2.4"
version = "0.2.5"
author = "Mattia Giambirtone"
description = "An advanced parsing library for mathematical expressions and equations"
license = "Apache 2.0"

28
src/nimkalc.nim

@ -18,34 +18,10 @@ import nimkalc/parsing/parser
import nimkalc/objects/ast
import nimkalc/parsing/lexer
import nimkalc/objects/token
import nimkalc/objects/error
import strutils
import strformat
proc `$`*(self: AstNode): string =
## Stringifies an AST node
case self.kind:
of NodeKind.Grouping:
result = &"Grouping({self.expr})"
of NodeKind.Unary:
result = &"Unary({$self.unOp.kind}, {$self.operand})"
of NodeKind.Binary:
result = &"Binary({$self.left}, {$self.binOp.kind}, {$self.right})"
of NodeKind.Integer:
result = &"Integer({$int(self.value)})"
of NodeKind.Float:
result = &"Float({$self.value})"
of NodeKind.Call:
result = &"Call({self.function.name}, {self.arguments.join(\", \")})"
of NodeKind.Ident:
result = &"Identifier({self.name})"
proc `$`*(self: Token): string =
## Returns a string representation of a token
result = &"Token({self.kind}, '{self.lexeme}')"
export lexer, parser, error, token, ast
proc eval*(source: string): AstNode =

1
src/nimkalc/objects/error.nim

@ -21,3 +21,4 @@ type
MathError* = object of NimKalcException
## An arithmetic error
EvaluationError* = object of NimKalcException

Loading…
Cancel
Save