David Vázquez


04 January 2013

Lisp to Javascript compiler

Tags:
  • lisp
  • javascript
  • compiler

Raimon Grau and I have been working on a simple Lisp to Javascript compiler, in order to better understand how to bootstrap it. Now we got it. It compiles itself and the resulting code can be used from the browser. It can even call its own internals, which is quite nice.

It is not very helpful, you can try it. Now I have to think if it could be useful for something… maybe a live Lisp tutorial, or make it suitable for Android. Any suggestions?

The repository with the whole history is at:

http://github.com/davazp/ecmalisp/

Comments
13 December 2012

Emphasizing text in ERC

Tags:
  • emacs
  • emacs-lisp
  • erc
  • irc

I have realized ERC supports control characters to emphasize text as bold, underline… However, I prefer *foo*, /bar/ and _foobar_ syntax, just like org-mode and Gnus. So I have added the following code to my .emacs to do the job:

(defun erc-emphasize ()
  (when (eq erc-interpret-controls-p t)
    (goto-char (point-min))
    (while (re-search-forward "\\(?:^\\|\\s-\\)\\(\\*\\([^*]+\\)\\*\\)\\(:?$\\|\\s-\\)" nil t)
      (replace-match "\C-b\\2\C-o" nil nil nil 1))
    (goto-char (point-min))
    (while (re-search-forward "\\(?:^\\|\\s-\\)\\(_\\([^_]+\\)_\\)\\(?:$\\|\\s-\\)" nil t)
      (replace-match "\C-_\\2\C-O" nil nil nil 1))
    (goto-char (point-min))
    (while (re-search-forward "\\(?:^\\|\\s-\\)\\(/\\([^/]+\\)/\\)\\(?:$\\|\\s-\\)" nil t)
      (replace-match "\C-V\\2\C-O" nil nil nil 1))))

(add-hook 'erc-send-pre-hook
          (lambda (string)
            (with-temp-buffer
              (insert string)
              (erc-emphasize)
              (setq str (buffer-substring (point-min) (point-max))))))

(add-hook 'erc-insert-modify-hook 'erc-emphasize)

The code transforms the text before sending it, so it will work without changes in the IRC client of the other person, supposed control characters is supported of course. If you want to send/receive text literally, just disable control characters interpretation with C-c C-c in the ERC buffer.

Comments
08 December 2012

I wrote a Forth implementation for x86

Tags:
  • forth
  • eulex

Yes, I did it, and I have to say that it was one of the most exciting hack experiences that I have had. But let us start in the beginning, four years ago. At that time, a friend and I was writing some toys related to operating systems. He was a C programmer, but knowing that nobody even will try our system, I wanted to have some extra fun! I convinced him to embed a Forth in the kernel. I did not know anything about Forth, but it promised to be interesting. I picked up an old specification of the Forth language and implement it in assembler for x86. It became a retro-software project. However, he moved to other projects and the project stopped.

A couple of years later, I found the code again. I have not any other thing to work in, so I chose to give it a try. I ported my original implementation to 32 bits and I integrated the Forth implementation with the C kernel. Once that I could write programs on it, I replaced C and grew the language with many words. I wrote output routines, a serial driver, keyboard and a video driver, memory management, exceptions, timer… It was so fun that I could not stop there and I wrote a speaker driver, an assembler and disassembler, trace/untrace utilities, a Sokoban game taken from GForth, emacs keybindings and autocompletion in the terminal like in readline, and a simple Lisp interpreter! Did I say how great growing the system from scratch was?

Try Eulex

The first thing you have to do is make sure you have a recent Qemu installation. It has to be enough recent to support multiboot kernels. Just installing the version of your distribution should work. Now, get the source code from the repository

git clone https://github.com/davazp/eulex.git

Move to the directory where it was cloned and type make to compile. In order to launch QEmu with the Eulex image, run the script ./run-eulex.sh. If you pass any argument to this script, they are passed to QEmu. It can be useful to enable sound, for example.

At this point, the system should boot as below. You can type Forth code right there and it will be evaluated. Indeed, you can type TAB key to autocomplete words, or use some emacs-like keybindings, e.g: C-a, C-e, M-b, M-f, C-l../../../img/eulex/1.png

By default, the context provides standard Forth words, but if you want to hack the internals you will need access to the internal vocabulary. Use the word eulex to do that. You can try to type words to list all the words in a context. In the internal vocabulary, there are about a thousand words.

It is possible to disassemble words, e.g: see edit-line. Note that the disassembler uses the current context to look call addresses back into word names. If many addresses are not showing, try moving to the eulex vocabulary.

../../../img/eulex/2.png

There is not persistent storage, after halting QEmu, or rebooting with the command reboot, changes will be lost. Therefore, you could want to write code in the file eulexrc.fs, which is loaded automatically when the system starts.

In closing, I would like to include a couple of nice pictures. The first one is an incomplete screen block editor,

../../../img/eulex/3.png

The second one is a little Lisp interpreter, written in Forth.

../../../img/eulex/4.png

I hope you liked it. If you fancy hacking this and enjoy it as I did, do not hesitate to write to me, I will help you with pleasure. There many minor details, if you would like to try it but no to spend so much time. If you feel adventurous, then the points to work are:

  • Bootstrapping

Rewriting forth.S as a crosscompiler to run on GForth, the 100% of the code would be Forth. The system could compile itself eventually. The assembler I wrote can be used to this task, as it executes both in Eulex as in GForth.

  • Improving the compiler

It is as native as described in this document. Code is very inefficient. We could optimize it, but we do not want if the compiler is written in assembler. So bootstraping should come first.

  • Hard disk driver and FAT implementation.

Actually, files are stored built-in with the same Eulex image, so they are read-only.

  • Dumping and restoring the world

When the floppy driver or a file system implementation is working, dumping the dictionary to the disk would have the nice effect ot saving the whole state of the system.

Comments
01 December 2012

First post

Hello everybody. Here it is, a new github-hosted blog. You can know something about me now, but what will this blog be about? Unsurprisingly, I will tell different things I hack, as the main reason to open this blog is because I do not know many people to talk about this kind of things face-to-face. You will find some Lisp and Emacs-related posts probably, as well as some light math, but I will post anything I find interesting.

The blog is quite simple. I do not provide tags yet, but I have enabled comments on the posts, so you can give some feedback.

Finally, I hope to share some nice things, and somebody likes it. Also, English is not my mother tongue, so writing it requires some extra effort, but I will do my best.

Welcome to my blog.

Comments

Copyright © 2013 David Vázquez
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Creative
  Commons License