Paste: vars

Author: mrjbq7
Mode: factor
Date: Thu, 1 Jun 2023 02:10:12
Plain Text |
USING: furnace.sessions kernel locals math namespaces parser
sequences threads words ;
IN: vars

GENERIC: var-get ( var -- value )
GENERIC: var-set ( value var -- )

: var-change ( variable quot -- )
    [ [ var-get ] keep ] dip dip var-set ; inline

: var-on ( variable -- ) t swap var-set ; inline
: var-off ( variable -- ) f swap var-set ; inline
: var-toggle ( variable -- ) [ not ] var-change ; inline
: var-+@ ( n variable -- ) [ 0 or + ] var-change ; inline
: var-inc ( variable -- ) 1 swap var-+@ ; inline
: var-dec ( variable -- ) -1 swap var-+@ ; inline

PREDICATE: dynamic-var < word "var" word-prop "dynamic" = ;
PREDICATE: global-var < word "var" word-prop "global" = ;
PREDICATE: thread-var < word "var" word-prop "thread" = ;
PREDICATE: session-var < word "var" word-prop "session" = ;

M: dynamic-var var-get get ; inline
M: dynamic-var var-set set ; inline

M: global-var var-get get-global ; inline
M: global-var var-set set-global ; inline

M: thread-var var-get tget ; inline
M: thread-var var-set tset ; inline

M: session-var var-get sget ; inline
M: session-var var-set sset ; inline

! TODO: add slot-var
! TODO: add local-var

:: define-var ( type -- )
    scan-word-name :> name
    name create-word-in :> getter
    name "!" append create-word-in :> setter
    getter type "var" set-word-prop
    getter dup [ var-get ] curry ( -- value ) define-declared
    getter make-inline
    setter getter [ var-set ] curry ( value -- ) define-declared
    setter make-inline ;

SYNTAX: DYNAMIC-VAR: "dynamic" define-var ;

SYNTAX: GLOBAL-VAR: "global" define-var ;

SYNTAX: THREAD-VAR: "thread" define-var ;

SYNTAX: SESSION-VAR: "session" define-var ;

New Annotation

Summary:
Author:
Mode:
Body: