Log in

View Full Version : shebang_mod - a modification to AviSynth to implement script pre-processing


wonkey_monkey
19th February 2022, 17:18
Download: shebang_mod.zip (https://horman.net/avisynth/download/shebang_mod.zip)


Now based on Avisynth+ 3.7.2


As mentioned here (https://forum.doom9.org/showthread.php?p=1963678), I had an idea to add a script pre-processor to AviSynth to allow for plugins that could free up the grammar a bit, or perform other tricks, while maximising backwards compatability and not putting any new restrictions on script writers or AviSynth itself.

I've managed to make it work with a short modification to scriptparser.cpp. Compiled AviSynth DLLs (based on 3.7.2 from https://github.com/AviSynth/AviSynthPlus) are in the zip file above along with an example preprocessing plugin (ScriptMonkey.dll) and a couple of example scripts, as well as the modified scriptparser.cpp and a diff file.

Preprocessing is triggered by a shebang (https://en.wikipedia.org/wiki/Shebang_(Unix)) ("#!") as the first characters of the first (and any immediately following) lines of the script, followed by the name of any function which takes a string as input and returns a string (the function name can be optionally followed by comments, as below):

#!revstr <- this internal AviSynth function reverses the characters of the script (including the order of lines)

21VYoTtrevnoC
srabroloC

As a more useful example, the included ScriptMonkey plugin reformats scripts so you no longer need to use backslashes to continue lines (within function parameter lists only), and allows inter-parameter # comments and trailing commas, so function parameters can be quickly and easily commented out:

#!ScriptMonkey <- this line enables the script

# The ScriptMonkey plugin function allows multi-line parameters without
# requiring backslashes (\) for line continuation, # comments in the middle of
# multi-line parameters, and trailing commas in parameter lists.

ColorBarsHD

ConvertToYV12(
interlaced = true,
matrix = "rec709", # this trailing comma is now allowed
# chromaresample = "point" # this parameter is commented out
)

I put it together pretty quickly so if anyone wants to look at the source code and see if I've made any mistakes I'd appreciate it.

Ceppo
19th February 2022, 18:06
I kinda like the idea. Thanks a lot. I will make some tests when I'm free.

wonkey_monkey
18th March 2022, 18:55
Updated from Avisynth+ 3.7.2.

pinterf
24th March 2022, 08:48
Can you remember what is the reason for requiring backslash for line continuation? I know - compatibility and probably handling some special function calling case. Surely there were discussions about it, Avisynth is mature enough.

wonkey_monkey
24th March 2022, 10:40
Nope, no idea. I did some Googling a few weeks back while I was working on this, and all I could find was an obscure case, that Gavino came up with I think, where getting rid of the backslash requirement could cause a change to the result of a script. It was something like


some_var = -5
+ 5 ? clip1 : clip2


Currently, some_var would equal 5 and clip1 would become implicit last. If lines auto-continued, some_var would be equal to clip2.

However, just to be on the safe side, my mod and filter only removes newlines while inside function parameter lists.

StainlessS
19th April 2022, 05:38
Can you remember what is the reason for requiring backslash for line continuation?

all I could find was an obscure case, that Gavino came up with

Not sure if this is same pre recent Avs+,

# OK

Function Fn() { return true }

x=Round(123.456)
(TRUE) ? Fn() : NOP # On separate line OK

return MessageClip("DONE")


Not OK

Function Fn() { return true }

x=Round(123.456) (TRUE) ? Fn() : NOP # "Script Error. 'Int' cannot be called. Give me A function!"

return MessageClip("DONE")


I sometimes like to pack a few things on a single line, so as to be easy to comment/uncomment the lot, during debug.

EDIT: Shifting NOP out of it, same.

Function Fn() { return true }

x=Round(123.456) (TRUE) ? Fn() : Fn() # "Script Error. 'Int' cannot be called. Give me A function!"

return MessageClip("DONE")


EDIT: Maybe I'm doin' something silly, its 05:45 and I aint been to bed yet. [maybe to do with the new function objects or whatever they are ???]

EDIT: Bit more of a clue,

Function Fn() { return true }

x=Float(123) (TRUE) ? Fn() : Fn() # "Script Error. 'float' cannot be called. Give me A function!"

return MessageClip("DONE")

Looks maybe something like "123.0(TRUE)" , some kind of Parser bug.

StainlessS
20th April 2022, 01:36
Further to above, no error,[removed () from around (TRUE) ]

Function Fn() { return true }

x=Float(123) TRUE ? Fn() : Fn() # NO ERROR

return MessageClip("DONE")