Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Named functions in VM #3

Open
jimd1989 opened this issue Jan 22, 2023 · 0 comments
Open

Named functions in VM #3

jimd1989 opened this issue Jan 22, 2023 · 0 comments
Labels
enhancement New feature or request

Comments

@jimd1989
Copy link
Owner

Because the VM is stack based, can make a simple stored function syntax. Because it is compiled, this doesn't require a particularly performant data structure. It will only be relevant the first time a user-defined program is entered, not during audio rendering.

The [ and ] chars are not used at the moment, so they might be good candidates.

Defining

Proposed syntax:[name proc]

For example: [avg +2/].

This would be easy to compile.

  1. When a [ is encountered, create a pointer to the next char: the start of name.
  2. Insert a null byte at the whitespace.
  3. Create a second pointer to the char after the space: the start of proc.
  4. Insert a null byte at the ].
  5. These two strings are placed in an unordered array of key:value pair structs for retrieval later in the compilation process.
  6. Since compilation takes place linearly, it should be possible to redefine a func whenever.

Invoking

Proposed syntax: [name]

For example: 2 4[avg]

  1. When a [ is encountered, create a pointer to the next char: the start of name.
  2. Read until either a space or a ], and create a null byte at either address.
  3. If no space was detected, it is not a function definition. Search linearly through the key:value array doing nothing more than strcmp over the keys.
  4. If a match is detected, compile the raw string value to bytecode.
  5. This method means recompiling strings every time they are referenced, but it is dead simple.
  6. Should work with recursive invocations, like if the definition of avg was +[divTwo].
@jimd1989 jimd1989 added the enhancement New feature or request label Jan 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant