References: 1. “Programming in LUA, 2 nd ed.”, Roberto Lerusalmschy Chapters 1-6 2. “Lua Reference Manual” (included in Lua installation or online)

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

References: 1. “Programming in LUA, 2 nd ed.”, Roberto Lerusalmschy Chapters (primarily) 2. “Lua Reference Manual” (included in Lua installation.
CS324e - Elements of Graphics and Visualization A Little Java A Little Python.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14 Web Database Programming Using PHP.
George Blank University Lecturer. CS 602 Java and the Web Object Oriented Software Development Using Java Chapter 4.
Unix Continuum of Tools Do something once: use the command line Do something many times: –Use an alias –Use a shell script Do something that is complex.
Introduction to Python. What is Python? Interpreted object oriented high level programming language – No compiling or linking neccesary Extensible: add.
CSC 9010: Natural Language Processing
CSE 380 – Computer Game Programming Scripting and Lua Lua.
PHP Overview CS PHP PHP = PHP: Hypertext Preprocessor Server-side scripting language that may be embedded into HTML One goal is to get PHP files.
Introduction to Python Lecture 1. CS 484 – Artificial Intelligence2 Big Picture Language Features Python is interpreted Not compiled Object-oriented language.
Introduction to Python
General Computer Science for Engineers CISC 106 Lecture 02 Dr. John Cavazos Computer and Information Sciences 09/03/2010.
School of Computing and Information Systems CS 371 Web Application Programming PHP - Basics Serving up web pages.
1 Python CIS*2450 Advanced Programming Concepts Material for this lecture was developed by Dr. D. Calvert.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
CS 11 java track: lecture 1 Administrivia need a CS cluster account cgi-bin/sysadmin/account_request.cgi need to know UNIX
20-753: Fundamentals of Web Programming 1 Lecture 12: Javascript I Fundamentals of Web Programming Lecture 12: Introduction to Javascript.
Netprog: Java Intro1 Crash Course in Java. Netprog: Java Intro2 Why Java? Network Programming in Java is very different than in C/C++ –much more language.
Games Development 2 Lua Scripting CO3301 Week 6. Contents Introducing Lua –Comparison with Python Lua Language Overview Interfacing Lua with C++
Fantasy && Smart 吴航.  Some concepts  Differences between Lua & C++  Application.
With Python.  One of the most useful abilities of programming is the ability to manipulate files.  Python’s operations for file management are relatively.
Comments in PHP In PHP, we use // to make a singleline comment or /* and */ to make a large comment block. Comment is a part of your PHP code that will.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
A Second Look At ML 1. Outline Patterns Local variable definitions A sorting example 2.
Introducing Python CS 4320, SPRING Resources We will be following the Python tutorialPython tutorial These notes will cover the following sections.
ICAPRG301A Week 2 Strings and things Charles Babbage Developed the design for the first computer which he called a difference engine, though.
CSC 110 Using Python [Reading: chapter 1] CSC 110 B 1.
CSE 154 LECTURE 5: INTRO TO PHP. URLs and web servers usually when you type a URL in your browser: your computer looks up the.
Introducing Python CS 4320, SPRING Lexical Structure Two aspects of Python syntax may be challenging to Java programmers Indenting ◦Indenting is.
Python I Some material adapted from Upenn cmpe391 slides and other sources.
Python Primer 1: Types and Operators © 2013 Goodrich, Tamassia, Goldwasser1Python Primer.
CS105 Computer Programming PYTHON (based on CS 11 Python track: lecture 1, CALTECH)
Perl Tutorial. Why PERL ??? Practical extraction and report language Similar to shell script but lot easier and more powerful Easy availablity All details.
Introduction to Python Dr. José M. Reyes Álamo. 2 Three Rules of Programming Rule 1: Think before you program Rule 2: A program is a human-readable set.
Jim Havrilla. Invoking Python Just type “python –m script.py [arg]” or “python –c command [arg]” To exit, quit() or Control-D is used To just use the.
Chapter SevenModern Programming Languages1 A Second Look At ML.
CSE 130 : Winter 2009 Programming Languages Lecture 11: What’s in a Name ?
Dr. Abdullah Almutairi Spring PHP is a server scripting language, and a powerful tool for making dynamic and interactive Web pages. PHP is a widely-used,
Introduction to Programming Oliver Hawkins. BACKGROUND TO PROGRAMMING LANGUAGES Introduction to Programming.
PROGRAMMING USING PYTHON LANGUAGE ASSIGNMENT 1. INSTALLATION OF RASPBERRY NOOB First prepare the SD card provided in the kit by loading an Operating System.
Lua for TerraME: A Short Introduction Pedro Ribeiro de Andrade Münster, 2013.
Getting Started With Python Brendan Routledge
Lists/Dictionaries. What we are covering Data structure basics Lists Dictionaries Json.
Python C API overview References:
Part 1 Learning Objectives To understand that variables are a temporary named location to store data and that programmers work with different data types.
Lua for TerraME: A Short Introduction Pedro Ribeiro de Andrade São José dos Campos, 2011.
Web Database Programming Using PHP
Lua Extending References:
Intro to ETEC Java.
CS170 – Week 1 Lecture 3: Foundation Ismail abumuhfouz.
Web Database Programming Using PHP
Quiz 11/15/16 – C functions, arrays and strings
بسم الله الرحمن الرحیم.
LING/C SC/PSYC 438/538 Lecture 4 Sandiway Fong.
Lua for TerraME: A Short Introduction
Introduction to Python
An Introduction to Java – Part I, language basics
Escape sequences: Practice using the escape sequences on the code below to see what happens. Try this next code to help you understand the last two sequences.
Teaching London Computing
COMPUTER PROGRAMMING PYTHON
Coding Concepts (Data- Types)
Catharine West Bo Aye Saljin LaRocca references
(more) Python.
CISC101 Reminders Assignment 2 due today.
General Computer Science for Engineers CISC 106 Lecture 03
LCC 6310 Computation as an Expressive Medium
More Basics of Python Common types of data we will work with
Introduction to Python
Presentation transcript:

References: 1. “Programming in LUA, 2 nd ed.”, Roberto Lerusalmschy Chapters “Lua Reference Manual” (included in Lua installation or online)

What is Lua?  A very lightweight scripting language Very bare-bones Easy C integration (note: NOT C++) Reasonably Fast (for a scripting language)  Used as a scripting language in many games WoW The Witcher Legend of Grimlock …  Getting it:  Documentation:  Trivia: “Lua” means moon in Portugese

Our Intro’s steps ( one lecture / mini-lab each / quiz for each ) 1. Get familiar with the non-OOP features (this lecture) 2. OOP-esque programming in Lua 3. Writing a C extension to Lua 4. Embedding Lua in a C program. 5. Integrate Lua into our Engine Note: We’re NOT going to cover a significant amount of Lua. We’ll just look at enough to get started – then it’s up to you!

Running Lua programs  For now… Create a.lua (text) file in Notepad++ Run through the command line  Eventually… No command-prompt. We’ll make ssuge contain an embedded Lua interpreter which works with our C/C++ code.

Simple example ( sample01.lua ) print(“Hello, World”) -- it has to be this…:-) save it to sample01.lua (mine is in c:\Shawnee\2014sp\etgg3802\lua) run “cmd” from the start menu

