# CS4001 程式語言 Programming Languague
Textbook: Sebesta, R. *Concepts of Programming Languages*. Pearson Education Limited.
Instructor: 吳怡樂 Yi-Leh Wu
###### tags: `TaiwanTech`, `CS`
https://faculty.csie.ntust.edu.tw/~ywu/cs4001301/
## Syllabus
* 課程宗旨
* 不教語法
* 作業有Java, LISP, Prolog
* 如何用語言表達想法
* 選擇適當程式語言
* Preliminaries: 馮紐曼架構
* CPU: ALU, Control unit
* Memory
* 兩者分離
* 分類
* Imperative: C, Pascal(超過半世紀)
* Object-oriented: Java, C++(80年代成形)
* Functional: LISP, Scheme(超過半世紀)
* Logic: Prolog(獨特的類型)
* Markup: XHTML, XML(不討論)
* 語法抽象模型 Backus-Naur Form
* Parse Tree
## Chapter 1 Preliminaries
* Aliasing 必要之惡,但應該減少
* 可靠性、執行花費、可寫性、可讀性的平衡
* 機器碼的執行:Fetch-execute-cycle
* Hybrid Implementation System(編譯直譯混合)快於純直譯
* JIT(Just-in-Time) Implementation Systems
* 保留預測未來會使用的機器碼
* Java, .NET
* 還在發展預測技術
* 機器架構與軟體開發哲學影響程式語言特性
## Chapter 3
* 語法可能有(優先序)歧異(如C++)
* APL所有運算子優先權相同,設計者需自行顯式指定
* operarional semantics
* virtual machine
* hardware pure interpreter too expensive
* software pure interpreter has sma problem
* machine-dependent
* computer simulation
* 使用一個語言去定義另一個語言將使定義用的語言比被定義的還複雜
* Axiomatic Semantics
* formal logic
* 轉換expression成assertion驗證定義
## Chapter 5
* Static
* 程式加速
* Structure Type Compatibility
* 結構型別相同就不管名稱
* global variable 應避免
* Dynamic scoping 利於使用不利閱讀
## Chapter 6
* Data type 也包含對於 operator 的定義
* bool 雖然用bit省空間,但用byte簡單
* String length
* Static: Java
* limited: C++
* dynamic: Javascript
* Slice
* subrange
* 科學應用
* Array
* Row major: most lang
* Column major: Fortran
* Associative Array
* build-in: Perl, Python, Ruby
* Record (struct)
* Move Corresponding (COBOL): 可以任意複製到不同名的record,不符部分會被丟棄
* record 的 dynamic subscripts 非常罕見且危險且緩慢
* Tuple
* 像是record但沒有名字
* List
* Lisp的主要型別
## Chapter 11
* struct
* public/private/protected
* hidden entities
* interface entities
* inheritance
* constructor
* allocate in heap
* initalize, not create
* destructor
* reclaim heap space
* Java, C#: Garbage collection, no need /rarely use destructor
* header file
* type checking
* friend
* necessary to C++
* Java: package scope
* Java
* all user-defined types are class
* all object allocate on heap
* C#
* struct is record type
* access data member: accessor method (getter/setter)
* property to implementing accessor
* parameterized ADTs
* C++
* parameterized constructor
* templated class
* Java
* must be class
* collection types (LinkedList, ArrayList)
* Encapsulation Constructs
* partial compilation
* Nested Subprogram
* Fortran, ada, Java(rare)
* C
* file can independently compiled
* `#include`
* linker does not ckeck types between header and implementation
* C#
* DLL: collection of classes
* modifier: internal
* Naming Encapsulations
* Namespaces (C++, C#)
* Packages (Java)
* classes in a packege ate partial friends
* `inport <package>` 非必要、但不必處理名稱問題就是方便
* **Javascript沒有這種概念(故不適用於大型程式**
## Chapter 12
* OOP
* procedural, data-oriented (ada, cpp)
* functional program (CLOS)
* pure OOP (Smalltalk)
* ADT + Inheritance + Polymorphism
* Inheritance
* reuse ADT
* term
* derived class/subclass
* parent class/superclass
* method: subprogram
* message: call method
* including name and desination object
* message interface/protocol: entire collection of methods
* access control
* subclass
* client
* diff of superclass and subclass
* add variable, method
* override method
* unvisible part
* variable
* class variable: one per class
* instance variable: one per object
* method
* class method: accept messages to the class
* instance method: accept messages to the object
* DISADVANTAGE
* interdependency
* 沒有達到減少軟體工程師的預期效果
* Dynamic Binding
* polymorphic variable: reference/point to object of class/...
* abstract/virtual method: have no implementation
* abstract class: include virtual method
* Design Issues
* Exclusivity of Object
* Everything is obj: elegance, slow for simple object
* add object to complete system: fast for simple object, confusing type system
* imperative style typeing system but all other are obj: fast for simple and small typing system, still confuse
* Subclass/Subtype diff
* is-a relation between super and sub: behave the same as parent
* derived is a subtype: only can add and override(in limited way)
* Type checking
* polymorphism require dynamic type checking
* but costly
* solutioon: check perameter and return type
* Single/Multiple Inheritance
* Multi: complexity, convenient, class graph not class tree
* Allocation/Deallocation
* where
* like ADT: anywhere (stack, heap)
* all heap: uniform thru pointer or reference 主流
* stack dynamic: if too big?
* Need delete?
* Dynamic/Static Binding
* all binding be dynamic?
* none: not OOP
* all: inefficient
* solution: user specify
* Nested Class
* a class only used by one class, neednt be seen by others
* nesetd inside the using class
* nested inside a subprogram
* how to let them seen each other
* Smalltalk
* pure OOP
* all obj have local memory
* alloc from heap
* implicit dealloc
* 作為純物件可行的指標(先不論效率
* all dynamic type checking
* inherit all instance var/method and class method
* implementation inheritance
* single inheritance
* simple BNF (but still can be ineffienent)
* type error undetected until runtime
* C++
* evolved from SIMULA 67
* widely used OOP (most popular languague already become python)
* mixed typing system
* constructor and destructor
* class need not be the subclass of any class
* access control
* public, private, protected, scope resolution operator
* usage of private derivation: 讓用戶准用不准看,基本上沒用
* dynamic binding: virtual function
* multi inheritance
* 100 times faster than smalltalk
* CIR (Class Instance Record)
* Reflection
* runtime access type and struct and dynamic modify
* 一般來說無用,sofrware tool會用
* IDE, Debugger
* Java部分支援
* getMethod
* getMethods
* getClass
* 缺點
* 效能
* 暴露物件內容
## Expressions and Assignment Statements
* operator
* 大多由左向右
* function side effect: 若function會改變prameter,複合運算無法確保結果
* function lang沒有這個問題(根本沒有變數
* overloading
* unary or binary? 編譯器無法辨認
* ada, C++ 允許自訂,新語言大多不容許
* cast
* 數學限制:除之以零
* 電腦限制:數值溢位
* Relational and Boolean
* Relational
* 各個語言符號可能不同
* Boolean
* 運算元與結果都是布林值
* C 沒有 built-in boolean
* (3<2<1) = true
* 3<2 = false = 0
* 0<1 = true
* Short Circuit
* 已經確定結果,非必要運算子就不處理
* C, C++, Java 會進行
* ada 可以指定是否短路
* 更加不可控的 side-effect
* conditional operator 可以用在 assignment 左側
## Chap8
* if then else 與 while 就可以完成所有控制
* Two-Way selection
* ALGOL: then else 可以有多個敘述,用分號隔開
* C-based(no java): 不用then,要大括號,else接到最接近的一個if上
* Perl: 空的也要大括號
* Python: 使用縮排
* Ruby: 結束要有end
* Multi-Way selection
* 可用two-way模擬
* C-based: switch(expression) case 'const expression':; default:;
* Ruby: case {when then} else end
* 把case編號放在array
* Python: elif
* Iterative statement
* counter control
* FORTRAN 90: label var = start, finish [, stepsize]
* loop variable cannot change, parameter can (因為只估算一次)
* 95: [name:] DO var \n END DO
* C: for 是一個偽裝過後的 while,不是真正的counter control
* C++: 允許宣告local var,control 可以是布林
* Java: control 只能是布林
* logic control
* Pascal: while-do, repeat-until
* C-based: while-do, do-while
* Java control 只能是布林,沒有goto強制切入
* ada沒有post-test
* FORTRAN沒有logic control
* Prel兩個pre-test while為真執行、until為假執行
* nested loop
* unconditional
* C-style break: 一層,有時是必要的
* Java-style labeled break: 類 goto,限定同個檔案內,不要用
* continue: skip this iteration,可以不用
* goto: 不能證明語法,現在的新語言大多不支援
* C# foreach, Java range for
* Cuarded Command (ada, CSP)
* 在程式建立過程中可以一併證明正確性0
* 並行估算多個boolean,多個為真隨機一個statement被選中,全部為假則run-time error/exit loop
## Subprogram
* process abstraction
* fundamental
* single entry(原則上)
* calling program is suspended
* return to calling program
* Term
* definition: interface and action
* call: explicit request to executed
* header: name, kind, formal parameter
* profile (signature): number, order, type of parameter
* C
* prototype: function declaration
* declaration: protocol
* formal parameter:
* Formal Parameter
* positional: 根據位置判定,常用
* keyword: 根據keyword
* default vaule: 不一定需要pass的parameter
* C++要求這類必須出現在末端,才不影響position
* C#可以pass不特定數量的同type
* Procedure
* collection of statement
* Function
* structurally resemble procedures but are semanticallly modeled on mathematical
* Design Issue
* passing method
* in mode: 沒有side effect
* out mode: 在subprogram配置
* inout mode: 有side effect
* pass by value: 複製值,不會有side effect
* pass by result: 結果回parameter
* pass by value result: 以上兩個的缺點
* pass by reference: pass快acess慢
* pass by name: late binding, suubprogram裡的code都被parameter的name取代,大多數時間與reference一樣,只有少數例外
* implement
* runtime stack
* pbr最簡單
* pbr與pbv遇到const無法保證const不被改變
* Fortran: io mode, PbR, 晚期常用PbVR
* C: 預設pbv, pointer則使用pbr
* C++: reference也使用pbr
* Java: 全是pbr,因為object都是用reference,所以pass object相當於pbr
* 考慮效率與單雙向
* 單向安全,但不可能永遠單向
* pass subprogram name
* C: function不行ptf可以
* 晚期語言: 不行
* type check
* FORTRAN 77, C: 沒有
* Pascal, FORTRAN 90, Java: 有
* ANSI C, C++: 自訂
* JS, Perl: 不需要
* local variable
* stack dynamic 最常見,可以反覆利用memory,可以recursion,alloc/dealloc需要時間,indirect addressing,沒有留下紀錄
* static 效率,沒有執行期問題,不能recursion
* nested subprogram
* Shallow binding: dynamic binding
* Deep binding: static binding
* Ad hoc binding:
* overload
* OOP 常見
* Subprogram 名稱相同,protocol 不同
* ada return type也可以用來區分兩個overload
* generic
* Multiarray in C and C++
* declared size of all but first subscript
* in Java and C#
* 只有一維固定長度
* side effect
* in mode 可以減低 se
* return value
* 大部分限制return type
* C不能return array 和 function
* C++允許udt
* ada都可以
* corotine (symmetric control)
* multiple entries
* control itself
* resume: corotine call
* first resume at its beginning
* repeatedly resume forever
* quasi-concurrent execution
## Functional Programming Language
* based on mathematicla functions
* 專注於數學結構而不在乎硬體結構
* functional form: 取 function 作為 parameter 和/或 return function
* 因為執行順序不影響估算結果,FPL內含平行性概念
* single linked list
* 所有的list都是function
* 可以在程式內runtime生成function by EVAL
## Logic programming Language
* declarative programming language
* 只有結果沒有過程
## Exception Handling and Event Handling
* without exception handling
* control goes to OS, program terminated
* with exception handling
* trap some exception (not all) and continuing
* exception
* **unusual** event
* either erroneous or not
* detect by either hardware or software
* may require special processing
* 沒有 exception handling 支援的語言仍可以自行撰寫 define, detect, raise and handle
* 多一個 auxiliary parameter/use return value
* pass label parameter to subprogram (有goto的語言才可行)
* pass exception handling subprogram to subprogram (允許program作為parameter)
* design issue
* scope
* ocurrence bound
* after handler where to continue
* user-defined
* binding
* procedure
* block
* package
* task
* C++
* exception沒有名稱
* user defined
* Java
* 繼承C++
* 把一些designed choice修改
* 所有exception都是object
* 有buildin exception
* IO 一定要handle
* runtime: unchecked complier 不檢查
## Implementing Subprograms
* part
* actural code
* noncode part
* activation record
* local vars
* parameters
* (dynamic link)
* return addr
## 業界常用程式語言概況與程式設計品質管理簡介
講者:政大資訊科學所博士 資策會軟體技術研究院 詹凱軒
* 程式語言類型
* PC
* C: 記憶體管理
* C++: 物件導向
* Java: 物件導向、不控制記憶體可自動回收、跨平台
* C#: 物件導向、自動垃圾回收、.NET
* R: 巨量資料、資料分析
* Python: 物件導向、直譯式、函式庫擴充
* *Matlab: 矩陣數學、初期分析驗證*
* Web
* PHP: 伺服器端、動態
* JSP: 使用Java的PHP
* ASP.NET: 微軟
* JavaScript: 原本是前端語言、後來也在後端可用
* App
* android
* Java
* OS
* Objective-C
* Swift
* 可到開發者官網參考學習
* 程式語言趨勢 2022
* Python 17%
* C
* Java
* C++
* 7 JavaScript
* 15 Matlab
* Frameworks
* 不用從頭開始開發程式,提供現成模板
* 網頁開發
* MVC: 分離讓各自只要專注自己部分,美工做美工、資料做資料
* vaadin: 從後端Java生成前端HTML
* IoT programming tool
* Quality coding
* open video analysis framework
* 分析影片內容、智慧推送廣告
* 函式庫串接 reuse
* 開發作業系統
* 開發IDE
* 開發環境可以使用VM、docker
* git版本控管
* bug tracking
* API格式確保住基本上不用太擔心不同程式語言
**期末考題型**
* 名詞
* process順序
* 物件導向多型是否合法
* exception handling
* 程式語言