Update documentation of instructions and change tostring and tonumber behaviour

This commit is contained in:
Hybrid Dog 2017-08-24 18:05:34 +02:00
parent 2c3f1edd81
commit 589575ef2d
3 changed files with 123 additions and 144 deletions

165
README.md
View File

@ -2,112 +2,78 @@ commands are read line by line
```[<labelname>:] [<commandname> [<arg1>[,<arg2[]]]] [; <comment>]``` ```[<labelname>:] [<commandname> [<arg1>[,<arg2[]]]] [; <comment>]```
arguments can be either variables, labels or immediates, arguments can be either variables, labels or immediates,<br/>
labels are replaced with immediates which have the line numbers of the labels as their values labels are replaced with immediates which have the line numbers of the labels as
true/false: boolean (immediate) their values<br/>
12.6: number (immediate) true/false: boolean (immediate)<br/>
$hello nwae: string (immediate) 12.6: number (immediate)<br/>
nodolla: either a label is called so (immediate) or it's a variable $hello nwae: string (immediate)<br/>
If you want to use : in a string, you need to put it at the beginning of the line to not treat the left part as label nodolla: either a label is called so (immediate) or it's a variable<br/>
If you want to use : in a string, you need to put it at the beginning of the
``` line to not treat the left part as label
Types:
e anything
v any variable
ve any set variable
s string
vs string variable
n number
vn number variable
ui unsigned integer number
b bool
vn bool variable
Instructions:
mov <v to>, <e from> ### Types
Copies from into to.
getvar <vs varname>[, <ve exists>] Name | Description
Sets varname to the current variable called <varname> if it's set. If exists is passed, it's set to a bool indicating whether the variable was set. ----|-------------
e| anything defined
v| any variable
ve| any defined variable
s| string
vs| string variable
n| number
vn| number variable
ui| unsigned integer number
b| bool
vb| bool variable
add <vn to>, <n from>
to += from
add <vb to>, <b from> ### Instructions
to = to and from
add <vs to>, <s from> Name | Parameters | Description
to = to .. from -----|------------|------------
mov | `<v to>, <e from>` | `to <= from`; Copies from into to.
xchg | `<v a>, <v b>` | `a,b <= b,a`; Switches a with b.
getvar | `<vs varname>[, <ve exists>]` | Sets varname to the current variable called <varname> if it's set. If exists is passed, it's set to a bool indicating whether the variable was set.
copytable | `<s target>, <s origin>` | Copies ${origin}.* to ${target}.*.
add | `<vn to>, <n from>` | `to <= to + from`
add | `<vb to>[, <b ba>[, <b bb>[…]]]` | `to <= to ba bb …`; logical or: *to* indicates whether at least one of the arguments is true
add | `<vs to>, <s from>` | `to <= to .. from`; string concatenation
sub | `<vn num>, <n tosub>` | `num <= num - tosub`
sub | `<vs str>, <n start>[, <n end>]` | `str <= str:sub(start, end)`; substring
mul | `<vn to>, <n from>` | `to <= to * from`; multiplication
mul | `<vb to>[, <b ba>[, <b bb>[…]]]` | `to <= to ∧ ba ∧ bb ∧ …`; logical and: *to* indicates whether all of the arguments are true
mul | `<vs str>, <n cnt>` | `str <= str:rep(cnt)`; repeating a string cnt times
div | `<vn num>, <n todiv>` | `num <= num / todiv`; division
inc | `<vn num>` | `num <= num + 1`
dec | `<vn num>` | `num <= num - 1`
neg | `<vn num>` | `num <= -num`; negation
neg | `<vb var>` | `var <= ¬var`; logical not
neg | `<vs str>` | `str <= str:rev()`; reverts a string
inv | `<vn num>` | `num <= 1 / num`
mod | `<vn num>, <n dv>` | `num <= num mod dv`; modulo
jmp | `<ui target>[, <e cond>]` | jump; If *cond* is not *false*, the instruction pointer is set to *target*. To disallow infinite loops, the program is interrupted after changing it, mods may have different heuristics about restarting it then.
call | `<ui target>[, <e cond>]` | same as pushing the current instruction pointer and then jumping (see jmp); used to execute subroutines
ret | no arguments | pop some unsigned integer, then jump there; used to exit subroutines
push | `<e a>[, <e b>[, <e c>[…]]]` | put values onto the stack; from left to right
pop | `<v a>[, <v b>[, <v c>[…]]]` | takes values from the stack and sets the passed variables to them; the last variable must be a defined one; from left to right
equal | `<v a>, <e b>` | `a <= (a = b)`; equality
less | `<vn a>, <n b>` | `a <= (a < b)`; after executing, a is a bool
less | `<vs a>, <s b>` | `a <= (a < b)`; less also works for strings, it compares characters from left to right, see man ASCII for the order
greater | `<v a>, <e b>` | executes less b,a
usleep | `<n t>` | aborts the program for at least ⌊max{0, t}⌋ microseconds
sleep | `<n t>` | executes usleep with t * 1000000 as argument
get_us_time | `<v to>` | stores current time in microsecond precision into *to*; can be used to measure time differences
tostring | `<v var>` | Sets *var* to a string representing its value; if it's not defined, it's set to `$nil`, if it's a boolean, it's set to `$true` or `$false`
tonumber | `<v var>` | Sets *var* to a number representing its value
toboolean | `<v var>` | If *var* is `false` or not defined, it's set to `false`, else it's set to `true`.
print | `<e a>[, <e b>[, <e c>[…]]]` | adds variables to the log, seperated by \t (tab), \n (newline) is added at the end
flush | no arguments | Output the log, what exactly happens should vary for every mod.
mul <vn to>, <n from> Note to Developers: Do not edit the instructions manually here in the Readme, do changes in util/standartbefehlssatz_doc.lua and execute it.
to *= from
mul <vb to>, <b from>
to = to or from
mul <vs str>, <n cnt>
str = str:rep(from)
neg <vn num>
num = -num
neg <vb var>
var = not var
neg <vs str>
str = str:rev()
inv <vn num>
num = 1 / num
mod <vn num>, <n dv>
num = num % dv
jmp <ui p>[, <e c>]
If c is not false, the instruction pointer is set to p. To disallow infinite loops, the program is interrupted after changing the ip, the mod should then consider restarting it.
call <ui p>
push the ip, then jmp p; used to execute subroutines
ret
pop something, then jmp there; used to exit subroutines
push <e a>[, <e b>[, <e c>[…]]]
put values onto the stack; from left to right
pop <v a>[, <v b>[, <v c>[…]]]
takes values from the stack and sets the passed variables to them; from left to right
equal <v a>, <e b>
a = a == b
less <vn a>, <n b>
a = a < b; after executing, a is a bool
less <vs a>, <s b>
a = a < b; less also works for strings
usleep <n t>
aborts the program for at least floor(max(0, t)) ms
sleep <n t>
executes usleep with t * 1000000 as argument
get_us_time <v to>
stores minetest.get_us_time() into to; can be used to measure time differences
tostring <ve var>
var = tostring(var)
print <e a>[, <e b>[, <e c>[…]]]
adds variables to the log, seperated by \t, \n is added at the end
flush
Output the log, this should vary for every mod.
```
@ -144,6 +110,7 @@ end
``` ```
TODO: TODO:
* add string. instructions * add string. instructions
* metatable * security: test table copy functions
* update README

