;;;; 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 #:calculator #:plus #:minus #:show)) (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 &optional 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)) ;;;; example behavior: calculator (defun calculator (&optional (val 0)) #'(lambda (ac msg) (destructuring-bind (fn &optional param) (content msg) (funcall fn ac msg val param)))) (defun plus (ac msg val param) (become ac (calculator (+ val param)))) (defun minus (ac msg val param) (become ac (calculator (- val param)))) (defun show (ac msg val param) (send (customer msg) val))