PLP: Lecture 41
Need to know about Perl w.r.t. what we talked about in “scope” discussion. Also NTK what we talk about Groovy and Lua in slides.
Scripting languages
- “glue languages” - combining existing components written in fast, compiled languages
- web scripting
- extension languages - extend functionality of “scriptable” tools
Design choices are usually flexibility, rapid development, local customization, and dynamic error detection.
History
Two sets of ancestors
- command interpreters like JCL and MS-DOS, sh, csh
- tools for text processing and report generation such as RPG, sed, awk
Characteristics
- Lack of declarations, simple scope rules. Different languages handle local vs global vars differently
- Flexible dynamic typing
- Easy access to other programs
Groovy
- Compiles straight to Java bytecode, so works cleanly with all existing Java objects and libraries
- Closures
Ex.
// initiate variables
int x, y;
x = 2;
// create closure and assign it to variable C
def c = {n -> n * n}
// using C as the ident for the closure
// make a call on that closure
y = c.call(x);
// y will equal 4
Recall that a closure may reference any variables defined within its enclosing lexical scope. Any such variable is said to be bound to the closure. Closures are first class objects in Groovy. A closure may be curried so that a copy of the closure is made w/ one or more of its params fixed to a constant value.
Similar w.r.t. our project in how they mapped constructs to Java byte code.
Lua
Lua 5.0 came out in 2005 with a register based virtual machine (before was stack-based) and co-routines.
Design goals
- Simplicity
- Efficiency- fast compilation (one pass compiler!). Our language from the project was done in multiple passes.
- Portability - clean ANSI C implementation of virtual machine. Also compiles as C++
Implementation
- hand-written scanner
- hand-written recursive descent parser- smaller, more efficient, more portable, fully reentrant, provides better error messages! But if you were designing a language that may be changing in the future, should generate with
yacc
etc - No intermediate representation - emits VM instructions on-the-fly
Types and Values
Dynamically typed - types attached to values rather than variables. Types include
- nil
- boolean
- number - double precision floating point, can select float or long
- string
- table - associative arrays
- function
- userdata - pointers to user memory blocks. heavy - allocated by lua and garbage collected. light - allocated and freed by user
- thread - coroutine
Values are represented as unions in C.
Table implementation
Originally a hash table. Lua 5.0 introduced hybrid structure with optimization for arrays starting at 1. Has hash part and array part.
Threads and coroutines
- asymmetric coroutines
- create, resume, yield (suspends execution of running coroutine and returns control to the call that resumed that coroutine. Stackful - stack saved when coroutine is suspended)
- can be used for cooperative multithreading (control only switches when you want it to), generators, symmetric coroutines