Skip to content

Commit

Permalink
!207 Improve error handling of define-case-class in (liii lang)
Browse files Browse the repository at this point in the history
  • Loading branch information
TREE37 authored and da-liii committed Feb 20, 2025
1 parent ef785cc commit ad7eb91
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
40 changes: 24 additions & 16 deletions GoldfishLang.tmu
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,15 @@

,@static-methods

\ \ \
\;

(define (is-normal-function? msg)

\ \ (and \ (symbol? msg)\

\ \ \ \ \ \ \ \ (char=? (string-ref (symbol-\<gtr\>string msg) 0) #\\:)))

\;

(define (static-dispatcher msg . args)

Expand Down Expand Up @@ -424,7 +432,7 @@

\ \ \ \ \ \ \ \ \ \ \ (list ,@(map (lambda (field key-field)

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (string-append
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ `(string-append

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ,(symbol-\<gtr\>string key-field) " "

Expand Down Expand Up @@ -466,29 +474,31 @@

\ \ \ \ \ \ \ \ ((eq? msg :to-string) (%to-string))

\ \ \ \ \ \ \ \ \ \ \ \ \

\ \ \ \ \ \ \ \ ,@(map (lambda (field) `((eq? msg ',(car field)) ,(car field))) fields)

\ \ \ \ \ \ \ \ ,@(map (lambda (field key-field)

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ `((eq? msg ,key-field)
\ \ \ \ \ \ \ \ \ \ \ \ `((eq? msg ,key-field)

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ (,class-name
\ \ \ \ \ \ \ \ \ \ \ \ \ \ (,class-name

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ ,@(map (lambda (f) (if (eq? (car f) (car field)) '(car args) (car f)))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ ,@(map (lambda (f) (if (eq? (car f) (car field)) '(car args) (car f)))

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fields))))
\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fields))))

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ fields key-fields)
\ \ \ \ \ \ \ \ \ \ fields key-fields)

\;
\ \ \ \ \ \ \ \ ((is-normal-function? msg)

\ \ \ \ \ \ \ \ ,@(map (lambda (method expected) `((eq? msg ,expected) (apply ,method args)))
\ \ \ \ \ \ \ \ \ \ (cond

\ \ \ \ \ \ \ \ \ \ \ \ \ \ ,@(map (lambda (method expected) `((eq? msg ,expected) (apply ,method args)))

\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ instance-method-symbols instance-messages)

\;
\ \ \ \ \ \ \ \ \ \ \ \ \ \ (else (value-error ,class-name "No such method: " msg))))

\ \ \ \ \ \ \ \

\ \ \ \ \ \ \ \ ,@(map (lambda (field) `((eq? msg ',(car field)) ,(car field))) fields)

\ \ \ \ \ \ \ \ (else (apply %apply (cons msg args))))))

Expand All @@ -513,8 +523,6 @@
) ; end of let

) ; end of define-macro

\;
</goldfish-chunk>

测试:不带用户自定义方法的样本类person
Expand Down
30 changes: 18 additions & 12 deletions goldfish/liii/lang.scm
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
(import (liii base) (liii string) (liii vector)
(liii list) (liii hash-table) (liii bitwise))
(export
@ cut_
@
define-case-class case-class? == != display* object->string
option none
rich-integer rich-char rich-string
Expand Down Expand Up @@ -83,7 +83,11 @@
`(define (,class-name msg . args)

,@static-methods


(define (is-normal-function? msg)
(and (symbol? msg)
(char=? (string-ref (symbol->string msg) 0) #\:)))

(define (static-dispatcher msg . args)
(cond
,@(map (lambda (method expected) `((eq? msg ,expected) (apply ,method args)))
Expand Down Expand Up @@ -130,18 +134,19 @@
((eq? msg :is-instance-of) (apply %is-instance-of args))
((eq? msg :equals) (apply %equals args))
((eq? msg :to-string) (%to-string))

,@(map (lambda (field) `((eq? msg ',(car field)) ,(car field))) fields)
,@(map (lambda (field key-field)
`((eq? msg ,key-field)
(,class-name
,@(map (lambda (f) (if (eq? (car f) (car field)) '(car args) (car f)))
fields))))
fields key-fields)

,@(map (lambda (method expected) `((eq? msg ,expected) (apply ,method args)))
`((eq? msg ,key-field)
(,class-name
,@(map (lambda (f) (if (eq? (car f) (car field)) '(car args) (car f)))
fields))))
fields key-fields)
((is-normal-function? msg)
(cond
,@(map (lambda (method expected) `((eq? msg ,expected) (apply ,method args)))
instance-method-symbols instance-messages)

(else (value-error ,class-name "No such method: " msg))))

,@(map (lambda (field) `((eq? msg ',(car field)) ,(car field))) fields)
(else (apply %apply (cons msg args))))))

(instance-dispatcher)
Expand All @@ -164,6 +169,7 @@
(is-cond? (eq? (car body) 'cond))
(pred1 ((body 1) 0))
(pred2 ((body 2) 0)))

(and (equal? pred1 '(eq? msg :is-instance-of))
(equal? pred2 '(eq? msg :equals)))))

Expand Down

0 comments on commit ad7eb91

Please sign in to comment.