Author Topic: Slightly esoteric WEIDU requests  (Read 2107 times)

Offline DavidW

  • Planewalker
  • *****
  • Posts: 316
Slightly esoteric WEIDU requests
« on: September 11, 2018, 12:12:50 AM »
I suspect these are only useful to me; I'll list them in increasing order of presumed implausibility.

1) The ability to detect whether a given string actually names a function. At the moment I do it with TRY: run the function, if it doesn't run then catch the exception and return that it doesn't exist. But it's scarcely elegant.

Illustrative syntax:     PATCH_IF IS_A_FUNCTION "%function%" THEN ...

2) Morphic functions that work either as ACTION or PATCH functions (say, with ACTION syntax for definiteness). So I define it with DEFINE_MORPHIC_FUNCTION, just as if it were an action function, then if I call the function in a patch it acts as if there was an INNER_ACTION present.

3) The ability for a function to return an array indexed by something other than integers. As in:
Code: [Select]
DEFINE_ACTION_FUNCTION my_function
RET_HASH blook
BEGIN

ACTION_DEFINE_ASSOCIATIVE_ARRAY blook BEGIN
this=>that
END
END

4) (This is a fairly wild ask, but...) access to a WEIDU version of OCAML lists. Something like

Code: [Select]

MAKE_LIST my_list first_entry second_entry third_entry

OUTER_SPRINT head (LIST_HEAD my_list)
LIST_TAIL my_tail my_list

SCS uses that format very extensively, but it has to fake it up: my "lists" are just space-separated strings, and I have functions to extract things from them, but I think I pay a reasonably high install-time price. I wouldn't even ask except that I assume the functionality is in OCAML anyway and it *might* be possible to expose it to WEIDU.

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Slightly esoteric WEIDU requests
« Reply #1 on: September 11, 2018, 12:28:35 PM »
I'll get back to you on the other stuff, but for #3, RET_ARRAY has no such limitation.

Offline DavidW

  • Planewalker
  • *****
  • Posts: 316
Re: Slightly esoteric WEIDU requests
« Reply #2 on: September 11, 2018, 02:51:00 PM »
That's fantastic. (Off to rewrite SCS's function library.)

Offline DavidW

  • Planewalker
  • *****
  • Posts: 316
Re: Slightly esoteric WEIDU requests
« Reply #3 on: September 11, 2018, 02:58:05 PM »
Actually, one follow-up: can I pass arrays as arguments to functions?

Offline Argent77

  • Planewalker
  • *****
  • Posts: 187
Re: Slightly esoteric WEIDU requests
« Reply #4 on: September 11, 2018, 04:03:31 PM »
Actually, one follow-up: can I pass arrays as arguments to functions?
I don't think that's possible. And a feature like this would probably introduce a noticeable performance hit.

However, you can pass the array name for use in a function. Example:
Code: [Select]
DEFINE_ACTION_FUNCTION MY_FUNC
STR_VAR array_name = ~~
BEGIN
  ACTION_PHP_EACH EVAL ~%array_name%~ AS key => value BEGIN
    PRINT ~Func: %key% -> %value%~
  END
END

ACTION_DEFINE_ASSOCIATIVE_ARRAY my_array BEGIN
  ~key1~ => ~value1~
  ~key2~ => ~value2~
  ~key3~ => ~value3~
END

ACTION_PHP_EACH my_array AS key => value BEGIN
  PRINT ~Global: %key% -> %value%~
END

LAF MY_FUNC
STR_VAR array_name = ~my_array~
END

Offline DavidW

  • Planewalker
  • *****
  • Posts: 316
Re: Slightly esoteric WEIDU requests
« Reply #5 on: September 11, 2018, 05:12:43 PM »
Yes, of course; that works fine.

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Slightly esoteric WEIDU requests
« Reply #6 on: September 20, 2018, 07:07:31 PM »
So, I'm doing the easiest bits first.

1) The ability to detect whether a given string actually names a function.
Should this also consider macros, or should that be a separate value?

Offline Wisp

  • Moderator
  • Planewalker
  • *****
  • Posts: 1176
Re: Slightly esoteric WEIDU requests
« Reply #7 on: September 20, 2018, 07:33:17 PM »
2) Morphic functions that work either as ACTION or PATCH functions (say, with ACTION syntax for definiteness).
And this is in: DEFINE_DIMORPHIC_FUNCTION. They're not really dimorphic, though, as actions functions already run as patches inside an INNER_ACTION; it's quite ridiculous, really.

