Lua introduction for new programmers. Download slides at:

Similar documents
PHP Personal Home Page PHP: Hypertext Preprocessor (Lecture 35-37)

Sprite an animation manipulation language Language Reference Manual

Chapter 4 Defining Classes I

Ruby: Introduction, Basics

Lecture 3. COMP1006/1406 (the Java course) Summer M. Jason Hinek Carleton University

Key Differences Between Python and Java

Paytm Programming Sample paper: 1) A copy constructor is called. a. when an object is returned by value

JavaScript: Sort of a Big Deal,

CSC Web Programming. Introduction to JavaScript

Programming in Lua Getting Started

Unit E Step-by-Step: Programming with Python

Ruby: Introduction, Basics

Flow Control. CSC215 Lecture

Object Oriented Programming is a programming method that combines: Advantage of Object Oriented Programming

Java+- Language Reference Manual

CSE : Python Programming

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Introduction to JavaScript p. 1 JavaScript Myths p. 2 Versions of JavaScript p. 2 Client-Side JavaScript p. 3 JavaScript in Other Contexts p.

Racket: Macros. Advanced Functional Programming. Jean-Noël Monette. November 2013

Object Class. EX: LightSwitch Class. Basic Class Concepts: Parts. CS257 Computer Science II Kevin Sahr, PhD. Lecture 5: Writing Object Classes

Go Cheat Sheet. Operators. Go in a Nutshell. Declarations. Basic Syntax. Hello World. Functions. Comparison. Arithmetic. Credits

Introduction to Programming Using Java (98-388)

SPARK-PL: Introduction

Computer Science II (20082) Week 1: Review and Inheritance

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Ruby: Introduction, Basics

6.001 Notes: Section 15.1

Day 2. COMP 1006/1406A Summer M. Jason Hinek Carleton University

Short Notes of CS201

COMP322 - Introduction to C++ Lecture 01 - Introduction

Functional Programming

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

JavaScript: More Syntax

CS 5142 Scripting Languages

ADVANTAGES. Via PL/SQL, all sorts of calculations can be done quickly and efficiently without use of Oracle engine.

LOON. Language Reference Manual THE LANGUAGE OF OBJECT NOTATION. Kyle Hughes, Jack Ricci, Chelci Houston-Borroughs, Niles Christensen, Habin Lee

CS201 - Introduction to Programming Glossary By

Your First Ruby Script

Lua Quick Reference. Mitchell

Babu Madhav Institute of Information Technology, UTU 2015

Java Bytecode (binary file)

Scheme: Data. CS F331 Programming Languages CSCE A331 Programming Language Concepts Lecture Slides Monday, April 3, Glenn G.

Introduction to Visual Basic and Visual C++ Introduction to Java. JDK Editions. Overview. Lesson 13. Overview

JavaScript: Coercion, Functions, Arrays

DOWNLOAD PDF CORE JAVA APTITUDE QUESTIONS AND ANSWERS

Introduction to C++ Introduction. Structure of a C++ Program. Structure of a C++ Program. C++ widely-used general-purpose programming language

MatchaScript: Language Reference Manual Programming Languages & Translators Spring 2017

egrapher Language Reference Manual

Pace University. Fundamental Concepts of CS121 1

Your first C++ program

Introduction to C++ with content from

QUIZ. How could we disable the automatic creation of copyconstructors

An Introduction to TypeScript. Personal Info


Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

CERTIFICATE IN WEB PROGRAMMING

8. Control statements

Index COPYRIGHTED MATERIAL

CHAPTER 1 Introduction to Computers and Programming CHAPTER 2 Introduction to C++ ( Hexadecimal 0xF4 and Octal literals 031) cout Object

Wrapper Classes double pi = new Double(3.14); 3 double pi = new Double("3.14"); 4... Zheng-Liang Lu Java Programming 290 / 321

Absolute C++ Walter Savitch

CGS 3066: Spring 2015 JavaScript Reference

JavaScript. Training Offer for JavaScript Introduction JavaScript. JavaScript Objects

JAVASCRIPT AND JQUERY: AN INTRODUCTION (WEB PROGRAMMING, X452.1)

Node.js Training JavaScript. Richard richardrodger.com

Decaf Language Reference Manual

