Emacs Lisp的第二个子项

Emacs Lisp的第二个子项,emacs,operands,Emacs,Operands,有了这些emac lisp定义,我需要得到(defun操作数(n ast))的正确结果。目前,第一个子对象的工作方式与预期的一样,但对于第二个子对象(操作数(-n1)(cadr ast))而言,第二个子对象为(INT_LITERAL pos),而不是子对象的其余部分((INT_LITERAL pos)(77))。不知道从这里到哪里去。正如你所看到的,我已经做了一些猜测和测试来修正我的解决方案,但没有任何效果。根据我的理解,当我的结果为零时,这意味着帧没有父帧,但我不确定它为什么不打印出整个操作数

有了这些emac lisp定义,我需要得到(defun操作数(n ast))的正确结果。目前,第一个子对象的工作方式与预期的一样,但对于第二个子对象(操作数(-n1)(cadr ast))而言,第二个子对象为(INT_LITERAL pos),而不是子对象的其余部分((INT_LITERAL pos)(77))。不知道从这里到哪里去。正如你所看到的,我已经做了一些猜测和测试来修正我的解决方案,但没有任何效果。根据我的理解,当我的结果为零时,这意味着帧没有父帧,但我不确定它为什么不打印出整个操作数

(defun store (offset value alist)
"Insert the value for this offset, replacing the previous value (if any)."
(cond
   ((null alist)             (list (cons offset value)))    ; ((offset . value))
   ((eq offset (caar alist)) (cons (cons offset value) (cdr alist)))
   (t                        (cons (car alist)
                                   (store offset value (cdr alist))))
   )
)

(defun lookup (offset alist)
"Return the value associated with this offset, or raise an error."
(cond
   ((null alist)             (user-error "UNINITIALISED %s" offset) (exit))
   ((eq (caar alist) offset) (cdar alist))
   (t                        (lookup offset (cdr alist)))
   )
)

;;(setq a (store 1 19 (store 0 17 ())))
;; a
;; (setq a (store 2 20 a))
;; (setq a (store 1 29 a))
;; (lookup 3 ())
;; (lookup 3 a)
;;(lookup 1 a)


;;; Accessors for the various fields in an AST node

(defun position (ast)
"The position stored in an AST node"
(cadar ast)
)

(defun kind (ast)
(caar ast)
)


(defun operand (n ast)
;; Your code goes here.
(if (eq n 0)
      (caadr ast) ;;first child
    (operand (- n 1)(cadr ast)) ;;second child
    )
)

;;(operand (- n 1)(cadr (cadr ast))) gives 77 (#o115, #x4d, ?M)
;;(operand (- n 1)(cadr ast)) gives (INT_LITERAL pos)
;;(operand (- n 1) (cadr (cddr ast))) gives nil
;;(operand (- n 1) (cdr (cadr ast))) gives nil
;; (operand (- n 1)(caddr ast)) gives nil
;;(operand (- n 1)(car ast)) gives wrong type argument listp, pos
;;(operand (- n 1)(cdr ast)) gives nil
;;cadadr, cadr, cadddr, cdadr, caddr, car, cdr

;; (setq ast '((PLUS pos) (( (VARIABLE pos) (b 1) ) ((INT_LITERAL pos) (77) ) ) ))
;; (kind ast) = PLUS
;; (position ast) = pos
;; (operand 0 at) = ((VARIABLE pos)(b 1))
;; (kind (operand 0 ast))= VARIABLE
;; (operand 1 ast)= supposed to equal ((INT_LITERAL pos) (77))
;; (kind (operand 1 ast)) = supposed to equal INT_LITERAL

您的问题不容易理解——我相信您可以将所有代码缩减为更简单的代码

目前,您正在递归调用
操作数
,但是
ast
数据没有该递归所需的嵌套结构,因此事情很快就会崩溃

我想你只是想要这个

(反运算操作数(n ast)
(第n个n(cadr ast)))

非常感谢您的解释和帮助我解决这个问题!现在说得通了