Wow, 2k code is a lot. I didn't do any optimizations, I bet that could be significantly reduced in both time and space. It is also a very, very simplistic algorithm - I'm sure there are better. The recursive call was laziness on my part, but you're right - it would be better implemented without recursion. Thinking about it further this morning, I realize that I didn't implement a unary '-' either, which could be bad as it makes the following test impossible: TA<-1 Instead one has to work around it by saying: TA < (0-1) Which is a hack! Oh well, I'll just leave that as an exercise for the reader. -Adam On Thu, Apr 23, 2009 at 7:54 AM, Isaac Marino Bavaresco wrote: > Nice code! > > Out of curiosity I downloaded your code and tried to compile with > Hi-Tech PICC. > > The function 'calc_number' calls itself =A0recursively because of the NOT > operator, so I commented it out. I had to do other minor PIC specific > changes. > > Compiled and run OK. > > Occupied 2087 words of FLASH and 140 bytes of RAM in a PIC16F648A. > > It calculates "(123+234)*34" in 11.89ms @ 20MHz. > > Regards, > > Isaac > > > M. Adam Davis escreveu: >> It took me a lot longer to find mine than I expected! =A0I posted one >> simple equation parser here: >> >> http://www.ubasics.com/simple_c_equation_parser >> >> The code and compiled test executable (windows) can also be >> downloaded, http://www.ubasics.com/files/equation.zip >> >> It uses a stack method with no operator precedence. =A0It handles binary >> operators (+, -, *, /, |, &, ^), unary operators(!), parenthesis((,)), >> and numbers. =A0Parenthesis can be used to force precedence, everything >> else is strictly left to right evaluation. >> >> You'll need to add < and > binary operators (and, or, not, xor, >> already exist), and you'll need to either add symbols to represent >> your inputs, or modify the parse routine to accept characters and use >> the correct variable when a word occurs in the equation. >> >> You'll also need to instruct users to use parenthesis rigorously: >> >> T1>T2&T1>72 becomes (T1>T2)&(T1>72) >> T1>T2+5&T2<32 becomes (T1>(T2+5))&(T2<32) >> >> The program I posted compiles under tcc (tiny c compiler) and should >> be standard ansi C, so it ought to port fine. =A0Of course you'll need >> to remove the example main() at the bottom, and perhaps the odd >> debugging printf scattered through the code (I believe they are all >> commented out...). >> >> Good luck, let me know how it works out, and if you do add the ability >> to use variables and <, > operators, feel free to send me the code and >> I'll drop it on the website for others. >> >> -Adam >> >> On Wed, Apr 22, 2009 at 9:26 PM, Forrest W Christian wrote: >> >>> I've got a temperature control application where I'd like to be able to >>> let the customers enter expressions (via serial) like: >>> >>> T1>T2&T1>72 >>> >>> or >>> >>> T1>T2+5&T2<32 >>> >>> Where T1 and T2 are temperature readings from a thermocouple. =A0 The >>> result of these expressions will be used to turn on and off fans and/or >>> heaters in an application I am working on. =A0 I could hardcode a lot of >>> options, but I think some simple customer-modfiable rules would be best >>> in this particular application. >>> >>> So, before I write my own parser and the like I figured I'd ask if >>> there's code out there which would be suited for this, and is small (the >>> target is a PIC16F886 and CCS C - and it's not easy to change it in this >>> application). >>> >>> I've found some example code but it's all overly big and not well suited >>> to what I'm looking for. =A0 =A0I know how to write this myself, but I'd >>> rather just use some existing code - or perhaps hear some other ideas. >>> >>> Oh, I really only need a few variables (I'll probably use TA, TB instead >>> of T1, T2 just to simplify parsing), a few boolean functions (and, or, >>> greater than, less than, maybe >=3D or <=3D), and possibly addition, >>> subtraction, and less possibly multiplication and/or division. >>> >>> -forrest > > __________________________________________________ > Fa=E7a liga=E7=F5es para outros computadores com o novo Yahoo! Messenger > http://br.beta.messenger.yahoo.com/ > > -- > http://www.piclist.com PIC/SX FAQ & list archive > View/change your membership options at > http://mailman.mit.edu/mailman/listinfo/piclist > -- = http://www.piclist.com PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist