How to write a code-generator/compiler - code-generation

I would like to know how you would generate some kind of bytecode from an abstract syntax tree.
I've searched the internet but I cannot find anything helpful.
I've seen some mention of using templates but cannot find someone that actually explains what it is, how it works or how to implement it.
Just one thing though, I don't have any real programming experience and I'm completely self-taught so I'm not looking for an 800 page book on the theory of writing a compiler, I'm looking for something a little more practical, some kind of tutorial working through an actual example.

I learned a lot by following Let's Build a Compiler, by Jack Crenshaw many moons ago. It uses Turbo Pascal for implementation and generates 68000 assembler, so there is some steps to get it running on a modern host but it works through a lot of steps that is still valid.

Take a look at Kaleidoscope tutorial in LLVM: http://llvm.org/docs/tutorial/
and for a somewhat different perspective, http://community.schemewiki.org/?90min-scheme2c

Related

porting java code to contiki-os

i am using contiki-os to simulate some motes which would have semantic capabilities. As the contiki-os (erbium) is written in C but our semantic libraries are written in java.
can anyone here guide me if it is possible to exploit these libraries in erbium or contiki-os. or i have to rewrite everything from scratch ?
update
just a minor update to the question. is it possible to use java code on the cooja simulator?
Cooja is indeed written in Java.
You can extend or modify Cooja if you need.
You can find out more about Cooja on the Contiki wiki as well as in numerous papres by Fredrik Österlind. Perhaps you should also take a look at Fredrik's PhD thesis "Improving Low-Power Wireless Protocols with Timing-Accurate Simulation", which is mostly about Cooja.
You might be able to use something like this:
http://www.codemesh.com/products/junction/
It appears to have a code generator that takes a java bytecode and create C code from it... but it might also need a runtime library that's platform specific.
With all that in mind, I don't think you will be successful. Most of the platforms are nearly out of space and/or flash by the time you are working with Erbuim; I doubt you'll have resources to process java code somehow.
And if you did get some success from this approach it would probably take a lot of time and effort to do so. With that time and effort you probably could have written the C code to do what you need instead.

Fake documentation/code generator or rewriter

This is a serious question, but the intent is not very serious; it's actually a joke I wanted to play on someone and while thinking about it I got quite interested.
Everyone knows http://pdos.csail.mit.edu/scigen/ I suppose: i was wondering ; is there any kind of code + documentation generator? Something which generates 'plausible' looking (doesn't even have to work) Java code and code documentation for instance?
Seems like a very interesting project to me and I couldn't find anything on Google. Does something like this exist?
Edit: something which would refactor a codebase beyond recognition would be good too; kind of a Markov chain for programming.
Well you could try Mechanical Turk if you have a budget for your practical joke. If you are more into an automated solution, a dissociated press type of algorithm would work. Call me old fashioned, but I prefer the real thing to what these previous two methods could do.

Has anyone had much success recently using the Cappuccino Framework?

I'm just creating my first project in Cappuccino without a lot of success. Looking for examples and documentation, there isn't a lot out there...
The differences from real Objective-C and the lack of libraries mean you cannot easily convert code directly from say a OS X desktop app. to Cappuccino.
As an experienced PHP, and RoR developer (besides the Java, VB, VC.....) I can't quite see the logic of abstracting/wrapping JavaScript in another language.
Any comments or suggestions for a more efficient framework?
If you have specific questions that can't be answered in the IRC channel (it just depends who's in there) you should post the question to the mailing list and someone will help you.
As for why you would want objective-j I'll point you to this blog post which contains several reasons why objj exists. http://cappuccino.org/discuss/2008/12/08/on-leaky-abstractions-and-objective-j/
I have converted several Objective-C classes nearly line for line into Cappuccino. Mostly it's a matter of changing NS to CP and changing pointers to non-pointers. What differences between Objective-C are you thinking about?
Indeed you could even take your existing XIB files from your Cocoa desktop app and convert them to Cappuccino using nib2cib. Here's how to work with Interface Builder using XCode 4: http://groups.google.com/group/objectivej/browse_thread/thread/786331dbcbc9c7b1.
I also recently started a project in Cappuccino and what I could say is: definitely it worth it. I'm used with javascript as well as (but less) cocoa (which helps). In short, the javascript is very fine, probably one of my favorite language because of it's flexibility. But it does lack of structure. For example there is no class in javascript and you have to trick with object inheritances, prototyping and so on. By using the Cappuccino framework at first you have all those very convenient facilities but also you do not reinvent the wheel each time. This is cross browser standardized so that you only have to focus on your code and not on the IE whims. As it is based on cocoa, cappuccino also comes with the MCV scheme in mind which again let you structure your project in an efficient way.
Ok you probably already know all this. What I could tell now about the learning curve is that I had to find the right tools to really get in. i.e.Installing cappuccino (sudo Starter/bootstrap.sh), creating a simple nib application (capp gen -t NibApplication YourApplication) then using xcodecapp-cocoa to listen, convert and open the project into Xcode what probably the hardest part of this learning curve to me…
Now if you are still not satisfied with cappuccino, I can only suggest to look at another framework like http://javascriptmvc.com/ , www.grooveshark.com used it to do their amazing webapp-site but I personally would never go back to html for building a web application. (conversely I would not use cappuccino for a webpage neither, "The cobbler should stick to his last")
I began writing cappuccino a couple of months ago and had a lot of difficulty when it came to setting up the proper development environment. I also had numerous problems writing working client server code that worked well.
I eventually broke through and started doing better with Cappuccino and then I found these video tutorials that would have helped so much earlier on. They did cost me $30 but I really wished I had watched these at the start because they help you get set up properly as a developer and get you started on the right learning curve.
I actually discovered these videos when using Cappuccinos IRC which has helped me a couple of times when I couldn't find an answer.
Info on the videos here: http://cappuccino.org/discuss/2011/10/19/cappuccino-training-course-ideveloper-tv/

