Skip to content

Support Arithmetic Operations#277

Merged
solara404 merged 26 commits into
mainfrom
math_for_var_276
May 8, 2026
Merged

Support Arithmetic Operations#277
solara404 merged 26 commits into
mainfrom
math_for_var_276

Conversation

@solara404

@solara404 solara404 commented Apr 21, 2026

Copy link
Copy Markdown
Collaborator

Fix: #276

The set() function has been extended to support arithmetic operations.
The existing behavior remains unchanged to preserve backward compatibility

Previously:
set (variable) (value)

New:
set (variable) (value) [operation] [value]
value is computed when the= symbol is used. Variables with numeric values can be used. Numbers, variables, and operators may be separated with spaces.

For example, set x =10*2+1 is equivalent to set x = 10 * 2 + 1.

Supported operators and symbols: +, -, *, /, %, (, )

Example:
set x 10
set y x + 5 // y = 15
set z y * x // z = 150

set x 10            // equal to "set x = 10" for numeric values
set y = x + 1       // y = 11
set z = 2             
set z = x + z * 2   // z = 14
set y = x y         // y = 110 
set x = 2x          // x = 20

When = is used, variables are resolved.

set xyz 1
set abc xyz       // abc = "xyz"
set mno = xyz     // mno = 1

@aaumar25

Copy link
Copy Markdown
Member

It is one way to do that. Since we are going to support arithmetic operation, why do not we try to support it more naturally? How about this use case

set x = 10
set x=10
set y = x * 2 + 10
set y=x*2 + 10

Lets commit into supporting it, but limit the supported operators to the one you mentioned. Also be careful with invalid operation like dividing by zero.

@solara404

Copy link
Copy Markdown
Collaborator Author

I was thinking of something like this. However, I wanted to use the existing parseAndRun() function.

@solara404

solara404 commented Apr 28, 2026

Copy link
Copy Markdown
Collaborator Author

@aaumar25 the more natural way has been implemented. It can now be used with and without adding space.

@solara404 solara404 force-pushed the math_for_var_276 branch from 821c4a4 to 86fdbbd Compare May 6, 2026 01:25
@solara404 solara404 requested a review from aaumar25 May 6, 2026 05:42
@solara404

Copy link
Copy Markdown
Collaborator Author

@aaumar25 Should the PR title be marked as breaking?
I would also change the commit message of the following commit:

feature: add 'rest' functionality to parseAndRun()
to:
feat!: add 'rest' functionality to parseAndRun()

And additionally, adding information to the PR description.

BREAKING CHANGE: A additional rest parameter has been added. Functions that rely on raw argument indexes beyond the final rest parameter will need to update their logic.

@aaumar25 aaumar25 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good approach regarding the calculation structure. I think the output of calc should be floating point instead of integer. What happen if want the following operation to change next carrier target position with car_pos + 0.33?

Comment thread src/command.zig Outdated
Comment thread src/command.zig Outdated
Comment thread src/command.zig Outdated
Comment thread src/command.zig Outdated
Comment thread src/command.zig Outdated
Comment thread src/command.zig Outdated
Comment thread src/command.zig Outdated
Comment thread src/command.zig Outdated
Comment thread src/command.zig
Comment thread src/command.zig
@aaumar25

aaumar25 commented May 7, 2026

Copy link
Copy Markdown
Member

@aaumar25 Should the PR title be marked as breaking?

I do not think it is breaking anything, it just added one field with default value, so existing parameters does not need to explicitly assign rest field.

However, I am thinking about catching the rest field in the compile time, so that we can return compile error instead of checking it in runtime.

Comment thread src/command.zig
@solara404 solara404 requested a review from aaumar25 May 8, 2026 06:33
@aaumar25

aaumar25 commented May 8, 2026

Copy link
Copy Markdown
Member

Just squash and merge

@solara404 solara404 merged commit f9a2f19 into main May 8, 2026
2 checks passed
@solara404 solara404 deleted the math_for_var_276 branch May 8, 2026 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Mathematical Operations for Variables

2 participants