7 ms·
Lisp implementation in sed
- mseri 12y agoAhahah. Awesome. There is even an 'awk' version: https://github.com/darius/awklisp https://github.com/darius/awklisp
- abecedarius 12y agoI wrote that, and I gotta say sed is a much crazier target. More sed fun: http://aurelio.net/projects/sedsokoban/ http://aurelio.net/projects/sedsokoban/
- breadbox 12y agoIndeed. There are few wide-spread programming languages around any more that lack such things as, say, any form of arithmetic whatsoever.
- mtdewcmu 12y agosed has regular expressions. I think you could do some binary arithmetic fairly easily with regular expressions and find and replace...
- breadbox 12y agoYep, that's pretty much how you have to do it. (If all you need is increment/decrement, though, you can actually do it pretty concisely with decimal-digit strings. Skipping the binary-to-decimal conversion greatly simplifies displaying numerical values.)
- mtdewcmu 12y agoThe GNU sed manual has examples like that at the end. It always seemed odd to show examples of things that are so awkward and inefficient. If they meant for sed to do those things, presumably they would have made commands for them.
- userbinator 12y ago> to show examples of things that are so awkward and inefficient
- deleted 12y ago[deleted]
- mtdewcmu 12y agoYeah. As in, better done in awk. The opposite of awkward is sedward.
- mzs 12y agoKudos, there was also this one: http://www.cs.cmu.edu/afs/cs/Web/Groups/AI/lang/lisp/impl/awk/0.html http://www.cs.cmu.edu/afs/cs/Web/Groups/AI/lang/lisp/impl/aw... Possibly based on the same newsgroup post?
- abecedarius 12y agoThat one's a cooler hack because it runs in old awk, from before they supported user-defined functions. The whole read-eval-print composition is a straight-line series of loops, with the recursions unrolled into explicit stack manipulation. If you want to see how to write fairly readable code within those restrictions, it's instructive. It even has a cooler name. (I said so when bringing it up in my documentation.) On the other hand, awklisp has some value as a readable Lisp interpreter in a lower-level but still reasonable language, in between the usual Lisp-in-Lisp and Lisp-in-C tutorials.
- mtdewcmu 12y agoThat's kind of funny, because awk is already a programming language.
- bndr 12y agoThis is amazing. Just reading the code makes my head hurt, but you implemented a language with it.
- lengads 12y agoHere, for example, is the implementation of `atom` (I think): /(atom /{ /\[\S\+\]/{ s/.*/[]/ bpop_context } s/.*/t/ bpop_context }
- sejje 12y agoIf this was (the only available) programming, I'd quit. Kudos, I think, to the author. His pain tolerance exceeds mine by a vast amount.
- quotemstr 12y ago> If this was (the only available) programming, I'd quit. We started with nothing, remember? Just the bare metal. If this were the only available programming, I'd write an interpreter for a better language. Never imagine that your current suite of tools is all there is.
- Someone 12y agoIt will be a challenge to make an interpreter for a better language faster than dead slow. I would bet on a compiler for a better language instead. That way, only one person would have to wait ages and ages. I doubt the result would be fast, but it wouldn't be insanely, insanely, insanely slow, either.
- ibrahima 12y agoI would much rather hand assemble machine code than program in sed. I imagine most people have a better understanding of how computer architecture works than how to do arithmetic and logic with sed.
- mtdewcmu 12y ago
- deleted 12y ago[deleted]
- nate_martin 12y agoWould someone mind giving a quick explanation of how this works? I have only used sed to replace text in files and directories. How do people write interpreters and game loops with this unix utility?
- mtdewcmu 12y agoWith difficulty. I started playing with the idea of making a programming language out of sed at one point, but then I decided that, even if I succeeded, it would be a waste of time. I didn't take into account Hacker News.
- jes5199 12y agothe sed language allows conditional jumps to labels. That makes it powerful enough to write programs in. If you had to just write programs by running a series of sed substitution expressions to a file over and over, and not use conditionals... it might still be possible to program in. Much harder to think about, though.
- rmorell 12y agosed is provably Turing complete: http://www.catonmat.net/blog/proof-that-sed-is-turing-complete/ http://www.catonmat.net/blog/proof-that-sed-is-turing-comple... which means that it's effectively able to simulate any other Turing-complete language (provided enough processing power and memory).
- IvyMike 12y agoNow you have three problems.
- krapp 12y agoOh come on. They took an unreadable language and implemented a slightly less unreadable language in it. That's at least two problems and one solution. To what, I don't know.
- catshirt 12y agodid I misinterpret or did you just call lisp "[nearly] unreadable"? all languages are unreadable for the uninitiated.
- krapp 12y agoYou didn't misinterpret.
- catshirt 12y agoi am not particularly gifted at computer science and especially not programming languages. by necessity, the only languages i know are JavaScript and lisp. so I am always surprised to hear lisp is "unreadable" as opposed to "unfamiliar".
- Crito 12y agoI think that Lisp got a reputation for being unreadable because some Lisp programmers have the tendency to get too far into macros to extend the language, resulting in a language that other developers have not seen before.
- mtdewcmu 12y agoMy theory is that it has to do with not having any operators, except parentheses, and using exclusively prefix notation, where other languages allow infix for doing arithmetic. Also, in Scheme loops are usually recursion, which means they have no standard form and they are upside-down from usual (the loop condition and increment are near the bottom, where the recursive call is).
- justinator 12y agoWell, OK: I'm curious, can I use the sed to Perl utility, to then make a Lisp implementation in Perl?
- justinator 12y agoTrying this myself, it returns an error: s2p: expression #1: extra characters after command (d) Thankfully, I've never had the job of porting a sed script to Perl, so I'm not exactly sure what that's all about. The docs mention this error, but don't explain what it means, http://perldoc.perl.org/s2p.html http://perldoc.perl.org/s2p.html
- justinator 12y agoAaaaand, obviously Perl has CPAN, so there's really no need (except to be evil): https://metacpan.org/pod/CljPerl https://metacpan.org/pod/CljPerl (there's a few, actually, because TIMTOWTDI!)
- kateperry 12y agomy neighbor's step-sister brought home $20864 a week ago. she has been making cash on the internet and bought a $519900 home. All she did was get lucky and apply the advice exposed on this link➜➜➜➜➜➜➜ MAX43.COM
- asgard1024 12y agoWouldn't the opposite - stream editor (programmable) in Lisp - be more useful?
- aaronem 12y agoWell, if you count Emacs in batch mode, such a thing already exists.
- bane 12y agoSo I guess if all languages are eventually converging on Lisp, are all Lisps eventually converging on sed? Is sed the bottom turtle or are there more?