Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add macro to Lisp #425

Merged
merged 19 commits into from
Oct 28, 2022
Merged

Add macro to Lisp #425

merged 19 commits into from
Oct 28, 2022

Conversation

vinc
Copy link
Owner

@vinc vinc commented Oct 25, 2022

  • Add macro form (aliased to mac)
  • Add define-macro form (aliased to def-mac)
  • Add quasiquote form (aliased to `)
  • Add unquote form (aliased to ,)
  • Add unquote-splicing form (aliased to ,@)
  • Add (function args body) for functions with multiple arguments
  • Add dotted pair (a . b) equivalent to (cons a b)

@vinc
Copy link
Owner Author

vinc commented Oct 25, 2022

Expanding function declaration shortcut:

> (expand (def (double x) (* x 2)))
(define double (function (x) (* x 2)))

> (def (double x) (* x 2))
double

> (double 2)
4

Expanding cond into if conditionals:

> (expand (cond (false 0) (true 1)))
(if false 0 (if true 1))

@vinc
Copy link
Owner Author

vinc commented Oct 26, 2022

> (def x 'a)
x

> `(x ,x y)
(x a y)

> `(x ,x y ,(+ 1 2))
(x a y 3)

> (expand `(x ,x y ,(+ 1 2)))
(cons (quote x) (cons x (cons (quote y) (cons (+ 1 2) (quote ())))))

@vinc vinc changed the title Add expand to Lisp Add macro to Lisp Oct 26, 2022
@vinc
Copy link
Owner Author

vinc commented Oct 26, 2022

> (define set-10 (macro (x) `(set ,x 10)))
set-10

> (expand (set-10 foo))
(set foo 10)

> (define foo 42)
foo

> (set-10 foo)
foo

> foo
10

@vinc
Copy link
Owner Author

vinc commented Oct 26, 2022

> (define-macro (set-10 x) `(set ,x 10)))
set-10

> (expand (define-macro (set-10 x) `(set ,x 10))))
(define set-10 (macro (x) (cons (quote set) (cons x (cons (quote 10) (quote ()))))))

@vinc
Copy link
Owner Author

vinc commented Oct 27, 2022

> (define x '(1 2 3))
x

> `(+ ,x)
(+ (1 2 3))

> `(+ ,@x)
(+ 1 2 3)

@vinc
Copy link
Owner Author

vinc commented Oct 28, 2022

> '(cons 1 (cons 2 (cons 3 '())))
(cons 1 (cons 2 (cons 3 (quote ()))))

> '(1 . (2 . (3 . '())))
(cons 1 (cons 2 (cons 3 (quote ()))))

@vinc
Copy link
Owner Author

vinc commented Oct 28, 2022

And now we can have:

(define (list . args) (append args '())))

That is equivalent to:

(define list (function args (append args '())))

This reverts commit 10544b5.
@vinc
Copy link
Owner Author

vinc commented Oct 28, 2022

The last commit should go into another PR implementing dotted lists.

@vinc vinc marked this pull request as ready for review October 28, 2022 16:39
@vinc vinc merged commit 0037e24 into trunk Oct 28, 2022
@vinc vinc deleted the feature/lisp-macro branch October 28, 2022 16:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant