rodentrabies / bp

Bitcoin Protocol components in Common Lisp
MIT License
37 stars 8 forks source link

Less verbose opcode definition #1

Open rodentrabies opened 5 years ago

rodentrabies commented 5 years ago

Ideally it should look like this:

(defmacro define-opcode (op-name op-code op-hex-code
                         (&rest args)
                         &body (doc &rest body))
  "Define opcode function named OP-NAME for a given OP-CODE.
OP-HEX-CODE is ignored and used only for documentation purposes.

  - Arithmetic:

      (define-opcode op_add 147 #x93 (state (a :integer) (b :integer)) :integer
        (@push (+ a b))
        t)

      (define-opcode op_add 147 #x93 (state)
        (when (>= (length (@stack state)) 2)
          (let ((b (decode-integer (pop (@stack state))))
                (a (decode-integer (pop (@stack state)))))
            (push (encode-integer (+ a b)) (@stack state))
            t)))

  - Stack:

      (define-opcode op_pick 121 #x79 (state (n :integer) :null
        (when (>= (length (@stack state)) (1- n))
          (push (nth (1- n) (@stack state)) (@stack state))
          t))))

      (define-opcode op_pick 121 #x79 (state)
        (when (>= (length (@stack state)) 1)
          (let ((n (decode-integer (pop (@stack state)))))
            (when (>= (length (@stack state)) (1- n))
              (push (nth (1- n) (@stack state)) (@stack state))
              t))))

  - Crypto:

      (define-opcode op_ripemd160 166 #xa6 (state (a :bytes)) :bytes
        (@push (ripemd160 a))
        t)

      (define-opcode op_ripemd160 166 #xa6 (state)
        (when (>= (length (@stack state)) 1)
          (push (ripemd160 (pop (@stack state))) (@stack state))
          t))
"
  )