tinyscript
tinyscript copied to clipboard
Logical and/or
I have added logical and/or functions and operators with a low precedence. It makes relational operators more C/C++/C#/Java/JavaScript/Python -like. Using bitwise and/or for comparison requires parenthesis that logical and/or does not. (see code snippet below)
print "a=", 2>1 & 3>1
print "b=", (2>1) & (3>1)
print "c=", 2>1 && 3>1
ouputs:
a=0
b=1
c=1
{ "&&", BINOP(5), (intptr_t)logicaland },
{ "||", BINOP(5), (intptr_t)logicalor },
static Val logicaland(Val x, Val y) { return x&&y; }
static Val logicalor(Val x, Val y) { return x||y; }
Speaking of being C-like, the BNF in the readme.md shows '==' for the equality operator, but the code actually uses '='. I have changed mine to '=='. I have also changed the inequality operator from the BASIC/Pascal-like '<>' to the C-like '!='. I realize changing them would break existing scripts, but for new users of TinyScript I think it makes sense.
I've also added a bitwise not and an Elvis operator. Maybe I am getting operator crazy?
I would rather discuss all this before submitting a pull request, as I want to improve the language, not change it.
I think this kind of thing could be added at run time with a TinyScript_Define(). At least, that's the intention. I'd like to minimize additions to the core language to keep it small (the target processor only has 32K of memory, total). I am thinking about a next generation tinyscript-ng that would run on much bigger processors (64K of RAM, wow!) and logical operators would be a good addition for that.
I understand. In the MCUs I work with, RAM is much smaller, and hence, more precious than ROM. In fact, I would like to see all of the operators in a ROM table rather than taking 16 bytes of RAM each (on a 32-bit MCU).
I have the same situation with ROM vs RAM, my MCU has 3MB of ROM, but 256KB of RAM (of course both are quite relatively large, but the system does other things too and quite a lot with tinyscript)