Shop OBEX P1 Docs P2 Docs Learn Events
CSPIN - A C-to-Spin Converter — Parallax Forums

CSPIN - A C-to-Spin Converter

Dave HeinDave Hein Posts: 6,347
edited 2017-07-11 23:43 in Propeller 1
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.
«13

Comments

  • HumanoidoHumanoido Posts: 5,770
    edited 2010-01-21 04:51
    I have the DOS window up on the screen in my Windows XP computer. In very simple words, what is the next step? With the unzipped files on the desktop, typing the pathname of Desktop\cspin hello.c will only give the response of usage: cspin infile, with no new file appearing on the desktop. What is the next step?

    Desktop\cspin hello.c
    usage: cspin infile
  • BaggersBaggers Posts: 3,019
    edited 2010-01-21 10:38
    humanoido, in your dos prompt type
    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

    ·
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-01-21 10:57
    Baggers said...
    humanoido, in your dos prompt type
    cspin hello.c
    it should then compile it to hello.spin [noparse]:)[/noparse]
    I'm sure it did. I just can't find it. Where to look?

    edit: can we make this simple to run, like PropBASIC?

    humanoido
  • BaggersBaggers Posts: 3,019
    edited 2010-01-21 12:17
    hello.spin is in the same folder. ( or should be, check the date and time of the file to see that it's re-written it. )

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    http://www.propgfx.co.uk/forum/·home of the PropGFX Lite

    ·
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-01-21 14:16
    Maybe the "simple to use" comment isn't quite accurate.· The current form of the program has no user interface.· One way to use the program is to put your C source files in the cspin directory where the cspin.exe and other files are.· From the DOS prompt you would "cd" to the cspin directory and run it there.· Another way to run it is to copy cspin.exe to a directory in the DOS path.· Type "path" from the DOS prompt to see which directories are used to run executables.

    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
  • potatoheadpotatohead Posts: 10,261
    edited 2010-01-21 16:15
    This is my PotatoOCD in play here, but: (UI characterization, design, and testing is something I do)

    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!
  • RossHRossH Posts: 5,462
    edited 2010-01-21 21:05
    Dave,

    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
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-01-21 22:41
    Thanks to everyone for the suggestions about the user interface.· I hadn't really though much about the need for a UI.· I thought everyone used "vi" for their editor, and then ran command-line commands. smile.gif·· I'll try to make it easier to use by adding a clickable icon (Windows shortcut) to start the program.· It will then prompt for commands to set the mode and convert a file.· I'll even add an "exit" command so the user doesn't have to control-C out of it.smilewinkgrin.gif

    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
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-01-22 05:03
    There are several languages that use command line interfaces. As a Windows and Macintosh user, I find the steps and instructions to use these inordinately cumbersome, somewhat difficult to understand, not easy to remember, and entirely incomplete and insufficient in simple instructional documentation. There are always some sequences of pathnames left out, and many things are assumed by the guru people offering the languages.

    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
  • BradCBradC Posts: 2,601
    edited 2010-01-22 05:57
    humanoido said...

    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'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.
  • potatoheadpotatohead Posts: 10,261
    edited 2010-01-22 06:51
    I believe the right solution here is to produce code that can have a GUI wrapper, but also continue to educate people on non GUI computing.

    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!
  • heaterheater Posts: 3,370
    edited 2010-01-22 07:27
    What's going on?

    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.
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-01-22 07:29
    BradC said...
    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
    It's been many years since I did anything with command lines and all my good DOS books have vanished. But putting excuses aside, thanks Brad, you jogged my mind with the precise syntax that worked perfect on the first try. In fact, now I can run other command line languages that evaded my computer recently. However, when we want to concentrate all our focus on programming the language and not DOS syntax, I agree with potatohead:
    potatohead said...
    ...the right solution here is to produce code that can have a GUI wrapper, but also continue to educate people on non GUI computing.
    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
  • heaterheater Posts: 3,370
    edited 2010-01-22 07:41
    Of course I forgot. DOS. The command line from hell. Almost as bad as CP/M [noparse]:)[/noparse]. The reason why most people don't see the use of a command line or are put off using one. Ah well.

    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.
  • HumanoidoHumanoido Posts: 5,770
    edited 2010-01-22 07:58
    heater said...
    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.
    I agree with heater. This is the year 2010, right? I find it unfathomable to think that, given the high level quality standards that Mac produced over 20 years ago for its "compatible in any case" GUI, that other vendors and programming masters cannot follow its example. There is simply no need to reinvent the GUI front end wheel every time we go from Windows to LINUX to MAC to etc. Also, the fine work accomplished, and continuing, by Dave should not be diluted with too many burdensome castings in alternate directions.

    humanoido
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-01-22 16:25
    I am surprised that most of the comments in this thread are focused on the UI.· I agree that a GUI would be very useful.· I have added this to my list of things to do before I release version 1.00.

    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
  • potatoheadpotatohead Posts: 10,261
    edited 2010-01-22 18:31
    If it were me, I would have the program state it's environment assumptions, and command line argument summary on a run with no arguments. Defining a few environment variables, that would override those assumptions then lets those people who get what an environment is make whatever adjustments make sense, given their working environment.

    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!
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-02-01 03:39
    I have an new update to the CSPIN program.· This version includes an interactive command mode.· To install the program copy the cpin directory from the zip file to c:\cspin.· There is a cspin shortcut that can be copied to the desktop or to the user's workspace.· Double-clicking the shortcut will start cspin in the interactive mode.· cspin can also be run from the command line as before.

    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
  • sssidneysssidney Posts: 64
    edited 2010-04-16 14:04
    This program is great!!!! Since I practically think in C this is an amazing help in writing spin code.
    Thank you Dave!!!!!
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-04-16 16:29
    Thanks.· I haven't worked on it for a while, but I'll probably start implementing a few more features and fix a few bugs.· I use it so I can maintain a C version of Spin programs that I develop.· That way I can test things on a PC, and then port it to the Propeller.· One bug I found is that I don't do the increment part of a "for" loop if it doesn't have braces.· The following code will spin forever without exiting the loop:

    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
  • sssidneysssidney Posts: 64
    edited 2010-04-17 02:21
    If you want some help adding new stuff let me know. I'd be willing to give it a shot.
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-04-17 11:56
    sssydney, thanks for the offer.· I'm working on something else right now, but I hope to get back to cspin in a few days.
  • simonlsimonl Posts: 866
    edited 2010-04-17 12:36
    @Dave: Nice work. I'm not a C programmer, but I'll try to give this a go soon.

    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
    <path to cspin>\cspin.exe "$(FULL_CURRENT_PATH)"
    

    That will run the current file through cspin wink.gif

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    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.
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-04-17 15:18
    simonl said...

    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
    <path to cspin>\cspin.exe "$(FULL_CURRENT_PATH)"
    

    That will run the current file through cspin wink.gif

    I hadn't heard about Notepad++ before.· I tried it out, and it's pretty cool!
  • JonnyMacJonnyMac Posts: 9,104
    edited 2010-04-17 15:23
    I'm using EditPlus to do the same thing -- works fine.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon McPhalen
    Hollywood, CA
  • Bob AndersonBob Anderson Posts: 122
    edited 2010-04-21 02:47
    Dave:

    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.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
  • Dave HeinDave Hein Posts: 6,347
    edited 2010-04-21 03:34
    Bob,

    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 HeinDave Hein Posts: 6,347
    edited 2011-04-13 09:24
    I've made a few improvements to cspin over the past month, so I thought I would attach the latest version. It still has a lot of limitations, but I did fix a few problems and added a few more standard C functions.

    Dave
  • RossHRossH Posts: 5,462
    edited 2011-04-13 15:57
    Dave Hein wrote: »
    Bob,

    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

    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!
  • Invent-O-DocInvent-O-Doc Posts: 768
    edited 2011-04-13 15:58
    I miss TRSDOS (well maybe not).

    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!
Sign In or Register to comment.