2020-11-14 21:09:54 +00:00
|
|
|
/*
|
|
|
|
Serene --- Yet an other Lisp
|
|
|
|
|
|
|
|
Copyright (c) 2020 Sameer Rahmani <lxsameer@gnu.org>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 2 of the License.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2020-11-15 22:16:48 +00:00
|
|
|
package core
|
2020-11-14 21:09:54 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2020-12-12 20:27:53 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"strings"
|
2020-11-14 21:09:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/** TODO:
|
|
|
|
Create an IRuntime interface to avoid using INamespace directly
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** TODO:
|
|
|
|
Handle concurrency on the runtime level
|
|
|
|
*/
|
2020-12-16 18:57:54 +00:00
|
|
|
|
|
|
|
// loadedForms is used as "pair" implementation to keep the loaded
|
|
|
|
// expressions and the source where the expressions are coming from
|
2020-12-12 20:27:53 +00:00
|
|
|
type loadedForms struct {
|
|
|
|
source string
|
|
|
|
forms *Block
|
|
|
|
}
|
2020-11-14 21:09:54 +00:00
|
|
|
|
2020-12-16 18:57:54 +00:00
|
|
|
// TODO: Make the Runtime and it's fields thread safe
|
|
|
|
|
|
|
|
// Runtime is the most important data structure in Serene which hold
|
|
|
|
// the necessary information and the state of the interpreter at runtime (duh!).
|
|
|
|
// At any given time and thread only on Runtime has to exist and we always need
|
|
|
|
// to pass a pointer to the runtime around and avoid copying. (We don't have
|
|
|
|
// multithread support just yet but the Runtime must be thread safe).
|
2020-11-14 21:09:54 +00:00
|
|
|
type Runtime struct {
|
2020-12-16 18:57:54 +00:00
|
|
|
// A mapping from ns names (e.g some.ns.over.there) to the namespace
|
|
|
|
// data. This hashmap is owner of the namespaces, meaning that we only
|
|
|
|
// pass pointers to the namespaces around and any mutation has to happen
|
|
|
|
// here
|
2020-11-15 22:16:48 +00:00
|
|
|
namespaces map[string]Namespace
|
2020-12-16 18:57:54 +00:00
|
|
|
|
|
|
|
// A mapping from the builtin function names to the corresponding
|
|
|
|
// NativeFunction struct that implements them as expressions (IExpr).
|
|
|
|
// native functions are those which can be special form as well but
|
|
|
|
// they are more suited to be a function and at the same time we
|
|
|
|
// can't implement them in Serene itself.
|
|
|
|
builtins map[string]NativeFunction
|
|
|
|
|
|
|
|
// currentNS is the fully qualified name of the current namespace which
|
|
|
|
// is being processed (evaluates) at any given time. Since it's not
|
|
|
|
// thread safe at the moment we need to be careful changeing its value.
|
|
|
|
currentNS string
|
|
|
|
|
|
|
|
// paths is an array of filesystem paths that have we need to look into
|
|
|
|
// in order to find and load the namespaces. Similar to `load_path` in other
|
|
|
|
// languages
|
|
|
|
paths []string
|
|
|
|
|
2020-12-25 22:03:21 +00:00
|
|
|
Stack CallStack
|
2020-12-16 18:57:54 +00:00
|
|
|
// A to turn on the verbose mode, FOR DEVELOPMENT USE ONLY
|
|
|
|
debugMode bool
|
2020-11-14 21:09:54 +00:00
|
|
|
}
|
|
|
|
|
2020-11-15 19:30:07 +00:00
|
|
|
func (r *Runtime) IsDebugMode() bool {
|
|
|
|
return r.debugMode
|
|
|
|
}
|
|
|
|
|
2020-11-15 22:16:48 +00:00
|
|
|
func (r *Runtime) CurrentNS() *Namespace {
|
2020-11-14 21:09:54 +00:00
|
|
|
if r.currentNS == "" {
|
|
|
|
panic("current ns is not set on the runtime.")
|
|
|
|
}
|
|
|
|
|
|
|
|
ns, ok := r.namespaces[r.currentNS]
|
|
|
|
|
|
|
|
if !ok {
|
|
|
|
panic(fmt.Sprintf("namespace '%s' doesn't exist in the runtime.", r.currentNS))
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ns
|
|
|
|
}
|
|
|
|
|
2020-12-12 20:27:53 +00:00
|
|
|
func (r *Runtime) setCurrentNS(nsName string) bool {
|
|
|
|
_, ok := r.namespaces[nsName]
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
r.currentNS = nsName
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:57:54 +00:00
|
|
|
// GetNS returns a pointer to the `Namespace` specified with the given name `ns`
|
|
|
|
// on the runtime.
|
2020-12-12 20:27:53 +00:00
|
|
|
func (r *Runtime) GetNS(ns string) (*Namespace, bool) {
|
|
|
|
namespace, ok := r.namespaces[ns]
|
|
|
|
return &namespace, ok
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:57:54 +00:00
|
|
|
// CreateNS is a helper function to create a namespace and set it to be
|
|
|
|
// the current namespace of the runtime. `MakeNS` is much preferred
|
2020-11-14 21:09:54 +00:00
|
|
|
func (r *Runtime) CreateNS(name string, source string, setAsCurrent bool) {
|
2021-01-07 19:45:07 +00:00
|
|
|
ns := MakeNS(r, name, source)
|
2020-11-14 21:09:54 +00:00
|
|
|
|
|
|
|
if setAsCurrent {
|
|
|
|
r.currentNS = name
|
|
|
|
}
|
|
|
|
r.namespaces[name] = ns
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:57:54 +00:00
|
|
|
// IsQQSimplificationEnabled returns a boolean value indicating whether
|
|
|
|
// simplification of quasiquotation is enabled or not. If yes, we have
|
|
|
|
// to replace the quasiquote expanded forms with a simplier form to gain
|
|
|
|
// a better performance.
|
2020-12-04 21:08:48 +00:00
|
|
|
func (r *Runtime) IsQQSimplificationEnabled() bool {
|
|
|
|
// TODO: read the value of this flag from the arguments of serene
|
|
|
|
// and set the default to true
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:57:54 +00:00
|
|
|
// nsNameToPath converts a namespace name to the filesystem equivilant path
|
2020-12-12 20:27:53 +00:00
|
|
|
func nsNameToPath(ns string) string {
|
|
|
|
replacer := strings.NewReplacer(
|
|
|
|
".", "/",
|
2020-12-22 23:54:21 +00:00
|
|
|
// TODO: checkout the different OSs for character supports in
|
|
|
|
// the filesystem level
|
2020-12-12 20:27:53 +00:00
|
|
|
//"-", "_",
|
|
|
|
)
|
2020-12-22 23:54:21 +00:00
|
|
|
return replacer.Replace(ns) + ".srn"
|
2020-12-12 20:27:53 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 18:57:54 +00:00
|
|
|
// LoadNS looks up the namespace specified by the given name `ns`
|
|
|
|
// and reads the content as expressions (parse it) and returns the
|
|
|
|
// expressions.
|
2020-12-30 17:50:00 +00:00
|
|
|
func (r *Runtime) LoadNS(ns *Symbol) (*loadedForms, IError) {
|
|
|
|
nsFile := nsNameToPath(ns.GetName())
|
2020-12-12 20:27:53 +00:00
|
|
|
for _, loadPath := range r.paths {
|
2020-12-16 18:57:54 +00:00
|
|
|
possibleFile := path.Join(loadPath, nsFile)
|
2020-12-12 20:27:53 +00:00
|
|
|
|
2020-12-22 23:54:21 +00:00
|
|
|
if r.debugMode {
|
|
|
|
fmt.Printf("[DEBUG] Looking for '%s' in '%s'", possibleFile, loadPath)
|
|
|
|
}
|
|
|
|
|
2020-12-12 20:27:53 +00:00
|
|
|
_, err := os.Stat(possibleFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(possibleFile)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
readError := MakeError(
|
|
|
|
r,
|
2020-12-30 17:50:00 +00:00
|
|
|
ns,
|
2020-12-12 20:27:53 +00:00
|
|
|
fmt.Sprintf("error while reading the file at %s", possibleFile),
|
|
|
|
)
|
|
|
|
readError.WithError(err)
|
|
|
|
return nil, readError
|
|
|
|
}
|
|
|
|
|
2020-12-30 17:50:00 +00:00
|
|
|
body, e := ReadString(possibleFile, string(data))
|
2020-12-12 20:27:53 +00:00
|
|
|
if e != nil {
|
|
|
|
return nil, e
|
|
|
|
}
|
|
|
|
|
|
|
|
return &loadedForms{possibleFile, body}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Add the load paths to the error message here
|
2020-12-30 17:50:00 +00:00
|
|
|
return nil, MakeError(r, ns, fmt.Sprintf("Can't find the namespace '%s' in any of load paths.", ns))
|
2020-12-12 20:27:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runtime) InsertNS(nsName string, ns *Namespace) {
|
|
|
|
r.namespaces[nsName] = *ns
|
|
|
|
}
|
|
|
|
|
2020-12-15 19:08:51 +00:00
|
|
|
func (r *Runtime) LookupBuiltin(k string) IExpr {
|
|
|
|
builtinfn, ok := r.builtins[k]
|
|
|
|
|
|
|
|
if ok {
|
|
|
|
return &builtinfn
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-12-16 18:57:54 +00:00
|
|
|
// MakeRuntime creates a Runtime and returns a pointer to it. Any
|
|
|
|
// runtime initialization such as adding default namespaces and vice
|
|
|
|
// versa has to happen here.
|
2020-12-25 22:03:21 +00:00
|
|
|
func MakeRuntime(paths []string, flags map[string]bool) *Runtime {
|
2020-12-15 19:08:51 +00:00
|
|
|
rt := Runtime{
|
2020-11-15 22:16:48 +00:00
|
|
|
namespaces: map[string]Namespace{},
|
2020-11-14 21:09:54 +00:00
|
|
|
currentNS: "",
|
2020-12-25 22:03:21 +00:00
|
|
|
debugMode: flags["debugMode"],
|
2020-12-12 20:27:53 +00:00
|
|
|
paths: paths,
|
2020-12-25 22:03:21 +00:00
|
|
|
Stack: MakeCallStack(flags["stackDebugMode"]),
|
2020-11-14 21:09:54 +00:00
|
|
|
}
|
2020-12-15 19:08:51 +00:00
|
|
|
|
|
|
|
rt.builtins = BUILTINS
|
|
|
|
return &rt
|
2020-11-14 21:09:54 +00:00
|
|
|
}
|