Friday, 29 July 2016

Continued with Expressions topic:

3.4 – Concatenation

Lua denotes the string concatenation operator by ".." (two dots). If any of its operands is a number, Lua converts that number to a string.
    print("Hello " .. "World")  --> Hello World
    print(0 .. 1)               --> 01
Remember that strings in Lua are immutable values. The concatenation operator always creates a new string, without any modification to its operands:
    a = "Hello"
    print(a .. " World")   --> Hello World
    print(a)               --> Hello

3.5 – Precedence

Operator precedence in Lua follows the table below, from the higher to the lower priority:
             ^
             not  - (unary)
             *   /
             +   -
             ..
             <   >   <=  >=  ~=  ==
             and
             or
All binary operators are left associative, except for `^´ (exponentiation) and `..´ (concatenation), which are right associative. Therefore, the following expressions on the left are equivalent to those on the right:
    a+i < b/2+1          <-->       (a+i) < ((b/2)+1)
    5+x^2*8              <-->       5+((x^2)*8)
    a < y and y <= z     <-->       (a < y) and (y <= z)
    -x^2                 <-->       -(x^2)
    x^y^z                <-->       x^(y^z)
When in doubt, always use explicit parentheses. It is easier than looking up in the manual and probably you will have the same doubt when you read the code again.

3.6 – Table Constructors

Constructors are expressions that create and initialize tables. They are a distinctive feature of Lua and one of its most useful and versatile mechanisms.
The simplest constructor is the empty constructor, {}, which creates an empty table; we saw it before. Constructors also initialize arrays (called also sequences or lists). For instance, the statement
    days = {"Sunday", "Monday", "Tuesday", "Wednesday",
            "Thursday", "Friday", "Saturday"}
will initialize days[1] with the string "Sunday" (the first element has always index 1, not 0), days[2] with "Monday", and so on:
    print(days[4])  --> Wednesday
Constructors do not need to use only constant expressions. We can use any kind of expression for the value of each element. For instance, we can build a short sine table as
    tab = {sin(1), sin(2), sin(3), sin(4),
           sin(5), sin(6), sin(7), sin(8)}
To initialize a table to be used as a record, Lua offers the following syntax:
    a = {x=0, y=0}
which is equivalent to
    a = {}; a.x=0; a.y=0
No matter what constructor we use to create a table, we can always add and remove other fields of any type to it:
    w = {x=0, y=0, label="console"}
    x = {sin(0), sin(1), sin(2)}
    w[1] = "another field"
    x.f = w
    print(w["x"])   --> 0
    print(w[1])     --> another field
    print(x.f[1])   --> another field
    w.x = nil       -- remove field "x"
That is, all tables are created equal; constructors only affect their initialization.
Every time Lua evaluates a constructor, it creates and initializes a new table. Consequently, we can use tables to implement linked lists:
    list = nil
    for line in io.lines() do
      list = {next=list, value=line}
    end
This code reads lines from the standard input and stores them in a linked list, in reverse order. Each node in the list is a table with two fields: value, with the line contents, and next, with a reference to the next node. The following code prints the list contents:
    l = list
    while l do
      print(l.value)
      l = l.next
    end
(Because we implemented our list as a stack, the lines will be printed in reverse order.) Although instructive, we hardly use the above implementation in real Lua programs; lists are better implemented as arrays,

Thursday, 28 July 2016

Today I started with Expressions which include Arithmatic, Relational,Logical operators.

Expressions denote values. Expressions in Lua include the numeric constants and string literals, variables, unary and binary operations, and function calls. Expressions can be also the unconventional function definitions and table constructors.

