Tokens: Difference between revisions

From Pico-8 Wiki
Jump to navigation Jump to search
No edit summary
imported>Felice Enellen
(style stuff, mostly just changing "you can do x" to "x can be done", as is usual in wiki style)
Line 20: Line 20:
* Instead of assigning one variable per statement, combine assignments into a single statement, saving on the '=' token: <br/><br/><code>a=1 b=2 c=3</code> → <code>a,b,c = 1,2,3</code>  <br/><br/><code>local a=1; local b=2</code> → <code>local a,b = 1,2</code>
* Instead of assigning one variable per statement, combine assignments into a single statement, saving on the '=' token: <br/><br/><code>a=1 b=2 c=3</code> → <code>a,b,c = 1,2,3</code>  <br/><br/><code>local a=1; local b=2</code> → <code>local a,b = 1,2</code>


* You can omit trailing nils when assigning variables: <br/><br/><code>mynum,myobj,myobj2 = 3,nil,nil</code> -> <code>mynum,myobj,myobj2 = 3</code>  <br/><br/><code>local a,b = nil,nil</code> → <code>local a,b</code>
* Trailing nils can be omitted when assigning variables: <br/><br/><code>mynum,myobj,myobj2 = 3,nil,nil</code> -> <code>mynum,myobj,myobj2 = 3</code>  <br/><br/><code>local a,b = nil,nil</code> → <code>local a,b</code>
**You can also omit the declaration entirely if it is a global and it will be equivalent to declaring it as nil. It is worth commenting so you do not lose track of your variables.
**A declaration can be omitted entirely if it is a global and it will be equivalent to declaring it as nil. It may be worth commenting this, so as not to lose track of variables.


*You can usually omit trailing nils when calling functions:   <br /><br /><code>f(a,b,nil,nil)</code> → <code>f(a,b)</code> <br /><br />Beware that some functions do check the number of arguments provided, and differ in behaviour between a nil and an unprovided argument
*Usually trailing nils can be omitted when calling functions: <br /><br /><code>f(a,b,nil,nil)</code> → <code>f(a,b)</code> <br /><br />Beware that some functions do check the number of arguments provided, and differ in behaviour between a nil and an unprovided argument.


* When calling a function with only 1 argument, and that argument is a string or table literal, you can omit the parentheses to save a token:   <br/><br/><code>f("smart")</code> → <code>f "smart"</code>    <br/><br/><code>f({1,2,3})</code> → <code>f {1,2,3}</code>
* When calling a function with only 1 argument, and that argument is a string or table literal, the parentheses can be omitted to save a token: <br/><br/><code>f("smart")</code> → <code>f "smart"</code>    <br/><br/><code>f({1,2,3})</code> → <code>f {1,2,3}</code>


* You can usually pass <code>nil</code> instead of <code>false</code> to boolean variables and parameters, allowing you to use other tricks in this list that allow omitting nil.   <br/><br/><code>f(a,b,false)</code> → <code>f(a,b)</code>    <br/><br/><code>local a,b = true, false</code> → <code>local a,b = true</code>    <br/><br/>Beware that while most operators and functions operating on booleans accept <code>nil</code> as <code>false</code>, a small few (like direct comparison via <code>==</code>) can still break this approach
* In many cases, <code>nil</code> can be used instead of <code>false</code> for boolean variables and parameters, allowing the use of other tricks in this list that allow omitting nil. <br/><br/><code>f(a,b,false)</code> → <code>f(a,b)</code>    <br/><br/><code>local a,b = true, false</code> → <code>local a,b = true</code>    <br/><br/>Beware that while most operators and functions operating on booleans accept <code>nil</code> as <code>false</code>, a small few (like direct comparison via <code>==</code>) can still break this approach.


* When creating your own functions, move any parameters that may not need to be specified or that can have a good default value to the end of the parameter list, so that callers can usually avoid passing them.
* When creating functions, move any parameters that may not need to be specified or that can have a good default value to the end of the parameter list, so that callers can usually avoid passing them.


