Code: Select all
(match '((square ?)) '((square s)))
Code: Select all
(match '((square ?)) '((square s)))
Code: Select all
#!/usr/bin/newlisp
(set 'result (match '((square ?)) '((square s))))
(println "=>" result)
(exit)
Code: Select all
~> ./match
=>(s)
>
Code: Select all
(setq *methods* '())
(define-macro (define-method)
(letex ((mtd (args 0 0))
(arglist (rest (args 0)))
(body (cons 'begin (rest (args)))))
(push (list 'mtd 'arglist 'body) *methods* -1)
(unless (lambda? 'm)
(set 'mtd (curry call-method 'mtd)))))
(define (arg-match arg-list , (lst '()))
(dolist (elt arg-list)
(if (and (list? elt) (context? (eval (first elt))))
(push (list (first elt) '?) lst -1)
(push '? lst -1)))
lst)
(define (method-lookup mtd-name arglist)
(catch
(begin
(dolist (mtd *methods*)
(let ((matcher (arg-match arglist))
(matchee (nth 1 mtd)))
(println (list 'match matcher matchee))
(if (and (= (first mtd) mtd-name)
(match matcher matchee)) ;; NOT MATCHING
(throw mtd))))
(throw-error "no matching method found"))))
Code: Select all
(define-class square ()
(len nil)
(wid nil))
(define-class cube (square)
(hgt nil))
(define-method (test (square s))
(+ (slot-value s 'len) (slot-value s 'wid)))
(define-method (test (cube s))
(+ (slot-value s 'len) (slot-value s 'wid) (slot-value s 'hgt)))
Code: Select all
> (context 'CTX) (context MAIN)
> (list CTX)
(CTX)
> (= (list CTX) '(CTX))
nil
> (= (list 'CTX) '(CTX))
true
> (symbol? (first (list CTX)))
nil
> (symbol? (first '(CTX)))
true
> (context? (first (list CTX)))
true
> (context? (first '(CTX)))
nil
> (context? (eval (first '(CTX))))
true
>
Code: Select all
(define (arg-match arg-list , (lst '()))
(map (lambda (elt)
(if (and (list? elt) (context? (eval (first elt))))
(list (sym (name (first elt))) '?) '?))
arg-list))