lect4_languages

lect4_languages - Talking to Computers Introduction to...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Talking to Computers Introduction to Programming Languages Professor Todd Millstein CS1 Fall 2007, Lecture 5: [email protected] http://www.cs.ucla.edu/~todd What is a Program? the way we make computers do anything instructions or recipe for the machine to execute all a computer does is execute programs iPod, ATM, Internet Explorer, Mac OS X, this talk programs are your computer's software Layers of Software hardware Windows Internet Explorer YouTube Two Views of a Program software layers user interface user's view programmer's view What is the Language of Programs? have to provide programs in a language that the computer understands problem: computers are stupid! machine code: the language of your computer 00000100 01001101 11010000 01010001 10111100 10100010 imagine programming in machine code! tedious, error prone, completely incomprehensible Assembly Language mnemonics for machine language names for each instruction names for storage locations lwz r0,24(r30) add r0,r2,r0 stw r0,28(r30) lwz r2,24(r30) addi r0,r2,1 still not very human friendly How to bridge the human-computer gap? I've got it! ???? High-level Programming Languages write programs in a style that is more natural for humans not tied to a particular machine code language may have a radically different structure than any machine code language Fortran was the first high-level language developed in the 1950s! now there are myriads of high-level languages C, C++, Java, Perl, Python, Ruby, Lisp, ML, Prolog, ... more come along every year But, How to Execute High-level Programs? a compiler automatically translates a high-level program into machine code need a different compiler for each kind of machine the machine program can then be executed (multiple times) an interpreter is a program that directly executes a high-level program executes appropriate machine-code instructions as it reads the HL program analogy: book translation vs. human interpreter Compilers (and Interpreters) are Programs Too! again, the idea of software layers is critical I compile my Java program with a Java compiler that Java compiler was written in C++ and compiled with a C++ compiler that C++ compiler was written in ... can even write a compiler for a language in itself! write Compiler #1 for Java in C++ write Compiler #2 for Java in Java compile Compiler #2 with Compiler #1 throw away Compiler #1 Design Goals for High-Level Languages a well-designed programming language should... be easy to learn support rapid initial development support easy program maintenance and evolution encourage and/or guarantee safety and reliability encourage and/or allow efficiency many of these design goals are in conflict! English as a High-level Language? would be ideal in many ways it's expressive and easy for humans to understand unfortunately, natural languages like English are not feasible programming languages (at least today) English is highly ambiguous and context-dependent "go to the store and get me some milk" computer needs an unambiguous and self-contained list of instructions remember, computers are stupid! So, How Should a High-level Language Look? there's no one right answer wouldn't life be boring if there were? gives me a job! high-level means different things to different people competing programming metaphors always striving toward higher and higher levels Fellini: "a different language is a different vision of life" lots of domain-specific languages Flash, CGI, and HTML for websites Excel for spreadsheet programming Two Common Programming Metaphors imperative programming (e.g., C, Pascal) give the computer a sequence of instructions a more abstract version of machine code / assembly infinite number of storage locations high-level commands like if-then-else, "loops" if x > y then max := x; else max := y; object-oriented programming (e.g., C++, Java) programs consist of communicating "smart objects," each modeling some important entity in the application objects often reflect real-life objects a car that knows how to drive, a door that knows how to open one of the pioneers of OO, Alan Kay, is an adjunct prof in CS won the Turing Award, "computing's Nobel Prize" Example: Tetris Tetris as an Imperative Program focus on actions and their relative orderings create an empty board repeat until no more space on the board: drop a piece when the user hits `s' when the user hits `l' when the user hits `r' stop the piece when it spin the piece clockwise move the piece left move the piece right hits another piece remove completed rows from the board each action may itself consist of multiple sub-actions Tetris as an Object-Oriented Program What are the game's objects? pieces, board What does each object know how to do? piece: be created fall spin stop board: be created remove rows check for end of game What are the relationships among the objects? a board contains multiple pieces Programming is about... controlling computer does exactly what you tell it to problem solving trying to make computer do something you couldn't have (easily) done otherwise e.g., finding an optimal travel route teaching computer can only "learn" to do new things if you tell it how modeling describe salient properties and behaviors of a system creativity limitless possibilities interaction with the computer and with other people fun! Running Programs on the Web What happens when I visit a website? HTML downloaded and displayed on your computer programs executed on my computer gmail runs a Javascript program in your browser web forms implemented as CGI or PHP scripts games and animations implemented as Java applets programs typically have two interacting components the "client" program runs in my browser the "server" program runs at "their" site see the networking lectures for their connection! Problem: How do I execute your programs? my machine is likely different than your machine Mac, Sun, IBM, Dell can all have incompatible instruction sets your machine code is useless to me! One Solution: Source Code you provide source code in a high-level language I compile or interpret this code myself this is how many scripting applications work e.g., my browser has a Javascript interpreter disadvantages I must have a compiler or interpreter for the language you used compiling / interpreting can be slow you have to be willing to give out your source code! A Cool Idea: Virtual Machines define a virtual machine with its own machine-code language not specific to a real architecture somewhat higher-level than real machine code compile programs to virtual machine code a virtual machine program (VM) interprets virtual machine code different VM exists for each kind of computer another example of software layers! same virtual machine code can be used on different computers without re-compiling source code Virtual Machines Compiler virtual machine code Your program Computer A virtual machine code user input Virtual Machine B output to user Computer B Virtual Machines advantages I don't need a compiler or interpreter for your language just an implementation of the virtual machine language-independent -- could compile multiple languages to the same virtual machine (e.g., Microsoft's Common Language Runtime) I don't have to take time to compile your application virtual machine code already close to my machine code you don't have to reveal your precious source code still high-level enough to perform important safety checks Java uses this approach programs compile to the Java "bytecode" language each platform has its own Java virtual machine (JVM) interpreter "write once, run anywhere" JVM performs "bytecode verification" before execution Summary all computers do is execute programs high-level programming languages make computers less stupid! allow programmers to significantly raise the level of discourse in talking to computers compilers and interpreters bridge the gap each high-level language provides its own unique way of thinking about computation programming is a fundamentally creative endeavor create your own model of an application domain abstract to the essence of a problem invent virtual machines and virtual worlds, layer upon layer ...
View Full Document

This note was uploaded on 05/03/2010 for the course CS 1 taught by Professor Rohr during the Spring '08 term at UCLA.

Ask a homework question - tutors are online