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"
|
|
|
|
)
|
|
|
|
|
|
|
|
/** TODO:
|
|
|
|
Create an IRuntime interface to avoid using INamespace directly
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** TODO:
|
|
|
|
Handle concurrency on the runtime level
|
|
|
|
*/
|
|
|
|
|
|
|
|
type Runtime struct {
|
2020-11-15 22:16:48 +00:00
|
|
|
namespaces map[string]Namespace
|
2020-11-14 21:09:54 +00:00
|
|
|
currentNS string
|
|
|
|
debugMode bool
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *Runtime) CreateNS(name string, source string, setAsCurrent bool) {
|
2020-11-15 22:16:48 +00:00
|
|
|
ns := MakeNS(name, source)
|
2020-11-14 21:09:54 +00:00
|
|
|
|
|
|
|
if setAsCurrent {
|
|
|
|
r.currentNS = name
|
|
|
|
}
|
|
|
|
r.namespaces[name] = ns
|
|
|
|
}
|
|
|
|
|
|
|
|
func MakeRuntime(debug bool) *Runtime {
|
|
|
|
return &Runtime{
|
2020-11-15 22:16:48 +00:00
|
|
|
namespaces: map[string]Namespace{},
|
2020-11-14 21:09:54 +00:00
|
|
|
currentNS: "",
|
|
|
|
debugMode: debug,
|
|
|
|
}
|
|
|
|
}
|