language-bash icon indicating copy to clipboard operation
language-bash copied to clipboard

Arithmetic is not parsed

Open pbiggar opened this issue 8 years ago • 2 comments

Looks like arithmetic becomes an ArithSubst String, and it isn't parsed any further. What's the plan there? Should I instead try to parse the arithmetic strings as C?

pbiggar avatar Oct 03 '16 20:10 pbiggar

I admit that I didn't implement it because my original use case (parsing config files) didn't use arithmetic expressions.

According to the docs, the innards of a $((...)) undergo variable expansion etc before the arithmetic is parsed. So really, we should parse the innards as a Word instead of a String. Since this expansion can only be performed at runtime, we can't parse the actual arithmetic expression when we parse the script.

However, it would be nice if we have a side module that can parse arithmetic from a string (similar to the Cond and Expand modules). For now, using a C parser could work (like the language-c package).

The fact that there could be command substitutions etc in the arithmetic expression is a little annoying. If you're writing a static analysis tool or a transpiler, maybe you could only accept Words that are plain strings in the arithmetic expression to rule out any funny business.

knrafto avatar Oct 04 '16 01:10 knrafto

Took me a while to find it so including it here: the bash arithmetic parser: http://git.savannah.gnu.org/cgit/bash.git/tree/expr.c

Entry point from bash is evalexp.

~~Nope, that's the src to expr.~~ (Actually it's both!)

pbiggar avatar Oct 06 '16 01:10 pbiggar