Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ doc/repl/glj.wasm
.direnv

# useful to symlink in for context
clojure
/clojure
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ gocmd:
.PHONY: generate
generate:
@go generate ./...
@echo "(map compile '[glojure.core glojure.core.async glojure.walk glojure.template])" | \
GLOJURE_STDLIB_PATH=./pkg/stdlib $(GO_CMD) run ./cmd/glj
@echo "(map compile '[glojure.core glojure.go.io glojure.core.async glojure.walk glojure.template glojure.go.types glojure.uuid])" | \
GLOJURE_USE_AOT=false GLOJURE_STDLIB_PATH=./pkg/stdlib $(GO_CMD) run ./cmd/glj

.PHONY: build
build: $(GLJ)
Expand Down
6 changes: 6 additions & 0 deletions pkg/glj/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
_ "github.com/glojurelang/glojure/pkg/gen/gljimports"
"github.com/glojurelang/glojure/pkg/lang"

// Add NS loaders for the standard library.
_ "github.com/glojurelang/glojure/pkg/stdlib/glojure/core"
_ "github.com/glojurelang/glojure/pkg/stdlib/glojure/core/async"
_ "github.com/glojurelang/glojure/pkg/stdlib/glojure/go/io"
_ "github.com/glojurelang/glojure/pkg/stdlib/glojure/protocols"

"github.com/glojurelang/glojure/pkg/runtime"
)

Expand Down
4 changes: 0 additions & 4 deletions pkg/gljmain/gljmain.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@ import (
// bootstrap the runtime
_ "github.com/glojurelang/glojure/pkg/glj"

_ "github.com/glojurelang/glojure/pkg/stdlib/glojure/core"
_ "github.com/glojurelang/glojure/pkg/stdlib/glojure/core/async"
_ "github.com/glojurelang/glojure/pkg/stdlib/glojure/protocols"

"github.com/glojurelang/glojure/pkg/lang"
"github.com/glojurelang/glojure/pkg/reader"
"github.com/glojurelang/glojure/pkg/repl"
Expand Down
13 changes: 11 additions & 2 deletions pkg/runtime/codegen.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ func (g *Generator) Generate(ns *lang.Namespace) error {
second, _ := lang.Nth(entry, 1)
vr, ok := second.(*lang.Var)
if !ok {
panic(fmt.Sprintf("expected var, got %T", second))
continue // skip non-var mappings
// TODO: handle non-var mappings like direct references to functions or values
// panic(fmt.Sprintf("can't codegen %v: expected var, got %T (%v)", name, second, second))
}

if !(vr.Namespace() == ns && lang.Equals(vr.Symbol(), name)) {
Expand Down Expand Up @@ -402,7 +404,14 @@ func (g *Generator) generateVar(nsVariableName string, name *lang.Symbol, vr *la

// check if the var has a value
if vr.IsBound() {
g.writef("%s = %s.InternWithValue(%s, %s, true)\n", varVar, nsVariableName, varSym, g.generateValue(vr.Get()))
// we call Get() on a new goroutine to ensure we get the root value in the case
// of dynamic vars
valChan := make(chan any)
go func() {
valChan <- vr.Get()
}()
v := <-valChan
g.writef("%s = %s.InternWithValue(%s, %s, true)\n", varVar, nsVariableName, varSym, g.generateValue(v))
} else {
g.writef("%s = %s.Intern(%s)\n", varVar, nsVariableName, varSym)
}
Expand Down
8 changes: 8 additions & 0 deletions pkg/runtime/envinit.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ func NewEnvironment(opts ...EvalOption) lang.Environment {
// Add stdlib
RT.Load("glojure/core")

// Workaround to ensure namespaces that are required by core are loaded.
// TODO: AOT should identify this dependency and generate code to load it.
if useAot {
RT.Load("glojure/core/protocols")
RT.Load("glojure/string")
RT.Load("glojure/go/io")
}

// Set the glojure version
core := lang.FindNamespace(lang.NewSymbol("glojure.core"))
versionVar := core.FindInternedVar(lang.NewSymbol("*glojure-version*"))
Expand Down
21 changes: 5 additions & 16 deletions pkg/runtime/evalast.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ func (env *environment) EvalASTTheVar(n *ast.Node) (interface{}, error) {
return n.Sub.(*ast.TheVarNode).Var, nil
}

// TODO: this is a bit of a mess
// TODO: fix up, as this is a bit of a mess. This is a compatibility
// layer to make certain uses of Clojure Compiler methods work in
// core.
type evalCompiler struct{}

var (
Expand Down Expand Up @@ -261,22 +263,9 @@ func (env *environment) EvalASTMaybeClass(n *ast.Node) (interface{}, error) {
func (env *environment) EvalASTMaybeHostForm(n *ast.Node) (interface{}, error) {
hostFormNode := n.Sub.(*ast.MaybeHostFormNode)
field := hostFormNode.Field
// TODO: implement this for real
switch hostFormNode.Class {
case "glojure.lang.PersistentTreeSet":
switch field.Name() {
case "create":
return func(keys interface{}) interface{} {
var ks []interface{}
for seq := lang.Seq(keys); seq != nil; seq = seq.Next() {
ks = append(ks, seq.First())
}
return lang.NewSet(ks...)
}, nil
}
}

// TODO: how to handle?
// TODO: should we support any version of this? Go doesn't have any notion of static
// vs instance fields.
panic("EvalASTMaybeHostForm: " + hostFormNode.Class + "/" + field.Name())
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/runtime/rtcompat.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,13 @@ func compileNSToFile(fs fs.FS, scriptBase string) {
var buf bytes.Buffer
gen := NewGenerator(&buf)
currNS := VarCurrentNS.Deref().(*lang.Namespace)
// only generate if the namespace has a name that matches the scriptBase
nsName := strings.ReplaceAll(scriptBase, string(os.PathSeparator), ".")
if currNS.Name().Name() != nsName {
fmt.Printf("Skipping code generation for namespace %s: does not match script base %s\n", currNS.Name(), nsName)
return
}

if err = gen.Generate(currNS); err != nil {
panic(fmt.Errorf("failed to generate code for namespace %s: %w", currNS.Name(), err))
}
Expand Down
34 changes: 17 additions & 17 deletions pkg/stdlib/glojure/core/async/loader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 18 additions & 3 deletions pkg/stdlib/glojure/core/loader.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading