Skip to content

Commit

Permalink
Bring verbosity improvements over from gen-code branch
Browse files Browse the repository at this point in the history
  • Loading branch information
jcburley committed Feb 1, 2020
1 parent d0def9f commit e1fa75c
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 39 deletions.
44 changes: 37 additions & 7 deletions core/data/core.joke
Original file line number Diff line number Diff line change
Expand Up @@ -3222,11 +3222,19 @@
(when-not (bound? v#)
(def ~name ~expr))))

(defonce ^:dynamic
^{:private true
:doc "True while a verbose load is pending"}
*loading-verbosely* false)

(defn in-ns
"Sets *ns* to the namespace named by the symbol, creating it if needed."
{:added "1.0"}
^Namespace [^Symbol name]
(var-set #'joker.core/*ns* (joker.core/create-ns name)))
(let [new-ns (joker.core/create-ns name)]
(when *loading-verbosely*
(printf "joker.core/in-ns: changing from %s to %s\n" joker.core/*ns* new-ns) (flush))
(var-set #'joker.core/*ns* new-ns)))

(defonce ^:dynamic
^{:private true
Expand All @@ -3246,11 +3254,6 @@
:doc "A stack of paths currently being loaded"}
*pending-paths* ())

(defonce ^:dynamic
^{:private true
:doc "True while a verbose load is pending"}
*loading-verbosely* false)

(defonce ^:dynamic
^{:private true
:doc "A vector of mappings of namespaces to root files
Expand Down Expand Up @@ -3546,7 +3549,7 @@
(doseq [^Symbol lib libs]
(let [^String path (lib-path__ lib)]
(when *loading-verbosely*
(printf "(joker.core/load \"%s\")\n" path))
(printf "(joker.core/load %s from \"%s\")\n" lib path))
(check-cyclic-dependency path lib)
(when-not (= path (first *pending-paths*))
(binding [*pending-paths* (conj *pending-paths* path)
Expand Down Expand Up @@ -3851,6 +3854,33 @@
Defaults to true"
{:added "1.0"})

(add-doc-and-meta *verbose*
"Whether --verbose (or equivalent) was specified by the Joker invocation.

Note: changing this (via e.g. var-set) affects only running Joker code; it
does not change the value used by the Go code that implements Joker. If
needed, it's probably better to add a set-verbose function that changes
both, rather than changing Joker's internals so this variable is always checked."
{:added "1.0"})

(add-doc-and-meta *verbosity-level*
"Verbosity level as (initially) specified by the Joker invocation.

Note: changing this (via e.g. var-set) affects only running Joker code; it
does not change the value used by the Go code that implements Joker. If
needed, it's probably better to add a set-verbosity-level function that
changes both, rather than changing Joker's internals so this variable is
always checked."
{:added "1.0"})

(defn- verbosity
"Verbosity level in effect; 0 if verbosity disabled."
{:added "1.0"}
[]
(if *verbose*
*verbosity-level*
0))

(defmacro letfn
"fnspec ==> (fname [params*] exprs) or (fname ([params*] exprs)+)

Expand Down
47 changes: 30 additions & 17 deletions core/environment.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package core

import (
"fmt"
"io"
"os"
"path/filepath"
Expand All @@ -10,10 +9,11 @@ import (
)

var (
Stdin io.Reader = os.Stdin
Stdout io.Writer = os.Stdout
Stderr io.Writer = os.Stderr
Verbose = false
Stdin io.Reader = os.Stdin
Stdout io.Writer = os.Stdout
Stderr io.Writer = os.Stderr
Verbose = false
VerbosityLevel = 0
)

type (
Expand All @@ -28,6 +28,8 @@ type (
MainFile *Var
args *Var
classPath *Var
verbose *Var
verbosity *Var
ns *Var
NS_VAR *Var
IN_NS_VAR *Var
Expand Down Expand Up @@ -72,6 +74,19 @@ func (env *Env) SetClassPath(cp string) {
env.classPath.Value = cpVec
}

func (env *Env) SetVerbosity() {
env.verbose.Value = Boolean{B: Verbose}
env.verbosity.Value = Int{I: VerbosityLevel}
}

func Verbosity() int {
if Verbose {
return VerbosityLevel
}
return 0
}

/* Called by parse.go in an outer var block, this runs before func main(). */
func NewEnv(currentNs Symbol, stdin io.Reader, stdout io.Writer, stderr io.Writer) *Env {
features := EmptySet()
features.Add(MakeKeyword("default"))
Expand Down Expand Up @@ -109,6 +124,12 @@ func NewEnv(currentNs Symbol, stdin io.Reader, stdout io.Writer, stderr io.Write
MakeMeta(nil, "true if Joker is running in linter mode", "1.0"))
res.CoreNamespace.InternVar("*linter-config*", EmptyArrayMap(),
MakeMeta(nil, "Map of configuration key/value pairs for linter mode", "1.0"))
res.verbose = res.CoreNamespace.Intern(MakeSymbol("*verbose*"))
res.verbose.Value = Boolean{B: false}
res.verbose.isPrivate = true
res.verbosity = res.CoreNamespace.Intern(MakeSymbol("*verbosity-level*"))
res.verbosity.Value = Int{I: 0}
res.verbosity.isPrivate = true
return res
}

Expand Down Expand Up @@ -154,12 +175,8 @@ func (env *Env) NamespaceFor(ns *Namespace, s Symbol) *Namespace {
res = env.Namespaces[s.ns]
}
}
if res != nil && res.Lazy != nil {
res.Lazy()
if Verbose {
fmt.Fprintf(Stderr, "NamespaceFor: Lazily initialized %s\n", *res.Name.name)
}
res.Lazy = nil
if res != nil {
res.MaybeLazy("NamespaceFor")
}
return res
}
Expand Down Expand Up @@ -190,12 +207,8 @@ func (env *Env) FindNamespace(s Symbol) *Namespace {
return nil
}
ns := env.Namespaces[s.name]
if ns != nil && ns.Lazy != nil {
ns.Lazy()
if Verbose {
fmt.Fprintf(Stderr, "FindNameSpace: Lazily initialized %s\n", *ns.Name.name)
}
ns.Lazy = nil
if ns != nil {
ns.MaybeLazy("FindNameSpace")
}
return ns
}
Expand Down
10 changes: 10 additions & 0 deletions core/ns.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ func (ns *Namespace) Hash() uint32 {
return ns.hash
}

func (ns *Namespace) MaybeLazy(doc string) {
if ns.Lazy != nil {
ns.Lazy()
if Verbosity() > 0 {
fmt.Fprintf(Stderr, "NamespaceFor: Lazily initialized %s for %s\n", *ns.Name.name, doc)
}
ns.Lazy = nil
}
}

const nsHashMask uint32 = 0x90569f6f

func NewNamespace(sym Symbol) *Namespace {
Expand Down
49 changes: 34 additions & 15 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,16 @@ func notOption(arg string) bool {
}

func parseArgs(args []string) {
if len(args) > 1 {
// peek to see if the first arg is "--debug*"
switch args[1] {
case "--debug", "--debug=stderr":
debugOut = Stderr
case "--debug=stdout":
debugOut = Stdout
}
}

length := len(args)
stop := false
missing := false
Expand Down Expand Up @@ -469,6 +479,22 @@ func parseArgs(args []string) {
debugOut = Stdout
case "--verbose":
Verbose = true
if i < length-1 && notOption(args[i+1]) {
i += 1 // shift
verbosity, err := strconv.ParseInt(args[i], 10, 64)
if err != nil {
fmt.Fprintln(Stderr, "Error: ", err)
return
}
if verbosity <= 0 {
Verbose = false
VerbosityLevel = 0
} else {
VerbosityLevel = int(verbosity)
}
} else {
VerbosityLevel++
}
case "--help", "-h":
helpFlag = true
return // don't bother parsing anything else
Expand Down Expand Up @@ -651,31 +677,24 @@ var runningProfile interface {
}

func main() {
RT.GIL.Lock()
InitInternalLibs()
ProcessCoreData()

SetExitJoker(func(code int) {
finish()
os.Exit(code)
})
GLOBAL_ENV.FindNamespace(MakeSymbol("user")).ReferAll(GLOBAL_ENV.CoreNamespace)

if len(os.Args) > 1 {
// peek to see if the first arg is "--debug*"
switch os.Args[1] {
case "--debug", "--debug=stderr":
debugOut = Stderr
case "--debug=stdout":
debugOut = Stdout
}
}
parseArgs(os.Args) // Do this early enough so --verbose can show joker.core being processed.

parseArgs(os.Args)
saveForRepl = saveForRepl && (exitToRepl || errorToRepl) // don't bother saving stuff if no repl

RT.GIL.Lock()
InitInternalLibs()
ProcessCoreData()

GLOBAL_ENV.FindNamespace(MakeSymbol("user")).ReferAll(GLOBAL_ENV.CoreNamespace)

GLOBAL_ENV.SetEnvArgs(remainingArgs)
GLOBAL_ENV.SetClassPath(classPath)
GLOBAL_ENV.SetVerbosity()

if debugOut != nil {
fmt.Fprintf(debugOut, "debugOut=%v\n", debugOut)
Expand Down

0 comments on commit e1fa75c

Please sign in to comment.