Loading...
Logo
Processing Forum

Preprocessor

in General Discussion  •  Other  •  1 year ago  
Hi.  I'm an instructor using Processing for the first time in a class where we just started using Processing instead of Phrogram for intro game programming (formally, algorithmic thinking and art).

I have a small problem I've been unable to resolve.  After a lot of searching, probably just in the wrong places, I cannot for the life of me locate where there is information on the preprocessor which turns a "sketch" into Java code.  You would think that "what is the appropriate grammar for a sketch" would be the most basic question of all, but I can't find it documented or even in the tutorials!

The best I can determine is that either a sketch consists of one of two things:

1) A sequence of statements that one could place inside the body of a main() method (or a static initialization block), OR

2) ...a sequence of statements which are treated as static variable declarations only(?), followed afterwards by any number of methods and/or nested classes, which may optionally include overridden versions of setup() and draw(), and are used with a single instance of whatever class this beast is in.

I have had to run tests to see which things work and which don't, and I have not yet exhausted all possibilities.  Exactly what is and what is not valid grammar for a sketch is driving me nuts (and regrettably I don't have a lot of time to futz with it this semester), so please point me to the resource that explains this simple concept, or else if you could give me the link to the preprocessor code that I was not able to locate under a cursory examination of the source, that would be fine too (I'll just unravel how it's done).  I must have missed this somewhere obvious, so I apologize for the question!  But perhaps this information should be easier to find, either way...

Anna

Replies(5)

Re: Preprocessor

1 year ago
Hey,

Not sure if this is much help, but processing uses basic functions like setup() and draw() (draw is special, it is run 30 times a second, unless changed by frameRate(), or if your pc is slow). When you hit run, the sketch goes through a series of steps. It first goes through the procompiler, which converts your processing code to java. Then, it goes to the java compiler, where the java is turned into something that the computer can read directly, it then goes to the computer, where it can be executed. Grammar? I think you mean syntax, which is periods and quotes for us, semicolons and parenthesis for the computer. I would reccomend the book Getting Started With Processing, written by the creators of processing themselves, as a good resource. Hope this helps! ;)

-Andrew G.

Re: Preprocessor

1 year ago
I mean formal grammar, not syntax.  I need to know how the code is parsed, not how it is lexed.  All I want is the basic CFG for a sketch so I can teach my students what will parse and what won't; syntax is not a problem.

Re: Preprocessor

1 year ago
andrewgiest17, by default Processing compiles to Java, not to JavaScript.

annatala, if you want the grammar, search a .g file in the source. That's an Antlr file, so you need to know this library to understand it.
It is basically a Java 5 grammar with some little changes to accommodate the dialect of Processing.
There is a bit more in code, I suppose.

Basically, if there is no function, everything is put in a generated setup() function.
Otherwise, everything is put in a class extending PApplet, so code outside of functions is limited to declarations.
Import statements are gathered and grouped on top.
Some things are adjusted: #ABCDEF is changed to an integer, float literals get a trailing F, calls like int(str) are changed to a call to a function, and so on.
You can export the sketch to see the resulting .java file.

Re: Preprocessor

1 year ago
Thank you SO much PhiLho!  That's the bulk of what I was looking for.

I really think the basic info here is missing in the reference and tutorials.  I'll reply in depth when I have time.

Re: Preprocessor

1 year ago
After seeing the info here, I did searches for "active mode" and "static mode" on processing.org.  There is virtually no information explaining this basic concept (even in the wiki), despite the fact that both "modes" are used throughout the examples.

Processing really needs more information in the introductory sections and reference sections.  Even in the rare instance I have found someone explaining what these "modes" are, they are usually explained incorrectly (you don't need a setup or draw definition to have it in "active" mode, for one).

I'll probably put up my own reference guide to using Processing after this semester is out.