From 372927d7ea08eedd7d84ae2098da8256d0b5c8ff Mon Sep 17 00:00:00 2001 From: nocturn9x Date: Wed, 28 Feb 2024 14:23:16 +0100 Subject: [PATCH] Update for recent nim versions and add simple example --- examples/simple.nim | 52 ++++++++++++++++++++++++++++++++++ src/nimkalc/parsing/parser.nim | 3 -- 2 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 examples/simple.nim diff --git a/examples/simple.nim b/examples/simple.nim new file mode 100644 index 0000000..43eb97f --- /dev/null +++ b/examples/simple.nim @@ -0,0 +1,52 @@ +# Copyright 2021 Mattia Giambirtone +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# A simple REPL to parse and evaluate mathematical expressions + + +import nimkalc + +import strformat +import strutils + + +proc repl() = + ## A simple REPL to demonstrate NimKalc's functionality + var result: AstNode + while true: + try: + stdout.write("=> ") + result = eval(stdin.readLine()) + case result.kind: + # The result is an AstNode object, specifically + # either a node of type NodeKind.Float or a NodeKind.Integer + of NodeKind.Float: + echo result.value + of NodeKind.Integer: + echo int(result.value) + else: + discard # Unreachable + except IOError: + echo "\nGoodbye." + break + except ParseError: + echo &"A parsing error occurred: {getCurrentExceptionMsg()}" + except MathError: + echo &"An arithmetic error occurred: {getCurrentExceptionMsg()}" + except OverflowDefect: + echo &"Value overflow/underflow detected: {getCurrentExceptionMsg()}" + + +when isMainModule: + repl() diff --git a/src/nimkalc/parsing/parser.nim b/src/nimkalc/parsing/parser.nim index 86011e6..f79298f 100644 --- a/src/nimkalc/parsing/parser.nim +++ b/src/nimkalc/parsing/parser.nim @@ -23,9 +23,6 @@ import strformat import tables -{.experimental: "implicitDeref".} - - type Parser* = ref object tokens: seq[Token]