cleolibrary / CLEO5

CLEO 5 for GTA San Andreas
https://cleo.li
MIT License
57 stars 6 forks source link

Feature - CLEO Containers #204

Open MiranDMC opened 2 months ago

MiranDMC commented 2 months ago

Introduce data container feature and opcodes to support it.

Container instance is meant to store single kind of data type. Available types: int, float, text As far as it is technically possible types validation is performed in opcode commands at runtime. In case of mismatch error is thrown.

Class name: container

Opcodes:

create_container - container type
remove_container

container_size
container_clear

container_insert - one or multiple values
container_insert_front - one or multiple values
container_insert_at - index and one or multiple values

container_remove - optional count
container_remove_front - optional count
container_remove_at -  index and optional count

container_get - one or multiple return values
container_get_front - one or multiple return values
container_get_at - index and one or multiple return values
container_get_random - one or multiple return values

container_contains - one or more search values
container_count - one search value
container_find - search value
container_find_back - search value

container_sort - sorting order and zero or more synchronized container handles

container_write_to_file
container_read_from_file
x87 commented 2 months ago

I think type should be set in the constructor.

int_container = create_container type 0
string_container = create_container type 1
container_add int_container 1 2 3
container_add int_container "runtime error"
container_add string_container "ok"
MiranDMC commented 2 months ago

I think type should be set in the constructor.

int_container = create_container type 0
string_container = create_container type 1
container_add int_container 1 2 3
container_add int_container "runtime error"
container_add string_container "ok"

How getters will look then?

x87 commented 2 months ago
int_container = create_container type 0
container_add int_container 1 2 3
int x = get_by_index int_container 0 // x= 1
int y = get_by_index int_container 1 // y= 2
int z = get_by_index int_container 2 // z= 3

string_container = create_container type 1
container_add string_container "ok"
longstring s = get_by_index string_container 0 // s= "ok"

names are temporary to illustrate the idea

MiranDMC commented 2 months ago

Will the getter opcode be able in every case to distinguish return parameter type and validate it? Especially float and int types.

x87 commented 2 months ago

addition

int_container = create_container type 0
container_add int_container 1 2 3
int x = get_by_index int_container 0 // x= 1
int y = get_by_index int_container 1 // y= 2
int z = get_by_index int_container 2 // z= 3

string_container = create_container type 1
container_add string_container "this is a very long string that couldn't live in a variable"
int s = get_by_index string_container 0 // s= pointer to "this is a very long string that couldn't live in a variable"
print_help_formatted s // prints "this is a very long string that couldn't live in a variable"

string containers return a pointer.

x87 commented 2 months ago

Will the getter opcode be able in every case to distinguish return parameter type and validate it? Especially float and int types.

I wouldn't care in runtime. you provide a 32-bit variable and runtime either copies the value (if it's int or float) or returns a pointer if it's a string, or other object.

validation can be done in compiler or linter

x87 commented 2 months ago

runtime should keep track of created containers and delete them / free memory when the script ends (on game reload)