Sprak syntax: Difference between revisions
From wikinotes
No edit summary |
(→Arrays) |
||
(20 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
= | = Example = | ||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
void Greet(string user) | |||
Print("Hello, " + user) | |||
end | |||
# create your own eventloops with infinite loops | |||
loop | |||
string user = GetUser() | |||
Sleep(1) | |||
Greet(user) | |||
end | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Print --> | </blockquote><!-- Example --> | ||
= I/O = | |||
<blockquote> | |||
<syntaxhighlight lang="bash"> | |||
Print("something") # prints text 'something' | |||
Say("something") # prints text in speech-bubble when interacted with. (ex. if no monitor) | |||
var cmd = Input(">") # save user-input to variable | |||
</syntaxhighlight> | |||
</blockquote><!-- I/O --> | |||
= Variables = | = Variables = | ||
Line 10: | Line 28: | ||
Assignment | Assignment | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
var | var list = [false, true] # a list | ||
var | var myVar = 1.0 # ? automatically determine type? | ||
</syntaxhighlight> | |||
Scope | |||
<syntaxhighlight lang="bash"> | |||
# looks like global variables are defined outside of function | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Variables --> | </blockquote><!-- Variables --> | ||
= Datatypes = | = Datatypes = | ||
<blockquote> | |||
== Primitives == | |||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang="bash"> | <syntaxhighlight lang="bash"> | ||
Line 21: | Line 46: | ||
string myStr = "test" | string myStr = "test" | ||
bool myBol = false | bool myBol = false | ||
array letters = " | array letters = ["a", "b", "c"] # define an array | ||
</syntaxhighlight> | |||
</blockquote><!-- Primitives --> | |||
== Strings == | |||
<blockquote> | |||
<syntaxhighlight lang="bash"> | |||
CharToInt("a") # (97) ascii value | |||
IntToChar(97) # ("a") ascii lookup | |||
# capitalize/uncapitalize by adding/subtracting 32 | |||
# iterate characters in string | |||
loop ch in mystring | |||
# ... | |||
end | |||
</syntaxhighlight> | |||
</blockquote><!-- Strings --> | |||
== Arrays == | |||
<blockquote> | |||
Arrays are 0-indexed. | |||
<syntaxhighlight lang="bash"> | |||
Count(myarray) # size/length of array | |||
GetIndexes(myarray) # | |||
Remove(myarray, 3) # remove index 3 from array | |||
Append(myarray, "foo") # append item to array | |||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Arrays --> | |||
</blockquote><!-- datatypes --> | </blockquote><!-- datatypes --> | ||
Line 46: | Line 95: | ||
<syntaxhighlight lang="C"> | <syntaxhighlight lang="C"> | ||
# Iterate over variable | # Iterate over variable | ||
loop | loop i from 1 to 8 | ||
Print( | Print(i) | ||
break | |||
end | end | ||
Line 70: | Line 120: | ||
= Functions = | = Functions = | ||
<blockquote> | <blockquote> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="bash"> | ||
# Define Function | # Define Function | ||
string MyFunction( string var ) | string MyFunction( string var ) | ||
Line 81: | Line 131: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
<syntaxhighlight lang=" | <syntaxhighlight lang="bash"> | ||
# Test if function exists on platform | # Test if function exists on platform | ||
if HasFunction("Print") | if HasFunction("Print") | ||
Line 88: | Line 138: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
</blockquote><!-- Functions --> | </blockquote><!-- Functions --> | ||
= Persistence = | |||
<blockquote> | |||
<syntaxhighlight lang="bash"> | |||
SaveMemory(key, data) | |||
LoadMemory(key) | |||
</syntaxhighlight> | |||
</blockquote><!-- Persistence --> | |||
= Misc = | |||
<blockquote> | |||
<syntaxhighlight lang="bash"> | |||
Random() # Random number | |||
Sleep(1) # Sleep time in seconds | |||
Time() # current time (in seconds?) | |||
GetHour() # | |||
ClearCode() # (?) | |||
AppendCode() # (?) | |||
Sin() # sine | |||
Mod() # modulus, I assume | |||
Round() # decimal-to-integer | |||
</syntaxhighlight> | |||
</blockquote><!-- Misc --> |
Latest revision as of 01:47, 12 January 2023
Example
void Greet(string user) Print("Hello, " + user) end # create your own eventloops with infinite loops loop string user = GetUser() Sleep(1) Greet(user) end
I/O
Print("something") # prints text 'something' Say("something") # prints text in speech-bubble when interacted with. (ex. if no monitor) var cmd = Input(">") # save user-input to variable
Variables
Assignment
var list = [false, true] # a list var myVar = 1.0 # ? automatically determine type?Scope
# looks like global variables are defined outside of function
Datatypes
Primitives
number myNum = 1234 string myStr = "test" bool myBol = false array letters = ["a", "b", "c"] # define an arrayStrings
CharToInt("a") # (97) ascii value IntToChar(97) # ("a") ascii lookup # capitalize/uncapitalize by adding/subtracting 32 # iterate characters in string loop ch in mystring # ... endArrays
Arrays are 0-indexed.
Count(myarray) # size/length of array GetIndexes(myarray) # Remove(myarray, 3) # remove index 3 from array Append(myarray, "foo") # append item to array
Object Types
you can determine Item types with the
GetType()
command.GetType(myVar) # type introspectionfloppy # Floppy Diskette key # Keys
Loops
# Iterate over variable loop i from 1 to 8 Print(i) break end # Loop indefinitely (accepting commands) loop var cmd = Input(") ") end
Conditionals
if varA == varB # Unconfirmed Print("Success") else if varA == varC Print("Alternative Success") End()
Functions
# Define Function string MyFunction( string var ) return var End() void MyFunction( string var ) Print("no return value") End()# Test if function exists on platform if HasFunction("Print") Print("Success") End()
Persistence
SaveMemory(key, data) LoadMemory(key)
Misc
Random() # Random number Sleep(1) # Sleep time in seconds Time() # current time (in seconds?) GetHour() # ClearCode() # (?) AppendCode() # (?) Sin() # sine Mod() # modulus, I assume Round() # decimal-to-integer