
Outline • Lua Introduction • Lua History • Lua Features Lua Foundation of PLs Yu Zhang Acknowledgement: Stanford CS242: Programming Languages, http://cs242.stanford.edu/ Course web site: http://staff.ustc.edu.cn/~yuzhang/fopl FOPL: Lua 2 What is a scripting language? Scripting langs: so productive • Common features of a scripting language • Key idea: encode program information as you go – Dynamically typed – e.g. type information, data lifetime – Garbage collected – No one likes commitment! – Rich standard library – Reflection/metaprogramming • Easy to use certain idioms hard to express in static types – Interfaces/duck typing – Polymorphism – Heterogeneous data structures – Extensible classes FOPL: Lua 3 FOPL: Lua 4 Script. langs: semi-specialized Why Lua? • Bash-like scripting languages • Simplest, cleanest scripting language still in use – e.g. Python, Perl • “Correct” scoping – For file manipulation,data crunching, command line parsing • No class system (but can build our own!) • Web scripting languages • Easy to learn in a day – e.g. JavaScript, PHP – Specialized constructs for dealing with webpages or HTTP requests • Born in 1993 at PUC-Rio, Brazil • Embedded scripting languages • https://www.lua.org/pil/ – Mostly just Lua • Data structure: tables (associative arrays) – Lightweight, easy to build, simple semantics for games, config • Coroutines, extensible semantics, embedding files The Evolution of Lua, HOPL III, Jun 9-10, 2007. FOPL: Lua 5 FOPL: Lua 6 1 Lua: Overview Lua Implementation • Lua function • https://www.lua.org/download.html -- Recursive impl. -- Iterative impl. – Lua 5.3: Jan 12, 2015, Lua 5.3.5: Jul 10, 2018 function fact(n) function fact(n) if n == 0 then local a = 1 – Smallish, e.g. Lua 5.1 17000 lines of C return 1 for i = 1, n do else a = a * i – Portable return n * fact(n-1) end end return a – Embeddable end end • can call Lua from C and C from Lua • C – Clean code -- Recursive impl. -- Iterative impl. int fact(int n) { int fact(int n){ • Good for your “code reading club” if (n == 0) int a = 1, i; – Efficiency return 1; for (i = 1; i<n; i++) else a = a * i; • Fast for an interpreted scripting language: lang. simplicity helps return n * fact(n-1); return a; • Presently has a register based VM, pre-compilation supported } } FOPL: Lua 7 FOPL: Lua 8 Lua vs. Modula Syntax Lua: similarities with Scheme • Designed for productive use “The influence of Scheme has gradually increased during Lua’s evolution.” – “Syntactically, Lua is reminiscent of Modula and uses • Simiarities familiar keywords.” [HOPL] – Dynamic typing, first-class values, anonymous functions, closures, ... -- Lua -- Modula-2 function fact(n) PROCEDURE Fact(n: CARDINAL): • would have wanted first-class continuations CARDINAL; • function foo() ... end is syntactic sugar for foo = function () ... end local a = 1 VAR a: CARDINAL; BEGIN a := 1 – Scheme has lists as its data structuring mechanism, for i = 1, n do FOR i := 1 TO n DO while Lua has tables. a = a * i a := a * i; end END; – No particular object or class model forced onto the return a RETURN a; programmer—choose or implement one yourself. end END Fact; FOPL: Lua 9 FOPL: Lua 10 Lua History SOL • Prehistory • SOL as in "Simple Object Language" – Born in 1993 inside Tecgraf – a DSL for a configurable report generator for lithology (Comp. Graphics Tech. Group of PUC-Rio in Brazil) profiles – Lua creators: Roberto Ierusalimschy, Luiz Henrique de • SOL interpreter Figueiredo, and Waldemar Celes – read a report description, and syntax and type check – Lua Ancestors: “These languages, called DEL and specified objects and attributes SOL, were the ancestors of Lua.” • syntax influenced by BibTeX • DEL and SOL were domain-specific languages (DSLs) for Tecgraf-developed interactive graphical programs type @track{ x:number, y:number=23, id=0 } – DEL as in "data-entry language“ type @line{ t:@track=@track{x=8}, z:number* } • for describing data-entry tasks: named and typed fields, T = @track{ y=9, x=10, id="1992-34" } data validation rules, how to input and output data L = @line{ t=@track{x=T.y, y=T.x}, z=[2,3,4] } FOPL: Lua 11 FOPL: Lua 12 2 Motivation for Lua Birth of Lua – DEL users began to ask for more power, e.g. control flow • "Lua"—"moon" in Portuguese (with conditionals and loops) – cf. "SOL"—"sun" in Portuguese – SOL implementation finished, but not delivered, as • SOL’s syntax for record and list construction support for procedural programming was soon to be required T = @track{ y=9, x=10, id="1992-34" } – Conclusion: replace both SOL and DEL by a single, valid in both SOL and Lua. more powerful language • Semantics differ: • Existing Alternatives – tables represent both records and lists; – Tcl: "unfamiliar syntax", bad data description support, – track (here) does not name a record type, it names a Unix only function to be applied. No match for the free, – Lisps: "unfriendly syntax" do-it-yourself atmosphere at Tecgraf – Python: still in its infancy FOPL: Lua 13 FOPL: Lua 14 Lua Feature Evolution Lua Types • Lua designers have shown good judgement. • Lua’s type selection has remained fairly stable. • Learn PL design by asking: – initially: numbers, strings, tables, nil, userdata (pointers to C objects), Lua functions, C functions – What features were added to Lua and why? – unified functions in v3.0; booleans and threads in v5.0 – What features were turned down and why? • Learn PL implementation by asking: • Tables: any value as index – early syntax: @(), @[1,2], @{x=1,y=2} – How were the features implemented? – later syntax: {}, {1,2}, {x=1,y=2}, {1,2,x=1,y=2} – What kind of implementations were not possible due to • sparse arrays OK: {[1000000000]=1} – other implementation choices? – element referencing sugar: a.x for a["x"] – tables with named functions for OO • for inheritance, define a table indexing operation FOPL: Lua 15 FOPL: Lua 16 Tables Extensible Semantics • The syntax of tables has evolved, the semantics of • Goals tables in Lua has not changed at all: – allow tables to be used as a basis for objects and classes – tables are still associative arrays and can store arbitrary • fallbacks in Lua 2.1(备选) pairs of values – One function per operation (table indexing, arithmetic • Effort in implementing tables efficiently operations, string concatenation, order comparisons, and – Lua 4.0, tables were implemented as pure hash tables, function calls) 当操作被应用到错误的值时,调用备选函数 with all pairs stored explicitly • tag methods in Lua 3.0 – Lua 5.0, a hybrid representation for tables: every table – tag-specific fallbacks, any value taggable contains a hash part and an array part, and both parts • metatables and metamethods in Lua 5.0 can be empty. Tables automatically adapt their two parts x = {} according to their contents. function f () return -5 end setmetatable(x, { __unm = f }) return -x --> -5 FOPL: Lua 17 FOPL: Lua 18 3 Expressing OOP Concepts Expressing OOP Concepts FOPL: Lua 19 FOPL: Lua 20 Expressing OOP Concepts Use metatables to add a layer of Overhead issue! indirection and to provide dynamic lookup on the metatable. According to the definition, each A group of related tables may share a instance of an account contains common metatable (which describes an entry for every method their common behavior). member, which leads to a lot of Line 3 creates object if user does not provide one; line 4 calls setmetatable pointers, and a lot of overhead. to set or change the metatable of any new object t, and make t inherit its Assume you have a class with 30 operations from the A table itself methods, then every time you using the index metamethod, accordingly reducing the overhead make an instance of the class, mentioned before. you have to allocate 30 strings and store them all in a table. FOPL: Lua 21 FOPL: Lua 22 Expressing OOP Concepts The derived class LA is just an Scope, Function Calls and instance of A but extended with member l. Storage Management LA inherits new from A. When new at line 20 executes, the self parameter will refer to LA. Therefore, value at “ConceptsinProgrammingLanguages” index index in the metatable of a will Chapter7:Scope,Functions,andStorage be LA. Thus a inherits from LA, which Management inherits from A. When calling a:w at line 21, Lua cannot find a w field in a, so it looks into LA and there it finds the implementation for LA:w. FOPL: Lua 23 4 Scope Simplified Machine Model • Nested blocks, local variables • Storage management – Enter block: allocate space for variables – Exits block: some or all space may be deallocated • Static (lexical) scoping (Lua, etc.) – Global refers to declaration in closest enclosing block • Dynamic scoping – Global refers to most recent activation record FOPL: Lua 25 FOPL: Lua 26 Activation record for in-link block Activation record for function • Control link • Return address – Pointer to previous record on – Location of code to execute stack on function return • Push record on stack • Return-result address – Set new control link to point to – Address in activation record old env ptr of calling block to store – Set env ptr to new record function return val • Pop record off stack • Parameters – Follow control link of current – Locations to contain data record to reset environment from calling block pointer FOPL: Lua 27 FOPL: Lua 28 First-order functions Activation record for static scope • Parameter passing • Control link – pass-by-value: copy value to new activation record – Link to activation record of previous (calling) block – pass-by-reference: copy ptr to new activation record • Access link – Link to activation record
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages8 Page
-
File Size-