skx / evalfilter

A bytecode-based virtual machine to implement scripting/filtering support in your golang project.
GNU General Public License v2.0
117 stars 12 forks source link

Support the use of hashes .. #155

Closed skx closed 4 years ago

skx commented 4 years ago

This pull-request will add support for Hashes, to our parser, run-time, and evaluation engine.

Once complete #154 will be closed.

skx commented 4 years ago

Sample program:

frodo ~/Repos/github.com/skx/evalfilter/cmd/evalfilter $ cat hash.in 
a = { "Name": "Steve", "Age": 2020 - 1976 }

printf("I am %s - %d\n", a["Name"], a["Age"] );

This works!

frodo ~/Repos/github.com/skx/evalfilter/cmd/evalfilter $ ./evalfilter run hash.in
I am Steve - 44
Script gave result type:NULL value:null - which is 'false'.
skx commented 4 years ago

To support foreach our signature needs to change from:

Object, int, bool

To :

Object, Object, bool

Next up ..

skx commented 4 years ago

Test-coverage needs to be improved, and documentation added; otherwise things work as you'd expect:

// Declare a hash
printf("Creating hash\n")
a = { "Name": "Steve", "Age": 2020 - 1976 }

// Get the keys which are present in the hash:
k = keys(a);

// Show the keys and their values
printf("Iterating over the hash via the output of keys():\n");
foreach name in k {
  printf("\tKEY:%s Value:'%s'\n", name, string(a[name]))
}

// Now do the same thing in one go, via our iteration interface.
printf("Iterating via foreach key,value:\n");
foreach key, value in a {
  printf("\t%s -> %s\n", key, string(value) )
}

// Finally test the index-operation
printf("I am %s - %d\n", a["Name"], a["Age"] );

printf("My hash is %s\n", string(a));
printf("My hash has len %d\n", len(a))

Of course I've not considered what happens if a user passes a map/interface from the golang side to the scripting language. It might be that the reflection code all needs to be updated.

If so I can handle that seperetely, as the language core now contains "hash-support".