Agent-E11 / m_calc_rs

MIT License
0 stars 0 forks source link

Add support for user defined functions #2

Open Agent-E11 opened 11 months ago

Agent-E11 commented 11 months ago

Possibly denoted by uppercase characters

Agent-E11 commented 11 months ago

There is a small implementation problem. How will I store user defined functions? The context HashMap is already kind of cumbersome to have to pass in to all the functions, so I don't want to create another func_context or something.

Agent-E11 commented 11 months ago

Possible solutions:

Store exactly the same as other variables

// Pseudo code
context: HashMap = [
    ("F", "x^2 + x + 1"),
    ("G", "\sin(x)"),
];

This would probably rule out multi-variable functions, would force all functions to be uppercase (or another distinguishing feature), and would require the free variable to be x (or another specific letter/identifier)

Store with an enum as a key

// Pseudo code
enum VarEntry = {
    Var(String), // Name
    UsrFn(String, Vec<String>), // Name, variables
}

use VarEntry::*;
context: HashMap = [
    (UsrFn("F", ["x", "y"]), "\sqrt(x^2 + y^2)"),
];

I don't think there are any drawbacks. But, I will need to make a way to index them (Using name probably)

(I just realized this might be a bad idea)

Store with String as key, enum as value

context: HashMap = [
    ("A", UsrFn(["x", "y"], "\sqrt(x^2 + y^2)")), // ("name", Type([vari, ables], "expression"))
]