Do Poor Code Samples Turn You Away From Libraries?

I've been evaluating a framework that on paper looks great. The problem is that the sample code is incomplete and of poor quality. The supplied reference implementations are for the most part not meant to be used (so they can be considered as sample code as well) and have only succeeded at confusing me.
I know that it's common for things to look better on paper, but my experience with the sample code is turning me away from further investigation.
Do you let poor code samples change your judgment of frameworks/libraries? So far my experience has been similar to the "resume effect": if someone doesn't put the effort into spell checking their resume, they probably won't get the job...
For me, it does. I tend to want to avoid libraries where the code samples are incomplete. If the library is open source, I will overlook it, since I can directly look at the code and see if the library's internals are reasonable, and I know that, if there is a problem someday, I could (if I had to) fix it.
If the library is commercial, and their samples and/or documentation is poor, I look elsewhere. I just see it as risk management - poor samples make me fear the quality of the library in general.
No matter how good something is on paper or in theory, it can still be crap when programmed.
I think this is a valid reason to turn away from and evaluate other libraries. As a potential user of a library a lack of documentation and/or bad code samples gives the impression that the library is not yet mature enough for use by third parties. In time it may well gain the missing pieces but until then I think its reasonable to look elsewhere.
I was recently evaluating the multitude of blogging applications that people have uploaded to github.com I quickly skipped ones that no documentation as they obviously weren't ready for others to use. The ones that remained at the end had a good README with info on how to get the app up and running as well as an online example of the code running.
Poor code samples combined with poor documentation will make me turn away from a library unless there is a compelling reason to use it. However, a library that has either good code samples or good documentation is usually worth using. (Assuming that the library itself otherwise meets my needs.)
If I can't find good examples (and/or documentation) illustrating how to use the library, I'm definitely less likely to use it - just as a practical matter, it'll be harder for me to figure out how. But I don't care what the code that implements the library itself looks like. I don't think I'd choose one library/framework over another just because the developers of the one have shown an ability to write cleaner code (which is what I understand the "resume effect" to mean).
Lack of documentation and examples makes me a whole lot less likely to use that particular library. It's not worth my time testing and trying to figure out how a black box works if there are alternate solutions to the problem out there.
Yes, definitely. Every library should come with a simple example using program and a CLI interface (for very simple libraries with <3 methods and <10 hooks, one example should suffice).
And why does your framework "look great" if it's so hard to use that even the original coders make mistakes using it?
It certainly matters to me. Evidence of sloppy/incomplete coding and poor communication decreases my confidence that the actual implementation code is stable and robust.
Myself yes, but there must be people out there who aren't turned off by this otherwise there are plenty of open source projects that would have died a long long time ago.

What inherited code has impressed or inspired you?

