CSPIN - A C-to-Spin Converter
Dave Hein
Posts: 6,347
I have been working on a program to convert from the C programming language to Spin.· I call the program CSPIN.· There is still a lot of work to be done it, but I feel it is ready for others to try it out.· The attached zip file contains the cspin.exe executable and clib.spin, which contains a few C string routines and printf.· It also contains a sample hello.c program and a readme file.
CSPIN is a command-line program, and it runs in a DOS window under the Windows operating system.· The program is simple to use.· Just type "cspin hello.c" to generate a hello.spin file.· With the -m option "cspin -m hello.c" will generate a few extra lines to set up the clock and start the serial I/O.
Give it a try and let me know what you think.
Dave
EDIT: The latest version of cspin is posted below, and also at the end of this thread. The current version is cspin084.
CSPIN is a command-line program, and it runs in a DOS window under the Windows operating system.· The program is simple to use.· Just type "cspin hello.c" to generate a hello.spin file.· With the -m option "cspin -m hello.c" will generate a few extra lines to set up the clock and start the serial I/O.
Give it a try and let me know what you think.
Dave
EDIT: The latest version of cspin is posted below, and also at the end of this thread. The current version is cspin084.
Comments
Desktop\cspin hello.c
usage: cspin infile
cspin hello.c
it should then compile it to hello.spin [noparse]:)[/noparse]
Dave, nice one [noparse]:)[/noparse] although I think you need to add a bit more to the output spin file, like
OBJ
c : "clib"
edit: maybe add·some dos prompt options·for example :-
cspin hello.c·-xinfreq 5000000 -pll16x
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
·
edit: can we make this simple to run, like PropBASIC?
humanoido
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
http://www.propgfx.co.uk/forum/·home of the PropGFX Lite
·
The -m option will add extra code to set _clkmode and _xinfreq and to include the object clib.spin.· It is executed by typing "cspin -m hello.c".· The spin file will be put in the same directory as the source file.· "cspin src\test.c" will generate the file src\hello.spin.
I'll modify the program to prompt the user if there are no command-line arguments.· This will allow it to be run as a clickable icon.
Dave
There is ease of use, productivity (input / action ratio), and discover ability in play here.
On "ease of use", this gets top marks. You provide appropriate input, it does the work, and deposits the output. That's easy. Really.
It's not very discoverable, and that's typically what people are referring to when they say, "it's not easy". Your change will significantly improve that.
On productivity, it's good marks, with Baggers chiming in on how to boost that. I would listen, as the man is wicked fast [noparse]:)[/noparse]
Cheers all, and the weekend is almost here!!
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Propeller Wiki: Share the coolness!
8x8 color 80 Column NTSC Text Object
Safety Tip: Life is as good as YOU think it is!
Good work on this. One thing that might help is if you added a GUI front end. I held out for a while on Catalina, partly because I personally prefer to use a command line interface (so much faster to do everything!). But once I realized how much difficulty many people have with command line utilities these days (many windows users don't even seem to know how to start a command interpreter) I decided I had to add a GUI, and in the end it proved not very difficult.
If you don't want to go the whole Code::Blocks route, even a simple "notepad" style GUI where you can just load a file, edit it, save it and then compile it with a button click would do the job.
Ross.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Catalina - a FREE C compiler for the Propeller - see Catalina
A GUI would be very nice, but it will be a while before I get to that.· I've played around with Microsoft Visual C++, but I've really never created a Windows app except for building and modifying sample apps.· There are a number of C features that I want to support before I add a GUI, such as function pointers, enums and floating point.
I'll post an update with the shortcut and command interpreter in a day or so.
Dave
Unfortunately, the lack of a front end interface has restricted the use of these languages to only a few people who still remember how to use the old DOS command line interface. Using the CLI would be possible if someone was able to provide exact instructions. So far, I don't see that. Also, using a CLI is not easy. I hope everyone developing a new language version can follow the fine example set by PROPBASIC or make it even more simple than that. I like the Propeller's SPIN software Tool. How would the ideal compiler function? Simply double click an icon and you are in the text editor to write the program. Select "compile" from the menu and the compiled file appears on the desktop.
humanoido
I've found that people who can actually use the language can use the tools (or can learn to pretty rapidly). On the other hand I've found windows users who can't grasp the concept of downloading & unzipping an .exe file and then clicking on it twice in rapid succession. I'm pretty sure those windows users would not be trying to write C code either.
Your mistake above is that you appear to have unzipped the file in ~/Desktop (or whatever it's called on Windows).
You are running the exe file in that location, however you are telling it the hello.c file is in your current directory.
You could do this one of two ways.
cd Desktop
cspin hello.c
Or
Desktop\cspin Desktop\hello.c
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Life may be "too short", but it's the longest thing we ever do.
Anyone doing anything other than general purpose computing can and absolutely should understand text and the command line.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Propeller Wiki: Share the coolness!
8x8 color 80 Column NTSC Text Object
Safety Tip: Life is as good as YOU think it is!
When I was a lad there were no personal computers and most of the world was computer illiterate. Now everyone has a PC and most of the world is computer illiterate.
Seems to me much better if Dave spends his valuable time and skill in perfecting the guts of the C to Spin translator rather than in creating yet another IDE. Perhaps that could come later.
Frankly I find it a pain in the neck to have to find my way around a new/different IDE for every new device or language I come across. Even more of a pain when I find that the GUI only works on Windows.
Just now I have:
1) For programming with QT the QtCreator IDE.
2) For the Prop the BST IDE
3) For the XMOS devices a version of Eclipse (XDE)
4) For an STM32 with ARM processor the Ride7 IDE (Windows only)
And so it goes...
All in all getting all of these to work as you like is as much trouble as working out a few command line options.
Luckily for 1), 2) and 3) programs can be built from the command line. 4) Is Windows only so that Dev kit sits a box on the shelf unused.
Dave, please, please, having said all that if you do get bullied into creating an editor/IDE do consider making it cross platform so Linux, BSD, Mac users aren't left out. You may never run it on Linux, BSD, Mac but if you write the thing using, say, the Qt tool kit then there would be a chance that it will build for those platforms as well.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For me, the past is not over yet.
If you want to have some more fun, type "set" and "dir" and "help" and .... In particular with "help," am I looking at the actual DOS command list from 5,000 years ago? On my NT OS machine??? I thought DOS was gone after WIN98.
humanoido
Post Edited (humanoido) : 1/22/2010 7:40:40 AM GMT
Yes potatohead is right. The tool and it's IDE should be separate.
By the way I could rephrase one of your sentences there: "when we want to concentrate all our focus on programming the language and not IDE quirks" [noparse]:)[/noparse]
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
For me, the past is not over yet.
humanoido
It seems like the main problem isn't the command-line interface, but knowing which directories the executable and source files are in.· I think the first problem can be solved by putting the cspin directory at c:\cspin.· This way the user will know exactly where cspin.exe and clib.spin are located.
The other problem can be solved by having the user start up the DOS window from his source folder.· I can provide a shortcut that can be copied from the \cspin directory to his source directory.· This should make it easier for people who are unfamiliar with the DOS window to run cspin.· I've also added an interactive mode, where cspin will prompt the user for a command if command-line arguments aren't provided at startup.
Dave
A GUI switch can be used to provide a verbose status on STDOUT and STDERR, while taking instructions from STDIN, as well, leaving the door open for any and all GUI integration and development options.
If default directories are not present, simply creating them when appropriate means the absolute minimum configuration being required, while not inhibiting those that have advanced configurations. (they set their own environment)
I like interactive mode, as that's really discoverable. Nice move there. I run a fair number of command line things for an advanced data management system for my day job. They do the interactive mode thing, and it works well. Works really well, when first learning how to provide proper input too, as when something is missing, the prompt really gets the brain zeroed in on what is needed.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Propeller Wiki: Share the coolness!
8x8 color 80 Column NTSC Text Object
Safety Tip: Life is as good as YOU think it is!
I improved support for signed and unsigned chars and shorts.· I had previously treated them as ints, which could have some strange side effects.· I added #define macro support as well.· All #defines are expanded before converting to Spin, except for #define that resolve to a constant value.· These are converted to Spin constants.
Look at the readme.txt file for more information about this release.
Dave
Thank you Dave!!!!!
for (i = 0; i < 10; i++)
· j += i;
A work-around is to use braces as follows:
for (i = 0; i < 10; i++)
{
· j += i;
}
Another thing to watch out for is incrementing pointers for types bigger than a byte.· As an example, an int pointer is only incremented by one instead of four.· This has caught me a few times.· CSPIN defines the symbol CSPIN_FLAG so you can use this with a #ifdef to insert work-arounds for CSPIN in your source code.· I had to do that for the following function:
int tokenize(char *buffer, char **tokens)
{
· int num = 0;
· while(*buffer)
· {
··· buffer = SkipChar(buffer, ' ');
··· num++;
#ifdef CSPIN_FLAG
··· *tokens = buffer;
··· tokens += 4;
#else
··· *tokens++ = buffer;
#endif
··· buffer = FindChar(buffer, ' ');
··· if (*buffer == 0) break;
··· *buffer = 0;
··· buffer++;
· }
· return num;
}
There was also a problem with "*buffer++ = 0", and I had to break it up into two lines.
CSPIN is aware of some of the functions in the CLIB in the OBEX.· A printf("x = %d\n", x) will automatically be converted to c.printf1(string("x = %d\n"), x).· However, there are a number of functions that I added to CLIB that CSPIN doesn't know about, such as scanf.· You'll have to fix that yourself.· I use the convention of prefixing those functions with a "c_" in my source code and I manually replace occurances of "c_" with "c." in my converted spin code.· My C sources looks something like the following:
#ifdef CSPIN_FLAG
#define scanf c_scanf1
#endif
int getvalue(void)
{
· int value;
· scanf("%d", &value);
· return value;
}
I'll fix this in the next update of CSPIN.
Dave
As for an IDE: On Windows, people could do worse than run the free Notepad++. This allows you to add "Run" commands, so it should be no problem adding
That will run the current file through cspin
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Cheers,
Simon
www.norfolkhelicopterclub.com
Announcement: To cut costs in the current economic climate, we have switched-off the light at the end of the tunnel.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Jon McPhalen
Hollywood, CA
I just wanted to let you know that I tried out your CSpin program.· It didn't work for me BUT that's because I was trying some test cases that I have used while working with Ross Higson during development of the BlackCat source code debugger for Catalina C.· So my tests involved unions of bit fields and other odd ducks.
I want to encourage your efforts with CSpin.· My reasoning is this: I prefer to use Catalina C because of its speed and integrated float support in one cog.· But if I run out of memory, I would like to be able to revert to SPIN and continue working.· What a nice set of tools then we have available: an ANSI C compiler with a debugger while code fits.· Apply CSpin and continue with ViewPort as the debugger if memory limitations threaten to kill the project.
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Thanks for the comments.· I actually started working on CSpin today after leaving it alone for almost 3 months.· The discussion on Parallax's proposed PMC got me thinking about it again.· I·am adding file I/O routines to the C library (clib) and I hope to post that to the OBEX in the next few days.· Once I complete the clib I plan on focusing on CSpin again.
Dave
Dave
Hi Dave,
Has there been any recent discussion of PMC? I've not seen any in the forums, but I may have missed it. I was beginning to think it was a dead product!
Ross.
P.S. very wierd - I originally posted this question an hour or so ago, but it seems to have disappeared - so I'm posting it again!
What a great project! I know that parallax was playing with a similar concept some time ago and never went forward with it. Your progress is encouraged - thanks Dave!