Skip to content

Commit

Permalink
static variable instantiation #2024-static-init
Browse files Browse the repository at this point in the history
  • Loading branch information
gewang committed Nov 30, 2024
1 parent 8275e05 commit 6b8acda
Show file tree
Hide file tree
Showing 25 changed files with 846 additions and 67 deletions.
39 changes: 38 additions & 1 deletion VERSIONS
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,45 @@ ChucK VERSIONS log
------------------


1.5.4.3
1.5.4.3 (December 2024)
=======
(added, at last) class static variable initialization for primitive
and Objects types. (Previously, static variables needed to be
instantiated and initialized separately.) Now they can, e.g.,
----------------------------------------
class Foo
{
// declare a static int in class Foo
5 => static int THE_NUM;
// declare a static SinOsc in class Foo
static SinOsc THE_UGEN(440);
}
// can be accessed as before
<<< Foo.THE_NUM, Foo.THE_UGEN.freq() >>>;
----------------------------------------
SEMANTICS for static variables
----------------------------------------
as before, static variable can only be DECLARED:
a) within a class definition
b) outside of any class function defintion
c) at the outer-most class scope (cannot be in nested { })
-----------------------------------------
COMPILE-TIME checks:
1) a statement containing a static variable declaration
CANNOT access member data/functions
2) otherwise, a statement can access both static and member data.
3) a statement containing a static variable declaration
CANNOT access local (i.e., outside of class def) vars or funcs
even in non-public classes
-----------------------------------------
RUNTIME checks:
4) static initialization statements are run in immediate mode
meaning no time advances; this is enforced at runtime; any
time advances, including waiting on events, will result in
a runtime exception
-----------------------------------------
(added) examples/class/static-init.ck
examples/import/import-test-3.ck
(added) '/usr/lib/chuck' to @import system search paths on Linux and MacOS.
.chug files in system paths will auto-load on start up (.ck files will
not auto-load and needs to be @imported in order to be used); also
Expand Down
56 changes: 56 additions & 0 deletions examples/class/static-init.ck
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// static variables & initialization example
// requires: chuck-1.5.4.3 or higher
//
//-----------------------------------------
// STATIC VARAIBLE INITIALIZATION SEMANTICS
//-----------------------------------------
// static variable can only be DECLARED:
// a) within a class definition
// b) outside of any class function defintion
// c) at the outer-most class scope (cannot be in nested { })
//-----------------------------------------
// COMPILE-TIME checks:
// 1) a statement containing a static variable declaration
// CANNOT access member data/functions
// 2) otherwise, a statement can access both static and member data.
// 3) a statement containing a static variable declaration
// CANNOT access local (i.e., outside of class def) vars or funcs
// even in non-public classes
//-----------------------------------------
// RUNTIME checks:
// 4) static initialization statements are run in immediate mode
// meaning no time advances; this is enforced at runtime; any
// time advances, including waiting on events, will result in
// a runtime exception
//-----------------------------------------

// a class
public class Foo
{
// int
1 => static int S_INT;
// float
2 => static float S_FLOAT;
// dur
3::second => static dur S_DUR;
// time
now + 4::second => static time S_TIME;
// vec3
@(5,6,7) => static vec3 S_VEC3;
// array
[8,9,10,11] @=> static int S_INT_ARRAY[];
// string
static string S_STRING("12");
// ugen
static SinOsc S_SINOSC(440);
}

// print static variables
<<< Foo.S_INT >>>;
<<< Foo.S_FLOAT >>>;
<<< Foo.S_DUR / second >>>;
<<< (Foo.S_TIME-now) / second >>>;
<<< Foo.S_VEC3 >>>;
for( auto i : Foo.S_INT_ARRAY ) <<< i >>>;
<<< Foo.S_STRING >>>;
<<< Foo.S_SINOSC.freq() >>>;
15 changes: 15 additions & 0 deletions examples/import/import-test-3.ck
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// test basic static initialization across @import
// see examples/class/static-init.ck for more info

// import
@import "../class/static-init.ck"

// print static variables from imported class Foo
<<< Foo.S_INT >>>;
<<< Foo.S_FLOAT >>>;
<<< Foo.S_DUR / second >>>;
<<< (Foo.S_TIME-now) / second >>>;
<<< Foo.S_VEC3 >>>;
for( auto i : Foo.S_INT_ARRAY ) <<< i >>>;
<<< Foo.S_STRING >>>;
<<< Foo.S_SINOSC.freq() >>>;
5 changes: 4 additions & 1 deletion src/core/chuck_absyn.h
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,11 @@ struct a_Stmt_
ae_Stmt_Type s_type;
// used to track control paths in non-void functions
t_CKBOOL allControlPathsReturn; // 1.5.1.0 (ge) added
// number of obj refs that needs releasing after | 1.5.1.7
// number of obj refs that needs releasing after | 1.5.1.7 (ge) added
t_CKUINT numObjsToRelease;
// does stmt have a static variable declaration | 1.5.4.3 (ge) added for #2024-static-init
// (used to mark a statement as part of static initializer for a class)
t_CKBOOL hasStaticDecl;

// mushed into one!
union
Expand Down
Loading

0 comments on commit 6b8acda

Please sign in to comment.