Lua supports the usual arithmetic operators: the binary `+´ (addition), `-´ (subtraction), `*´ (multiplication), `/´ (division), and the unary `-´ (negation). All of them operate on real numbers.
Lua also offers partial support for `^´ (exponentiation). One of the design goals of Lua is to have a tiny core. An exponentiation operation (implemented through the pow function in C) would mean that we should always need to link Lua with the C mathematical library. To avoid this need, the core of Lua offers only the syntax for the `^´ binary operator, which has the higher precedence among all operations. The mathematical library (which is standard, but not part of the Lua core) gives to this operator its expected meaning.

3.2 – Relational Operators

Lua provides the following relational operators:
    <   >   <=  >=  ==  ~=
All these operators always result in true or false.
The operator == tests for equality; the operator ~= is the negation of equality. We can apply both operators to any two values. If the values have different types, Lua considers them different values. Otherwise, Lua compares them according to their types. Specifically, nil is equal only to itself.
Lua compares tables, userdata, and functions by reference, that is, two such values are considered equal only if they are the very same object. For instance, after the code
    a = {}; a.x = 1; a.y = 0
    b = {}; b.x = 1; b.y = 0
    c = a
you have that a==c but a~=b.
We can apply the order operators only to two numbers or to two strings. Lua compares numbers in the usual way. Lua compares strings in alphabetical order, which follows the locale set for Lua. For instance, with the European Latin-1 locale, we have "acai" < "açaí" < "acorde". Other types can be compared only for equality (and inequality).
When comparing values with different types, you must be careful: Remember that "0"==0 is false. Moreover, 2<15 is obviously true, but "2"<"15" is false (alphabetical order!). To avoid inconsistent results, Lua raises an error when you mix strings and numbers in an order comparison, such as 2<"15".

3.3 – Logical Operators

The logical operators are andor, and not. Like control structures, all logical operators consider false and nil as false and anything else as true. The operator and returns its first argument if it is false; otherwise, it returns its second argument. The operator or returns its first argument if it is not false; otherwise, it returns its second argument:
    print(4 and 5)         --> 5
    print(nil and 13)      --> nil
    print(false and 13)    --> false
    print(4 or 5)          --> 4
    print(false or 5)      --> 5
Both and and or use short-cut evaluation, that is, they evaluate their second operand only when necessary.
A useful Lua idiom is x = x or v, which is equivalent to
    if not x then x = v end
i.e., it sets x to a default value v when x is not set (provided that x is not set to false).
Another useful idiom is (a and b) or c (or simply a and b or c, because and has a higher precedence than or), which is equivalent to the C expression
    a ? b : c
provided that b is not false. For instance, we can select the maximum of two numbers x and y with a statement like
    max = (x > y) and x or y
When x > y, the first expression of the and is true, so the and results in its second expression (x) (which is also true, because it is a number), and then the or expression results in the value of its first expression, x. When x > y is false, the and expression is false and so the or results in its second expression, y.
The operator not always returns true or false:
    print(not nil)      --> true
    print(not false)    --> true
    print(not 0)        --> false
    print(not not nil)  --> false

Tuesday, 26 July 2016

Lua tables are an important part in lua coding. So here is a brief what are lua tables.

The table type implements associative arrays. An associative array is an array that can be indexed not only with numbers, but also with strings or any other value of the language, except nil. Moreover, tables have no fixed size; you can add as many elements as you want to a table dynamically. Tables are the main (in fact, the only) data structuring mechanism in Lua, and a powerful one. We use tables to represent ordinary arrays, symbol tables, sets, records, queues, and other data structures, in a simple, uniform, and efficient way. Lua uses tables to represent packages as well. When we write io.read, we mean "the read entry from the io package". For Lua, that means "index the table io using the string "read" as the key".
Tables in Lua are neither values nor variables; they are objects. If you are familiar with arrays in Java or Scheme, then you have a fair idea of what we mean. However, if your idea of an array comes from C or Pascal, you have to open your mind a bit. You may think of a table as a dynamically allocated object; your program only manipulates references (or pointers) to them. There are no hidden copies or creation of new tables behind the scenes. Moreover, you do not have to declare a table in Lua; in fact, there is no way to declare one. You create tables by means of a constructor expression, which in its simplest form is written as {}:
    a = {}     -- create a table and store its reference in `a'
    k = "x"
    a[k] = 10        -- new entry, with key="x" and value=10
    a[20] = "great"  -- new entry, with key=20 and value="great"
    print(a["x"])    --> 10
    k = 20
    print(a[k])      --> "great"
    a["x"] = a["x"] + 1     -- increments entry "x"
    print(a["x"])    --> 11
