Stlisp

From Esolang
Jump to: navigation, search

Stlisp is a stack-based esoteric programming language with a syntax that looks just like lisp.

Stlisp was created in February 2010 by User:Feuermonster while learning Haskell and Lisp.

Stlisp allows prefix, postfix and infix notation. It's a stack-basend language.

Stlisp is turing complete since you can write a Brainfuckinterpreter in it.

(load "modules/mod_io.so")

;Global stuff there

(let ">+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.>>>++++++++[<++++>-]<.>>>++++++++++[<+++++++++>-]<---.<<<<.+++.------.--------.>>+." "code-str")
;(let "+" "code-str")
(let (str-to-ls (code-str)) "code-ls")
(let 0 "i")
(let (fill 32000 0) "tape")
(let 16000 "ptr")

(def "bf-inc" '(`
 (let (set-element (add 1 (get-element (ptr) (tape))) (ptr) (tape)) "tape")
 ;(print (get-element (ptr) (tape)))
 )
)

(def "bf-dec" '(`
 (let (set-element (sub (get-element (ptr) (tape)) 1) (ptr) (tape)) "tape")
 )
)

(def "bf-mvl" '(let (sub (ptr) 1) "ptr"))

(def "bf-mvr" '(let (add (ptr) 1) "ptr"))

(def "bf-lft" '(`
 (let (add 1 (i)) "i")
 ;(print (get-element (ptr) (tape)))
 (eq (get-element (ptr) (tape)) 0 '(`
  (eq (get-element (i) (code-ls)) "]" '(let (add 1 (i)) "i") '(bf-lft))
 ) '(`))
 )
)

(let 0 "level")

(def "bf-rgt" '(`
 (let (sub (i) 1) "i")
 ;(print "bf-rgt")
 ;(print (i))
 ;(print (get-element (i) (code-ls)))
 ;(print (level))
 ;(print "----")
 (eq (get-element (i) (code-ls)) "]" '(let (add 1 (level)) "level") '(`))
 (eq 0 (level)
  '(`
     (eq (get-element (i) (code-ls)) "[" '(`) '(bf-rgt))
   )
  '(`
     (eq (get-element (i) (code-ls)) "[" '(let (sub (level) 1) "level") '(`))
     (bf-rgt)
   )
 )
 )
)

(def "bf-out" '(IO:put-char (get-element (ptr) (tape))))

(def "bf-interp" '(`
 ;(print (get-element (ptr) (tape)))
 (let (get-element (i) (code-ls)) "cur")
 ;(IO:put-str (IO (cur)))
 (eq "+" (cur) '(` (bf-inc) (let (add 1 (i)) "i")) '(`))
 (eq "-" (cur) '(` (bf-dec) (let (add 1 (i)) "i")) '(`))
 (eq "." (cur) '(` (bf-out) (let (add 1 (i)) "i")) '(`))
 (eq "[" (cur) '(bf-lft) '(`))
 (eq "]" (cur) '(bf-rgt) '(`))
 (eq ">" (cur) '(` (bf-mvl) (let (add 1 (i)) "i")) '(`))
 (eq "<" (cur) '(` (bf-mvr) (let (add 1 (i)) "i")) '(`))
 (lt (i) (count (code-ls)) '(bf-interp) '(`))
 )
)

(bf-interp)

Note: To execute the "Hello World!" the interpreter has to execute 35'000 functions which takes about 0.3s. An execution log can be found here: http://mroman.ch/cgi/slisp/log.txt

Hello, World!

(print "Hello, World!")

or (and that's because it's stack-based and esoteric)

("Hello, World!" print)
"Hello, World!" print ()
print ("Hello, World!")
print "Hello, World!" ()
"Hello, World" (print)

Notice that print ("Hello, World!") is also valid. Which means that instead of e.g. (print (add 5 5)) it's also valid to write print(add(5 5)). You actually can code just like in any other language, you just MUST NOT use ','.

More Examples

MySQL

#!/usr/bin/stlisp
(load "/usr/lib/stlisp/mod_io.so")
(load "/usr/lib/stlisp/mod_cgi.so")
(load "/usr/lib/stlisp/mod_mysql.so")
(IO:put-str-crlf (IO "Content-Type: text/html"))
(IO:put-str-crlf (IO ""))
(let (MYSQL:connect "dbPlog" "localhost" "test" "somepass") "con")
(let (MYSQL:query "SELECT * FROM plog_test" (con)) "result")
(def "output-result" '(` (print "ID:")(print (first))(print "<br>")))
(reduce "output-result" (result))
(print "<br><i>Source:</i><br>")
(IO:put-str (IO "<plaintext>"))
(IO:put-str-crlf (IO:read-all-text (IO "mysql.slisp")))

CGI/HTML

#!/usr/bin/stlisp
(load "/usr/lib/stlisp/mod_io.so")
(load "/usr/lib/stlisp/mod_cgi.so")


(IO:put-str-crlf (IO "Content-Type: text/html"))
(IO:put-str-crlf (IO ""))

(let (str-split (extract (IO:read-all-text (IO "images.txt"))) (str-lf)) "images-lines")

(def "proc-image"
 '(`
    (let "proc-image-arg")
    (ne (proc-image-arg) "" '(`
     (let (str-split (proc-image-arg) ";") "proc-image-values")
     (let (first (proc-image-values)) "proc-image-name")
     (let (first (reverse (proc-image-values))) "proc-image-desc")
     (print "<h1>")
     (print (proc-image-desc))
     (print "</h1>")
     (print (str-cnat (str-cnat (str-cnat (str-cnat "<img src=" (str-qt)) (proc-image-name)) (str-qt)) ">"))
     (print "<br>")
     )
     '(`)
    )
  )
)

(reduce "proc-image" (images-lines))

(print "<br><i>Source:</i><br>")
(IO:put-str (IO "<plaintext>"))
(IO:put-str-crlf (IO:read-all-text (IO "images.slisp")))
#!/usr/bin/stlisp
(load "/usr/lib/stlisp/mod_io.so")
(load "/usr/lib/stlisp/mod_cgi.so")
(IO:put-str-crlf (IO "Content-Type: text/html"))
(IO:put-str-crlf (IO ""))
(print "<b>Hello, World!</b>")
(print "You are visitor number:")
(srand (time))
(print (rand 1337))
(print "<br>")
(let (CGI:get-element "name") "name")
(let (CGI:get-environment) "env")
(print "Your name is:")(print (name))
(print "<br>")
(print "And your IP is:")
(print (CGI:get-remote-addr (env)))
(print "<br>Version:")(print (info-version))
(print "<br>Modules:")(print (info-modules))
(print "<br><i>Source:</i><br>")
(IO:put-str (IO "<plaintext>"))
(IO:put-str-crlf (IO:read-all-text (IO "cgitest.slisp")))