-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
WIP/UNTESTED Add := for simple math operations
expressions still aren't supported and probably won't for a long time, but now you can write basic additions etc. in a single line. Example usage: `my_var := other_var * (16 + 5)` So you'll need to omit percentage signs around variables. This is done for compatibility purposes, even though this adds unnecessary complexity. Expression syntax was a mistake!!1
- Loading branch information
Philip Waritschlager
committed
Dec 26, 2023
1 parent
5839992
commit ef29d81
Showing
4 changed files
with
27 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
class Cmd::Math::EnvMath < Cmd::Base | ||
def self.min_args; 1 end | ||
def self.max_args; 2 end | ||
def self.sets_error_level; true end | ||
|
||
def run(thread, args) | ||
var = args[0] | ||
formula = args[1]? || "" | ||
formula = formula.replace_all /\b[^0-9.()%/*<>+-]\w*\b/g do |word| | ||
thread.runner.get_var(word) | ||
end | ||
stdout_m = IO::Memory.new | ||
stderr_m = IO::Memory.new | ||
result = Process.run "awk", "BEGIN {print #{formula}}", output: stdout_m, error: stderr_m | ||
if result.exit_code != 0 | ||
thread.runner.set_user_var var, "" | ||
return stderr_m.to_s | ||
end | ||
thread.runner.set_user_var var, stdout_m.to_s | ||
return "0" | ||
end | ||
end |