A table is always anonymous. There is no fixed relationship between a variable that holds a table and the table itself:
    a = {}
    a["x"] = 10
    b = a      -- `b' refers to the same table as `a'
    print(b["x"])  --> 10
    b["x"] = 20
    print(a["x"])  --> 20
    a = nil    -- now only `b' still refers to the table
    b = nil    -- now there are no references left to the table
When a program has no references to a table left, Lua memory management will eventually delete the table and reuse its memory.
Each table may store values with different types of indices and it grows as it needs to accommodate new entries:
    a = {}     -- empty table
    -- create 1000 new entries
    for i=1,1000 do a[i] = i*2 end
    print(a[9])    --> 18
    a["x"] = 10
    print(a["x"])  --> 10
    print(a["y"])  --> nil
Notice the last line: Like global variables, table fields evaluate to nil if they are not initialized. Also like global variables, you can assign nilto a table field to delete it. That is not a coincidence: Lua stores global variables in ordinary tables. More about this subject in Chapter 14.
To represent records, you use the field name as an index. Lua supports this representation by providing a.name as syntactic sugar for a["name"]. So, we could write the last lines of the previous example in a cleanlier manner as
    a.x = 10                    -- same as a["x"] = 10
    print(a.x)                  -- same as print(a["x"])
    print(a.y)                  -- same as print(a["y"])
For Lua, the two forms are equivalent and can be intermixed freely; but for a human reader, each form may signal a different intention.
A common mistake for beginners is to confuse a.x with a[x]. The first form represents a["x"], that is, a table indexed by the string "x". The second form is a table indexed by the value of the variable x. See the difference:
    a = {}
    x = "y"
    a[x] = 10                 -- put 10 in field "y"
    print(a[x])   --> 10      -- value of field "y"
    print(a.x)    --> nil     -- value of field "x" (undefined)
    print(a.y)    --> 10      -- value of field "y"
To represent a conventional array, you simply use a table with integer keys. There is no way to declare its size; you just initialize the elements you need:
    -- read 10 lines storing them in a table
    a = {}
    for i=1,10 do
      a[i] = io.read()
    end
When you iterate over the elements of the array, the first non-initialized index will result in nil; you can use this value as a sentinel to represent the end of the array. For instance, you could print the lines read in the last example with the following code:
    -- print the lines
    for i,line in ipairs(a) do
      print(line)
    end
The basic Lua library provides ipairs, a handy function that allows you to iterate over the elements of an array, following the convention that the array ends at its first nil element.
Since you can index a table with any value, you can start the indices of an array with any number that pleases you. However, it is customary in Lua to start arrays with one (and not with zero, as in C) and the standard libraries stick to this convention.
Because we can index a table with any type, when indexing a table we have the same subtleties that arise in equality. Although we can index a table both with the number 0 and with the string "0", these two values are different (according to equality) and therefore denote different positions in a table. By the same token, the strings "+1""01", and "1" all denote different positions. When in doubt about the actual types of your indices, use an explicit conversion to be sure:
    i = 10; j = "10"; k = "+10"
    a = {}
    a[i] = "one value"
    a[j] = "another value"
    a[k] = "yet another value"
    print(a[j])            --> another value
    print(a[k])            --> yet another value
    print(a[tonumber(j)])  --> one value
    print(a[tonumber(k)])  --> one value
You can introduce subtle bugs in your program if you do not pay attention to this point.

Monday, 25 July 2016

Now its the second week that we are into the internship. today I am starting Lua data types.
Lua is a dynamically typed language. There are no type definitions in the language; each value carries its own type.
There are eight basic types in Lua: nilbooleannumberstringuserdatafunctionthread, and table. The type function gives the type name of a given value:
print(type("Hello world")) --> string
print(type(10.4*3)) --> number
print(type(print)) --> function
print(type(type)) --> function
print(type(true)) --> boolean
print(type(nil)) --> nil
print(type(type(X))) --> string
print(type(a)) --> nil (`a' is not initialized)
a = 10
print(type(a)) --> number
a = "a string!!"
print(type(a)) --> string
a = print -- yes, this is valid!
a(type(a)) --> function
The last example will result in "string" no matter the value of X, because the result of type is always a string.
Variables have no predefined types; any variable may contain values of any type:
Notice the last two lines: Functions are first-class values in Lua; so, we can manipulate them like any other value. (More about that in Chapter 6.)
Usually, when you use a single variable for different types, the result is messy code. However, sometimes the judicious use of this facility is helpful, for instance in the use of nil to differentiate a normal return value from an exceptional condition.

