Skip to content

Commit

Permalink
micros/walker: add lambda call form
Browse files Browse the repository at this point in the history
  • Loading branch information
cxxxr committed Nov 7, 2023
1 parent 930f17f commit 14276e4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions contrib/walker/example.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,9 @@

(defmethod add :before ((x integer) (y integer))
(print (list x y)))


((lambda (a b)
(+ a b))
1
2)
15 changes: 14 additions & 1 deletion contrib/walker/tests/tests.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -1715,7 +1715,20 @@
((2 1 4) (0 1 3)))
((MICROS/WALKER:COLLECT-HIGHLIGHT-PATHS (DEFMETHOD ADD :BEFORE ((X INTEGER) (Y INTEGER)) (PRINT (LIST X Y)))
(0 0 3))
((1 1 4) (0 0 3)))))
((1 1 4) (0 0 3)))
((MICROS/WALKER:COLLECT-HIGHLIGHT-PATHS
((LAMBDA (MICROS/WALKER::A MICROS/WALKER::B) (+ MICROS/WALKER::A MICROS/WALKER::B)) 1 2) (2 2 0))
((2 2 0) (1 1 0)))
((MICROS/WALKER:COLLECT-HIGHLIGHT-PATHS
((LAMBDA (MICROS/WALKER::A MICROS/WALKER::B) (+ MICROS/WALKER::A MICROS/WALKER::B)) 1 2) (1 2 0))
((1 2 0) (0 1 0)))
((MICROS/WALKER:COLLECT-HIGHLIGHT-PATHS
((LAMBDA (MICROS/WALKER::A MICROS/WALKER::B) (+ MICROS/WALKER::A MICROS/WALKER::B)) 1 2) (1 1 0))
((2 2 0) (1 1 0)))
((MICROS/WALKER:COLLECT-HIGHLIGHT-PATHS
((LAMBDA (MICROS/WALKER::A MICROS/WALKER::B) (+ MICROS/WALKER::A MICROS/WALKER::B)) 1 2)
(0 1 0))
((1 2 0) (0 1 0)))))

(deftest random
(loop :for (act-form expected) :in *test-cases*
Expand Down
21 changes: 20 additions & 1 deletion contrib/walker/walker.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -254,6 +254,13 @@
(arguments :initarg :arguments
:reader ast-arguments)))

(defclass lambda-call-form (ast)
((lambda-form :initarg :lambda-form
:type lambda-form
:reader ast-lambda-form)
(arguments :initarg :arguments
:reader ast-arguments)))

(defclass dynamic-variable (ast)
((symbol :initarg :symbol
:reader ast-symbol)))
Expand Down Expand Up @@ -666,6 +673,14 @@
env
(micros/backend:arglist (first form))))

(defmethod walk-lambda-call-form ((walker walker) form env path)
(with-walker-bindings (lambda-form &rest args) form
(make-instance 'lambda-call-form
:lambda-form (walk-lambda-form walker lambda-form env (cons 0 path))
:arguments (loop :for arg :in args
:for n :from 1
:collect (walk walker arg env (cons n path))))))

(defmethod walk-form ((walker walker) name form env path)
(let ((macrolet-binding (lookup-macrolet-binding env name)))
(if macrolet-binding
Expand All @@ -679,7 +694,7 @@
(walk-macro walker form env path expansion)
(let ((name (first form)))
(if (consp name)
(error 'unimplemented :context "((lambda ...) ...)") ; TODO: lambda form
(walk-lambda-call-form walker form env path)
(let ((binding (lookup-function-binding env name))
(arguments (loop :for arg :in (rest form)
:for n :from 1
Expand Down Expand Up @@ -821,6 +836,10 @@
(defmethod visit (visitor (ast call-local-function-form))
(visit-foreach visitor (ast-arguments ast)))

(defmethod visit (visitor (ast lambda-call-form))
(visit visitor (ast-lambda-form ast))
(visit-foreach visitor (ast-arguments ast)))

(defmethod visit (visitor (ast dynamic-variable))
(values))

Expand Down

0 comments on commit 14276e4

Please sign in to comment.