Sie sind auf Seite 1von 29

Elements of Computing Systems, Nisan & Schocken, MIT Press

www.idc.ac.il/tecs

High-Level Language
Usage and Copyright Notice: Copyright 2005 Noam Nisan and Shimon Schocken This presentation contains lecture materials that accompany the textbook The Elements of Computing Systems by Noam Nisan & Shimon Schocken, MIT Press, 2005. We provide both PPT and PDF versions. The book web site, www.idc.ac.il/tecs , features 13 such presentations, one for each book chapter. Each presentation is designed to support about 3 hours of classroom or self-study instruction. You are welcome to use or edit this presentation as you see fit for instructional and noncommercial purposes. If you use our materials, we will appreciate it if you will include in them a reference to the books web site. If you have any questions or comments, you can reach us at tecs.ta@gmail.com

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 1

Where we are at:


Human Thought Abstract design
abstract interface
Chapters 9, 12

H.L. Language & Operating Sys.

Compiler
abstract interface
Chapters 10 - 11

Software hierarchy

Virtual Machine

VM Translator
abstract interface
Chapters 7 - 8

Assembly Language
Assembler
Chapter 6

abstract interface

Machine Language

Computer Architecture
abstract interface
Chapters 4 - 5

Hardware Platform

Gate Logic
abstract interface
Chapters 1 - 3

Hardware hierarchy

Chips & Logic Gates

Electrical Engineering Physics

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 2

Brief history of programming languages


Machine language Assembler: symbolic programming Fortran: formula translation Algol: structured programming, recursion Pascal, C: industrial strength compilers C++: OO Java, C#: OO done reasonably well Lisp / Scheme / Haskell: Functional programming Many other programming paradigms and languages.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 3

The OO approach to programming


Object = entity associated with properties (fields) and operations (methods) Objects are instances of classes E.g. bank account, employee, transaction, window, gameSession, OO programming: identifying, designing and implementing classes Static class: a collection of static methods.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 4

An OO programming language can be used for


Procedural programming Abstract data types Concrete objects Abstract objects Graphical objects Software libraries And more.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 5

Jack: a typical OO language -- sample applications

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 6

Disclaimer
Although Jack is a real programming language, we dont view it as an end Rather, we view Jack as a means for teaching How to build a compiler How the compiler and the language interface with the OS How the topmost piece in the software hierarchy fits into the picture Jack can be learned (and un-learned) in one hour.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 7

Example 0: hello world

