180 likes | 343 Views
Domain specific programming languages Context Free Art. UC Santa Cruz CMPS 10 – Introduction to Computer Science www.soe.ucsc.edu/classes/cmps010/Spring11 ejw@cs.ucsc.edu 4 May 2011. Homework #3. Will be an assignment working with Context Free Art Described in class today
E N D
Domain specific programming languagesContext Free Art UC Santa Cruz CMPS 10 – Introduction to Computer Science www.soe.ucsc.edu/classes/cmps010/Spring11 ejw@cs.ucsc.edu 4 May 2011
Homework #3 • Will be an assignment working with Context Free Art • Described in class today • Context Free Art is a domain specific programming language for creating computer-generated artwork • www.contextfreeart.org • Program is freely available, PC/Mac/Linux • Standalone version works in PC labs (BE 109) • In the assignment, you will • Take an existing context free art program and modify some of the numbers in it, and then describe the impact on the final artworks generated • In the same program, you will add a few new lines to the program, and then describe the impact on the final artworks • For extra credit, you can write a context free art program that is completely new (from scratch), describe how it works, and give an example of some artwork
Homework #3 • Assignment due Friday, May 13 • Help sessions: • Thursday, May 5, 4-5pm, E2 307 • Tuesday, May 10, 3-5pm, E2 307 • Thursday, May 12, 4-6pm, location TBD • Assignment details will be posted on web by end of day
Recap: Compilation • Compilation process • A programmer writes the source code of a program • Source code: A file of text with programming language instructions • A program called a compiler converts this text into assembly langauge • An assembler then converts the assembly language into machine code • Drawback • The final machine code can only be executed on microprocessors that understand that specific type of machine code • That is, machine code is microprocessor-specific • 6502 machine code does not work on an 8080, etc. source code compiler assembly assembler machine code Microprocessor hardware can understand and execute
Interpreted languages (compiled) • Ideally, we would like the compiled code for a program to be able to run on any machine, no matter what machine code it uses • That is, compile it once, but then run it anywhere • This can be accomplished by creating a machine-independent assembly language and bytecode (machine code) • Then, an interpreter can execute this machine code on each specific microprocessor source code compiler machine-independent assembly assembler machine-independent bytecode No processing required, is input to interpreter #1 interpreter #2 Runs on top of machine code #1 machine code #2
Pros/Cons of interpreted languages • Advantages • A program can be compiled once, and then run on many different types of computer (types of microprocessor) • Write once, run anywhere • Java, C#, Flash/Actionscript are good examples of this • Especially useful for web applications • More control over execution environment • Someone trying to break into a computer system needs to compromise the program, and then the interpreter • Compare with just compromising the program, which then provides direct access to the machine • Disadvantages • Can be slower than languages that compile straight to machine code • Some technologies exist to counteract this: JIT, just-in-time compilation • Doesn’t work for some kinds of applications (e.g., device drivers) that require direct access to the hardware
Interpreters: emulators Assume was written in assembly; might have been C • The interpreter part can be put into several different places in the program stack • Emulator • Machine code runs inside an emulator program (interpreter), instead of directly on hardware • Examples: older game system emulators, MAME source code compiler Assembly source code for Chrono Trigger assembly assembler assembler machine code (original) 65c816 machine code (original SNES) No processing required, is input to No processing required, is input to emulator SNES9X emulator Runs on top of Runs on top of machine code (physical) Intel x86 machine code (physical)
Directly interpreted languages • Some languages completely avoid the compilation step • These languages directly execute inside the interpreter • Advantage • Avoids compilation step (and time required to compile) • Same program runs on many different machines • A developer can make a change and quickly see its effect • More interactive • It is sometimes possible to change a program while it is running • Disadvantage • Typically runs much slower than compiled languages • Examples: PHP, Javascript, Logo, Context Free Art source code compiler interpreter Runs on top of machine code (physical)
Summary of different types of programming langs. • Imperative • Procedural • C, FORTRAN (older versions) • Object-oriented • C++, Java, C#, PHP, Smalltalk • Functional • Haskell, Miranda • Logic programming (not covered in class) • Prolog • Other (domain-specific)
Domain specific programming languages • Most programming languages we have examined so far are called general purpose langauges • That is, they are designed so they are flexible enough to be capable of solving a wide range of problems • The general idea is that most computational problems can be solved by writing a program in a general purpose language • Sometimes solving a specific problem in a general purpose language is awkward • That is, it can be done, but it requires a lot of code to accomplish a conceptually simple task • In this case, sometimes it is possible to create a new programming language dedicated to just one type of task • Such a language is not expected to be useful for solving a wide range of problems: it solves one problem, and does this well • Such languages are known as domain-specific
Context Free Art • A example of a domain specific programming language • Designed to make it easy to create computer-generated artwork startshape T rule T{ 4*{r 90}{ SU{} } } rule SU{ S{} SU{x -2.2 y 3.4 s .8 r -31.1} } rule S{ CIRCLE{} S{y .2 r 3 s .994} } By: joshijwww.contextfreeart.org/gallery/view.php?id=2461
Execution model of Context Free Art • A CFA program begins with a startshape directive • It names the first rule to be executed • In CFA, drawing instructions are given in the form of rules • A rule consists of a series of • 0 or more primitive drawing commands (e.g., CIRCLE) • 0 or more rules • A rule can call itself Definition of rule S Name of the rule is “S” Primitive drawing command (CIRCLE) rule S { CIRCLE{} S {y .2 r 3 s .994} } A call to execute a rule, in this case rule “S” (i.e., call itself). An example of recursion.
Random Rule Selection • For each rule, there can be one definition, or more than one definition • How they are executed varies by each case • One definition for a rule, R • When another rule calls R, the one definition is executed • More than one definition for a rule R: R1, R2, … Rn • When another rule calls R, one of the definitions is picked at random • This is the main way randomness is added into CFA programs • That is, use of multiple rules allows each picture to be different
Example of multiple rule selection startshape SIX rule SIX { FOUR { }} rule FOUR { THREE { } FOUR { x 1 r 5 s 0.4 alpha -0.0009 } } rule FOUR { THREE { } FOUR { y 1 r -5 s 0.4 alpha -0.0009 hue 0.7 sat 0.9 b 1 skew 1.5 0 } } rule THREE { TWO { } THREE { flip 32 s 0.99 alpha -0.0009 hue 0.7 sat 0.9 b 1 skew 2 0 } } rule THREE { TWO { } THREE { flip -32 s 0.99 alpha -0.0009 hue 0.9 sat 0.9 b 1 } } … Either-or Two examples of rule FOUR – CFA will pick one of these two when the rule FOUR is called (see rules SIX and FOUR) Two examples of rule THREE – CFA will pick one of these two when the rule THREE is called (see rule FOUR)
What do all those letter and numbers mean? • Consider this primitive shape • CIRCLE {x 1 y 2 r 45} • This means • Add 1 to the current x position • Add 2 to the current y position • Rotate by 45 degrees • Now draw a circle • By shifting each drawn shape away from the current position, each shape ends up in a different location • Without these position shifts, all shapes would just overwrite each other
Primitive shapes • Square • A unit square • SQUARE{} • Circle • A unit circle – diameter is the same as the side of a square • CIRCLE{} • Triangle • Unit triangle. Same side length as squares • TRIANGLE{} • Defined in terms of path operations • Not covered in class, can draw arcs, splines, etc. • http://www.contextfreeart.org/mediawiki/index.php/Paths