Skip to content

Variables

SirWolf edited this page Aug 29, 2022 · 6 revisions

Variables

Variables can store values.
Possible types of a variables are:

  • Number
  • String
  • List

MeowScript is lexical scopes, so variables stop to exist when their scope ends.

new/set

With new you create a new variable with a given value.

new <name> <value>

For example:

new the_answer 42
new text "Hello, World!"
new shopping_list ["Apple","Bread"]

This creates three variables in the current scope.

With set you set an already existing variable in your current scope or in any scope above.

set <name> <value>

For example:

new a 44

# ... new scope

set a 43 # a from above is now 43
set b 10 # if b does not exist -> make new variable

const

With the command const you can make a new variable which will never change its value.
If you try to change it, an error will be thrown.

const new_const 12
set new_const 3 # error!

look

The command look returns the content of a variable.

look <variable>

For example:

new a 12
new b "text"

look a # output: 12
look b # output: text

typeof

typeof returns the analyzed type of a token

typeof <token>

For example:

new b 34

typeof a # "Name"
typeof b # "Number"
typeof 12 # "Number"
typeof [1,2,3] # "List"
...

casts

You can also cast one type to another type.

string

The command string casts its parameter to a string.

string(12) # "12"
string("string") # "string"
string([1,2,3]) # "[1,2,3]"

number

The command number casts its parameter to a number.
If it fails, it returns a token with the type "Void" which can be checked with typeof.

new a "123"
number("12.43") # 12.43
number(a) # 123

typeof {number([1,3,6])} # "Void"
typeof {number("12a")} # "Void"

list

The command list casts it's parameter to a list.
If it fails, it returns a token with the type "Void" which can be checked with typeof.

list("Hello") # ["H","e","l","l","o"]
list(5) # [0,1,2,3,4]

typeof {list(unknown)} # "Void"

<<< Previous article
>>> Next article

Clone this wiki locally