Skip to content

Commit

Permalink
fix env-math := expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
phil294 committed Jan 14, 2024
1 parent 3defb83 commit 68e3308
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ Besides the [Legacy Syntax](https://www.autohotkey.com/docs/v1/Language.htm#lega
- `x := 1` -> `x = 1`
- `x := y` -> `x = %y%`
- `b := a + 3` ->

Simple math instructions with `:=` like this one are actually wonkily supported, so chances are you can leave it as is. In this case, you'd omit the percent signs `%` unless you want to double-deref, just like on Windows. But for more complicated stuff such as string concatenation, you'll have to resort back to classic syntax:
```ahk
b = %a%
Expand Down
8 changes: 4 additions & 4 deletions src/cmd/math/env-math.cr
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ class Cmd::Math::EnvMath < Cmd::Base
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)
formula = formula.gsub /\b[^0-9.()%\/*<>+-]\w*\b/ do |word|
thread.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
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
thread.runner.set_user_var var, stdout_m.to_s.strip
return "0"
end
end

0 comments on commit 68e3308

Please sign in to comment.