Falcon Programming Language - PowerPoint PPT Presentation

About This Presentation
Title:

Falcon Programming Language

Description:

Name of Falcon has been taken by the other two languages: ... if 'abba' in name if 'abba' not in name. printl('true') printl('true') end end ... – PowerPoint PPT presentation

Number of Views:111
Avg rating:3.0/5.0
Slides: 31
Provided by: yin3
Learn more at: https://www.nku.edu
Category:

less

Transcript and Presenter's Notes

Title: Falcon Programming Language


1
Falcon Programming Language
  • By Yin Shi

2
Introduction of Falcon
  • Name of Falcon has been taken by the other two
    languages
  • 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

3
Falcon - Basic Syntax
  • 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
  • Use / / and // for comments
  • Define variable without a type
  • Variable name must start with a lower or
    uppercase letter
  • Elementary value types
  • Integer numbers
  • Floating point number
  • strings
  • Mathematical operator supported are , -, , /,
    and
  • Falcon supports also self operator , -, ,
    /,
  • I/O input() and print()/printl()

4
Falcon Control Structures
  • Conditional Control Structures
  • if/elif/else statements
  • Relational operators are , gt, lt, gt, lt and !
  • if expression
  • statements
  • else
  • statements
  • if expression
  • statements
  • elif expression
  • statements
  • /other elif /
  • else
  • statements
  • end
  • -

5
Falcon Control Structures
  • 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/elif/else expression statement
  • Logical Operators (and, or, not )
  • Falcon supports Self-evaluation of expression
  • if can be shorten like
  • a gt 0 and print (agt0) or print(alt0)
  • 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)
  • ltconditiongt ? ltif truegt ltif falsegt

6
Falcon Control Structures
  • The switch statement
  • When a single value is need to be checked against
    several different values.
  • switch expression
  • case expression , expression, , expression
  • statements
  • 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.

7
Falcon Control Structures
  • Loop Control structures
  • The while statement
  • it is just like the Cs 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

8
Falcon Control Structures
  • 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

9
Falcon More on Lines and statements
  • 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_very_long_name
  • 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

10
Falcon Basic Data Structures
  • 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
    Javas
  • Example
  • List person, 1, 3.5, int(123),
    var1
  • List person, 1, 3.5, int(123), var1
  • 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

11
Falcon Basic Data Structures
  • Accessing an array by operator
  • for i 0 to len(List)-1
  • printl(Element n, I, , Listi)
  • end
  • It is possible to access more than one item at a
    time, a range is defined as a part of integers so
    that Rnm (all items from n to m-1)
  • List1 List // the
    whole array
  • List2 List04 // 0, 1, 2, 3
  • A range can contain negative indexes, negative
    indexes mean distance from end, -1 being the
    last item

12
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
  • List4 List30 // the first 4
    elements in reverse order
  • List5 List-10 // 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 ab // c 1,
    2, 3, 4
  • c data // c 1, 2, 3, 4, data
  • Multi-dimensional array
  • List 1,2, 3, 4, 4, 5
  • List0 1, 2 List00 1

13
Falcon Basic Data Structures
  • 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(stringi, ,)
  • end
  • printl(string-1) // the last letter
  • 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 (gt)
  • dict gt dict a gt 123, b gt
    onetwothree dicta 123

14
Falcon - The in Operator
  • 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
    if abba not in name
  • printl(true)
    printl(true)
  • end end
  • dict one gt 1
  • if one in dict
  • printl(aways true)
  • end
  • 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.

15
Falcon The in Operator
  • for variable , variable in collection
  • statements
  • break continue
  • statements
  • first
  • 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!

16
Falcon The Functions
  • 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 function_name (parameters)
  • statements..
  • end
  • Functions executing just one statement may be
    abbreviated with the colon indicator()
  • function function_name (parameters)
    statement return value
  • Example
  • function square(x)
  • y xx return y
  • end
  • function square(x) return xx
  • Calling a function a square(8)

17
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.
  • Recursion
  • Falcon provides recursion
  • function sum_of_first(x)
  • if x gt 1 return x sum_of_first(x-1) end
  • return x
  • end
  • 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.

18
Falcon - Functions
  • Using global keyword to access the variable
    outside the function
  • function square_in_z(x)
  • global z z xx
  • 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 function_name (parameters)
  • static
  • statements
  • end
  • statements
  • end

19
Falcon - Functions
  • Example
  • function say_something()
  • static
  • data have, a, nice, day
  • current 0
  • end
  • if current len(data) return end
  • element datacurrent current 1
    return element
  • end
  • thing say_something()
  • while thing ! nil
  • print(thing, )
  • thing say_something() // current will
    remember its last position
  • end
  • printl()

20
Falcon Objects
  • Falcon stand alone objects
  • Contains properties and methods
  • Methods in object without keyword function
  • Object declaration
  • object object_name from class1, class2,
  • property1 constant
  • property2 constant
  • method1 (parameters)
  • statements
  • end
  • method2 (parameters)
  • statements
  • end
  • end
  • Objects can inherit from classes

21
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
  • 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

22
Falcon Objects
  • Example
  • function new_deposit(qt)
  • if self.amount qt gt 5000
  • printl(Sorry, amount is gt 5000)
  • else
  • printl(Ok, will deposit the money)
  • self.amount qt
  • end
  • end
  • old_deposit cashbox.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

23
Falcon Objects
  • 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
  • Objects and attributes
  • Every object can be given a set of a maximum 32
    boolean attributes
  • attributes name
  • attribute1
  • attribute2
  • /other attributes
  • end
  • attributesname attribute1, attribute2

24
Falcon - Objects
  • 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 att1 att2 att3
  • - 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
  • end

25
Falcon - Classes
  • Falcon classes are a mix of data and code that
    can be used to instantiate objects.
  • Define a class using keyword class
  • class class_name (param_list) from inh1,
    inh2, ..inhN
  • static block
  • constructor statements
  • method list
  • end
  • object object_name from class1, class2
    classN
  • methods
  • end
  • Example

26
Falcon - Classes
  • class mailbox (max_msg, is_public)
  • if is_public
  • self.capacity max_msg10
  • else
  • self.capacity max_msg
  • self.name none
  • end
  • self.messages
  • function slot_left()
  • return self.max_msg len
  • (self.messages)
  • end
  • 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

27
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
  • static
  • self.element Initial Value
  • end
  • function getElement() return self.element
  • function setElement(e) self.element e
  • end
  • obj_a with_static() obj_b with_static()
  • obj_a.setElement(value from A)
  • printl(The value in B is , obj_b.getElement())
    // value from A

28
Falcon - More
  • Error Recovery
  • 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

29
Falcon - More
  • Variables aliases and pass by reference
  • 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
  • Constants declaration and include directive
  • 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(param1, param2, )
  • satatements
  • end
  • Falcon provides the ability to created unnamed
    functions that can be handled as variable
    directly inside function, method or class bodies.

30
Falcon - More
  • Coroutines
  • 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
  • Synchronization
  • More information at lthttp//www.falconpl.org/gt
Write a Comment
User Comments (0)
About PowerShow.com