From bbf6ae9cbd8f46e9f2e13b8ae7f4fc4bd5161008 Mon Sep 17 00:00:00 2001 From: MadMaurice Date: Fri, 2 Apr 2021 01:07:17 +0200 Subject: [PATCH] Add defun macro --- lib/Minilisp.pm | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lib/Minilisp.pm b/lib/Minilisp.pm index 2d1d303..2e575c2 100644 --- a/lib/Minilisp.pm +++ b/lib/Minilisp.pm @@ -331,6 +331,23 @@ sub macro_progn { } $macros{progn} = \¯o_progn; +sub macro_defun { + my $ts = shift; + + my $ident = shift @$ts; + die "Expected identifier in defun" unless $ident->{type} == IDENT; + + my $body = macro_lambda($ts); + + return sub { + my $ctx = shift; + my $fn = $body->($ctx); + $ctx->{$ident->{value}} = $fn; + return $fn; + } +} +$macros{defun} = \¯o_defun; + sub compile { my ($term) = @_; my @tokens = tokenize($term);