JimBoyd wrote:
Druzyek wrote:
The number of words is not unknown to the programmer in the moment they write it
Do you mean the number of cells?
Right
Quote:
Quote:
but it is unknown to someone else reading what they wrote or to themselves if enough time has gone by until they hunt down the stack comment. This is what I mean by unreadable.
Funny, I've never had that problem.
That's interesting. Do you read other people's Forth code? This happened enough times to be annoying in my game even though I only worked on it over a short period. I'll come back in a year and see how much I've forgotten.
Quote:
Does your editor of choice display the function declaration when you type the name of a C function you've already declared elsewhere? Mine never did. Then again, I was using VI so yes I did have to go look elsewhere for the function declaration if I wasn't sure of the arguments it required.
Nope, mine doesn't show me function declarations either. I think you're still missing the point here. If you just see a function call without the declaration, you can tell how many arguments it takes and whether it's returning anything. You can't do that in Forth without the comments. There are other things that you can't tell instantly in C, and I won't argue if you point those out since my point is not that C is perfect, but when it comes to just this one point, there is at least one thing that C has and Forth doesn't. Pointing out unrelated things doesn't make that less true.
Quote:
There's more than one way to get arguments.
Code:
#include <stdio.h>
static void func1(void);
int main(void)
{
func1();
return 0;
}
static void func1(void)
{
int i;
int j;
printf("\nEnter a number: ");
scanf(" %d",&i);
printf("\nEnter another number: ");
scanf(" %d",&j);
int k;
k=i*j;
printf("\n %d times %d is %d.\n", i, j, k);
return ;
}
I'm not sure what your point is. Are you showing that you can pass a pointer that's used to return a value? Are you pointing out that some functions have varargs? Neither of those contradict what I said.
Code:
#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
int sum(int, ...);
int main(void){
int a=2;
int b=42;
int c=137;
int d=43;
int e=-22;
int f=11;
int g=0;
printf("%d\n",sum(3, a, b, c));
printf("%d\n",sum(5, c, d, e, f, g));
return 0;
}
int sum(int n_args, ...){
register int i;
int a, accum;
va_list ap;
va_start(ap, n_args);
accum = va_arg(ap, int);
for(i = 2; i <= n_args; i++) {
accum += va_arg(ap, int);
}
va_end(ap);
return accum;
}
Quote:
I suppose it did keep that promise. In this case the promise was one argument of type int plus an unknown number of arguments. No, the initial argument, n_args, does not tell you how many additional arguments the function requires, A different function with a variable number of arguments could, for example, take n_args number of pairs, or triplets.
Ya, you can use varargs to set up this type of situation, but as you know, the great majority of C code doesn't rely on this. It's telling that you had to go to something relatively uncommon to try to make the point. Still, even with varargs we can tell how many arguments a call is taking even if it doesn't take the same number of arguments every time. Not so with Forth.
Quote:
Quote:
In Forth, you don't know what happens to the stack without the comment, the comment could be wrong (because you've changed the code and forgot to update the comment) or it might not exist if it's someone else's code. This never happens to the stack in C. Ever. Also, having to copy the stack comment down to where the word it relates to is used just to keep things straight is absurd enough to illustrate the point.
I've never actually had to do that. It was a suggestion to help a new Forth programmer who seemed to be in over his head.
Thanks but I'm certainly not in over my head. It's a valid criticism of the design of the language.
Quote:
I don't have a problem with keeping up with what is on the data stack in Forth. Although my Forth has .S to display the contents of the data stack, I rarely use it. I don't have a problem reading and understanding Forth. I find Forth very readable.
I know C. I've used C. If need be, I'll use it again, but I prefer Forth.
Forth allows me to prototype, to flesh out an idea with a word, or a few words, and interactively test and refine said idea.
If you're saying you prefer Forth, I'm not arguing with you. Interactively testing is useful.
Quote:
I suppose I could build the words interactively but it doesn't seem any faster that way.
The context of this was compared to writing the code out into a file then having the emulator type that in for you. It's true that it's hardly faster to type it into the console than it is to type it into a file that's instantly relayed to the Forth prompt. This makes sense anyway when you're cross compiling and not saving compiled words out of the target system back to the host. This doesn't mean I don't use the stack interactively sometimes.
Quote:
Quote:
Sometimes I feel like a human C compiler
Why not a human python compiler? Or some other language?
Because C is simpler than Python and almost any other language you can think of. Even in a language as simple as C with very few comforts, the compiler handles a minimal amount of stack housekeeping so you don't waste brain power on stack gymnastics like you do in Forth. For example, you'll never have to reorder your stack every iteration though a loop in C so they are back in the right order.
Quote:
Six years ago you said:
Quote:
Hi! I just started with 6502 assembly and I find myself often trying to translate from C
As I suggested earlier, you may be doing the same with Forth. Forth and C are so different that a direct translation (even if it is unintentional because you think 'C' when it comes to programming) yields poor results.
No, I don't translate directly from C to Forth (even if I may have been inclined to six years ago), but it's a valid point that I may be doing it unintentionally without realizing it. Now we're back to the same point from previous posts that you still haven't been able to answer. I'm claiming that the performance of Forth on the 6502 is
much worse than both C and assembly and your response is to blame it on my lack of knowledge and experience. I admit that this could be playing a role, which is why I offered to let you improve on my example or point to another example as evidence. You haven't done either of these. At this point, you need to either concede that Forth on the 6502 has many advantages, but performance isn't one of them, or you need to provide something other than dogma to demonstrate that well-written Forth (again admitting that my code may not qualify) runs anywhere near the speed of well-written C or assembly.
Quote:
Quote:
Do you mean my Forth programming is 10% the performance it would be if it were rewritten?
What I meant was that in this game
your Forth programming is 10% the performance of
your assembly language programming.
Fair point. What do you think the percentage would be if you or someone else had written it instead of me? Can you demonstrate how you know that?
Quote:
Quote:
No, I don't think I write Forth nearly as well as an experienced Forth programmer
You don't. Have you read "Starting Forth" and "Thinking Forth" by Leo Brodie?
What about issues of the "Forth Dimensions" magazine?
Yes. Of course I've read Starting Forth. I've also read a good chunk of Thinking Forth. Again, if you're arguing that Forth performed poorly because I lack knowledge, you'll need to demonstrate that somehow.
Quote:
Quote:
How about another example you could point to then that would better reflect the performance difference than the example I've given? That would eliminate my skill as a Forth programmer from the equation.
The only programs I know of for the 6502 family would be programs for the Commodore 64 and I do not know what language was used to write these except one. Starflight was written in Forth and Assembly language.
Yikes! Maybe you shouldn't be making any claims at all then when it comes to performance if you have nothing to back them up with. Or go and run your own tests then report back with the results like I did.
Quote:
Quote:
I decided on Tali because I wanted to generate the fastest forth code possible even if it's larger.
But in the previous post, scotws told you,
Quote:
Tali was written for clarity (in other words, it needed to fit my Forth beginner's brain), not max speed or min size
Right. If you have a Forth that generates faster code, just send me a link and I'll give it a try. As I mentioned, you could cross-compile Forth and do a lot of optimizing to generate a binary that runs significantly faster. I can't port the game to that though if it doesn't exist or I don't have access to it. On the other hand, CC65 could also produce better optimized code, but we can't compare the ideal Forth implementation to the ideal C implementation if they don't exist.
Quote:
leepivonka said,
Quote:
There are several places where Tali could be modifed or extended to generate significantly smaller but slightly slower code
followed by examples. In that same post:
Quote:
The phrase 2 * takes 6 bytes for each use.
The application could be recoded to use 2* to use 3 or 4 bytes & run much faster.
And your reply:
Quote:
I don't plan to change any of the words like leeviponka suggests.
I said at the beginning that I was going for speed over size, so why would I make changes that slow things down to save space as leepivonka suggests? As for the example of 2*, the game uses that extensively. I checked and 2 * appears only four times and always between [ and ] so there is no runtime penalty.
Quote:
I see that you showed an interest in Forth in this forum over five years ago, yet you didn't familiarize yourself with the core words of the Ansi Forth Standard (the Forth you chose to use conforms to that standard) nor with the core extentsion words like 2>R and 2R>.
Wrong again. I see below you found a few improvements, but it's a leap to say I didn't bother familiarizing myself with the core words.
Quote:
It seems like you are resisting doing what is natural in Forth. You didn't like the behavior of 2>R and 2R> so you came up with this:
Code:
: >>r r> -rot >r >r >r ; compile-only
: >>>r r> swap >r -rot >r >r >r ; compile-only
: r>> r> r> r> rot >r ; compile-only
: r>>> r> r> r> rot r> swap >r ; compile-only
In one place you used r>> like this:
Code:
r>> swap
which could have been replaced with:
Code:
2R>
which is much smaller and faster.
That is faster. Thanks.
Quote:
To summarize:
It is not my intention to debate the relative merits of the two languages and I'm not criticizing C. I'm not trying to criticize your programming ability. I'm not trying to convert you to Forth, you took on this project. I am trying to get you to think "outside the box" of C programming to help you.
Hard to read code can be written in any language. Once you become comfortable with Forth, it will be easier to write efficient and readable programs.
If you think that only unreadable code can be written in Forth, how will you be able to write readable code?
If you think the data stack is difficult to keep track of, how will you get comfortable with postfix notation?
If you don't get comfortable with programming in ways that are natural to Forth, if you can't set aside the 'C' mindset when writing Forth then it will be difficult to make progress.
I'm suggesting you set aside your preconceptions. Download and read
"Starting Forth" then
"Thinking Forth". Keep an open mind when reading these books.
This is my advice if you are still interested in learning Forth.
I'm also not trying to convert you to not use Forth. If you are honest about it's shortcomings and still prefer to use it, there is nothing left for us to argue about. That is a matter of preference. I'm working on a project now that involves Forth because despite the disadvantages mentioned above, I still think it's a good fit. My objection is to the misinformation (or maybe even dishonesty) about the capabilities of the language that are floating around. This project is partly about trying to put those claims to the test, at least for the 6502.
It's strange that you say you're not debating the merits of the languages since that's the reason I started the thread, and you've spent pages doing just that. In any case, I think you could use a good dose of the "thinking outside the box" that you prescribe above. Like most of the Forth fans I've met online outside of this forum, you're convinced of the greatness of the language and if anything disagrees with that, then it must be wrong. Hence your suggestion that I read books I've already read and "learn" Forth despite having written thousands of lines of it. How have you determined that my Forth code is slow because I'm used to programming in C versus my Forth code being slow because Forth is slow? You don't seem to see how it could be that someone could know enough to understand the situation and still disagree with you. That's backwards. First, let's look at the data (again happy to consider alternatives if you don't like mine) then determine the language's greatness rather than taking the greatness of the language for granted and dismissing anything that doesn't agree out of hand.
Thanks for the debate. I'm enjoying the discussion and happy to continue if you have more thoughts.