fiddle icon indicating copy to clipboard operation
fiddle copied to clipboard

Refactored parse_signature

Open kojix2 opened this issue 5 years ago • 10 comments

Hi fiddle developers.

This is just an idea. A proposal to improve the parse_signature method.

Background: I wanted to parse_signature recursively when the argument was a function (i.e. a function pointer). When I wrote a monkey patch for it, I noticed that parse_signature was unnecessarily complicated.

  1. Line 114 and line 117 are almost the same code.
  2. Each line is unnecessarily long.
  3. Some variable names are confusing.
  4. Also, the variables $1, $2, etc. looks old-fashioned.

2 - 4 are more of a library-wide issue, and are my personal opinions rather than objective facts. Each library has its own history.

Here, I am focusing on 1.

If you think the pull request is not good, just close it.

Thank you.

kojix2 avatar Dec 23 '20 13:12 kojix2

Hey, wait a minute. I think I made a mistake.

kojix2 avatar Dec 23 '20 13:12 kojix2

You modified the regular expressions, from \* to *. Also, you are calling an extra parse_ctype(ret, tymap) instead of just TYPE_VOIDP. Probably better to keep such call inside the case statement: [parse_ctype($1.strip, tymap), $2, $3].

Maumagnaguagno avatar Dec 23 '20 14:12 Maumagnaguagno

Fixed. @Maumagnaguagno I appreciate the help.

kojix2 avatar Dec 23 '20 14:12 kojix2

I guess you need to propagate tymap to parse_ctype to avoid creating a new Hash in tymap ||= {}.

Maumagnaguagno avatar Dec 23 '20 14:12 Maumagnaguagno

Yes, I think you're right. But this is the first time I've sent a PR to Fiddle. I think I'll leave it as is and see what the project owner has to say about it...

Anyway, thank you again.

kojix2 avatar Dec 23 '20 14:12 kojix2

I went back to work on the monkey patch one more time, but it wouldn't work without passing the tymap :facepalm: I have to fix it. Maybe we should add a test for typealias...

kojix2 avatar Dec 23 '20 15:12 kojix2

I wanted to parse_signature recursively when the argument was a function (i.e. a function pointer).

Could you show a signature you want to parse?

kou avatar Dec 23 '20 20:12 kou

An example: void uiSpinboxOnChanged(uiSpinbox *s, void (*f)(uiSpinbox *s, void *data), void *data)

kou avatar Jan 05 '21 00:01 kou

Yes, thanks. I would like to create a customized method. When you pass a block to that method, it will create a Fidle::Closure::BlockCaller with the appropriate type. To do this, each function needs to know the type information beforehand.

kojix2 avatar Jan 05 '21 06:01 kojix2

Idea:

We use [TYPE, SIZE] for sized array type. e.g.: [Fiddle::TYPE_CHAR, 80] for char x[80]

It may be a good idea that we use [Fiddle::TYPE_VOIDP, RETURN_TYPE, [ARGUMENT_TYPE, ...]] for function pointer. e.g.: [Fiddle::TYPE_VOIDP, Fiddle::TYPE_VOID, [Fiddle::TYPE_VOIDP, Fiddle::Type_VOIDP]] for void (*f)(uiSpinbox *s, void *data)

kou avatar Apr 12 '21 09:04 kou