Skip to content

lit_syntax_and_semantics

uint256_t edited this page Jul 2, 2016 · 8 revisions

Syntax and Semantics

Comments

  • Comments start with # character
# This is a comment.
# Support only one line.

Types

Bool

  • Bool has two values
true 
false

Integers

  • There is only 32 bit singed type
314
123456789

String

  • A string literal starts with a double quote ( " ) and end in a double quote ( " )
"Hello world!"
"\n" # new line
"\t" # tab
"\'" # single quote
"\"" # double quote

Array

  • An Array is created with only one type.
  • An Array cannot have mixed types.
[1, 2, 3] #OK
["hello", "world!"] #OK
[1, "hello"] # NG

Local variables

  • Local variables doesn't start with dollar sign ( $ )
  • They are declared when first assignment.
  • They are possible to specify types.
n = 10
str = "hello"
i:string = "hello" # specify
ary = [1, 2]

Global variables

  • Global variables start with dollar sign ( $ )
  • Under the circumstances, Global variables are not possible to specify types and support only Integer type...
$g = 10
$str = "hello" # impossible..

Control Syntax

If

  • if evaluates the then branch if its condition is true, and evaluates the else or elsif branches.
i = 10
if i < 5
  i = 0
elsif i == 5
  i = 1
else
  i = 2
end
i # => 2 

While

  • while executes the body while the conditon is true.
i = 0
while i < 10
 puts i
 i +=1
end

For

  • for executes the body.
# First syntax (like C)
for i = 0, i < 10, i += 1
 puts i
end
# Second syntax (like Ruby)
for i in 0..9 # 0...10
 puts i
 # unnecessary increment
end

Break

  • break breaks out of the loop
i = 0
while 1
 if i >= 5
  break
 end
 i +=1
end

Function

  • Follow this to define the functions
def f(x) # parentheses may not need
	x + 1
end
def f x:string # support overload of function
	puts x
end
def f:string # set return type
	"hello"
end	
def add x:string y:string :string
	x + y
end
puts add "hel", "lo" # => hello

Standard Libraries

require

  • require includes the specified file
require "std" # includes all stdlibs
require "Math" # library related to the Mathematics
require "File"

puts File::read "story.txt"
puts Math::sqrt 9.0 # => 3
puts length [1, 2, 3] # => 3
puts length "hello" # => 5 

Thank you for visiting my reference!

  • I hope you try using Lit;)
Clone this wiki locally