4 ms·
Inline C/Assembler in Bash
- tehrei 10y agowhile read line; do echo $line>>$cfile done Okay, I know this is a joke and all, but that's the exact wrong way to do it. What you want to do is just cat > $cfile
- ob 10y ago> Has you ever wanted to combine the speed and safety of Bash with the raw, unbridled power of C? Uh, no? :)
- susanthesquark 10y agothis is cool do more
- unsignedqword 10y agoThe insane things people do in Bash sometimes...although I don't think anything can top the x86 Assembler that was written entirely in Bash: http://lists.gnu.org/archive/html/bug-bash/2001-02/msg00054.html http://lists.gnu.org/archive/html/bug-bash/2001-02/msg00054....
- ravenstine 10y agoNeat!
- skynetv2 10y agowhy ....
- foxhill 10y agoto call this an assembler is to not give it credit where it's due - this is not an assembler, it's much more than that. it's a set of functions for bash that when sourced make the assembly a valid bash script which then runs and assembles itself. this is genius.
- gcr 10y agoIt's using the shell's own tokenizer to parse asm! That's glorious! I don't really understand why the code works since I can't read x86 asm yet, but I think how it works is reasonably clear. Each instruction has its own bash function that writes its opcodes to the output file and the asm script to be assembled is (I think) sourced directly into the running shell session.
- projectramo 10y agoThe next step is to write a new Bash in that C, and write a C compiled inside that Bash and so on. (See Godel, Escher, Bach)
- unsignedqword 10y agoGodel, Escher, Bash?
- teddyh 10y agohttps://en.wikipedia.org/wiki/G%C3%B6del,_Escher,_Bach https://en.wikipedia.org/wiki/G%C3%B6del,_Escher,_Bach
- turbohedgehog 10y agoSadly, you missed the pun
- FatalBaboon 10y agoCould he not remove the temp files and use process substitution instead? (looks cleaner in my opinion) i.e: cc -fPIC -o $sofile -shared <(cat <<'EOF' ... EOF )
- tehrei 10y agoThat's some nice useless use of cat you have there ;) . You can just do cc -fPIC -o $sofile -shared -x c - <<'EOF' ... EOF
- radiospiel 10y agoI once built a package to allow redistribution of C/Flex/Go-scripts with just-in-time compilation (https://github.com/radiospiel/jit https://github.com/radiospiel/jit) but mine does not allow to mix the to-be-compiled language into the bash script itself; so count me impressed :)
- gumby 10y agoI used to write CGI in shell scripts. OK, it was the mid 1990s.
- deleted 10y ago[deleted]