Furman - Languages Slides.pptx

advertisement
Lua, Rust, and D
Programming Languages and Design Paradigms
Thomas Furman
•
•
•
•
•
•
Designed and Developed by Roberto Ierusalimschy
Pontifical Catholic University of Rio de Janeiro, Brazil
Created in 1993; Publicly released in July 1994
Most Recent Release: Lua 5.2
Scripting Language
Multi-Paradigm Programming Language
• Imperative and Functional
• Written in only 20,000 lines of code
Lua Introduction
• Written in ANSI C
• Utilizes C API
• Interpreted via a Register-based Virtual Machine
• Fastest Interpreted Scripting Language
• Just-in-time compiler implementation available
• Runnable Cross-Platform using ANSI C
• Embeddable into many other languages
• C, C++, C#, Java, Smalltalk, Ada, Erlang, and more.
Lua Portability
• Small set of reserved words
• {and, break, do, else, elseif, end, false, for, function, if in,
local, nil, not or, repeat, return, then, true, until, while}
• Dynamically typed language
• Variables do not have a type explicitly assigned to them
• Data types are assigned based on the value
• Atomic Data Types
• Double-precision floating point numbers
• Booleans
• Strings
Lua Reserved Words and
Typing
• A built-in native data structure
• Modeled after heterogeneous associative array
• Uses Key and Data Pairing
• Table entries referred to by either
• Key variable name string
• Index number
• Table indices begin at 1 instead of 0
Lua Tables
• Allow for user defined operations for specific values
• For example:
• “meta” + “table”
• Use a metatable to define a operation to concatenate both
strings.
• Metatables are stored as normal Lua tables
• Lua’s form of creating objectivity and overriding
operators
Lua Metatables
• Lua functions are classified as “First-Class” functions
• Treated with high priority by the language
• Given a significant degree of autonomy
• Closures are a form of keeping a value from a function
intact after the function finishes execution
• Closure values are held in memory using special pointers
• Called Upvalues
Lua Functions and Closures
• During compile time, an error will throw control back to
the host and a C error code will be generated
• The user can explicitly generate an error using Lua
functions
• These are similar to catch-try blocks that will throw
potential errors
Lua Error Handling
• Garbage collection occurs automatically via mark-andsweep method.
• Objects no longer reachable or with weak references are
marked
• A table with only weak references is marked
• Garbage collection can be called explicitly
Lua Garbage Collection
•
•
•
•
•
Lua’s form of concurrency
Coroutine exists as a thread
Functions: Create, resume, or yield
Each coroutine has a personal stack
When a coroutine yields, its stack is set aside.
Lua Coroutines
•
•
•
•
•
•
•
Designed by Graydon Hoare
Develop started in 2006
Mozilla took over development in 2009
First released: January 2012
Current Release: Rust v0.8, September 2013
Supported primarily by the open source community
Major Application: Servo
Rust Introduction
• Multi-paradigm language modeled after C and C++
• Functional, Imperative, Object-Oriented
• Primary Goals:
• Safe Memory Management
• Safe Concurrency and Concurrent Controls
Rust Paradigms and Goals
• The Rust Compiler, rustc, is written in Rust itself
• Must use a precompiled “snapshot” version to install
itself
• Uses the LLVM compiler on the backend
• Compiles into a executable binary / machine code file
Rust Compiler
• Claims to be a safe environment for memory
• No Null, Dangling, or Wild Pointers
• Objects are immutable by default
• User has more explicit control over mutability
• Declared variables must be initialized prior to execution
Rust Memory Management
• Functions are determined to be unsafe via specific behavior
• Deferencing null or raw pointers
• Data Races
• Type Inheritance
• Using the “unsafe” keyword before a function allows the
user to bypass the compiler’s strict enforcement
• The user is left to dealing with the integrity of the code
Rust’s “Unsafe” Functions
• First-Class Functions and Closures
• Similar to Lua
• Algebraic data types (enums)
• Class Traits
• Similar to Java interfaces
• Allows classes to share aspects
Rust’s Other Features
• Tasks are Rust’s threads
• Each task is given a stack and a heap
• Stack Memory Allocation – A Slot
• Heap Memory Allocation – A Box
• Tasks can share portions of their stacks with other tasks
• However, these objects must be immutable
• Task States: Running, Blocked, Failing, Dead
• A failing task has been interuptted by another process
• A dead task is only viewable by other tasks
• Task Scheduling
• Each task is given a finite time-slice
• If a task doesn’t finish executing in that time, it is deferred until
later
Rust’s Concurrency
•
•
•
•
•
Designed by Walter Bright
First Alpha Release December 2001
D1 Released in 2007
Current Release: D 2.063.2
Used in production by Facebook as of October 2013
D Introduction
• D is modeled after C and C++
• Originally meant as a reengineering of C++
• Multi-paradigm Language
• Imperative
• Mimics C’s procedural flow and syntact rules
• Object-Oriented
• Single Inheritance and Interfaces like Java
• Meta-programming
• Able to modify other programs during compile and run time
• Functional
• Supports function literals, closures, immutable data types
• Concurrency
• Creation and management of threads and their communication
D’s Paradigms and Goals
• Built as a stand alone language after being modeled from
C and C++
• The DMD compiler converts D source code into
executable machine code
• GDC is a D front-end based gcc compiler
• C and C++ source code cannot be natively compiled in D
• D is able to be extended using the C Runtime Library
D’s Compiler
• Auto-declaration of variables
• Use the “auto” keyword in place of a data type in front of a
variable
• Allows the compiler to choose the most appropriate data type
• Foreach keyword
• Creates a loop to cycle through all entries in a data structure
• Scope Statements
• Executable statements that occur when leaving a scope
• Difference statements occur based on successful, failure, or
abnormal exits out of the scope
• Useful in conjunction with error handling
D’s New Features
• Garbage collector scans threads, stacks, and heaps for
objects and pointers that are no longer used.
• Garbage collection occurs automatically and
independently
• Negative Drawbacks in D:
• Unpredictable when it runs
• Requires all threads to block while garbage collection
occurs
• May run only when memory is close to running out
D’s Garbage Collecting
Download