View File

@ -298,11 +298,11 @@ s = {
end, end,
tostring = function(params) tostring = function(params)
return true, params[1] and tostring(params[1]) or nil return true, tostring(params[1])
end, end,
tonumber = function(params) tonumber = function(params)
return true, params[1] and tonumber(params[1]) return true, tonumber(params[1])
end, end,
toboolean = function(params) toboolean = function(params)

View File

@ -1,54 +1,54 @@
local types = { local types = {
{"e", "anything"}, {"e", "anything defined"},
{"v", "any variable"}, {"v", "any variable"},
{"ve", "any set variable"}, {"ve", "any defined variable"},
{"s", "string"}, {"s", "string"},
{"vs", "string variable"}, {"vs", "string variable"},
{"n", "number"}, {"n", "number"},
{"vn", "number variable"}, {"vn", "number variable"},
{"ui", "unsigned integer number"}, {"ui", "unsigned integer number"},
{"b", "bool"}, {"b", "bool"},
{"vn", "bool variable"} {"vb", "bool variable"}
} }
local instr = { local instr = {
{"mov", "<v to>, <e from>", "Copies from into to."}, {"mov", "<v to>, <e from>", "`to <= from`; Copies from into to."},
{"xchg", "<v a>, <v b>", "Switches a with b."}, {"xchg", "<v a>, <v b>", "`a,b <= b,a`; Switches a with b."},
{"getvar", "<vs varname>[, <ve exists>]", "Sets varname to the current variable called <varname> if it's set. If exists is passed, it's set to a bool indicating whether the variable was set."}, {"getvar", "<vs varname>[, <ve exists>]", "Sets varname to the current variable called <varname> if it's set. If exists is passed, it's set to a bool indicating whether the variable was set."},
{"copytable", "<s target>, <s origin>", "Copies ${origin}.* to ${target}.*."}, {"copytable", "<s target>, <s origin>", "Copies ${origin}.* to ${target}.*."},
{"add", "<vn to>, <n from>", "to += from"}, {"add", "<vn to>, <n from>", "`to <= to + from`"},
{"add", "<vb to>[, <b ba>[, <b bb>[…]]]", "to indicates whether at least one of the arguments is true"}, {"add", "<vb to>[, <b ba>[, <b bb>[…]]]", "`to <= to ba bb …`; logical or: *to* indicates whether at least one of the arguments is true"},
{"add", "<vs to>, <s from>", "to = to .. from"}, {"add", "<vs to>, <s from>", "`to <= to .. from`; string concatenation"},
{"sub", "<vn num>, <n tosub>", "num -= tosub"}, {"sub", "<vn num>, <n tosub>", "`num <= num - tosub`"},
{"sub", "<vs str>, <n start>[, <n end>]", "str = str:sub(start, end)"}, {"sub", "<vs str>, <n start>[, <n end>]", "`str <= str:sub(start, end)`; substring"},
{"mul", "<vn to>, <n from>", "to *= from"}, {"mul", "<vn to>, <n from>", "`to <= to * from`; multiplication"},
{"mul", "<vb to>[, <b ba>[, <b bb>[…]]]", "to indicates whether all of the arguments are true"}, {"mul", "<vb to>[, <b ba>[, <b bb>[…]]]", "`to <= to ∧ ba ∧ bb ∧ …`; logical and: *to* indicates whether all of the arguments are true"},
{"mul", "<vs str>, <n cnt>", "str = str:rep(from)"}, {"mul", "<vs str>, <n cnt>", "`str <= str:rep(cnt)`; repeating a string cnt times"},
{"div", "<vn num>, <n todiv>", "num /= todiv"}, {"div", "<vn num>, <n todiv>", "`num <= num / todiv`; division"},
{"inc", "<vn num>", "++num"}, {"inc", "<vn num>", "`num <= num + 1`"},
{"dec", "<vn num>", "--num"}, {"dec", "<vn num>", "`num <= num - 1`"},
{"neg", "<vn num>", "num = -num"}, {"neg", "<vn num>", "`num <= -num`; negation"},
{"neg", "<vb var>", "var = not var"}, {"neg", "<vb var>", "`var <= ¬var`; logical not"},
{"neg", "<vs str>", "str = str:rev()"}, {"neg", "<vs str>", "`str <= str:rev()`; reverts a string"},
{"inv", "<vn num>", "num = 1 / num"}, {"inv", "<vn num>", "`num <= 1 / num`"},
{"mod", "<vn num>, <n dv>", "num = num mod dv"}, {"mod", "<vn num>, <n dv>", "`num <= num mod dv`; modulo"},
{"jmp", "<ui p>[, <e c>]", "If c is not false, the instruction pointer is set to p. To disallow infinite loops, the program is interrupted after changing the ip, the mod should then consider restarting it."}, {"jmp", "<ui target>[, <e cond>]", "jump; If *cond* is not *false*, the instruction pointer is set to *target*. To disallow infinite loops, the program is interrupted after changing it, mods may have different heuristics about restarting it then."},
{"call", "<ui p>[, <e c>]", "push the ip, then jmp p; used to execute subroutines"}, {"call", "<ui target>[, <e cond>]", "same as pushing the current instruction pointer and then jumping (see jmp); used to execute subroutines"},
{"ret", "", "pop something, then jmp there; used to exit subroutines"}, {"ret", "", "pop some unsigned integer, then jump there; used to exit subroutines"},
{"push", "<e a>[, <e b>[, <e c>[…]]]", "put values onto the stack; from left to right"}, {"push", "<e a>[, <e b>[, <e c>[…]]]", "put values onto the stack; from left to right"},
{"pop", "<v a>[, <v b>[, <v c>[…]]]", "takes values from the stack and sets the passed variables to them; from left to right"}, {"pop", "<v a>[, <v b>[, <v c>[…]]]", "takes values from the stack and sets the passed variables to them; the last variable must be a defined one; from left to right"},
{"equal", "<v a>, <e b>", "a = a == b"}, {"equal", "<v a>, <e b>", "`a <= (a = b)`; equality"},
{"less", "<vn a>, <n b>", "a = a < b; after executing, a is a bool"}, {"less", "<vn a>, <n b>", "`a <= (a < b)`; after executing, a is a bool"},
{"less", "<vs a>, <s b>", "a = a < b; less also works for strings"}, {"less", "<vs a>, <s b>", "`a <= (a < b)`; less also works for strings, it compares characters from left to right, see man ASCII for the order"},
{"greater", "<v a>, <e b>", "executes less b,a"}, {"greater", "<v a>, <e b>", "executes less b,a"},
{"usleep", "<n t>", "aborts the program for at least floor(max(0, t)) ms"}, {"usleep", "<n t>", "aborts the program for at least ⌊max{0, t}⌋ microseconds"},
{"sleep", "<n t>", "executes usleep with t * 1000000 as argument"}, {"sleep", "<n t>", "executes usleep with t * 1000000 as argument"},
{"get_us_time", "<v to>", "stores minetest.get_us_time() into to; can be used to measure time differences"}, {"get_us_time", "<v to>", "stores current time in microsecond precision into *to*; can be used to measure time differences"},
{"tostring", "<ve var>", "var = tostring(var)"}, {"tostring", "<v var>", "Sets *var* to a string representing its value; if it's not defined, it's set to `$nil`, if it's a boolean, it's set to `$true` or `$false`"},
{"tonumber", "<ve var>", "var = tonumber(var)"}, {"tonumber", "<v var>", "Sets *var* to a number representing its value"},
{"toboolean", "<ve var>", "var = var and true or false"}, {"toboolean", "<v var>", "If *var* is `false` or not defined, it's set to `false`, else it's set to `true`."},
{"print", "<e a>[, <e b>[, <e c>[…]]]", "adds variables to the log, seperated by \\t, \\n is added at the end"}, {"print", "<e a>[, <e b>[, <e c>[…]]]", "adds variables to the log, seperated by \\t (tab), \\n (newline) is added at the end"},
{"flush", "", "Output the log, this should vary for every mod."}, {"flush", "", "Output the log, what exactly happens should vary for every mod."},
} }
--[[ --[[
@ -60,19 +60,31 @@ for i = 1,#instr do
mal = math.max(#bef[2], mal) mal = math.max(#bef[2], mal)
end--]] end--]]
local mtl = 2 local o = "### Types\n\n" ..
"Name | Description\n" ..
local o = "Types:\n\n" "----|-------------\n"
for i = 1,#types do for i = 1,#types do
local t = types[i] local t = types[i]
o = o .. t[1] .. (" "):rep(mtl - #t[1] + 2) .. t[2] .. "\n" o = o .. t[1] .. (" "):rep(1 - #t[1]) .. "| " .. t[2] .. "\n"
end end
o = o .. "\n\nInstructions:\n\n" o = o .. "\n\n### Instructions\n\n" ..
"Name | Parameters | Description\n" ..
"-----|------------|------------\n"
for i = 1,#instr do for i = 1,#instr do
i = instr[i] i = instr[i]
o = o .. i[1] .. " " .. i[2] .. "\n" if i[2] ~= "" then
.. " " .. i[3] .. "\n\n" -- TODO: max 80 letters each line i[2] = "`" .. i[2] .. "`"
else
i[2] = "no arguments"
end
o = o .. i[1] .. " | " .. i[2] .. " | " .. i[3] .. "\n"
end end
print(o) o = o ..
"\nNote to Developers: Do not edit the instructions manually " ..
"here in the Readme, do changes in util/standartbefehlssatz_doc.lua and " ..
"execute it.\n"
io.write(o)
io.flush()