Offline DavidW

  • Planewalker
  • *****
  • Posts: 316
Re: Slightly esoteric WEIDU requests
« Reply #8 on: September 21, 2018, 01:47:15 AM »
So, I'm doing the easiest bits first.

1) The ability to detect whether a given string actually names a function.
Should this also consider macros, or should that be a separate value?

Well, my use case is generally that some function takes a STR_VAR argument and tries to run it as a function, and I'd like to fail gracefully if it's not actually a function. (Right now I use _TRY.) So from my point of view detecting macros isn't useful.

e.g., here's a fairly commonly-used bit of SCS code:

Code: [Select]

<<<<<<<< .../stratagems-inline/demo_table.2da

first_variable          second_variable      third_variable
foo                        bar                         minsc
boo                       far                          dynaheir
>>>>>>>>

DEFINE_ACTION_FUNCTION my_function
    STR_VAR
           first_variable=""
           second_variable=""
           third_variable=""
[do some stuff]
END
LAF process_table STR_VAR table=blah inline=yes function=my_function END
By default, if my_function isn't actually defined, the process_table function fails hard. I can catch it in a _TRY but it would be cleaner - especially given WEIDU's health warnings about _TRY - just to be able to check if "function" really is a function.

Offline DavidW

  • Planewalker
  • *****
  • Posts: 316
Re: Slightly esoteric WEIDU requests
« Reply #9 on: September 21, 2018, 01:53:52 AM »
PS: as a point of interest SCS explicitly defines:

- 660 ACTION_FUNCTIONs
- 567 PATCH_FUNCTIONs (with another 1600 built at install time)

but only

- 44 ACTION_MACROs
- 12 PATCH_MACROs

So I'm very much interested in FUNCTION support; MACROs are to be used only as a last resort!


Gob Oafenplug

  • Guest
Re: Slightly esoteric WEIDU requests
« Reply #10 on: August 22, 2020, 12:10:18 PM »
I suspect these are only useful to me; I'll list them in increasing order of presumed implausibility.

1) The ability to detect whether a given string actually names a function. At the moment I do it with TRY: run the function, if it doesn't run then catch the exception and return that it doesn't exist. But it's scarcely elegant.

If this is still under consideration, here's something I've been doing a whole lot of:

Code: [Select]
DEFINE_ACTION_FUNCTION MAKE_PRO STR_VAR bindTraceFn = ~~ resProVisualEffect  = ~~ BEGIN
  OUTER_TEXT_SPRINT bindTraceFn ~%bindTraceFn% MAKE_PRO->:~
  OUTER_PATCH lots_of_functions_that_construct_a_projectile_such_as_the_below BEGIN
    LPF setRefProVisualEffect STR_VAR bindTraceFn resProVisualEffect END
  END
END

DEFINE_PATCH_FUNCTION setResProVisualEffect STR_VAR bindTraceFn = ~~ BEGIN
  TEXT_SPRINT bindTraceFn ~%bindTraceFn% setResProVisualEffect->:~
  LPF testResExists STR_VAR bindTraceFn res = EVAL~%refProVisualEffect%~ ext = BAM END
  WRITE_ASCIIE 0x020 ~%refProVisualEffect%~ (8)
END

DEFINE_PATCH_FUNCTION testResExists STR_VAR bindTraceFn = ~~ BEGIN
  TEXT_SPRINT bindTraceFn ~%bindTraceFn% testResExists->:~
  PATCH_IF !FILE_EXISTS_IN_GAME ~%res%.%ext%~ BEGIN
    PATCH_FAIL ~%bindTraceFn% no such file as %res%.%ext%~
  END
END

LAF MAKE_PRO STR_VAR resProVisualEffect = SomeBamFile NED

for error handling and runtime type checking. If the fulfillment of DavidW's request were to expose function name as an automatic, implicit variable, this would remove some line noise from every function definition (at the obvious expense of transferring aforementioned line noise to the WeiDU source. :D )

If not, well, I've already implemented this the other way, so I'd have a compelling excuse not to change anything in code that already works.

 

With Quick-Reply you can write a post when viewing a topic without loading a new page. You can still use bulletin board code and smileys as you would in a normal post.

Warning: this topic has not been posted in for at least 120 days.
Unless you're sure you want to reply, please consider starting a new topic.

Name: Email:
Verification:
Type the letters shown in the picture
Listen to the letters / Request another image
Type the letters shown in the picture:
What color is grass?:
What is the seventh word in this sentence?:
What is five minus two (use the full word)?: