Falcon Programming Language By Yin Shi Introduction of

Falcon Programming Language By Yin Shi

Introduction of Falcon l Name of Falcon has been taken by the other two languages: ¡ ¡ l FALCON (Formula and Algorithm Compilation and Notation) FALCON (Fast Array Language Compilation) Falcon is commonly named as a “scripting language” ¡ ¡ ¡ It is an open source project The VM is written in C++ Easily embeddable, and powerful to do small thing Easily extensible and maintainable Full multiplatform

Falcon - Basic Syntax l It uses end for blocks if a do stuff end One line control structure if you use a colon, but only for single statement if a : do stuff l Use /* */ and // for comments l Define variable without a type l ¡ l Variable name must start with a lower or uppercase letter Elementary value types Integer numbers ¡ Floating point number ¡ strings l Mathematical operator supported are: +, -, *, /, and % l Falcon supports also self operator : +=, -=, *=, /=, %= l I/O: input() and print()/printl() ¡

Falcon – Control Structures l Conditional Control Structures l if/else statements ¡ Relational operators are: =, >, <, >=, <= and != - if expression statements [else statements] if expression statements elif expression statements /*other elif */ else statements end

Falcon – Control Structures l if, elif and else block may be shortened to a single row if it contains only one statement, but only for the last statement of the if sequence if/else expression : statement l Logical Operators (and, or, not ) l Falcon supports Self-evaluation of expression ¡ “if” can be shorten like: a > 0 and print (“a>0”) or print(“a<0”) a == 0 and print(“a == 0”) or print (“a!=0”) (a = 0 and print (“a = 0”) or print(“a !=0”)) Using “let” to assign values in self-evaluating expression a == 0 and let (b = “a is zero”) or let (b = “a is not zero”) ¡ <condition> ? <if true>[: <if false>]

Falcon – Control Structures l The switch statement ¡ When a single value is need to be checked against several different values. switch expression case expression [, expression, … , expression] statements… /* more case */ default statements… end Notice: it is very different than C++ and Java, here is no break statement. If any of them is equal to the switch expression, then its statements are executed. Only one case is selected.

Falcon – Control Structures l Loop Control structures l The while statement ¡ it is just like the C++’s while loop while expression statements… [break] statements… [continue] statements… end - The while statement can be abbreviated with colon if the loop is composed by only one statement while expression : statement

Falcon – Control Structures l The for statement for variable = expression_start to expression_end [step expression_amount] statements… [break | continue] statements… end ¡ for statement can be abbreviated on a single line if the loop contains only one statement using colon “: ” for variable = exprssion_start to expression_end [step expression_amount] : statement

Falcon – More on Lines and statements l It is possible to split a statement on more than one line by putting a backslash(“” and the end of it) Example: if a_very_long_name and another_very_long_name or a_very_long_name l It is possible to put statements in one line, this can be achieved by separating different statement with the semicolon sign (“; ”) Example: if a = 0; print(“Expression is “); a += 2; printl(a); end This just a if statement, end can not be ignored