Friday, 22 July 2016

Identifiers in Lua can be any string of letters, digits, and underscores, not beginning with a digit; for instance
    i      j       i10      _ij
    aSomewhatLongName    _INPUT
You should avoid identifiers starting with an underscore followed by one or more uppercase letters (e.g., _VERSION); they are reserved for special uses in Lua. Usually, I reserve the identifier _ (a single underscore) for a dummy variable.
In Lua, the concept of what is a letter is locale dependent. Therefore, with a proper locale, you can use variable names such as índice or ação. However, such names will make your program unsuitable to run in systems that do not support that locale.
The following words are reserved; we cannot use them as identifiers:
    and       break     do        else      elseif
    end       false     for       function  if
    in        local     nil       not       or
    repeat    return    then      true      until
    while
Lua is case-sensitive: and is a reserved word, but And and AND are two other different identifiers.
A comment starts anywhere with a double hyphen (--) and runs until the end of the line. Lua also offers block comments, which start with --[[ and run until the corresponding ]]. A common trick, when we want to comment out a piece of code, is to write the following:
    --[[
    print(10)         -- no action (comment)
    --]]
Now, if we add a single hyphen to the first line, the code is in again:
    ---[[
    print(10)         --> 10
    --]]
In the first example, the -- in the last line is still inside the block comment. In the second example, the sequence ---[[ does not start a block comment; so, the print is outside comments. In this case, the last line becomes an independent comment, as it starts with --.

Wednesday, 20 July 2016

the basic tagline for my project is to import lua on to the micro controller board.  So i started with learning the basic syntax and semantics of LUA.

Lua is a powerful and fast programming language that is easy to learn and use and to embed into your application.
Lua is designed to be a lightweight embeddable scripting language and is used for all sorts of applications from games to web applications and image processing.
See the about page for details and some reasons why you should choose Lua.

-- hello.lua
-- the first program in every language

io.write("Hello world, from ",_VERSION,"!\n")

Lua has 2 parts :
Luainterpreter part 
functioningsoftware system

Tuesday, 19 July 2016

Day 2

Today we were given the presentation on which we have to do work i.e STM 32.It is the microcontroller that is the main thing on which we will code for the whole 6 months.

We were given our respective machines and cubicles where we will work.

We studied about STM 32,nucleo boards, expansions.

I was given a project on LUA, a light-weight scripting language and was told to study about the basic syntax about it.


Monday, 18 July 2016

first day

Today was my first day  for the internship at ST Microelectronics, Greater noida. There was an orientation program for the new interns with the brief introduction about company, work culture and a ll the security setup etc. We were given ID cards for our entry there.

In the Mid half we were introduced to our mentors who gave a brief introduction about the department and the technologies they are working in.

I was allotted AST department and my mentor name was Mr. Anup Kumar Das.