* Avoid defining constants or computing constants, preferring to use a single constant instead.   <br/><br/><code>hour=60*60; f(hour); g(hour)</code> → <code>f(3600); g(3600)</code>   <br/><br/>Beware that this can make changing those constants harder, so you may want to do it only once you're sure the constants won't change anymore.
* Avoid defining constants or computing constants, preferring to use a single constant instead. <br/><br/><code>hour=60*60; f(hour); g(hour)</code> → <code>f(3600); g(3600)</code> <br/><br/>Beware that this can make changing those constants harder, so prefer to do it only once after being sure the constants won't change anymore.


* Use a local to save the value of a repeated computation, when doing so will save tokens. (The more complex the computation and the more it is repeated, the more likely that a local will save tokens)  <br/><br/><code>f(a+1); g(a+1); h(a+1)</code> → <code>local ap1=a+1; f(ap1); g(ap1); h(ap1)</code>    <br/><br/><code>f(a.x, a.x+1, a.x+5, a.x+10, a.x+12); g(a.x, -a.x)</code> → <code>local ax=a.x; f(ax, ax+1, ax+5, ax+10, ax+12); g(ax, -ax)</code>
* Use a local variable to save the value of a computation that will be used again later, when doing so will save tokens. (The more complex the computation and the more it is repeated, the more likely that a local will save tokens)  <br/><br/><code>f(a+1); g(a+1); h(a+1)</code> → <code>local ap1=a+1; f(ap1); g(ap1); h(ap1)</code>    <br/><br/><code>f(a.x, a.x+1, a.x+5, a.x+10, a.x+12); g(a.x, -a.x)</code> → <code>local ax=a.x; f(ax, ax+1, ax+5, ax+10, ax+12); g(ax, -ax)</code>


* For constant tables of numbers or strings, you can use a string literal with <code>split()</code> to save many tokens. <br/><br/><code>t={1,2,3,10,9,8,99,-1}</code> → <code>t=split "1,2,3,10,9,8,99,-1"</code>
* For constant tables of numbers or strings, a string literal may be <code>split()</code> to save many tokens. <br/><br/><code>t={1,2,3,10,9,8,99,-1}</code> → <code>t=split "1,2,3,10,9,8,99,-1"</code>


* Use <code>and</code> and <code>or</code> instead of <code>if</code> when possible:   <br/><br/><code>if (f()) a=x else a=y</code> →  <code>a = f() and x or y</code> (Beware: only works if x can never be false or nil!)
* Use <code>and</code> and <code>or</code> instead of <code>if</code> when possible: <br/><br/><code>if (f()) a=x else a=y</code> →  <code>a = f() and x or y</code> (Beware: this only works if <var>x</var> can never be <code>false</code> or <code>nil</code>!)


* Prefer member access over array indexing:   <br/><br/><code>tbl['special']</code> → <code>tbl.special</code>   <br/><br/>An object like <code>{a=1,b=2,c=3}</code> takes fewer tokens to read/write values from that <code>{1,2,3}</code>, though the latter takes fewer tokens to construct. This is usually beneficial as objects usually need to be accessed in multiple places but only need to be constructed in one function.
* Prefer member access over array indexing: <br/><br/><code>tbl['special']</code> → <code>tbl.special</code> <br/><br/>An object like <code>{a=1,b=2,c=3}</code> takes fewer tokens to read/write values from that <code>{1,2,3}</code>, though the latter takes fewer tokens to construct. This is usually beneficial as objects usually need to be accessed in multiple places but only need to be constructed in one function.


Miscallenous token saving tips for specific situations:
Miscellaneous token saving tips for specific situations:


* You can save 1 token by converting <code>pairs(mytable)</code> to <code>next, mytable</code> (which is equivalent, with the minor exception that pairs calls the <code>__pairs</code> metamethod if you implemented one)
* It is possible to save 1 token by converting <code>pairs(mytable)</code> to <code>next, mytable</code> (which is equivalent, with the minor exception that <code>pairs()</code> calls the <code>__pairs()</code> metamethod if one is implemented in <var>mytable</var>)
[[Category:Research]]
[[Category:Research]]