Introduction to Visual Basic and Visual C++ Arithmetic Expression. Arithmetic Expression. Using Arithmetic Expression. Lesson 4.

Introduction to Python programming, II

QUIZ. How could we disable the automatic creation of copyconstructors

COP 3014: Fall Final Study Guide. December 5, You will have an opportunity to earn 15 extra credit points.

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Introduce C# as Object Oriented programming language. Explain, tokens,

Lecture 09: Data Abstraction ++ Parsing is the process of translating a sequence of characters (a string) into an abstract syntax tree.

Coral Programming Language Reference Manual

MATLIP: MATLAB-Like Language for Image Processing

the gamedesigninitiative at cornell university Lecture 7 C++ Overview

Objectives. Introduce the core C# language features class Main types variables basic input and output operators arrays control constructs comments

JScript Reference. Contents

PHPoC vs PHP > Overview. Overview

1007 Imperative Programming Part II

Introduction... ix. Chapter 1: Exploring Fundamental Programming Concepts... 1

Appendix. Grammar. A.1 Introduction. A.2 Keywords. There is no worse danger for a teacher than to teach words instead of things.

Tokens, Expressions and Control Structures

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Java Programming with Eclipse

JavaScript. History. Adding JavaScript to a page. CS144: Web Applications

Common LISP-Introduction

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

An Introduction to Scheme

Timing for Interfaces and Abstract Classes

Web Site Design and Development JavaScript

JavaScript Syntax. Web Authoring and Design. Benjamin Kenwright

Comprehensive AngularJS Programming (5 Days)

Python I. Some material adapted from Upenn cmpe391 slides and other sources


PHPoC. PHPoC vs PHP. Version 1.1. Sollae Systems Co., Ttd. PHPoC Forum: Homepage:

Your First C++ Program. September 1, 2010

Object-Oriented Programming in C# (VS 2015)

(800) Toll Free (804) Fax Introduction to Java and Enterprise Java using Eclipse IDE Duration: 5 days

Transcription:

Lua introduction for new programmers Download slides at: http://tstarling.com/presentations

