Presentation is loading. Please wait.

Presentation is loading. Please wait.

Domain specific programming languages Context Free Art UC Santa Cruz CMPS 10 – Introduction to Computer Science www.soe.ucsc.edu/classes/cmps010/Spring11.

Similar presentations


Presentation on theme: "Domain specific programming languages Context Free Art UC Santa Cruz CMPS 10 – Introduction to Computer Science www.soe.ucsc.edu/classes/cmps010/Spring11."— Presentation transcript:

1 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

2 UC SANTA CRUZ 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 1.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 2.In the same program, you will add a few new lines to the program, and then describe the impact on the final artworks 3.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

3 UC SANTA CRUZ 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

4 UC SANTA CRUZ 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 assembly machine code compiler assembler Microprocessor hardware can understand and execute

5 UC SANTA CRUZ 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 machine-independent assembly machine code #2 compiler assembler machine-independent bytecode interpreter #2 No processing required, is input to Runs on top of machine code #1 interpreter #1

6 UC SANTA CRUZ 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 Doesnt work for some kinds of applications (e.g., device drivers) that require direct access to the hardware

7 UC SANTA CRUZ Interpreters: emulators 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 assembly machine code (original) compiler assembler emulator machine code (physical) No processing required, is input to Runs on top of Assembly source code for Chrono Trigger 65c816 machine code (original SNES) assembler SNES9X emulator Intel x86 machine code (physical) No processing required, is input to Runs on top of Assume was written in assembly; might have been C

8 UC SANTA CRUZ 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 machine code (physical) Runs on top of

9 UC SANTA CRUZ 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)

10 UC SANTA CRUZ 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

11 UC SANTA CRUZ 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: joshij www.contextfreeart.org/gallery/view.php?id=2461

12 UC SANTA CRUZ 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 rule S { CIRCLE{} S {y.2 r 3 s.994} } Name of the rule is S Primitive drawing command (CIRCLE) A call to execute a rule, in this case rule S (i.e., call itself). An example of recursion. Definition of rule S

13 UC SANTA CRUZ 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: R 1, R 2, … R n 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

14 UC SANTA CRUZ 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 } } … Two examples of rule FOUR – CFA will pick one of these two when the rule FOUR is called (see rules SIX and FOUR) Either-or Two examples of rule THREE – CFA will pick one of these two when the rule THREE is called (see rule FOUR)

15 UC SANTA CRUZ 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

16 UC SANTA CRUZ 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


Download ppt "Domain specific programming languages Context Free Art UC Santa Cruz CMPS 10 – Introduction to Computer Science www.soe.ucsc.edu/classes/cmps010/Spring11."

Similar presentations


Ads by Google