fivemoreminix / rsc

A handwritten scientific calculator for interpreting equations.
https://crates.io/crates/rsc
MIT License
37 stars 9 forks source link

Need To Decide a Syntax For Functions #2

Closed fivemoreminix closed 5 years ago

fivemoreminix commented 5 years ago

Since functions having a syntax like func var1 (var2) poses two problems:

  1. How do we know var1 isn't a function, since it has parenthesis around var2; presumably a variable since it has no potential arguments, and then, assuming var1 is the function, that must mean func is too since it has an argument. But we were only multiplying var1 by var2! We need to find a way to determine this.
  2. This determination needs to be done at computation time, therefore we have to export some information from the interpreter to the computer for it to determine what is a function call and what is not.

We could potentially solve this by having the parser export potential discrepencies (identifier followed by parenthetical expressions) as FunctionOrVariable with its variable name and expression tree, which the computer can possibly index the variables and functions maps to see if there is a function that matches, or if there is a variable, and select one of the two, probably preferring functions.

Since as of posting this issue, computation time only accounts for about 500 ns of time out of the 7.5 microseconds of time the entire evaluation takes, implementing this in the computer should not be such a bad idea, or add much delay.

fivemoreminix commented 5 years ago

Seems that by design or flaw, depending on how you look at it, the parser does prefer functions over multiplying the two variables. For example:

>sqrt = 5
5
>sin sqrt (5)
0.786749131547214
>sin (5*5)
-0.13235175009777303
>sqrt 5
2.23606797749979
>sin ans
0.786749131547214