0
0
This repository has been archived on 2024-05-25. You can view files and clone it, but cannot push or open issues or pull requests.
legacy-code/doc/concept.sh

260 lines
4.7 KiB
Bash
Raw Normal View History

2022-04-17 12:28:48 +02:00
## COMMENTS ##
# one-line comments (from prefix to EOL)
2022-09-01 18:31:39 +02:00
mov $1 42 # comment
mov $1 42 // comment
2022-04-17 12:28:48 +02:00
# block comments
/*
Block comment
*/
/* Also a block comment */
2022-09-01 18:31:39 +02:00
mov /* Inline block comment */ $1 42
2022-04-17 12:28:48 +02:00
## DELIMITERS ##
# `;` acts like a fake newline
mov $1 42; typ $1; echo $1
## ESCAPE SEQUENCES AND NUMBER PREFIXES ##
# quote escaping
"Here comes a quote: \"Lorem ipsum\". This had to be escaped!"
"Here comes another quote: 'Lorem ipsum'. This didn't require escaping!"
# in strings (only those in double quotes)
"\n" # newline
"\t" # tab
"\u2764\ufe0f" # utf-8 for ❤️
...
# numbers
0x10 # hexadecimal: 16
0o10 # octal: 8
0b0100 # binary: 4
2022-04-17 12:28:48 +02:00
2021-06-03 20:31:48 +02:00
## VARIABLES ##
# $2 => $1
move|mov|set $1 $2
2021-06-03 20:31:48 +02:00
2022-10-05 21:00:36 +02:00
# use $1 from root scope in current scope
2021-06-03 22:41:41 +02:00
glob|global|pub|public $1
2021-06-03 20:31:48 +02:00
2022-10-05 21:01:08 +02:00
# use all variables from root scope in current scope
glob|global|pub|public *
# vartypes
null # null (internal: null)
true|false # bool (internal: boolean)
eg. -44 27 287634 # num (internal: float)
eg. 4.0 -3.0 0.125 # num (internal: float)
eg. "bla" 'blub' # str (internal: string)
2021-06-03 20:31:48 +02:00
2022-04-17 12:34:12 +02:00
# vartype($1) => $1
typ|type $1
# cast $1 to type-string stored in $2 / to num
2022-04-17 12:34:12 +02:00
cst|cast $1 $2
cst|cast $1 "num"
2022-04-17 12:34:12 +02:00
2022-10-05 21:03:21 +02:00
# use contents of a var as var-name
${$name}
2022-04-17 12:34:12 +02:00
2021-06-03 20:31:48 +02:00
## MATH ##
2021-06-03 22:41:41 +02:00
# $1 + $2 + ... => $1
add $1 $2 ...
2021-06-03 20:31:48 +02:00
2021-06-03 22:41:41 +02:00
# $1 - ($2 + ...) => $1
sub $1 $2 ...
2021-06-03 20:31:48 +02:00
2021-06-03 22:41:41 +02:00
# $1 * $2 * ... => $1
mul $1 $2 ...
2021-06-03 20:31:48 +02:00
2021-06-03 22:41:41 +02:00
# $1 / ($2 * ...) => $1
div $1 $2 ...
2021-06-03 20:31:48 +02:00
2021-06-03 22:41:41 +02:00
# $1 % $2 % ... => $1
mod $1 $2 ...
# $1 ^ $2 ^ ... => $1
2021-06-04 12:46:34 +02:00
pow $1 $2 ...
2021-06-03 20:31:48 +02:00
2021-07-17 13:17:16 +02:00
# root of $1 (power = $2; if not given: square-root) => $1
root $1 $2
2022-05-16 19:08:52 +02:00
# log of $1 (base = $2; if not given: base = 2) => $1
2021-07-17 13:17:16 +02:00
log $1 $2
# round to the nearest integer (.000 - .499 => round down; .500 - 0.999 => round up) => $1
round $1
# round up to the next integer => $1
ceil $1
# round down to the last integer => $1
floor $1
2021-06-03 20:31:48 +02:00
## CONDITIONS ##
# only execute next line if <cond> is true
if|ifl <cond>
2021-06-03 20:31:48 +02:00
2022-09-03 18:39:59 +02:00
# value to bool
$1 # same as `$1 == true`
!$1 # same as `$1 != true`
# strongly typed compare
2022-09-03 18:39:59 +02:00
$1 == $2
$1 != $2
# numerical / alphabetical compare
$1 < $2
$1 <= $2
$1 > $2
$1 >= $2
2021-06-03 20:31:48 +02:00
## JUMPS ##
# jump to a flag called "flag1" (flags are stored per-scope)
2021-06-03 22:41:41 +02:00
jmp|jump|goto "flag1"
2021-06-03 20:31:48 +02:00
# set flag called "flag1"
:flag1 | :flag1: | flag1:
2022-04-17 12:40:10 +02:00
## TEXT IO ##
# print "blabliblub" in its own line
2021-06-03 22:41:41 +02:00
drw|draw|echo "bla" "bli" "blub"
2021-06-03 20:31:48 +02:00
2022-04-17 12:40:10 +02:00
# print "blabliblub", await user response (until newline/enter) and return entered line as $1
ask $1 "bla" "bli" "blub"
## GRAPHICAL OUTPUT ##
2021-06-03 20:31:48 +02:00
# initialize matrix with 16x16
2021-06-03 22:41:41 +02:00
pxl|pixel|canv|canvas size 16 16 | pxl|pixel|canv|canvas size 16
2021-06-03 20:31:48 +02:00
# set output resolution to 256x256
2021-06-03 22:41:41 +02:00
pxl|pixel|canv|canvas res 256 256 | pxl|pixel|canv|canvas res 256
2021-06-03 20:31:48 +02:00
# enable autodraw
2021-06-03 22:41:41 +02:00
pxl|pixel|canv|canvas autodraw true
2021-06-03 20:31:48 +02:00
# set pixel at 2|12 to color #F6F6F6
2021-06-03 22:41:41 +02:00
pxl|pixel|canv|canvas set 2 12 "F6"
2021-06-03 20:31:48 +02:00
# update matrix to screen
2021-06-03 22:41:41 +02:00
pxl|pixel|canv|canvas draw
2021-06-03 20:31:48 +02:00
2022-09-08 21:33:52 +02:00
## OWN COMMANDS ##
# - command definitions have their own scopes
# - they can call other commands
2021-06-03 20:31:48 +02:00
# - their own vars are private
# - they can't acces root-scope's vars (except they were made global using `glob`)
# - args are copied into local scope
# - args with &-prefix modify corresponding var in global scope
2021-06-03 20:31:48 +02:00
2022-09-08 21:33:52 +02:00
# the command scope "mycommand1" starts here
2022-09-08 21:31:22 +02:00
# argument $1 is readonly; $2 is read/write; $3 and $4 are optional and will default to `null`
def mycommand1 $1 &$2 $3? $4?
2021-06-03 20:31:48 +02:00
2022-09-08 21:33:52 +02:00
# current command scope ends here
2021-06-03 22:41:41 +02:00
end
2021-06-03 20:31:48 +02:00
2022-09-08 21:33:52 +02:00
# call a command called "mycommand1"
2021-06-03 20:31:48 +02:00
# arg $1 = 42; arg $2 = $test (returned to parent scope)
2022-09-08 21:33:52 +02:00
mycommand1 42 $test
2021-06-03 20:31:48 +02:00
## RETURN, END, ETC. ##
2022-09-08 14:06:46 +02:00
# halt the program
halt|stop
2021-06-03 20:31:48 +02:00
2022-09-08 21:33:52 +02:00
# return from command to parent scope
2022-09-08 14:06:46 +02:00
# halt program in root scope
exit|return
2021-06-03 20:31:48 +02:00
## LISTS ##
# - if a list index is undefined, its value will be `null`
2022-09-01 18:31:39 +02:00
# set $1 to empty list
set $1 []
2021-06-03 20:31:48 +02:00
2022-09-01 18:31:39 +02:00
# set $1 to list
set $1 [1, 5, 2345]
2021-06-03 20:31:48 +02:00
2021-06-03 22:41:41 +02:00
# print 2nd element of 4th list
2022-09-01 18:31:39 +02:00
echo $1[3][1]
2021-06-03 20:31:48 +02:00
# remove 2nd element (maintaining key association)
2022-09-01 18:31:39 +02:00
set $1[1] null
2021-06-03 20:31:48 +02:00
# push element
2022-09-01 18:31:39 +02:00
set $1[] "blabla" | add $1 "blabla"
2021-06-03 20:31:48 +02:00
# print last element
2022-09-01 18:31:39 +02:00
echo $1[-1]
2021-06-03 20:31:48 +02:00
# remove (and get) last element (keys will be re-ordered)
2022-09-01 18:31:39 +02:00
pop $1[-1] | pop $1[-1] $2
2021-06-03 22:41:41 +02:00
2022-09-01 18:31:39 +02:00
# get length of list $2 => $1
len $1 $2
2021-06-03 22:41:41 +02:00
## EXTERNAL VALUES ##
2022-09-01 18:31:39 +02:00
# get unix time => $1
time $1
2021-06-03 22:41:41 +02:00
2022-09-01 18:31:39 +02:00
# get formated time => $1
time $1 "YYYY-MM-DD"
2021-06-03 22:41:41 +02:00
# get random float between 0.0 and 1.0 => $1
rnd|rand|random $1
2022-09-01 18:31:39 +02:00
# get random integer between 0 and 3 => $1
rnd|rand|random $1 3
2021-06-03 22:41:41 +02:00
2022-09-01 18:31:39 +02:00
# get random integer between 5 and 13 => $1
rnd|rand|random $1 5 13
2021-06-03 22:41:41 +02:00
## STRING MANIPULATION ##
# get number $1 as string
2022-10-02 19:54:22 +02:00
cst|cast $1 "str"
# get utf-8 char with id $1 => $1
chr $1
2022-09-01 18:31:39 +02:00
# get length of string $2 => $1
len $1 $2
2021-06-03 22:41:41 +02:00
2022-09-01 18:31:39 +02:00
# concat strings $1 + $2 => $1
add $1 $2 ...
2021-06-03 22:41:41 +02:00
2022-09-01 18:31:39 +02:00
# get substr 2(-5) of string $1 => $1
slice|substr $1 2 5 | slice|substr $1 2
2021-06-03 22:41:41 +02:00
2022-09-01 18:31:39 +02:00
# repeat string $1 3 times => $1
mul $1 3
2021-06-03 22:41:41 +02:00
2022-09-01 18:31:39 +02:00
# get position of first occourance of $3 (=needle) in $2 (=haystack) => $1
# pos >= 0 if found; pos = false if nothing found
pos|strpos $1 $2 $3