The interpreter is an object that handles the translation and execution of code at runtime. It is that what runs any program code and defines a context for it.
( a = 5 + 7; this.cmdLine.postln; )
In the interpreter, this
refers to the interpreter itself, e.g.: this.postln
The interpreter defines global variables (a
… z
), that can be used for interactive programming. Except these single letter variables ("interpreter variables"), all variables have to be defined by the keyword var
(see: Assignment Statements, and Scoping and Closure).
// typical usage a = 4; b = 3; b = b + a; // some sound a = Synth(\default); g = fork { loop { 0.1.wait; a.set(\freq, 200 + 20.0.rand2.postln) } }; g.stop; a.free; // an overview of all the variables this.inspect;
s
is by convention bound to the default server (Server) and should not be changed.set the values of the variables a
through z
to nil.
x = 123; x.postln; this.clearAll; x.postln;
Compile and execute a String.
this.interpret("(123 + 4000).postln");
Compile and execute a String, printing the result.
this.interpretPrint("123 + 4000");
Compile a String and return a Function.
( z = this.compile("(123 + 4000).postln"); z.postln; z.value; )
Reads the file at pathName, compiles it and returns a Function. The file must contain a valid SuperCollider expression, naturally. This will not compile class definitions, only expressions.
Reads the file at pathName, compiles it and executes it, returning the result. The file must contain a valid SuperCollider expression, naturally. This will not compile class definitions, only expressions.
Returns the previously interpreted code.
1 + 2; this.cmdLine
this interpreter variable can be set to evaluate a function with any successfully compiled code. see e.g. the class History.
a = [ ]; // store all the code evaluated in a this.codeDump = { |code| a = a.add(code) }; 1 + 3; f = { "hallo" }; a.postcs; codeDump = nil; // reset to nil.
can be used to modify code before it is interpreted. Given appropriate delimiters, this can be used to implement little languages.
// silly but simple: understand a Saw for every SinOsc this.preProcessor = { |code| code.replace("SinOsc", "Saw") }; { SinOsc.ar(200) * 0.1 }.play; preProcessor = nil; // reset to nil.
Global variables ("interpreter variables") for interactive programming (see Accessing).