Hello world In [[Module:Hello]] put: Then in a wiki page: local p = {} function p.hello() return 'Hello, world!' return p {{#invoke: Hello hello }} Try it now at http://scribunto.wmflabs.org/

if if colour == 'black' then csscolour = '#000'

if if colour == 'black' then csscolour = '#000' else csscolour = colour

if if colour == 'black' then csscolour = '#000' elseif colour == 'white' then csscolour = '#fff' else csscolour = colour

Equality Single equals is only for assignment if x = y then -- Error: 'then' expected near '=' return 'something' Use double equals for equality if x == y then return 'something'

for f = 1 for i = 1, 5 do f = f * i return 'The factorial of 5 is '.. f

Types String Number Boolean (true/false) nil A few other things

Logic and: both are true if beans and toast then return 'breakfast' or: one or the other or both if chicken or beef then return 'dinner' not: the following thing is false if not hungry then return 'nothing'

Functions Calling functions colour = getdivcolour() Defining functions local function getdivcolour() return 'blue'

Functions Functions let you avoid duplication Functions can have arguments: local function plural(word) return word.. 's' German version left as an exercise to the reader

Two types of functions Functions Local functions for private use within the module Exported functions local function plural(word) return word.. 's' local p = {} function p.hello() return 'hello' return p

Local variables function getdivstart() colour = getdivcolour() return '<div style="background-color: '.. colour.. '">' colour = 'Fuschia' return getdivstart().. colour.. '</div>'

Local variables function getdivstart() local colour = getdivcolour() return '<div style="background-color: '.. colour.. '">' colour = 'Fuschia' return getdivstart().. colour.. '</div>'

Local variables If you don't set a variable to something, it will be nil by default local x if ready then x = 'GO!' -- Now x has "GO!" or nil

Tables Creating a table numbers = { one = 1, two = 2, three = 3 } Accessing a table element return numbers.one -- returns 1 return numbers['one'] -- also returns 1

Numbered tables africanflatbreads = { 'Aish Mehahra', 'Injera', 'Lahoh', 'Ngome' } return africanflatbreads[2] -- returns 'Injera'

Visiting each table element pairs: key/value pairs in random order for name, number in pairs(numbers) do... ipairs: Numeric keys in ascing order for index, bread in ipairs(africanflatbreads) do...

Strings Length s = 'hello' return #s -- returns 5 sub s = 'hello' return s:sub(2, 3) return s:sub(2) return s:sub(-2) -- returns 'el' -- returns 'ello' -- returns 'lo'

Further reading Programming in Lua: http://www.lua.org/pil/ Reference manual: http://www.lua.org/manual/5.2/ Scribunto: https://www.mediawiki.org/wiki/extension:scribunto lua-users.org

Lua introduction for programmers

Lexical Comments reminiscent of SQL Line breaks ignored Semicolons to terminate statements optional, discouraged -- Single line comment --[[ long comment --]]

Data types nil Numbers Single type, floating point Strings 8-bit clean boolean

Data types Functions First class values Return multiple values Multiple return values are not bundled into a data type Anonymous syntax: x = function ()...

Data types Tables Implemented as a hashtable Used for OOP, like JavaScript Literal syntax: {name = value} or {a, b, c} Access with foo.bar or foo['bar']

Operators Not equals: ~= instead of!= Concatenation:.. Length: # Logical: and, or, not Exponentiation: ^ Usual meanings: <, >, <=, >=, ==, +, -, *, /, %

Operator omissions No assignment operators like += Even plain = is not really an operator No bitwise operators No ternary?:

Assignment Like BASIC, assignment is a complete statement, not an expression Multiple assignment: a, b = c, d a, b = foo() Assignment with local variable declaration: Not like this! local a, b = c, d local a = b, c = d

Explicit block Precondition loop Postcondition loop Control structures do... while cond do... repeat... until cond

Control structures If if cond then... elseif cond then... else...

Control structures Numeric for Generic for for i = start, stop do... for i in iterator do... Index variable is local to the loop

Variables Lexical scoping, almost identical to JavaScript An unset variable is identical to a nil variable No special syntax for deletion, just x = nil No error raised for access to undefined variables

Objects Made from tables using a variety of syntaxes, similar to JavaScript Private member variables implemented using lexical scoping, as in JavaScript Dot for static methods: obj.func() Colon for non-static methods: obj:func()

Objects Factory function style example function newobj() local private = 1 local obj = {} function obj:getprivate() return private return obj

Metatables Each table may have an attached metatable Provides operator overloading "index" metatable entry is used for object inheritance and prototype-based OOP

MediaWiki/Lua interface

Module namespace All Lua code will be inside the Module namespace Code editor provided "ace" JavaScript code editor Automatic indenting Syntax highlighting

Invocation {{ #invoke: module_name function_name arg1 arg2 name1 = value1 }} #invoke instances are isolated, globals defined in one are not available in another Only caches are shared

Module structure A module is a Lua chunk that returns an export table require() provided not isolated package library provided

Return value The exported function returns a wikitext string Multiple return values are concatenated Non-string return values are converted to string Metatable entry "tostring" supported

Frame methods Argument access: args local name1 = frame.args.name1 argumentpairs() local t = {} for name, value in frame:argumentpairs() do t[name] = value getparent() Provides access to the parent frame, i.e. the arguments to the template which called #invoke

Frame methods Wikitext preprocessing frame:preprocess('{{template}}') Structured template invocation frame:expandtemplate{ title = 'template', args = {foo = foo}}

Avoiding double-expansion Arguments are already expanded Don't construct preprocessor input from arguments Use frame:expandtemplate()

Future directions Gabriel Wicke's interface: frame:getargument() frame:newparservalue() frame:newtemplateparservalue() All provided but only with stub functionality

Future directions Interwiki module invocation Languages other than Lua Date/time functions Direct access to other core parser functions and variables {{PAGENAME}} {{#ifexist:}} etc.

Further reading Programming in Lua: http://www.lua.org/pil/ Reference manual: http://www.lua.org/manual/5.2/ Scribunto: https://www.mediawiki.org/wiki/extension:scribunto lua-users.org

Try it out Go to http://scribunto.wmflabs.org/ Create a function that takes several arguments and does something to them local p = {} function p.hello(frame) return 'Hello '.. frame.args[1] return p