Recent
:: Anonimo
7 hrs ago
:: RetroFOX
8 hrs ago
:: anonymous
8 hrs ago
:: RetroFOX
8 hrs ago
:: This post is secret Bystroushaak
11 hrs ago
:: Bystroushaak
11 hrs ago
:: Bystroushaak
11 hrs ago
:: Bystroushaak
11 hrs ago
:: anonymous
11 hrs ago
:: Bystroushaak
11 hrs ago
:: anonymous
12 hrs ago
:: This post is secret Bystroushaak
13 hrs ago
:: RIFAS
14 hrs ago
:: buy clomid onli
16 hrs ago
:: RIFAS
16 hrs ago
rss 2.0 feed

Make New Post
Posts: 19135

Syntax:       Wrapping:  

   #15367 Posted by david 2009-07-02 08:49:52
This post has description awaillable
Formated by GeSHi
  1. ;;#!/usr/bin/clisp --ansi
  2. ;;
  3. ;; lang.lisp -- Un interprete simple para el lenguaje de ~~
  4. ;;
  5.  
  6. (defvar *syntax* (copy-readtable *readtable*))
  7. (defvar *stack* nil)
  8. (defvar *stack-aux* nil)
  9. (defvar *dictionary* (make-hash-table))
  10.  
  11.  
  12. (defmacro define-builtin (name &body code)
  13. `(define-symbol-macro ,name (progn ,@code nil)))
  14.  
  15. (defmacro with-syntax (() &body code)
  16. `(let ((*readtable* *syntax*)
  17. (*read-eval* nil))
  18. ,@code))
  19.  
  20. (defun autoinsert (form)
  21. (typecase form
  22. ((or integer string function)
  23. `(push ,form *stack*))
  24. (t
  25. form)))
  26.  
  27. (defun compile-procedure (stream ch)
  28. (declare (ignore ch))
  29. (with-syntax ()
  30. `(lambda ()
  31. ,@(mapcar #'autoinsert (read-delimited-list #\} stream )))))
  32.  
  33.  
  34. (defun compile-definition (stream ch)
  35. (declare (ignore ch))
  36. (with-syntax ()
  37. (let* ((name (read stream))
  38. (body (read stream)))
  39. `(define-builtin ,name (funcall #',body)))))
  40.  
  41. (set-macro-character #\: #'compile-definition nil *syntax*)
  42. (set-macro-character #\{ #'compile-procedure nil *syntax*)
  43. (set-macro-character #\} (get-macro-character #\)) nil *syntax*)
  44.  
  45.  
  46. ;;; Primitives
  47.  
  48. (define-builtin drop
  49. (pop *stack*))
  50.  
  51. (define-builtin dup
  52. (push (car *stack*) *stack*))
  53.  
  54. (define-builtin >>
  55. (push (pop *stack*) *stack-aux*))
  56.  
  57. (define-builtin <<
  58. (push (pop *stack-aux*) *stack*))
  59.  
  60. (define-builtin ifelse
  61. (let* ((condi (third *stack*))
  62. (true (second *stack*))
  63. (false (first *stack*)))
  64.  
  65. drop drop drop
  66.  
  67. (if (zerop condi)
  68. (funcall false)
  69. (funcall true))))
  70.  
  71. (define-builtin display
  72. (format t "~a~%" (pop *stack*)))
  73.  
  74.  
  75. (define-builtin add
  76. (push (+ (pop *stack*)
  77. (pop *stack*))
  78. *stack*))
  79.  
  80. (define-builtin sub
  81. (push (- (- (pop *stack*)
  82. (pop *stack*)))
  83. *stack*))
  84.  
  85. (define-builtin mul
  86. (push (* (pop *stack*)
  87. (pop *stack*))
  88. *stack*))
  89.  
  90. (define-builtin div
  91. (push (/ (/ (pop *stack*)
  92. (pop *stack*)))
  93. *stack*))
  94.  
  95. (define-builtin bye
  96. #+clisp (ext:quit)
  97. #+sbcl (sb-ext:quit))
  98.  
  99. (define-builtin exit bye)
  100.  
  101.  
  102. (defun prompt ()
  103. (fresh-line)
  104. (format t "> ")
  105. (with-syntax ()
  106. (read)))
  107.  
  108.  
  109. (defun repl ()
  110. (let ((*stack* nil)
  111. (*stack-aux* nil))
  112. (loop for tok = (prompt) do
  113. (let ((result (eval tok)))
  114. (typecase result
  115. ((or integer string function)
  116. (eval (autoinsert result)))))
  117. (format t "[~{~a~^, ~}]~%" (reverse *stack*)))))
  118.  
  119.  
  120. ;; startup
  121. (format t "; ~~ language implementation (020709) ~%")
  122. (repl)
  123.  
Parsed in 0.06136894 seconds
::  Inline view Inline view ::  Email this post Email  ::  Print Print   

:: Download   Download Text File15367.txt   Download Gziped text File15367.txt.gz   Download HTML File15367.html   Download PDF File15367.pdf
:: Print into    Print into HTML FileHTML document   Print into PDF FilePDF document

:: Make Diff

:: Erase Post

* Code:

To highlight particular lines, prefix each line with @@


Description:


Secret key (for later deletion)
Syntax:     


comments (0)


Copyright © 2006 Openpastebin