Skip to content

Commit

Permalink
WIP/UNTESTED Add := for simple math operations
Browse files Browse the repository at this point in the history
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
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 1 deletion.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ Still, here is a brief peek at the syntax. (click the arrows)
</details>


AHK_X11 is a very basic but functional reimplementation AutoHotkey v1.0.24 (2004) for Unix-like systems with an X window system (X11), written from ground up in [Crystal](https://crystal-lang.org/), with the eventual goal of 80% feature parity, but most likely never full compatibility. Currently about 80% of work of getting there is done, but even at 100%, because of the old version of the spec (at least for now), many modern AHK features are missing, especially expressions (`:=`, `% v`), classes, objects and functions, so you probably can't just port your scripts from Windows. More to read: [Project goals](https://github.com/phil294/AHK_X11/issues/8)
AHK_X11 is a very basic but functional reimplementation AutoHotkey v1.0.24 (2004) for Unix-like systems with an X window system (X11), written from ground up in [Crystal](https://crystal-lang.org/), with the eventual goal of 80% feature parity, but most likely never full compatibility. Currently about 80% of work of getting there is done, but even at 100%, because of the old version of the spec (at least for now), many modern AHK features are missing, especially advanced expressions and functions (`x := y(z)`, `% v`), classes, and objects, so you probably can't just port your scripts from Windows. More to read: [Project goals](https://github.com/phil294/AHK_X11/issues/8)

This AHK is shipped as a single executable native binary with very low resource overhead and fast execution time. You can use AHK_X11 to create stand-alone binaries with no dependencies, including full functionality like Hotkeys and GUIs. (just like on Windows)

Expand Down Expand Up @@ -289,6 +289,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%
b += 3
Expand Down
1 change: 1 addition & 0 deletions docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8855,6 +8855,7 @@ <h2 class="calibre9"><span class="calibre23">The "Last Found" Window </span></h2
<p class="calibre8">Variables can be increased or decreased by 1 by using Var++, Var--, ++Var, or --Var.</p>
<p class="calibre8">If either <em class="calibre21">Var</em> or <em class="calibre21">Value</em> is blank or does not start with a number, it is considered to be 0 for the purpose of the calculation (except when using <em class="calibre21">TimeUnits</em>, see above).</p>
<p class="calibre8">If either <em class="calibre21">Var</em> or <em class="calibre21">Value</em> contains a decimal point, the end result will be a floating point number in the format set by <a href="#SetFormat.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">SetFormat</a>. </p>
<p class="calibre8 x11">For general math operations, you can also resort back to the more modern := operator, which you might know from modern Windows AHK. In this case, you want to omit the percent signs % unless you want to double-deref. This := operator (for "expressions") is much more limited on AHK_X11 though, and also performs poorly. Example usage: my_var := other_var * (16 + 5)</p>
<p class="calibre8"> </p>
<p class="calibre8"><strong class="calibre14">Related</strong></p>
<p class="calibre8"><a href="#EnvSub.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">EnvSub</a>, <a href="#EnvMult.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">EnvMult</a>, <a href="#EnvDiv.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">EnvDiv</a>, <a href="#SetFormat.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">SetFormat</a>, <a href="#IfIs.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">If var is [not] type</a>, <a href="#SetEnv.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">SetEnv</a>, <a href="#FileGetTime.htm" class="pcalibre3 pcalibre1 pcalibre calibre5 pcalibre2">FileGetTime</a></p>
Expand Down
2 changes: 2 additions & 0 deletions src/build/parser.cr
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ module Build
case second_word
when "="
cmd_class = Cmd::Math::SetEnv
when ":="
cmd_class = Cmd::Math::EnvMath
when "+="
cmd_class = Cmd::Math::EnvAdd
raise "Add value missing for '+=' expression" if ! other_arg
Expand Down
22 changes: 22 additions & 0 deletions src/cmd/math/env-math.cr
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

0 comments on commit ef29d81

Please sign in to comment.