CCodeGenerator: suggestion
Stephan Rudlof
sr at evolgo.de
Sat Dec 25 16:54:31 UTC 1999
"Andrew C. Greenberg" wrote:
>
> >Dear Andrew,
> >
> >it would be nice if ST constructs like
> >
> > digit _ count = fromLen
> > ifTrue: [0]
> > ifFalse: [pFrom at: count].
> >wouldn't be compiled to
> >
> > digit = if (count == fromLen) {
> > 0;
> > } else {
> > pFrom[count];
> > }
> >
> >, what leads to a C compiler error,
> >but instead to something like
> >
> > digit = (count == fromLen)
> > ? 0
> > : pFrom[count];
>
> I'm not sure what to make of this. If the compiler did that, then:
>
> boolean ifTrue: [ [stmtList boolean] whileTrue ]
>
> would yield an error. The problem is that we are mixing statements
> with expressions, a non-problem for Smalltalk, but a serious
> dificulty for C. I think enough code in the interpreter depends upon
> the ability to nest control statements inside #ifTrue:ifFalse that it
> would break a lot.
>
> I suppose that we could implement something like #expIfTrue:ifFalse:
> to give you access to the c alternative expression, but is it worth
> it?
I think I understand the point. Something like #expIfTrue:ifFalse: would
C'ify the ST code, what's ugly.
I'm not a compiler specialist, but couldn't the compiler recognize if
there are control structures like 'for' or 'while' which aren't allowed
in C construct like
(expr) ? (commaSeparatedStmtList, expr) : (commaSeparatedStmtList,
expr);
? Then it could generate an error.
My C compiler accepts constructs like:
---
void vf( int arg) { printf("\nvf( %d)", arg); }
double df( int arg) { printf("\ndf( %d)", arg); return arg; }
void main()
{
int k;
k = 0 == 0 ? vf(1), df(2) : df(3), df(4);
printf("\nk: %d", k);
k = 0 == 1 ? vf(1), df(2) : df(3), df(4);
printf("\nk: %d", k);
// interpreted as: ( k = (0 == num) ? (vf(1), df(2)) : df(3) ), df(4);
// so it makes sense to use more braces...
}
---
, so it is possible to use - not control - statements inside
expressions.
> The point of CCompiler is not to implement Smalltalk, but just a
> subset sufficient to express a hunk-o-code.
But it is as easier to write plugins as more ST constructs are eaten by
the ST2C compiler...;-)
Merry Christmas,
Stephan
P.S.: To avoid misunderstandings: This is not a criticism of your work
(it is great!), but a suggestion for - possible and nice, but not
necessary - improvements!
P.S.2: For other people possibly working at this compiler we should post
such conversations to the ML, isn't it?
--
Stephan Rudlof (sr at evolgo.de)
"Genius doesn't work on an assembly line basis.
You can't simply say, 'Today I will be brilliant.'"
-- Kirk, "The Ultimate Computer", stardate 4731.3
More information about the Squeak-dev
mailing list
|