Revision as of 05:58, 5 July 2022

One of the stricter limits PICO-8 places on its carts' code is that they must contain no more than 8192 tokens.

A token is one of:

  • A literal value, such as nil, false, true, any number (e.g. 123, 0xff.ff, -3, ~1e4), or any string (of any size).
  • A variable or operator
  • An opening bracket ('(', '[', '{')
  • A keyword, with the exception of end and local

In particular, the following are not counted as tokens:

  • The comma (,), semicolon (;), period (.), colon (:), and double-colon (::)
  • Closing brackets (')', ']', '}')
  • The end and local keywords
  • The unary minus (-) and complement (~) operators when applied on a numeric literal

Token saving tips

Common token saving tips that apply to most situations:

  • Instead of assigning one variable per statement, combine assignments into a single statement, saving on the '=' token:

    a=1 b=2 c=3 → a,b,c = 1,2,3

    local a=1; local b=2 → local a,b = 1,2
  • Trailing nils can be omitted when assigning variables:

    mynum,myobj,myobj2 = 3,nil,nil -> mynum,myobj,myobj2 = 3

    local a,b = nil,nil → local a,b
    • A declaration can be omitted entirely if it is a global and it will be equivalent to declaring it as nil. It may be worth commenting this, so as not to lose track of variables.
  • Usually trailing nils can be omitted when calling functions:

    f(a,b,nil,nil) → f(a,b)

    Beware that some functions do check the number of arguments provided, and differ in behaviour between a nil and an unprovided argument.
  • When calling a function with only 1 argument, and that argument is a string or table literal, the parentheses can be omitted to save a token:

    f("smart") → f "smart"

    f({1,2,3}) → f {1,2,3}
  • In many cases, nil can be used instead of false for boolean variables and parameters, allowing the use of other tricks in this list that allow omitting nil.

    f(a,b,false) → f(a,b)

    local a,b = true, false → local a,b = true

    Beware that while most operators and functions operating on booleans accept nil as false, a small few (like direct comparison via ==) can still break this approach.
  • When creating functions, move any parameters that may not need to be specified or that can have a good default value to the end of the parameter list, so that callers can usually avoid passing them.
  • Avoid defining constants or computing constants, preferring to use a single constant instead.

    hour=60*60; f(hour); g(hour) → f(3600); g(3600)

    Beware that this can make changing those constants harder, so prefer to do it only once after being sure the constants won't change anymore.
  • Use a local variable to save the value of a computation that will be used again later, when doing so will save tokens. (The more complex the computation and the more it is repeated, the more likely that a local will save tokens)

    f(a+1); g(a+1); h(a+1) → local ap1=a+1; f(ap1); g(ap1); h(ap1)

    f(a.x, a.x+1, a.x+5, a.x+10, a.x+12); g(a.x, -a.x) → local ax=a.x; f(ax, ax+1, ax+5, ax+10, ax+12); g(ax, -ax)
  • For constant tables of numbers or strings, a string literal may be split() to save many tokens.

    t={1,2,3,10,9,8,99,-1} → t=split "1,2,3,10,9,8,99,-1"
  • Use and and or instead of if when possible:

    if (f()) a=x else a=y → a = f() and x or y (Beware: this only works if x can never be false or nil!)
  • Prefer member access over array indexing:

    tbl['special'] → tbl.special

    An object like {a=1,b=2,c=3} takes fewer tokens to read/write values from that {1,2,3}, though the latter takes fewer tokens to construct. This is usually beneficial as objects usually need to be accessed in multiple places but only need to be constructed in one function.

Miscellaneous token saving tips for specific situations:

  • It is possible to save 1 token by converting pairs(mytable) to next, mytable (which is equivalent, with the minor exception that pairs() calls the __pairs() metamethod if one is implemented in mytable)