Falcon – Basic Data Structures l Arrays ¡ An array is a list of items, and it can be simply defined using the [], items inside the array can be of any kind, it is very different than C++ and Java’s Example: List = [“person”, 1, 3. 5, int(“ 123”), var 1] List = “person”, 1, 3. 5, int(“ 123”), var 1 ¡ A list may be immediately assigned to a literal list of symbols to “expand it” a, b, c = 1, 2, 3 list = 4, 5, 6 a, b, c = list Note: If the size of the list is different from the target set, the compiler will raise an error
![Falcon – Basic Data Structures ¡ Accessing an array by [] operator for i Falcon – Basic Data Structures ¡ Accessing an array by [] operator for i](http://slidetodoc.com/presentation_image_h2/eac65f6b8424d25ef6daaa371003f105/image-11.jpg)
Falcon – Basic Data Structures ¡ Accessing an array by [] operator for i = 0 to len(List)-1 printl(“Element n”, I, “: “, List[i]) end ¡ It is possible to access more than one item at a time, a range is defined as a part of integers so that R[n: m] (all items from n to m-1) List 1 = List[: ] // the whole array List 2 = List[0: 4] // 0, 1, 2, 3 ¡ A range can contain negative indexes, negative indexes mean “distance from end”, -1 being the last item

Falcon – Basic Data Structures ¡ An array can have ranges with the first number being greater than the last one, in this case, the last index is inclusive List 4 = List[3: 0] // the first 4 elements in reverse order List 5 = List[-1: 0] // The whole array reverse ¡ It is possible to use plus operator “+” or self assignment operator to extend a list a = [1, 2]; b = [3, 4]; c = a+b // c = [1, 2, 3, 4] c += “data” // c = [1, 2, 3, 4, “data” ¡ Multi-dimensional array List = [[1, 2], [3, 4], [4, 5]] List[0] = [1, 2]; List[0][0] = 1

Falcon – Basic Data Structures l Strings ¡ Strings can be considered a basic structures as they can be accessed exactly as arrays that may only have characters as items. ¡ Access a String using a for loop for i = 0 to len(string) -2 print(string[i], “, ”) end printl(string[-1]) // the last letter l Dictionaries It is the most flexible basic structure ¡ The dictionary index may be a string, or any kind of object ¡ The dictionary is defined as a set of pairs, key and value ¡ It is possible to find a value in a dictionary by knowing its key ¡ Define a dictionary using arrow operator (=>) dict = [=>]; dict = [“a” => 123, “b” => “onetwothree”]; dict[“a”] = 123

Falcon - The “in” Operator l The in relational operator checks for an item exist in a sequence. If the item is found the value is true (1) otherwise the value is false (0) - Example: name = “abbaab” if “abba” in name printl(“true”) end if “abba” not in name printl(“true”) end dict = [“one” => 1] if “one” in dict printl(“aways true”) end l The for/in loop - The for/in loop traverses a collection of items (an array or a dictionary), usually from the first item to the last one, and provides the user with a variable assuming the value of each element in turn.
![Falcon – The “in” Operator for variable [, variable …] in collection …statements… [break Falcon – The “in” Operator for variable [, variable …] in collection …statements… [break](http://slidetodoc.com/presentation_image_h2/eac65f6b8424d25ef6daaa371003f105/image-15.jpg)
Falcon – The “in” Operator for variable [, variable …] in collection …statements… [break | continue] …statements… first time only statements last … last time only statements end Examples: array = [“have”, “a”, “nice”, “day”] for element in array print(element, “”) last printl(element, “. ”) end Have a nice day!

Falcon – The Functions l Functions are piece of code that may be reused again and again by providing them with different values, called parameters. ¡ Declare a function using keyword “function” function_name ([parameters]) statements. . end ¡ Functions executing just one statement may be abbreviated with the colon indicator(“: ”) function_name ([parameters]) : statement | return value ¡ Example: function square(x) y = x*x ; return y end function square(x) : return x*x Calling a function: a = square(8)

Falcon – The Functions All the code that is not in a function is considered to be “the main program”. ¡ A function may be called with any number of parameters. If less than the declared parameters are passed to the function, the missing ones will be filled with nil. l Recursion Falcon provides recursion function sum_of_first(x) if x > 1; return x + sum_of_first(x-1); end return x end l Local and global variable names ¡ Whenever you declare a parameter or assign variable in a function for the first time, that name becomes “local”. This prevents from accidentally overwrite a variable that may be useful elsewhere.

Falcon - Functions ¡ l Using “global” keyword to access the variable outside the function square_in_z(x) global z; z = x*x end z = 0; square_in_z(8); printl(z) // z is 64 Static block ¡ Sometimes it is useful to have a function that remember how its variables were configured when it last was called. ¡ Using global imported names is dangerous ¡ The statements inside the static block are executed only once, the first time the function is ever called, and they stay the same up to the next call. function_name ([parameters]) static statements… end statements end
![Falcon - Functions • Example: function say_something() static data = [“have”, “a”, “nice”, day”] Falcon - Functions • Example: function say_something() static data = [“have”, “a”, “nice”, day”]](http://slidetodoc.com/presentation_image_h2/eac65f6b8424d25ef6daaa371003f105/image-19.jpg)
Falcon - Functions • Example: function say_something() static data = [“have”, “a”, “nice”, day”] current = 0 end if current = len(data) ; return; end element = data[current]; current += 1; return element end thing = say_something() while thing != nil print(thing, “ ”) thing = say_something() // current will remember its last position end printl()

Falcon – Objects l Falcon stand alone objects ¡ ¡ ¡ Contains properties and methods Methods in object without keyword “function” Object declaration: object_name [from class 1, class 2, …] property 1 = constant property 2 = constant … method 1 ([parameters]) statements… end method 2 ([parameters]) statements… end ¡ end Objects can “inherit” from classes

Falcon - Objects ¡ The new keyword “self”, refers to the “object that is currently handled by the method”. It is necessary for every method to use the self object to access object properties. Example: object cashbox amount = 0 deposit(qt, interest_rate) amount = qt * interest_rate self. amount += amount end ¡ Once an object is defined, it is not possible to add new properties or new methods, but it is possible to change its methods and properties at will

Falcon – Objects ¡ Example: function new_deposit(qt) if self. amount + qt > 5000 printl(“Sorry, amount is > 5000”) else printl(“Ok, will deposit the money”) self. amount += qt end old_deposit = cashbox. deposit = new_deposit cashbox. deposit(10000) // we are now forbidden to do that old_deposit(10000) // but the old method still works printl(cashbox. amount) // will be initial amount + 10000

Falcon – Objects l The “provides” and “in” operators for objects ¡ The “provides” keyword is a relational operator that assumes the value of 1 (true) if a certain object provides a certain property. if not self provides amount ¡ The “in” will check for a string to be the same as a property names in the object if not “amount” in self l Objects and attributes Every object can be given a set of a maximum 32 boolean attributes [name] attribute 1 attribute 2 /*other attributes end attributes[name] : attribute 1, attribute 2 …

Falcon - Objects - Example: attributes for_a_cashbox open end object cashbox amount = 0 deposit(dt) if self hasnt open printl(“closed”) return end self. amount += qt end - It is possible to control the attributes in an object with the give statement give obj_name [!] att_name [, [!] att_name_1, …] give cashbox open cashbox. deposit(10) // all fine give cashbox ! Open cashbox. deposit(10) //complains - if self has att 1 | att 2 | att 3 : …

Falcon - Classes l Falcon classes are a mix of data and code that can be used to instantiate objects. ¡ Define a class using keyword “class” class_name [ (param_list)] [from inh 1[, inh 2, . . inh. N]] [static block] [constructor statements] [method list] end object_name [from class 1, class 2 … class. N] methods end Example:

Falcon - Classes class mailbox (max_msg, is_public) if is_public self. capacity =max_msg*10 else self. capacity = max_msg self. name = “none” end self. messages = [] function slot_left() return self. max_msg – len (self. messages) end - Calling the class as it were a function - Use object semantic to expand a base class or initialize normally uninitialized member my_box = mailbox(10, false) object my_box from mailbox(10, false) name = “Yin” has is_mine end - To define a method in a class, the function keyword must be used

Falcon - Classes A class static block executed only once, and the properties that are defined inside the static block will retain their values across different instance of the same class • Static properties are private to a class • Example: class with_static self. element = “Initial Value” end function get. Element() : return self. element function set. Element(e) : self. element = e end obj_a = with_static(); obj_b = with_static() obj_a. set. Element(“value from A”) printl(“The value in B is: “, obj_b. get. Element()) // value from A

Falcon - More l Error Recovery ¡ ¡ ¡ l Use try; statements; catch [var]; end for exceptions Uses integer codes to distinguish exceptions from each other No way to catch specific exception Falcon Modules ¡ ¡ Core module (A set of functions, classes and objects that are somehow part of the language) Real Time Library module (I/O, print/printl) The export directive export symbol_name [, symbol_name, …] The load directive load library/module_logical_name

Falcon - More l Variables aliases and pass by reference ¡ ¡ l Constants declaration and include directive ¡ ¡ l “variable aliasing”, so that one variable tracks the changes in another variable – using $ var = “old”; var_alias = $var; var_alias = “new” print(var) // now will print “new” Pass by reference function modify(parameter) : parameter = “new” var = “old”; modify($var); print(var) // print “new” const name = value Using include “filename” to textually include another file Lambda Functions ¡ ¡ The keyword lambda defines a new function without a function name lambda([param 1, param 2, …]) [satatements] end Falcon provides the ability to created unnamed functions that can be handled as variable directly inside function, method or class bodies.

Falcon - More l Coroutines are routines that run concurrently at the same time in the same VM ¡ launch function_name([function parameters]) ¡ Coroutines can launch other coroutines, each one is independent from its parent ¡ exit() function is explicitly called can terminated the coroutines l Synchronization l More information at <http: //www. falconpl. org/>
- Slides: 30