-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprinter.lisp
More file actions
23 lines (18 loc) · 898 Bytes
/
printer.lisp
File metadata and controls
23 lines (18 loc) · 898 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(defpackage :printer
(:use :common-lisp))
(in-package #:printer)
(ast:defvisit literal (value) value)
(ast:defvisit unary (operator right) (format nil "(~a ~a)" (lexer:token-lexeme operator) (accept right)))
(ast:defvisit binary (left operator right) (format nil "(~a ~a ~a)" (lexer:token-lexeme operator) (accept left) (accept right)))
(ast:defvisit grouping (expression) (format nil "(group ~a)" (accept expression)))
(defun visit-printer (object)
"Implements the operation for OBJECT using the visitor pattern."
(case (type-of object)
((ast:binary) (visit-binary object))
((ast:unary) (visit-unary object))
((ast:grouping) (visit-grouping object))
((ast:literal) (visit-literal object))))
(defun accept (obj)
(ast:accept obj #'visit-printer))
(let ((pack (find-package :printer)))
(do-all-symbols (sym pack) (when (eql (symbol-package sym) pack) (export sym))))