Skip to content

EOBot Scripting Language Advancements #416

@ethanmoffat

Description

@ethanmoffat

The following features should be added to EOBot scripts:

  1. ✅ user-defined functions
  2. ✅ object/list initializers
  3. include/import
  4. ✅ ++ and -- operators
  5. ✅ += and -= operators
  6. ✅=== and !== operators
  7. is operator (type assert) - bool, int, string, object, array, undefined literals
  8. 'on event call func()' operator -- language extension just for eobot?
  9. Enum constant
  10. Auto-mapper for enum types
    • Auto-mapper for .Net functions
    • "standard library" of files via include/import (rather than big block in BuiltInIdentifierConfigurator)
  11. stdin/stdout enhancements
  12. function overloads

Implementation notes/ideas:

✅ Functions

  • Optional return type - not specified = void
  • Parameters don't need type specifiers, just variable declarations
  • Each function is its own separate "sub-program"; need to introduce concept of call stack
  • Calling a function executes that sub-program and passes in outer scope (symbol table)
    • Variables set in a function scope are not set in the outer scope and take precedence over outer scope variables
    • Inner scope variables are a completely separate symbol table that gets reset between invocations
    • Symbol search: check inner scope table first, then outer table for variable name
    • This can be repeated recursively for multiple levels of nested function definitions
  • Main program can just be statements, execution skips over any functions
  • User-defined functions are added to the symbol table during token parsing so they don't need to be defined in order (kind of like labels)
  • goto from within a function should only respect labels scoped to that function
func foo() int {
    $var = 1
    return $var
}

func bar() {
    print ("hello from bar")
}

func baz($in1, $in2, $in3) bool {
    print ($in1 + " is a " + type($in1))
    print ($in2)
    print ($in3)
    return true
}

$res = foo()
bar()
print (baz(1, 2, 3))

Crude grammar:

{IDENTIFIER}:      valid identifer, following C identifier rules
{TYPE}:            one of the core types: int, string, bool, object, array
{STATEMENT_LIST}:  list of statements (from existing grammar)

'X' (in single quotes) indicates the literal token X
[..] indicates optional
... means repeating 1 or more times

func {IDENTIFIER} '(' {IDENTIFIER} {TYPE} [',' {IDENTIFIER} {TYPE}]... ')' [ {TYPE} ] '{' {STATEMENT_LIST} '}'

✅ Object and List initializers

$var2 = 2
$objvar = { x = 1, y = $var2, z = 3 }
// equivalent to:
// $objvar= object()
// $objvar.$x = 1
// $objvar.$y = $var2
// $objvar.$z = 3

$listvar = [ 1, "2", object(), false ]
// equivalent to:
// $listvar = array()
// append($listvar, 1)
// append($listvar, "2")
// append($listvar, object())
// append($listvar, false)

Crude grammar:

{EXPRESSION} |= '{' [{IDENTIFIER} '=' {EXPRESSION} [',' {IDENTIFIER} '=' {EXPRESSION}]... ] ] '}' | '[' [{EXPRESSION} [',' {EXPRESSION}]... ] ']'

Enums

  • values of int type only
  • optional assignment to values (defaults starting at 0 and incrementing)
  • resolved via scope resolution (::) between two identifiers
enum MyEnum {
    A = 1,
    B,
    C,
}

$varA = MyEnum::A
$varB = MyEnum::B

AutoMapper

  • Can be implicit or explicit
  • implicit will throw error on duplicate name; must be unique among loaded assemblies
  • Auto detect if it's a function or enum

For functions:

  • Functions require explicit import
  • Functions require explicit import source to be a class
  • Functions with explicit mapping must be static
  • Functions must have simple type parameters (int, string, bool, enum, maybe dict/array)
    • objects might work with some reflection nonsense but that's a lot of effort
// explicit namespace source
from "Moffat.EndlessOnline.SDK.Protocol.Net.Server" map "LoginReply" to LoginReply

// implicit namespace source (only supported for enums; duplicates throw)
map "CharacterReply" to enum CharacterReply

// function (requires explicit)
from "System.IO.File" map "ReadAllBytes" to ReadAllBytes

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions