Qais "qaisjp" Patankar

DSL to translate SAMP Pawn to MTA Lua

24 Apr 2019

This is a very quick example of what a DSL would look like to translate SAMP Pawn code to MTA Lua code. The aim here is to map each SAMP function to one or more MTA functions.

Important: It’s just something I wrote up as procrastination for exams. I just wanted to dump this page somewhere. I have no idea if this will go anywhere, and if it did, it probably wouldn’t be in this form. I’ve probably overcomplicated it, but hey, it was fun typing this out!


The DSL:

# `->` means that we print (rewrite) this output instead
# Variables are automatically replaced as necessary, no need for interpolation like `${z_angle}`
# Other values can be inserted as necessary (e.g. x/y rotation angle)
# Conflicts in names will not be noticed by the semantic analyser, so maybe we do need interpolation?

# `=>` is the assignment operator
# The RHS may evaluated to check if it contains existing variables à la `${color1}`...
# ... otherwise the RHS is directly replaced.
#
# Not sure if we want to allow more complex generated code that uses DSL-only variables,
# hence the assignment operator above being odd.

# Obviously the `->` on the function declaration line (non-blank line below this)
# is the return variable, and not a hint for rewriting output.
#
# Perhaps the print/rewrite operator should be something else?

func AddStaticVehicle(modelid, Float:spawn_x, Float:spawn_y, Float:spawn_z, Float:z_angle, color1, color2) -> vehicle
    -> vehicle = createVehicle(modelid, spawn_x, spawn_y, spawn_z, 0, 0, spawn_z)

    # Short circuit if both colors are random
    if color1 == -1 and color2 == -1:
        return

    # Reassign values of source variables if a random value is requested
    if color1 == -1:
        color1 => math.random(0, 126)
    if color2 == -1:
        color2 => math.random(0, 126)

    # Palette colors, so we must provide exactly 4 arguments
    -> setVehicleColor(vehicle, color1, color2, 0, 0)

So an input text of:

vehid = AddStaticVehicle(411, 1, 2, 3, 90, -1, 6)

Is rewritten to:

vehid = createVehicle(411, 1, 2, 3, 0, 0, 90)
setVehicleColor(vehid, math.random(0, 126), 6)

Similarly:

vehicle = AddStaticVehicle(411, 1, 2, 3, 90, -1, -1)

Is rewritten to just:

vehicle = createVehicle(411, 1, 2, 3, 0, 0, 90)

References: