That's a lot of code

Mar 29, 2006 12:20

So Prabhakar estimates that a reasonable compiler written in Scheme will have about 15000 lines of code. pcolijn_feed's and my current count: slightly under 7000. Perhaps that's why we're not generating very much code yet.

Actually it makes me feel a little relieved because I was wondering if our code count was high given the expressiveness of Scheme.

compilers, scheme

Leave a comment

Re: 80-columns anonymous March 29 2006, 23:20:12 UTC
That's 'cause you're using SCSH. I would have told you to use PLT Scheme, which has a proper IDE, and gives you a complete stack trace when there's an error. (There's even a scriptable debugger in the latest release, though I haven't attempted to try using it.)

But you are right about the 80 columns. If you have to break up argument lists so that there's one argument per line, that's the right thing to do, so maybe LOC isn't the right measure. That has nothing to do with expressiveness. Lack of expressiveness in Scheme is not coding idiomatically, not taking advantage of higher-order functions (built-in and ones you write, e.g. tree walkers), doing things several times in a slightly different way instead of abstracting out the commonality.

I don't entirely understand the rules for 444 -- you have to write your own tools, but how far does that extend? Can you use the Scheme SRFIs? PLT Scheme's "parser-tools" collection is clearly out; that's a port of lex/bison. But what about their extensions -- structures, objects, regexps, ML-style pattern matching? (That last one alone seems to be able to cut code size in half at least, by not requiring laborious constructing/deconstructing of data structures; using it, one can do insertion into a red-black (balanced) BST in about twenty lines of code.)

Reply

Re: 80-columns morethanreal March 29 2006, 23:51:17 UTC
You can get a backtrace with scsh. If the script is run interactively, it will stop on exception and you can use ,debug to get a backtrace. No line numbers, but I've found it pretty useful. caffeinemonkey, the start-scsh script will start scsh with all the libraries we need.

We can use the SRFIs and all the extensions. I'm not sure if we're coding idiomatically since I'm not sure what the Scheme idioms are, and whether we have good abstractions depends on whether the code is written at 2pm or 4am :)

Reply

Re: 80-columns anonymous March 29 2006, 23:59:35 UTC
Two weeks of Scheme in 241 doesn't really teach you about idiomatic coding, does it? So send me your code already...

Reply

Re: 80-columns anonymous March 30 2006, 01:23:47 UTC
Okay, got the code.

