Code Monkey home page Code Monkey logo

Comments (8)

VinGarcia avatar VinGarcia commented on May 29, 2024

Hello @grazba, this is an issue we still have on the system #52, I was postponing this because it is somewhat complex, but I will put my hands on it so it is fixed asap.

In the short term, we will have to think of an alternative solution.
One suggestion would be to implement it as a update() type-specific function, e.g.:

  • myClass.update(MyObj)

I am not entirely sure this solves it for you (at least temporarily), but inside the function you would have access to a "this" object that will be your "myClass" instance. e.g.:

packToken myClass_update(TokenMap scope) {
  MyClass& m = scope["this"].as<MyClass>();
  // ... do stuff ...
}

Also, if your class is based on the TokenMap type, you might prefer to put this function in the prototype of your class, since as a type-specific function it would affect all the tokens of type MAP, not only the ones which are instances of your class.


If you are curious the problem with assignment operations is that it should receive not only the left/right operands as most operations do but also some kind of reference of the TokenMap where the left operand is stored. Otherwise changing the left token would not necessarily affect the original object, e.g.:

  • a.b = 10 requires the function to replace the value o the key "b" of the map "a".

I will take some time to think the best way to do this. And hopefully, this weekend, I will have time to fix it permanently.

from cparse.

 avatar commented on May 29, 2024

I have already considered other solutions for now since my projects relies on a user interface (it's a calculator for "special data objects") but the goal too is to develop a small scripting language so that the users could write complex scripts and run them directly without relying on the UI.

Well i hope you will find a solution soon until then i'll focus on finishing the project on using the Ui to execute reads and writes where it should.

Thanks again 👍 !

from cparse.

VinGarcia avatar VinGarcia commented on May 29, 2024

That's cool @grazba. I am happy to know you are really using this library in something interesting =].

And about the issue I think I have fixed it =].

There are still some steps before I close issue #52:

  • I need to write some tests
  • I need to write some documentation
  • I want to add 2 new built-in operations: ++ and --

However, if you pull the most recent version of the library and read the builtin-features/operations.inc file you'll see that the first operation defined is now the assignment operation. You may just copy it to your project and edit it as you would with any other operation. It should work fine.

from cparse.

 avatar commented on May 29, 2024

Hi @VinGarcia sorry for the late reply, i was pretty busy with some other projects :) !

I have two questions about the assignment operator , what am i supposed to return ? and is the return value what makes the assignment ?

from cparse.

VinGarcia avatar VinGarcia commented on May 29, 2024

This type of operator usually return the same thing that was assigned and it is meant to be used for assignments in strange places e.g.:

a = b = c
// or:
f(d = e)

a will receive the return value of the operation b = c in the first case and in the second case the function f would receive the return value of the operation d = e.

The actual assignment, i.e. changing the value in its original storage location, is performed on the object which stores the variable being assigned, i.e. the data->left->origin object, which may be either a MAP or a LIST, in both cases you must use the index of the variable stored on data->left->key, which on the first case is a string and on the second case is an integer. The lines on the built-in operation that actually perform the assignment are:

  • map[var_name] = right; - if it is inside a local object
  • data->scope[var_name] = right; - if it is a new local variable (the scope is also of type MAP)
  • (*map)[var_name] = right; - if it is an existing local variable

And for list containers:

  • list[index] = right;

So as you can see this is a slightly more complicated operator, modifying it would probably be in the advanced features category, however, if you just want to implement a new behavior when the type in the left is an instance of your class you may just add an if (left->type == MY_TYPE) on the begining of the function, or alternatively add a new operation associated with the "=" operator and register it to be evaluated before the built-in one, e.g.:

opMap_t& opMap = calculator::Default().opMap;
opMap.add({MY_TYPE, "=", ANY_TYPE}, &MyAssign);
opMap.add({ANY_TYPE, "=", ANY_TYPE}, &Assign);

Should do the trick.

from cparse.

VinGarcia avatar VinGarcia commented on May 29, 2024

from cparse.

 avatar commented on May 29, 2024

i deleted the messages because it was my fault , i tried to get rid of the .inc files replaced them with regular .h ones and for some reason it did not work, so every built-feature was missing assignment and str but weirdly enough my types were still there. So yeah i deleted the messages because everything is working as expected (y) !

from cparse.

VinGarcia avatar VinGarcia commented on May 29, 2024

@grazba, I just wanted you to know I have written a new section on the docs explaining how to create new operations of this type, I called it Creating Advanced Operations.

I will probably read it again someday, I never trust the first version I write =P.

Also, you don't have to read it but if you do, it would be great to have your feedback.

Regards

from cparse.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.