1
0
mirror of https://github.com/janet-lang/janet synced 2024-06-26 23:23:16 +00:00

Add all-symbols, easy way to see all symbols in an environment.

This commit is contained in:
Calvin Rose 2018-05-21 22:08:16 -04:00
parent de59c57e48
commit f8bda3af51
3 changed files with 32 additions and 9 deletions

View File

@ -32,13 +32,20 @@ BINDIR=$(PREFIX)/bin
# TODO - when api is finalized, only export public symbols instead of using rdynamic # TODO - when api is finalized, only export public symbols instead of using rdynamic
# which exports all symbols. Saves a few KB in binary. # which exports all symbols. Saves a few KB in binary.
CFLAGS=-std=c99 -Wall -Wextra -Isrc/include -rdynamic -fpic -O2 CFLAGS=-std=c99 -Wall -Wextra -Isrc/include -fpic -O2
CLIBS=-lm -ldl CLIBS=-lm -ldl
PREFIX=/usr/local PREFIX=/usr/local
DST_TARGET=dst DST_TARGET=dst
DST_LIBRARY=libdst.so DST_LIBRARY=libdst.so
DEBUGGER=gdb DEBUGGER=gdb
UNAME:=$(shell uname -s)
ifeq ($(UNAME), Darwin)
# Add other macos/clang flags
else
CFLAGS:=$(CFLAGS) -rdynamic
endif
# Source headers # Source headers
DST_HEADERS=$(sort $(wildcard src/include/dst/*.h)) DST_HEADERS=$(sort $(wildcard src/include/dst/*.h))
DST_LIBHEADERS=$(sort $(wildcard src/include/headerlibs/*.h)) DST_LIBHEADERS=$(sort $(wildcard src/include/headerlibs/*.h))

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2017 Calvin Rose * Copyright (c) 2018 Calvin Rose
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to * of this software and associated documentation files (the "Software"), to

View File

@ -758,7 +758,8 @@ to call on any table. Does not print table prototype information."
(recur x) (recur x)
(buffer.push-string buf "\n") (buffer.push-string buf "\n")
(file.write stdout buf)) (file.write stdout buf)
nil)
### ###
### ###
@ -766,7 +767,7 @@ to call on any table. Does not print table prototype information."
### ###
### ###
(defn macroexpand1 (defn macroexpand-1
"Expand macros in a form, but do not recursively expand macros." "Expand macros in a form, but do not recursively expand macros."
[x] [x]
@ -774,21 +775,21 @@ to call on any table. Does not print table prototype information."
(def len (length a)) (def len (length a))
(def newa @[]) (def newa @[])
(for [i 0 len] (for [i 0 len]
(array.push newa (macroexpand1 (get a i)))) (array.push newa (macroexpand-1 (get a i))))
newa) newa)
(defn dotable [t] (defn dotable [t]
(def newt @{}) (def newt @{})
(var key (next t nil)) (var key (next t nil))
(while (not= nil key) (while (not= nil key)
(put newt (macroexpand1 key) (macroexpand1 (get t key))) (put newt (macroexpand-1 key) (macroexpand-1 (get t key)))
(:= key (next t key))) (:= key (next t key)))
newt) newt)
(defn expandlast [t] (defn expandlast [t]
(def len (length t)) (def len (length t))
(def last (get t (- len 1))) (def last (get t (- len 1)))
(tuple.append (tuple.slice t 0 -2) (macroexpand1 last))) (tuple.append (tuple.slice t 0 -2) (macroexpand-1 last)))
(defn expandall [t] (defn expandall [t]
(def args (doarray (tuple.slice t 1))) (def args (doarray (tuple.slice t 1)))
@ -841,13 +842,13 @@ to call on any table. Does not print table prototype information."
"Expand macros completely." "Expand macros completely."
[x] [x]
(var previous x) (var previous x)
(var current (macroexpand1 x)) (var current (macroexpand-1 x))
(var counter 0) (var counter 0)
(while (not= current previous) (while (not= current previous)
(if (> (++ counter) 200) (if (> (++ counter) 200)
(error "macro expansion too nested")) (error "macro expansion too nested"))
(:= previous current) (:= previous current)
(:= current (macroexpand1 current))) (:= current (macroexpand-1 current)))
current) current)
### ###
@ -1093,3 +1094,18 @@ get a chunk of source code. Should return nil for end of file."
(pp x))) (pp x)))
(default onerr default-error-handler) (default onerr default-error-handler)
(run-context newenv getchunk onvalue onerr)) (run-context newenv getchunk onvalue onerr))
(defn all-symbols
"Get all symbols available in the current environment."
[env]
(default env *env*)
(def envs @[])
(do (var e env) (while e (array.push envs e) (:= e (table.getproto e))))
(array.reverse envs)
(def symbol-set @{})
(defn onenv [envi]
(defn onk [k]
(put symbol-set k true))
(each onk (keys envi)))
(each onenv envs)
(sort (keys symbol-set)))