marekjm / viuavm

Parallel virtual machine designed to reliably run massively concurrent programs
https://viuavm.org/
GNU General Public License v3.0
71 stars 11 forks source link

Implement iota keyword #163

Closed marekjm closed 7 years ago

marekjm commented 7 years ago

Implement iota keyword that would have similar function as in Go (or APL before), and generate consecutive unsigned integers.

The iota keyword would be used to enumerate register indexes in blocks, and in instructions wrapped in []. Iota enumeration in blocks starts from 1 (to prevent accidental returns), is scoped, and incremented by 1 after each use. Iota enumeration in [] starts from 0, is scoped, and incremented by 1 after each use. The following examples are provided as explanation.

Use-case 1: enumerating register indexes in blocks

Frees programmers from manually assigning register indexes to names.

.function: foo/0
    .name: iota foo
    .name: iota bar
    istore iota 42  ; iota can also be used outside of .name: directives

expands to

.function: foo/0
    .name: 1 foo
    .name: 2 bar
    istore 3 iota

Use-case 2: enumerating function parameters in frames

Frees programmers from manually managing frame slots.

frame ^[(pamv iota (istore 1 42)) (pamv iota (istore 1 16)]

expands to

frame ^[(pamv 0 (istore 1 42)) (pamv 1 (istore 1 16)]

Use-case 3: enumerating parameters in called function

Current iota value can be manipulated using .iota: <value> directive, and enables third use-case.

.function: replace/3
    .name: iota source_string
    .name: iota string_to_replace
    .name: iota replacement
    .iota: 0
    arg source_string iota
    arg string_to_replace iota
    arg replacement iota

expands to

.function: replace/3
    .name: 1 source_string
    .name: 2 string_to_replace
    .name: 3 replacement
    arg source_string 0
    arg string_to_replace 1
    arg replacement 2