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

Indexing arrays programatically with a variable #31

Open
yousifBilal opened this issue Jun 5, 2024 · 4 comments
Open

Indexing arrays programatically with a variable #31

yousifBilal opened this issue Jun 5, 2024 · 4 comments

Comments

@yousifBilal
Copy link

Hello,

we have a case where we want to access an array element programatically:

(set [some_index 3])
(reval my_signal.my_array<some_index> 0)

This however does not work. It is reading the whole my_signal.my_array<some_index> as a variable name and I can't seem to figure out how to make it evaluate some_index so that eventually compiles to this: my_signal.my_array<3>.

I managed to do it by defining a custom operator:

index_signal = lambda seval, args: seval.eval(
    str(args[0]) + "<" + str(seval.eval(args[1])) + ">"
)
wal.register_operator("index_signal", index_signal)

then doing

(define indexed_value (index_signal "my_signal.my_array" some_index))

Only then I can successfully evaluate the index.

It would be nice if whatever between <> is evaluated rather then being taken as is.

@LucasKl
Copy link
Member

LucasKl commented Jun 20, 2024

Hi,

Thanks for being patient, I had a few very busy weeks.
I had indexing problems like this myself. You can solve cases like this with a similar idea to index_signal in pure WAL for example, with a macro like this.

(defmacro get-index [signal idx] 
  `(get (symbol-add ',signal "<" ,idx ">")))

Evaluating whatever is between <> would be handy. However, this could potentially clash with signals in the waveform.
I will think about it, if you have any ideas feel free to share :)

@yousifBilal
Copy link
Author

yousifBilal commented Jun 20, 2024

Thank you for the reply.

I see. I guess I wasn't aware of the symbol-add operator. So this is what we use to create a symbol that we build at runtime, am I correct?

@LucasKl
Copy link
Member

LucasKl commented Jun 20, 2024

Yes, with symbol-add you can concatenate multiple symbols, strings, or numbers at runtime. E.g.

>-> (symbol-add 'a 'b 3 (+ 2 2) "-def")
ab34-def

@yousifBilal
Copy link
Author

Great! Maybe consider adding it to the documentation so other people can see the feature. You can close the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants