mirror of
https://github.com/janet-lang/janet
synced 2025-12-06 00:28:08 +00:00
More work on adding c functions. Added buffer literals again.
This commit is contained in:
67
test/boot.dst
Normal file
67
test/boot.dst
Normal file
@@ -0,0 +1,67 @@
|
||||
|
||||
# This file is executed without any macro expansion (macros are not
|
||||
# yet defined). Cannot use macros or anything outside the stl.
|
||||
|
||||
(var macros @{})
|
||||
|
||||
# Helper for macro expansion
|
||||
(def macroexpand (fn recur [x]
|
||||
(def x (ast-unwrap x))
|
||||
(if (= (type x) :tuple)
|
||||
(if (> (length x) 0)
|
||||
(do
|
||||
(def first (get x 0))
|
||||
(def rest (array-slice x 1))
|
||||
(def macro (get macros first))
|
||||
(if macro (recur (apply macro rest)) x))
|
||||
x)
|
||||
x)))
|
||||
|
||||
# Function to create macros
|
||||
(def _defmacro (fn [name f]
|
||||
(set macros name f)
|
||||
f))
|
||||
|
||||
# Make defn
|
||||
(_defmacro "defn" (fn [name &]
|
||||
(tuple 'def name (apply tuple 'fn &))))
|
||||
|
||||
# Make defmacro
|
||||
(_defmacro "defmacro" (fn [name &]
|
||||
(tuple global-macro (string name) (apply tuple 'fn &))))
|
||||
|
||||
# Comment returns nil
|
||||
(_defmacro "comment" (fn [] nil))
|
||||
|
||||
# The source file to read from
|
||||
(var *sourcefile* stdin)
|
||||
|
||||
# The *read* macro gets the next form from the source file, and
|
||||
# returns it. It is a var and therefor can be overwritten.
|
||||
(var *read* (fn []
|
||||
(def b (buffer))
|
||||
(def p (parser))
|
||||
(while (not (parse-hasvalue p))
|
||||
(read *sourcefile* 1 b)
|
||||
(if (= (length b) 0)
|
||||
(error "parse error: unexpected end of source"))
|
||||
(parse-charseq p b)
|
||||
(if (= (parse-status p) :error)
|
||||
(error (string "parse error: " (parse-consume p))))
|
||||
(clear b))
|
||||
(parse-consume p)))
|
||||
|
||||
# Evaluates a form by macro-expanding it, compiling it, and
|
||||
# then executing it.
|
||||
(def eval (fn [x]
|
||||
(def func (compile (macroexpand x)))
|
||||
(if (= :function (type func))
|
||||
(func)
|
||||
(error (string "compiler error: " func)))))
|
||||
|
||||
# A simple repl for testing.
|
||||
(while true
|
||||
(def t (thread (fn []
|
||||
(while true
|
||||
(print (eval (*read*)))))))
|
||||
(print (tran t)))
|
||||
Reference in New Issue
Block a user