/** Hello World program. */ /** Hello World program. */ class Main { class Main { function void main() { function void main() { /* Prints some text using the standard library. */ /* Prints some text using the standard library. */ do Output.printString(Hello World); do Output.printString(Hello World); do Output.println(); // New line do Output.println(); // New line return; return; } } } }

Java-like syntax Comments Standard library.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 8

Example 1: procedural programming


class Main { class Main { /* Sums up 1 + 2 + 3 + ... + n */ /* Sums up 1 + 2 + 3 + ... + n */ function int sum(int n) { function int sum(int n) { var int i, sum; var int i, sum; let sum = 0; let sum = 0; let i = 1; let i = 1; while (~(i > n)) { while (~(i > n)) { let sum = sum + i; let sum = sum + i; let i = i + 1; let i = i + 1; } } return sum; return sum; } } function void main() { function void main() { var int n, x; var int n, x; let n = Keyboard.readInt("Enter n: "); let n = Keyboard.readInt("Enter n: "); let x = Main.sum(n); let x = Main.sum(n); do Output.printString("The result is: "); do Output.printString("The result is: "); do Output.printInt(sum); do Output.printInt(sum); do Output.println(); do Output.println(); return; return; } } } // Main } // Main

Jack program = collection of one or more classes Jack class = collection of one or more subroutines Jack subroutine: Function Method Constructor
(the example on the left has functions only, as it is object-less)

There must be at least one class named Main, and one of its methods must be named main.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 9

Example 2: OO programming
class BankAccount { class BankAccount { static int nAccounts; static int nAccounts; // account properties // account properties field int id; field int id; field String owner; field String owner; field int balance; field int balance; /* Constructs a new bank account. */ /* Constructs a new bank account. */ constructor BankAccount new(String aOwner) { constructor BankAccount new(String aOwner) { let id = nAccounts; let id = nAccounts; let nAccounts = nAccounts + 1; let nAccounts = nAccounts + 1; let owner = aOwner; let owner = aOwner; let balance = 0; let balance = 0; return this; return this; ... ... } } var int sum; var int sum; // ... More BankAccount methods. // ... More BankAccount methods. var BankAccount b, c; var BankAccount b, c; } // BankAccount } // BankAccount let b = BankAccount.new(Joe); let b = BankAccount.new(Joe); ... ...

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 10

Example 2: typical OO programming (cont.)


class BankAccount { class BankAccount { static int nAccounts; static int nAccounts; // account properties // account properties field int id; field int id; field String owner; field String owner; field int balance; field int balance; // Constructor ... (omitted) // Constructor ... (omitted) /* Deposits money in this account. */ /* Deposits money in this account. */ method void deposit(int amount) { method void deposit(int amount) { let balance = balance + amount; let balance = balance + amount; return; return; } } /* Withdraws money from this account. */ /* Withdraws money from this account. */ method void withdraw(int amount){ method void withdraw(int amount){ if (balance > amount) { if (balance > amount) { let balance = balance - amount; let balance = balance - amount; } } return; return; } } // ... More BankAccount methods. // ... More BankAccount methods. } // BankAccount } // BankAccount

... ... var int sum; var int sum; var BankAccount b, c; var BankAccount b, c; let b = BankAccount.new("Joe"); let b = BankAccount.new("Joe"); do b.deposit(5000); do b.deposit(5000); let c = BankAccount.new("jane"); let c = BankAccount.new("jane"); let sum = 1000; let sum = 1000; do b.withdraw(sum); do b.withdraw(sum); ... ...
slide 11

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

Example 2: typical OO programming (cont.)


class BankAccount { class BankAccount { static int nAccounts; static int nAccounts; // account properties // account properties field int id; field int id; field String owner; field String owner; field int balance; field int balance; // Constructor ... (omitted) // Constructor ... (omitted) /* Prints information about this account. */ /* Prints information about this account. */ method void printInfo() { method void printInfo() { do Output.printInt(ID); do Output.printInt(ID); do Output.printString(owner); do Output.printString(owner); ... ... do Output.printInt(balance); do Output.printInt(balance); var int sum; var int sum; return; return; var BankAccount b, c; var BankAccount b, c; } } /* Destroys this account. */ /* Destroys this account. */ method void dispose() { method void dispose() { do Memory.deAlloc(this); do Memory.deAlloc(this); return; return; } } // ... More BankAccount methods. // ... More BankAccount methods. } // BankAccount } // BankAccount let b = BankAccount.new("Joe"); let b = BankAccount.new("Joe"); // manipulates b... // manipulates b... do b.printInfo(); do b.printInfo(); do b.dispose(); do b.dispose(); ... ...

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 12

Example 3: abstract data types (API + usage)


Motivation: Jack has three primitive data types: int, char, boolean

Fraction API

Using the Fraction API (example)

API = public contract Interface / implementation.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 13

Example 3: abstract data types (implementation)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 14

Example 3: abstract data types (implementation cont.)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 15

Example 4: linked list


/** Represents a linked list. */ /** Represents a linked list. */ class List { class List { field int data; field int data; field List next; field List next; /* Creates a new List object. */ /* Creates a new List object. */ constructor List new(int car, List cdr) { constructor List new(int car, List cdr) { let data = car; let data = car; let next = cdr; let next = cdr; return this; return this; } } /* Disposes this List by recursively disposing its tail. */ /* Disposes this List by recursively disposing its tail. */ method void dispose() { method void dispose() { if (~(next = null)) { if (~(next = null)) { do next.dispose(); do next.dispose(); } } class Foo { class Foo { do Memory.deAlloc(this); do Memory.deAlloc(this); ... ... return; return; // Creates a list holding the numbers (2,3,5). // Creates a list holding the numbers (2,3,5). } } function void create235() { function void create235() { ... ... var List v; var List v; } // class List. } // class List. let v = List.new(5,null); let v = List.new(5,null); let v = List.new(2,List.new(3,v)); let v = List.new(2,List.new(3,v)); ... ... } }
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language slide 16

Jack language specification


Syntax Data types Variable kinds Expressions Statements Subroutine calling Program structure Standard library (for complete language specification, see the book).

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 17

Jack syntax

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 18

Jack syntax (cont.)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 19

Jack data types


Primitive types: Int 16-bit 2s complement (15, -2, 3, ...) Boolean 0 and 1, standing for true and false Char unicode character (a, x, +, %, ...) Abstract data types (supplied by the OS or by the user): String Fraction List

...

Application-specific types: BankAccount Bat / Ball

...

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 20

Jack data types: memory allocation

Object types are represented by a class name and implemented as a reference, i.e. a memory address Memory allocation: Primitive variables are allocated memory space when they are declared Object variables are allocated memory space when they are created via a constructor.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 21

Jack variable kinds and scope

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 22

Jack expressions

No operator priority!
Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language slide 23

Jack Statements
let variable ==expression; let variable expression;
or or

let variable [expression] = expression; let variable [expression] = expression; if (expression) { if (expression) {

} }

statements statements statements statements

else { else {
} }

while (expression) { while (expression) {


} } statements statements

do function-or-method-call; do function-or-method-call;

return expression; return expression;


or or

return; return;
slide 24

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

Jack subroutine calls


General syntax: subroutineName(arg1, arg2, ) Each argument is a valid Jack expression Parameter passing is by value (primitive types) or by reference (object types)

Example: suppose we have

function int sqrt(int n)

This function can be invoked as follows:


sqrt(17) sqrt(x) sqrt(a*c-17) sqrt(a*sqrt(c-17)+3)

Etc.

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 25

Jack subroutine calls (cont.)

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 26

Jack program structure


Each class in a separate file (compilation unit) Jack program = collection of classes, containing a Main.main().

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 27

Jack standard library = language extensions = OS


class Math { class Math { function void init() function void init() Class String abs(int x) Class String { function int { function int abs(int x) constructor String new(int maxLength) constructor String new(int maxLength) function int multiply(int x, int y) function int multiply(int x, int y) Class Array { dispose() Class void method void { dispose() method Array function int divide(int x, int y) function int divide(int x, int y) method int length() function Array new(int method int length() y) functionfunction Arrayx,new(int size) function int min(int {x, int y) size) int min(int class Output { int j) method charOutput method char charAt(int j) charAt(int function class max(int x, int y) function int max(intvoid int y) int x, function dispose() method void dispose() function void moveCursor(int c) int j) i, method method void void setCharAt(int j, char i, int j) method void function intClass Screen { sqrt(int x) moveCursor(int c) function intClass setCharAt(int j, char c) sqrt(int x) printChar(char functionScreen printChar(char c) function void { void method String appendChar(char c) method String appendChar(char c) } } function void clearScreen() s) } } function void clearScreen() s) function void printString(String function void printString(String method void class Memory { eraseLastChar() method voidfunctionMemory setColor(boolean b) eraseLastChar() class void setColor(boolean b) function printInt(int i) function void void { function void printInt(int i) method int intValue() method int function void drawPixel(int x, int y) intValue() function println() function function int peek(int address) y) function drawPixel(int x, int function void voidj) peek(int address) void Keyboard method void setInt(int int Class println() { method voidfunction void drawLine(int x1, int y1, setInt(int j) Class Keyboard function backSpace(){ function void void drawLine(int x1, int y1, void backSpace() int address, int functionfunctionfunction void poke(int x2, int y2) value) char backSpace() function char backSpace()void poke(int x2, int y2) value) int address, int function function char keyPressed() } function } function char doubleQuote() char keyPressed() int y1, function void drawRectangle(int x1, int y1, function char doubleQuote() Class Sys { function Class drawRectangle(int x1, void Sys { size) function Array alloc(intint x2, int y2) function size) function char newLine() Array alloc(intint x2, int y2) function char readChar() function char newLine() function charvoid halt(): readChar() function function voidfunction void halt(): y, int r) drawCircle(int x, int } function voidvoid deAlloc(Array int y, int r) drawCircle(int x, function void deAlloc(Array o) } function o) function String readLine(String message) } function String readLine(String message) } function void error(int errorCode) function void error(int errorCode) } } function int readInt(String message) function int void wait(int duration) message) function readInt(String duration) function void wait(int } } } }

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 28

Perspective
Jack is an object-based language: no inheritance Primitive type system Standard library Our hidden agenda: gearing up to understand how to develop the ... Compiler (projects 10 and 11) OS
(project 12).

Elements of Computing Systems, Nisan & Schocken, MIT Press, www.idc.ac.il/tecs , Chapter 9: High-Level Language

slide 29

Das könnte Ihnen auch gefallen