Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Adding error checking for enums: making sure that, if a value is

declared with a parent, it's actually a value of the enum's parent.
(This isn't a perfect check, because it could be a descended value; in
enum.+, the example would be if OK were descended from Continue.)
  • Loading branch information...
commit edced3971d7c0ea30abee2aedca8d9c965b846ce 1 parent 662d648
John Stracke authored
Showing with 19 additions and 3 deletions.
  1. +19 −3 adder/prelude.+
View
22 adder/prelude.+
@@ -237,6 +237,12 @@
(define (__str__ self)
(% "Non-root enum %s attempts to add a new value, %s."
self.args)))
+(class NonRootValueWithInvalidBase (HasParentMismatch)
+ (define (__init__ self name value-name)
+ ((. HasParentMismatch __init__) self name value-name))
+ (define (__str__ self)
+ (% "Non-root enum %s has a value, %s, which is not a value of the base enum."
+ self.args)))
(defmacro enum (name-and-maybe-parent &rest values)
(define (pred-name name)
@@ -252,8 +258,11 @@
([] name-and-maybe-parent 0)
name-and-maybe-parent))
(name? (pred-name name))
+ (parent (if has-parent
+ ([] name-and-maybe-parent 1)))
+ (parent? (if parent (pred-name parent)))
(parent-list (if has-parent
- `(,([] name-and-maybe-parent 1))
+ `(,parent)
'())))
(+ `(begin
(class ,name ,parent-list)
@@ -265,14 +274,21 @@
([] v 0)))
(v-has-parent (not (or (symbol? v)
(keyword? ([] v 1)))))
+ (v-parent (if v-has-parent
+ ([] v 1)))
(v-parents (if v-has-parent
- `(,name ,([] v 1))
+ `(,name ,v-parent)
`(,name))))
(when (and has-parent (not v-has-parent))
(raise (NonRootEnumWithRootValue name v-name)))
(when (and (not has-parent) v-has-parent)
(raise (RootEnumWithParentValue name v-name)))
- `(begin (class ,v-name ,v-parents
+ `(begin ,@(if v-has-parent
+ `((unless (,parent? ,v-parent)
+ (raise (NonRootValueWithInvalidBase
+ (quote ,name) (quote ,v-name)))))
+ '())
+ (class ,v-name ,v-parents
,@(if (symbol? v)
'()
(list (extract-fields

0 comments on commit edced39

Please sign in to comment.
Something went wrong with that request. Please try again.