dschmenk / PLASMA

Proto Language AsSeMbler for All (formerly Apple)
MIT License
189 stars 26 forks source link

Error in const calculation #7

Closed tingtron closed 9 years ago

tingtron commented 9 years ago

Expressions in const and initialized variable declaration evaluate arithmetic expression incorrectly. In contrast, the same expression in assignment statement is evaluated correctly.

Example, program DIGERR.PLA

// error in const calculation

const   N = 240
const   S = N*14/25+15

byte NS = "N = "
byte SS = ", S = "
byte XS = ", X = "
byte YS = ", Y = "

word X = N*14/25+15
word Y

puts(@NS); puti(N);
puts(@SS); puti(S);
puts(@XS); puti(X);

Y = N*14/25+15
puts(@YS); puti(Y);

Result:

N = 240, S = 3360, X = 3360, Y = 149

Expected:

tingtron commented 9 years ago

The cause seems to be that in const and initialization, only the first operator term is evaluated and the rest of the expression is ignored.

Workaround: break expression into single operator terms.

const S1 = N*14
const S2 = S1/25
const S  = S2+15

But it would still be good to have the ability to evaluate more complex expressions at compile time.

dschmenk commented 9 years ago

You caught me being lazy! As you found out, My const evaluation only uses two operands. I should do a full evaluation, but it adds a great deal of code, or I should extend the expression evaluator to return a flag if the expression results in a constant. I will leave this as a bug so I will remember to return to it. Thanks for all you interest in PLASMA.

Dave...

On Jan 10, 2015, at 3:07 AM, Ting Tron notifications@github.com wrote:

The cause seems to be that in const and initialization, only the first operator term is evaluated and the rest of the expression is ignored.

Workaround: break expression into single operator terms.

const S1 = N*14 const S2 = S1/25 const S = S2+15 But it would still be good to have the ability to evaluate more complex expressions at compile time.

— Reply to this email directly or view it on GitHub.

tingtron commented 9 years ago

I did later take a look at the code, in sandbox, and saw the separate simplified const evaluator. I was also expecting that for the const, the same parser as for expressions be used. I didn't look closely at the expression parser. Does it per-calculate constant branches of an expression? E.g. given 2*3 + Var*5 to emit the equivalent of 6 + Var*5. It seems that a "const" flag/return vaue would help in that regard, and simultaneously be useful in calculating a complete const / initialization expression.

dschmenk commented 9 years ago

Implement full constant expression evaluation.

tingtron commented 9 years ago

Also verified to be working. Thank you for quick turn-around.