I've heard a ton of complaining over the years about inherited projects that us developers have to work with. The WTF site has tons of examples of code that make me actually mutter under my breath "WTF?"
But have any of you actually been presented with code that made you go, "Holy crap this was well thought out!" or "Wow, I never thought of that!"
What inherited code have you had to work with that made you smile and why?
Long ago, I was responsible for the Turbo C/C++ run-time library. Tanj Bennett wrote the original 80x87 floating point emulator in 16-bit assembler. I hadn't looked closely at Tanj's code since it worked well and didn't require attention. But we were making the move to 32-bits and the task fell to me to stretch the emulator.
If programming could ever be said to have something in common with art this was it.
Tanj's core math functions managed to keep an 80-bit floating point temporary result in five 16-bit registers without having to save and restore them from memory. X86 assembly programmers will understand just what an accomplishment this was. Register space was scarce and keeping five registers as your temp while simultaneously doing complex math was a beautiful site to behold.
If it was only a matter of clever coding that would have been enough to qualify it as art but it was more than that. Tanj had carefully picked the underlying math algorithms that would be most suitable for keeping the temp in registers. The result was a blazing-fast floating point emulator which was an important selling point for many of our customers.
By the time the 386 came along most people who cared about floating-point performance weren't using an emulator but we had to support Intel's 386SX so the emulator needed an overhaul. I rewrote the instruction-decode logic and exception handling but left the core math functions completely untouched.
In my first job, I was amazed to discover a "safe ID" class in the codebase (c++), which was wrapping numerical IDs in a class templated with an empty tag class, that ensured that the compiler would complain if you tried for example to compare or assign a UserId into an OrderId.
Not only did I made sure that I had an equivalent Id class in all subsequent codebases I would be using, but it actually opened my eyes on what the compiler could do to guarantee correctness and help writing stronger code.
The code that impresses me the most, and which I try to emulate - is code that seems too simple and easy to understand.
It is damn difficult to write that kind of code. :-)
I have a funny story to tell here.
I was working on this Javaish application, filled with getters & setters that did nothing but get or set and interfaces and everything ever invented to make code unreadable. One day I stumbled upon some code which seemed very well crafted -- it was basically an algorithm implementation that looked very elegant = few lines of readable code, even though it respected every possible rule the project had to adhere to (it was checkstyled automatically).
I couldn't figure out who on the team could have written such code. I was dying to discuss with him and share thoughts. Thankfully, we had switched to subversion (from cvs) a few months earlier and I quickly ran am 'svn blame'. I loled all over the place, seeing my name next to the implementation.
I had heard stories about people not remembering code they wrote 6 months back, code that is a nightmare to maintain. I could not believe such a thing could happen: how can you forget code you wrote? Well, now I'm convinced it can happen. Thankfully the code was alright and easy to extend, so I've only experienced half of the story.
Some VB6 code by another programmer at my company I came across that handled the error conditions very well (whether it be deal with them directly or log them).
Along with some rather complex code that was well commented.
I know this will bring a lot of answers like,
"I've never find good code before I step in" and variations.
I think the real problem there is not that there isn't good coders or excellent projects out there, is that there's an excess of NIH syndrome and the fact that no body likes code from others. The latter is just because you have to make an intellectual effort to understand it, a much bigger effort than you need to understand you own code so that you dislike it (it's making you think and work after all).
Personally I can remember (as everyone I guess) some cases of really bad code but also I remember some pretty well documented, elegant code.
Currently, the project that most impressed me was a very potent, Dynamic Workflow Engine, not only by the simplicity but also for the way it is coded. I can remember some very clever snippets here and there, as well as a beautiful metaprogramming library based on a full IDL developed by some friends of mine (Aspl.es)
I inherited a large bunch of code that was SO well written I actually spent the $40 online to find the guy, I went to his house and thanked him.
I think Rocky Lhotka should get the credit, but I had to touch a CSLA.NET application recently {in my private practice on the side} and I was very impressed with the orderliness of the code. The app worked extremely well, but the client needed a few extensions. The original author had died tragically, and the new guy was unsophisticated. He didn't understand CSLA.NET's business object based approach, and he wanted to do it all over again in cut-and-dried VB.NET, without any fancy framework.
So I got the call. Looking at a working example of WinForm binding and CSLA.NET was pretty instructive about a lot of things.
Symbian OS - the old core bit of it anyway, the bit that dated back to the Psion days or those who even today keep that spirit alive.
And sitting right along side it and all over it is all the new crap created by the lowest bidders hired by the big phone corporations. It was startling, you could actually feel in your bones whether a bit of the code-base was old or new somehow.
I remember when I wrote my bachelor thesis on type inference, my Pascal-to-Pascal 'compiler' was an extension of a Parser my supervisor programmed (in Java). It had a pretty good structure as far as I can remember, and for me who had never done any serious Object-oriented programming, it was quite a revelation.
I've been doing a lot of Eclipse plug-in development and often had to debug into the actual Eclipse source code. While I haven't "inherited" it in the sense that I'm not continuing work on it, I've always been impressed with the design and quality of the early core.