I'm not a Scheme expert, though I've probably done more coding in it than you have (that is, more diverse coding -- all of what I've written combined may not total 7000 lines). Your code isn't as idiomatic as mine would be (and mine is probably not as idiomatic as a Scheme expert's would be). You don't seem to be using higher-order functions (map, for-each, filter, etc.) as much as I would expect, though it's variable; they show up in some places but not others (is one of you more comfortable with them than the other?). Leaning on SRFI 1 more would probably have helped. There are definitely places where more abstraction would help, for instance in the "dependencies" in parser.scm or in the large number of table-set!'s in analyser.scm (in fact there are more set!'s than I would like overall, though admittedly it is hard when you are being taught the algorithms in imperative pseudocode). You're not using symbols very much -- why give tokens numerical values? And there's no internal documentation at all, except apparently in useful.scm. You're leaning on string-append a lot; SRFI 28 (format) or fprintf to string ports (neither of which you have in Scheme 48, apparently) would have helped (this is going to bite you hard in the code generation you haven't done yet). Some of your functions go on for several screenfuls, which is definitely unidiomatic.

Okay, that's me being critical, which is my default mode. On the positive side, there doesn't appear to be a lot of code bloat here; you're not writing Java or C++ in Scheme or anything like that; you're using records and hash tables intelligently; and you have a sensible organization. It looks pretty good.

Reply

Re: 80-columns caffeinemonkey March 30 2006, 02:51:19 UTC
Yeah, I knew next to no scheme when we started. I now know a bit more, so I'm more comfortable with things like map and such, which is why there's some really awkward code in some places (written the first night I did any scheme) and things are a little nicer in some places. I must say that I'm still not entirely comfortable in scheme. I mean I can do things competently now, but it takes a while before you really start thinking in the idioms of the language.

I don't think either of us really knew a lot about the advantages/disadvantages of the various scheme variants when we started, and scsh worked so we stuck with it.

You mention that string-appends will be problematic; are they O(n^2) in scheme48 or something? Would it therefore be better to join a list of strings in lieu of a big string-append?

Reply

Re: 80-columns anonymous March 30 2006, 03:34:59 UTC
I mean string-appends are problematic from the point of view of writing legible code. What you have in generator/bindings.scm, for example, gluing together the result of function applications with string literals. You're better off "printing" values into a format string; it's easier to understand (though Google "quasistring" for an even nicer alternative). But this isn't really an option for you in Scheme 48. Any decent implementation of string-append does it in O(n) time.

Reply

Re: 80-columns morethanreal March 30 2006, 05:06:14 UTC
But format is an option in scsh!

I don't understand why printing values into a format string makes code more legible than using string-append to glue a bunch of string together. For example if we do what we're trying to do in generator/bindings.scm with a format string we'll have a huge string that's difficult to linebreak.

Reply

Re: 80-columns anonymous March 30 2006, 11:33:13 UTC
Sorry, I didn't know that format was available in scsh; I didn't find it in my quick troll through the documentation. I guess I was looking at the list of SRFI's available in s48 0.53.

I was referring to the first use of string-append in generator/bindings.scm:

(string-append tab "ld" tab "[%sp+" (number->string offset) "],"
"%o" (number->string reg-num) "\n")

which I think is a bit more readable as

(format "\tld\t[%sp+~a],%o~a\n" offset reg-num)

though admittedly those tab characters make it ugly. Anyway, the point is that printing into a string is often cleaner; it doesn't break up the fixed portion as much.

Your big use of string-append in bind-c-func keeps repeating tab and newline chars. I'd write either a function or macro to produce a formatted line or sequence of lines of assembler, and just supply the parts that differ. Something like (asm-str "mov" "%fp, %sp").

Reply

Re: 80-columns caffeinemonkey March 30 2006, 02:41:40 UTC
Oh I know about the stack trace ("preview") but without line numbers it's still not great. We have some rather *ahem* large functions...

Reply

Re: 80-columns musicdieu March 30 2006, 00:06:09 UTC
Down with IDEs!

vim is the only IDE you need. An IDE is slow, doesn't work well over remote X connections, and has you depending on the IDE creators to add extensions for things like Subversion; if they don't, you constantly flip back and forth between the console and IDE to get anything done, and are non-productive.

Reply

Re: 80-columns anonymous March 30 2006, 00:30:42 UTC
vim does most of what you want, but it can't handle the graphical stuff. Why would you ever use an IDE over a remote X connection? Nothing works well over a remote X connection (twitch the mouse and a packet gets sent). You'd run it locally. And DrScheme is not your typical butt-ugly Linux IDE. If you run a test suite, it colours in red any line of code left unexercised. If you mouse over an identifier, it draws an arrow to the binding occurrence; if you mouse over a parameter, it draws arrows to all bound occurrences. (Who cares in Java or C++, where you can't nest functions except by cheating using inner classes, but it's important in Scheme.) There are menu items to hit any define'd variable, to navigate using the program profile, Help Desk windows to surf the documentation... and Emacs keybindings by default.

Reply

Re: 80-columns wlach March 30 2006, 02:01:04 UTC
On the topic of IDEs (especially lisp IDEs), SLIME sounds interesting. See this description: http://www.advogato.org/person/wingo/diary.html?start=148

My main problem with standard IDEs (Visual Studio, Eclipse) is their emphasis on mouse usage and dialogs. But that doesn't necessarily need to be the case:
I barely, if ever, touch the mouse (or switch Windows) when I'm using XEmacs, which incidentally, has the ultimate in trivial extensibility if you don't feel like writing elisp: M-x shell. :)

Reply

Re: 80-columns caffeinemonkey March 30 2006, 02:59:12 UTC
I generally agree, though having good debugger integration in your editor can be really helpful. I did some super-massive hideous hacks at Google to get jdb to tell vim to go the current line during debugging, and it worked ok-ish but the hackiness was pretty awful. Still, I found it really useful at times, and compared to running eclipse (which eats RAM like nobody's business and rarely, if ever, manages to actually resolve symbols properly) it was pretty good.

My main issue with vim is that the syntax-highlighting is braindead. It's always confused about matching parens in scheme and it's really annoying, and there's apparently no easy way to fix it since the syntax-highlighting is really just a hacky set of regexes that get run, and they don't get run on off-screen areas of the buffer, so if a matching paren goes off-screen, vim doesn't know about it anymore. Grr! Though I suppose that encourages not writing functions > 1 screen in length...

Reply

Re: 80-columns andukar March 30 2006, 03:31:55 UTC
Theoretically vim7 has much better syntax-highlighting.

Reply


Leave a comment

Up