cl-scopes/core/actor.lisp

54 lines
1.3 KiB
Common Lisp

;;;; cl-scopes/core/actor - basic actor definitions
(defpackage :scopes/core/actor
(:use :common-lisp)
(:local-nicknames (:util :scopes/util))
(:export #:actor #:become #:create #:send
#:echo #:inc #:lgi))
(in-package :scopes/core/actor)
;;;; basic message and actor implementations
(defclass message ()
((content :reader content :initarg :content :initform nil)
(customer :reader customer :initarg :customer :initform nil)))
(defun message (content customer)
(make-instance 'message :content content :customer customer))
(defclass actor ()
((behavior :accessor behavior :initarg :behavior :initform #'no-op)))
(defgeneric deliver (ac msg)
(:method ((ac actor) msg)
(funcall (behavior ac) ac msg)))
;;;; the core (Hewitt) actor API
(defun become (ac bhv)
(setf (behavior ac) bhv))
(defun create (bhv &optional (cls 'actor))
(make-instance cls :behavior bhv))
(defun send (addr content &key customer)
(let ((ac addr) (msg (message content customer)))
(deliver ac msg)))
;;;; predefined behaviors
(defun no-op (ac msg))
(defun lgi (ac msg)
(util:lgi (content msg)))
(defun echo (ac msg)
(send (customer msg) msg))
(defun inc (&optional (val 0))
#'(lambda (ac msg)
(let ((c (content msg)))
(if (eq c :show)
(send (create #'lgi) val)
(become ac (inc (+ c val)))))))