FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Suggestions
   Software Suggestions
(Moderator: fry)
   evaluate a string as code?
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: evaluate a string as code?  (Read 425 times)
megamu

megamu+loves+you WWW
evaluate a string as code?
« on: Nov 7th, 2003, 11:23pm »

Here's the scenario:
 
I have a list of objects: object0, object1, object2 etc. with very similar structures.
 
I want to be able to initialize one based on a variable:
 
obj = eval("new Object"+variable);
or
eval("obj = new Object"+variable+";");
 
is there any way to do this?
 
currently I just have a switch loop that I have to modify everytime I add a new Object# object.
 
Thanks
-Lee
 
arielm

WWW
Re: evaluate a string as code?
« Reply #1 on: Nov 8th, 2003, 12:53pm »

unlike javascript, java is a not an interpreted language, thus it's very complicate to simulate the "eval" behavior,
 
but if i understand well what you want to achieve, real-time code-evaluation is not required here.
 
it's rather a matter of using an array of objects of a certain type, e.g.:
 
you have a class named Phasor and you want to instantiate 10 objects of that type:
Code:
Phasor[] phasors = new Phasor[10];

is declaring the array (which is empty at this time, i.e. full of "null" entries)
 
Code:
for (int i = 0; i < 10; i++)
{
  phasors[i] = new Phasor();
}

is populating the array with objects, that from now will be accessible in such a way:
Code:
phasors[3].stop(); // providing stop() is a method of the Phasor class...

 
for more examples, check this tutorial about object-oriented programming in processing:
http://proce55ing.net/discourse/yabb/board_TheoryandPractice_ac_tion_display_num_1065619874.html
 
hth
 

Ariel Malka | www.chronotext.org
megamu

megamu+loves+you WWW
Re: evaluate a string as code?
« Reply #2 on: Nov 9th, 2003, 8:38pm »

ok thanks. I guess I just have to stick with what I have
 
madmerv
Guest
Email
Re: evaluate a string as code?
« Reply #3 on: Nov 26th, 2003, 6:03pm »

Actually, writing an interpreter can be a lot of fun; depending on what you want it to do.
 
Easiest way to do it is parse a string, and send the parsed string parts to a case statement to determine output / etc.
 
More complex ways involve "half-compiling" (making the strings into some unreadible code) --- if you partially compile the code into a more efficient datastream, all sorts of nifty things can happen.. then you're on your way to understanding bitstreams, compression and code optimization..
 
You could, for instance, take processing's major components (video, audio and text) and store the objects elsewhere, then interpret the objects.
 
I wrote a server designed to do just this, but anybody can do it anyway they wish!  try http://www.nationwideinteractive.com/coo2
 
It has a built-in interpreter.
 
 
Pages: 1 

« Previous topic | Next topic »