Hacker News new | ask | show | jobs
by siqu 2619 days ago
(define (get) (get1 (quote ()) (read)))(define (get1 b r) (if (eof-object? r) (reverse b) (get1 (cons r b) (read))))(define (put) (put1 (quote ()) (read-char)))(define (put1 b r) (if (eof-object? r) (list->string (reverse b)) (if (char=? #\) r) (put1 (cons #\. (cons #\. (cons r b))) (read-char)) (put1 (cons r b) (read-char)))))(define (final lis) (if (null? lis) lis (final1 (car lis) (cdr lis))))(define (final1 f r) (if (and (pair? f) (pair? (cdr f)) (eqv? (quote quote) (car f))) (cons f (final (cdr r))) (if (list? f) (final2 f (gather (count (car r)) (cons (quote ()) (final (cdr r))))) (if (or (vector? f) (pair? f) (eq? #\) f)) (cons f (final (cdr r))) (cons f (final r))))))(define (final2 f g) (cons (append (final f) (car g)) (cdr g)))(define (count s) (- (string-length (symbol->string s)) 2))(define (gather c lis) (if (= c 0) (cons (reverse (car lis)) (cdr lis)) (gather (- c 1) (cons (cons (cadr lis) (car lis)) (cddr lis)))))(for-each write (final (with-input-from-string (put) get)))
3 comments

I'm bored and we are on a site called hacker news; anyone want to join me on de-obfuscating this code?
It's a self-hosting compiler written in Scheme:

https://ideone.com/dvhScJ

Here's the best I could do at formatting and reading/commenting it, so far:

    ;; Usage: (get)
    ;; Parse the entirety of stdin as a sequence of s-expressions.  Return
    ;; that list of expressions.
    ;;
    ;; The definition of this function is a little contrived, because in
    ;; lisp it's simpler to prepend to a list than to append to it, which
    ;; would be the natural thing to do.  So instead, we build the list
    ;; such that it is backwards, then we call (reverse LIST) on it when
    ;; we read EOF.
    (define (get)
      (get1 (quote ()) (read)))
    ;; Usage: (get CURRENT-BUFFER JUSt-READ-EXPRESSION)
    (define (get1 b r)
      (if (eof-object? r)
          (reverse b)
        (get1 (cons r b) (read))))
    
    ;; Usage: (put)
    ;;
    ;; Read the entirety of stdin as a sequence of characters, perform the
    ;; global string substitution ")" -> ")..", then return the result as
    ;; a string.
    ;;
    ;; The definition of this function is a little contrived, because in
    ;; lisp it's simpler to prepend to a list than to append to it, which
    ;; would be the natural thing to do.  So instead, we build the list
    ;; such that it is backwards, then we call (reverse LIST) on it when
    ;; we read EOF.
    (define (put)
      (put1 (quote ()) (read-char)))
    ;; Usage: (put CURRENT-BUFFER LAST-READ-CHAR)
    (define (put1 b r)
      (if (eof-object? r)
          (list->string (reverse b))
        (if (char=? #\) r)
            (put1 (cons #\. (cons #\. (cons r b))) (read-char))
          (put1 (cons r b) (read-char)))))
    
    ;; Usage: (final LIST)
    ;; ???
    (define (final lis)
      (if (null? lis)
          lis
        (final1 (car lis) (cdr lis))))
    ;; Usage: (final1 FIRST REST)
    (define (final1 f r)
      (if (and (pair? f) (pair? (cdr f)) (eqv? (quote quote) (car f)))
          (cons f (final (cdr r)))
        (if (list? f)
            (final2 f (gather (count (car r)) (cons (quote ()) (final (cdr r)))))
          (if (or (vector? f) (pair? f) (eq? #\) f))
              (cons f (final (cdr r)))
            (cons f (final r))))))
    ;; Usage: (final2 f g)
    (define (final2 f g)
      (cons (append (final f) (car g)) (cdr g)))
    ;; Usage: (count SYMBOL)
    (define (count s)
      (- (string-length (symbol->string s)) 2))
    ;; Usage: (gather c lis) 
    (define (gather c lis)
      (if (= c 0)
          (cons (reverse (car lis)) (cdr lis))
        (gather (- c 1) (cons (cons (cadr lis) (car lis)) (cddr lis)))))
    
    ;; 1. Use (put) to read stdin as a string and perform the ")" -> ").."
    ;;    global string replacement.
    ;; 2. Feed that resulting string to (get), which parses it in to a
    ;;    sequence of s-expressions.
    ;; 3. Feed that list of expressions to (final), which does something
    ;;    to it and returns a list.
    ;; 4. (write) each item that (final) returns.
    (for-each write (final (with-input-from-string (put) get)))
It looks like you cracked open the lobster. I tried compiling this:

(+ 6 (*).... 2 3 2 3)

Nice try bot, whatever it is you were trying to do
It's more likely to be a human testing something than a bot. TBF, this comment and the replies were more interesting than any of the on topic comments or the article itself.
You have to at least give credit that the bot was apparently written in lisp.
Scheme, actually, since it uses "define" instead of "defun." I formatted it reasonably, but it still looked like a pain to untangle.
The original Lisp also used DEFINE. See the LISP 1 Programmer's Manual, Mac Carthy et al. 1960, P. 23.
Scheme is a lisp.
Seems legit.