Skip to content

Values, Symbols and Variables

Jinze Wu edited this page Oct 2, 2022 · 6 revisions

Before you write a real world Tinymoe program, you should know how Tinymoe processes data.

There are 3 kinds of data

  • Literals
  • Symbols
  • Objects

Literals

Literals are constant numbers and strings. 1 is a literal of integer. 1.2 is a literal of float. "Text" is a literal of string.

Symbols

Symbols are constants using a name. Like true and false is a symbol of boolean. null represents nothing, is it doesn't belong to any type. You can define your own symbol, but you cannot give them a type. The type of a user defined symbol is always symbol. For example:

module seasons
using standard library

symbol spring
symbol summer
symbol autumn
symbol winter

sentence print (message)
	redirect to "Print"
end

phrase main
	print "My favorite season is " & summer & "!"
end

This program will print "My favorite season is summer!".

You can compare symbols using = and <> for equality. For example:

module Seasons
using standard library

symbol spring
symbol summer
symbol autumn
symbol winter

sentence print (message)
	redirect to "Print"
end

phrase (a) and (b) are the same season
	set the result to a = b
end

phrase main
	if spring and summer are the same season
		print "The world is mad!"
	end
end

This programm will print nothing, because spring = summer is false.

Objects

An object is an instance of array or a custom type. Equality between two objects is different from literals and symbols. If you create two objects, say a and b, then a = b will always be false. But if you create an object and store it to two different variables, say a and b, then a = b will be true.

module students and scores
using standard library

sentence print (message)
	redirect to "Print"
end

type student
	name
	identifier
	score
end

phrase main
	set a to new student of ("John", "1", 100)
	set b to new student of ("John", "1", 100)
	print a = b
	-- it prints "false", even the content in these 2 objects are the same

	set c to new student of () -- create an object with all fields setting to null
	set d to c
	print c = d
	-- it prints "true", because c and d store the same object

	set field name of c to "Bob"
	print field name of d
	-- it prints "Bob", because c and d store the same object, so changing anything inside c will reflect on d
end

Variables

You can create a variable anywhere by using set <variable name> to <value>. If you call it on an existing variable, you will change the variable to a new value.

module variables

phrase main
	set a to 10 -- create a new variable: a
	set b to 10 -- create a new variable: b
	set a to 20 -- change a, because a has been created in the scope

	if true
		set b to 30 -- change b, because b is accessible here
		set c to 30 -- create a new variable: c
	end
	set c to 40 -- create a new variable: c, because the last c only affect codes inside "if"
end