Something a little more exciting (sample02.lua) -- defines a new factorial function function fact(n) if n == 0 then return 1 else return n * fact(n - 1) end print("Enter a number: ") a = io.read("*number") -- could also be [no args] waits for enter -- "*all" reads file contents(not useful here) -- "*line" reads a line of text -- n reads n (int) characters print(a.. "! = ".. fact(a)) -- concatenation with auto-conversion

Lua types  These are the only built-in constructs in Lua nil (a non-value; useful to indicate garbage) booleans numbers (doubles, (sort of) int’s) strings (ascii 8-bit) tables (the biggie) functions userdata (we’ll see these when embedding) threads (we won’t cover these)  Note: no classes, but we can use tables to do OOP.

string examples (sample03.lua) x = "This is a \"test\"" print(x) y = string.gsub(x, "test", "quiz") print(y) -- “This is a “quiz”” z = [[This is a really long string covering four lines]] print(z) -- useful for a block "comment" q = [==[Another multi-line text]==] -- This is necessary because sometimes we'll use [[ and ]] -- for other things. e.g. a = b[c[i]] (list-like access) -- You can have any number of '=' characters. print(q) r = string.rep(“abc”, 3) print(r) -- abcabcabc

string examples, cont. print("10" + 1) -- "11" print("10 + 1") -- "10 + 1" print("15" * "3") -- "45" (auto-conversion to number) print("-5.3e-10" * "2") e-9 (auto-conversion to number) --print("hello" + 1) -- ERROR print( ) -- "1020" (auto-conversion to string) print(tostring(10).. tostring(20)) -- "1020" (preferred way)

