Skip to content

Commit

Permalink
Improved Error Handling
Browse files Browse the repository at this point in the history
  • Loading branch information
benzap committed Aug 6, 2018
1 parent 01476f1 commit 8f74af7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 24 deletions.
20 changes: 12 additions & 8 deletions src/eden/std/exceptions.cljc
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
(ns eden.std.exceptions)


(defn generate-window
"TODO: write this function, with bigger window."
[{:keys [tokens index] :as astm}]
(let []))


(defn generate-parser-data
[{:keys [tokens index] :as astm}]
{:index index
{:position index
:window
(str
(if (> index 1) "... " "")
(nth tokens (dec index)) " "
"<" (nth tokens index) "> "
(nth tokens (inc index))
(if (< index (- (count tokens) 3)) " ..." ""))})
(if (> index 1) "... " "|BEGINNING| ")
(get tokens (dec index) "") " "
"'" (get tokens index) "' "
(get tokens (inc index) "")
(if (< index (- (count tokens) 3)) " ..." " |END|"))})


(defn parser-error
Expand Down Expand Up @@ -51,12 +57,10 @@
(cond
(parser-error? ex)
(do
(println "Parser Error Caught")
(throw ex))

(runtime-error? ex)
(do
(println "Runtime Error Caught")
(throw ex))

:else (throw ex)))
68 changes: 52 additions & 16 deletions src/eden/std/impl/expression.cljc
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@

Expression
(evaluate-expression [_]
(<=
(evaluate-expression left)
(evaluate-expression right)))
(let [lvalue (evaluate-expression left)
rvalue (evaluate-expression right)]
(<= lvalue rvalue)))

display/Display
(display-node [_]
Expand All @@ -155,9 +155,17 @@

Expression
(evaluate-expression [_]
(+
(evaluate-expression left)
(evaluate-expression right)))
(let [lvalue (evaluate-expression left)
rvalue (evaluate-expression right)]
(when-not (number? lvalue)
(runtime-error "Given lvalue for '+' operation is not a number"
{:display (display-node left) :value lvalue}))

(when-not (number? rvalue)
(runtime-error "Given rvalue for '+' operation is not a number"
{:display (display-node right) :value rvalue}))

(+ lvalue rvalue)))

display/Display
(display-node [_]
Expand All @@ -170,9 +178,17 @@

Expression
(evaluate-expression [_]
(-
(evaluate-expression left)
(evaluate-expression right)))
(let [lvalue (evaluate-expression left)
rvalue (evaluate-expression right)]
(when-not (number? lvalue)
(runtime-error "Given lvalue for '-' operation is not a number"
{:display (display-node left) :value lvalue}))

(when-not (number? rvalue)
(runtime-error "Given rvalue for '-' operation is not a number"
{:display (display-node right) :value rvalue}))

(- lvalue rvalue)))

display/Display
(display-node [_]
Expand All @@ -190,9 +206,17 @@

Expression
(evaluate-expression [_]
(*
(evaluate-expression left)
(evaluate-expression right)))
(let [lvalue (evaluate-expression left)
rvalue (evaluate-expression right)]
(when-not (number? lvalue)
(runtime-error "Given lvalue for '*' operation is not a number"
{:display (display-node left) :value lvalue}))

(when-not (number? rvalue)
(runtime-error "Given rvalue for '*' operation is not a number"
{:display (display-node right) :value rvalue}))

(* lvalue rvalue)))

display/Display
(display-node [_]
Expand All @@ -205,9 +229,17 @@

Expression
(evaluate-expression [_]
(/
(evaluate-expression left)
(evaluate-expression right)))
(let [lvalue (evaluate-expression left)
rvalue (evaluate-expression right)]
(when-not (number? lvalue)
(runtime-error "Given lvalue for '/' operation is not a number"
{:display (display-node left) :value lvalue}))

(when-not (number? rvalue)
(runtime-error "Given rvalue for '/' operation is not a number"
{:display (display-node right) :value rvalue}))

(/ lvalue rvalue)))

display/Display
(display-node [_]
Expand Down Expand Up @@ -238,7 +270,11 @@

Expression
(evaluate-expression [_]
(- (evaluate-expression value)))
(let [evalue (evaluate-expression value)]
(when-not (number? evalue)
(runtime-error "Given single value for '-' operation is not a number"
{:display (display-node value) :value evalue}))
(- evalue)))

display/Display
(display-node [_]
Expand Down

0 comments on commit 8f74af7

Please sign in to comment.