关于rust:又一个lisp-解释器-rustlisp

40次阅读

共计 2600 个字符,预计需要花费 7 分钟才能阅读完成。

2 个月前用 java 实现了一个 lisp 解释器,这个月用 rust 又写了一个,对于为什么选用 rust 起因有三,第一个能够间接编译成指标机器的可执行文件,第二个无 GC,第三个文件小运行速度快占用内存少。

比照 java 版

那么相比于之前的 java 版咱们实现了哪些性能呢?

能够分为三块
第一块反对 java 版 95% 以上的办法(有些办法名做了调整)
第二块 io 已反对 file io, socket(tcp) io, console io
第三块反对多线程 thread lock barrier channel

办法 (过程 procedure) 清单

让咱们看一下已反对的办法具体清单

Implemented procedure (method)

  • [x] number procedure (+ – * / < <= > >= = number? number=? number->string number->char)
  • [x] boolean operation (and or not)
  • [x] byte byte-vector->string
  • [x] char procedure (char? char=? char->number)
  • [x] symbol procedure (symbol? symbol=? symbol->string)
  • [x] string procedure (string? string=? mark-string string-length string-ref string-set! substring string-append string-copy string-find string-trim string-replace string->list …)
  • [x] cons procedure (cons car cdr set-car! set-cdr!)
  • [x] list procedure (list list? list=? list-ref list-tail list-set! list_length append reverse list->vector list->string map for-each filter reduce)
  • [x] vector procedure (vector? vector=? make-vector vector vector-length vector-ref vector-set! vector-fill! vector->list)
  • [x] dict procedure (dict? dict=? make-dict dict dict-length dict-get dict-rm! dict-put! dict-clear!dict-contains-key? dict-keys->list dict-value->list dict->list)
  • [x] quote ‘
  • [x] procedure? method?
  • [x] nil?
  • [x] type procedure (get-type-name is-type?)
  • [x] define
  • [x] let + let*
  • [x] set
  • [x] lambda support closure
  • [x] branch (if do while)
  • [x] apply
  • [x] define-macro support ` , ,@
  • [x] display + newline
  • [x] load support ; #
  • [x] eval
  • [x] lazy evaluation (delay promise? force)
  • [x] io (file io | socket(net) io | console io) support (display newline call-with-tcp-listener call-with-input-file call-with-output-file open-input-file open-output-file input-port? output-port? port? read-char read-line read-string read-u8 read-byte-vector write-char write-string write-byte-vector write-u8)
  • [x] concurrency (async await | thread) support (thread-run sleep make-lock lock-exp make-barrier barrier-wait make-channel channel-send:(-> chan x) channel-done channel-recv:(<- chan) channel-for-each :(<-for-each fn chan) channel-map:(<– fn chan)
  • [] more can be implemented through macros

如何应用

本机打包

cargo build --release
cd ./target/release

run

./rust-lisp run ./aa.lisp 

cmd

./rust-lisp cmd 

or

./rust-lisp 

示例

  • easy-demo

    (+ 1 2)

方言

  • call-with-tcp-listener

    ((define f (call-with-input-file "./demo.html" read-string))
      (call-with-tcp-listener "127.0.0.1:8088" ( lambda (in) ((display  (byte-vector->string (read-byte-vector in 4096)))
         (string-append "HTTP/1.1 200 OK\r\n\r\n" f)
      ))))
  • thread + channel

    ((define chan (make-channel))
      (define barrier (make-barrier 3))
      (define p (lambda (x) ((define i 0)
          (while (< i 10) ((display x i)
              (newline)
              (set! i (+ i 1))
          ))
          (barrier-wait barrier)
          (-> chan x)
          (channel-done chan)
      )))
      (thread-run p "你好, 世界 1!")
      (thread-run p "你好, 世界 2!")
      (thread-run p "你好, 世界 3!")
      (channel-for-each (lambda (x) ((display x)
          (newline)
      )) chan))

    源码地址

    sunxyz/rust-lisp
    liunx 零碎能够间接下载体验

正文完
 0