string examples, cont. print("Enter something: ") response = io.read() n = tonumber(response) -- nil if unconvertable if n == nil then error("'".. response.. "' can't be converted to a number" ) else print(n * 2) end xlen = #x -- length of (string) x print("'".. x.. "' is ".. xlen.. " characters long") -- a wrapper for C’s printf function print(string.format("[%s, %d::%0.2f]", "hello", 5, ))

string examples, cont. s = "==qasdlkfjh==slkjh==u==" -- Extracting a sub-string subs = string.sub(s, 2, 10) -- includes start / end indicies -- finds the character *after* each ==, if there is one indx = 1 while true do new_indx, end_indx = string.find(s, "==", indx) if new_indx == nil then break elseif end_indx < #s then print("(".. new_indx.. ") = ".. string.sub(s, end_indx + 1, end_indx + 1)) end indx = new_indx + 1 end

Tables  Nearly everything in Lua is a table.  A table is an associative array A collection of key-value pairs ○ Both can be any Lua “thing” (including other tables!) Unordered Like a python dictionary or STL (C++) maps

Table example (sample04.lua) a = {} -- an empty table a.x = 15 a.y = "hello" a.z = 3.7 print(a) -- table: 003FC9B0 for k, v in pairs(a) do print("key=".. k.. ", value=".. v) end

Table example, cont. a.x = a.x + 1 a["x"] = a["x"] same as line before print(a.x) print(a.q) -- nil: a non-existent key (not an error...) b = a -- Note: a and b are *pointers* to the *same* table b.y = "goodbye" print(a.y) -- goodbye

Table example, cont. -- sorting the keys of a table temp_keys = {} for k, v in pairs(a) do temp_keys[#temp_keys + 1] = k end table.sort(temp_keys) for _, v in pairs(temp_keys) do -- _ is a "don't-care" variable print("a[".. v.. "] = ".. a[v]) end

Table example, cont. -- creating a table with (integer) values c = {[5] = "abc", [6] = "def"} -- integer keys c[7] = "xyz" -- new int key for i = 5, 7 do print(c[i]) end -- Looks a lot like an array, doesn't it in fact there is NO array type in lua. This is it!

Table example, cont. -- lua "lists" start with index 1 by convention. d = {} for i = 1, 10 do d[i] = i * i + 2 end print(#d) (length -- only works for 1-based "lists") print(d[#d]) (last element) print(d[10]) print(d["10"]) -- nil print(d["+10"]) -- nil (not the same key as "10")

Table example, cont. -- table with table values (a 2d table) times_table = {} for i = 1, 10 do times_table[i] = {} for j = 1, 10 do times_table[i][j] = i * j end -- pull out result for 5 * 7 print(times_table[5][7]) -- or times_table[7][5] here

[File] I/O (sample5.lua)  (This is the more general method) And a taste of OOP in Lua -- Wrapping in the assert call will fail if io.open returns nil (error) local f = assert(io.open("test.txt", "r")) count = 1 while true do -- Note the colon here. f is an "object" and we're calling -- the read "method" L = f:read("*line")-- reads one line of text. Could also be -- "*all": reads entire file -- n: Reads n characters if L == nil then break end print("Line #".. count.. " = '".. L.. "'") count = count + 1 end f:close()

local vs. global variables  Normally all variables are global. Regardless of where they're defined  Put the local keyword before the name when creating to limit it to that block  It's good practice to use locals to hide "unncessary" variables Encapsulation

local example (sample06.lua) x = a global function foo() y = also a global print(x) local x = 11 print(x) (masks the global x) if x < 50 then local x = 12 print(x) masks the local above end print(x) (the 12 is no longer masking) end foo() print(x)-- 10 (function locals are gone)

command-line args  An alternate way to call lua from command line: lua script_name.lua arg1 arg2 … lua sample1_07.lua abc -- command-line arguments test for i, val in pairs(arg) do print("arg#".. i.. " = '".. val.. "'") end