mirror of https://github.com/nocturn9x/nimkalc.git
Added $ operator in root package, minor formatting fixes
This commit is contained in:
parent
d0bd200428
commit
4330411843
|
@ -149,7 +149,7 @@ echo eval("2+2") # Prints Integer(4)
|
|||
|
||||
## Installing
|
||||
|
||||
You can install the package via nimble with this command: `nimble install https://github.com/nocturn9x/nimkalc`
|
||||
You can install the package via nimble with this command: `nimble install nimkalc`
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
# Package
|
||||
|
||||
version = "0.2"
|
||||
version = "0.2.1"
|
||||
author = "Mattia Giambirtone"
|
||||
description = "An advanced parsing library for mathematical expressions and equations"
|
||||
license = "Apache 2.0"
|
||||
|
|
|
@ -12,11 +12,40 @@
|
|||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
##
|
||||
## Top-level module for nimkalc
|
||||
|
||||
import nimkalc/parsing/parser
|
||||
import nimkalc/objects/ast
|
||||
import nimkalc/parsing/lexer
|
||||
import nimkalc/parsing/token
|
||||
|
||||
|
||||
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})"
|
||||
of NodeKind.Ident:
|
||||
result = &"Identifier({self.name})"
|
||||
|
||||
|
||||
proc `$`*(self: Token): string =
|
||||
## Returns a string representation of a token
|
||||
result = &"Token({self.kind}, '{self.lexeme}')"
|
||||
|
||||
|
||||
proc eval*(source: string): AstNode =
|
||||
|
|
|
@ -54,11 +54,6 @@ type
|
|||
# A node visitor object
|
||||
|
||||
|
||||
proc initNodeVisitor*(): NodeVisitor =
|
||||
## Initializes a node visitor
|
||||
new(result)
|
||||
|
||||
|
||||
proc `$`*(self: AstNode): string =
|
||||
## Stringifies an AST node
|
||||
case self.kind:
|
||||
|
@ -78,6 +73,12 @@ proc `$`*(self: AstNode): string =
|
|||
result = &"Identifier({self.name})"
|
||||
|
||||
|
||||
|
||||
proc initNodeVisitor*(): NodeVisitor =
|
||||
## Initializes a node visitor
|
||||
new(result)
|
||||
|
||||
|
||||
template handleBinary(left, right: AstNode, operator: untyped): AstNode =
|
||||
## Handy template that avoids us the hassle of copy-pasting
|
||||
## the same checks over and over again in the visitor
|
||||
|
|
|
@ -33,5 +33,5 @@ type
|
|||
|
||||
|
||||
proc `$`*(self: Token): string =
|
||||
## Returns a string representation of self
|
||||
## Returns a string representation of a token
|
||||
result = &"Token({self.kind}, '{self.lexeme}')"
|
Loading…
Reference in New Issue