Skip to content

Commit

Permalink
refact: project layout
Browse files Browse the repository at this point in the history
  • Loading branch information
qjpcpu committed Jul 23, 2022
1 parent 4a607b0 commit 08dc7a0
Show file tree
Hide file tree
Showing 22 changed files with 380 additions and 366 deletions.
6 changes: 3 additions & 3 deletions address.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
package glisp

type Address struct {
function SexpFunction
function *SexpFunction
position int
}

func (a Address) IsStackElem() {}

func (stack *Stack) PushAddr(function SexpFunction, pc int) {
func (stack *Stack) PushAddr(function *SexpFunction, pc int) {
stack.Push(Address{function, pc})
}

func (stack *Stack) PopAddr() (SexpFunction, int, error) {
func (stack *Stack) PopAddr() (*SexpFunction, int, error) {
elem, err := stack.Pop()
if err != nil {
return MissingFunction, 0, err
Expand Down
2 changes: 1 addition & 1 deletion sys.go → builtin.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func GetTypeFunction(name string) UserFunction {
present = `char`
case SexpFloat:
present = `float`
case SexpFunction:
case *SexpFunction:
present = `function`
case SexpHash:
present = `hash`
Expand Down
File renamed without changes.
24 changes: 12 additions & 12 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ type Environment struct {
stackstack *Stack
symtable map[string]int
revsymtable map[int]string
builtins map[int]SexpFunction
macros map[int]SexpFunction
curfunc SexpFunction
mainfunc SexpFunction
builtins map[int]*SexpFunction
macros map[int]*SexpFunction
curfunc *SexpFunction
mainfunc *SexpFunction
pc int
nextsymbol int
before []PreHook
Expand All @@ -43,8 +43,8 @@ func New() *Environment {
env.scopestack.PushScope()
env.stackstack = NewStack(StackStackSize)
env.addrstack = NewStack(CallStackSize)
env.builtins = make(map[int]SexpFunction)
env.macros = make(map[int]SexpFunction)
env.builtins = make(map[int]*SexpFunction)
env.macros = make(map[int]*SexpFunction)
env.symtable = make(map[string]int)
env.revsymtable = make(map[int]string)
env.nextsymbol = 1
Expand Down Expand Up @@ -154,7 +154,7 @@ func (env *Environment) wrangleOptargs(fnargs, nargs int) error {
return nil
}

func (env *Environment) CallFunction(function SexpFunction, nargs int) error {
func (env *Environment) CallFunction(function *SexpFunction, nargs int) error {
for _, prehook := range env.before {
expressions, err := env.datastack.GetExpressions(nargs)
if err != nil {
Expand Down Expand Up @@ -250,7 +250,7 @@ func (env *Environment) ReturnFromFunction() error {
}

func (env *Environment) CallUserFunction(
function SexpFunction, name string, nargs int) error {
function *SexpFunction, name string, nargs int) error {

for _, prehook := range env.before {
expressions, err := env.datastack.GetExpressions(nargs)
Expand Down Expand Up @@ -430,7 +430,7 @@ func (env *Environment) DumpFunctionByName(name string) error {

var fun Function
switch t := obj.(type) {
case SexpFunction:
case *SexpFunction:
if !t.user {
fun = t.fun
} else {
Expand Down Expand Up @@ -497,14 +497,14 @@ func (env *Environment) ApplyByName(fun string, args []Sexp) (Sexp, error) {
if !ok {
return SexpNull, fmt.Errorf("function %s not found", fun)
}
fn, ok := f.(SexpFunction)
fn, ok := f.(*SexpFunction)
if !ok {
return SexpNull, fmt.Errorf("%s(%T) is not a function", fun, f)
}
return env.Apply(fn, args)
}

func (env *Environment) Apply(fun SexpFunction, args []Sexp) (Sexp, error) {
func (env *Environment) Apply(fun *SexpFunction, args []Sexp) (Sexp, error) {
if fun.user {
return fun.userfun(env, args)
}
Expand Down Expand Up @@ -551,7 +551,7 @@ func (env *Environment) GlobalFunctions() []string {
var ret []string
for _, scope := range env.globalScopes() {
for _, v := range scope.(Scope) {
if fn, ok := v.(SexpFunction); ok {
if fn, ok := v.(*SexpFunction); ok {
ret = append(ret, fn.name)
}
}
Expand Down
Loading

0 comments on commit 08dc7a0

Please sign in to comment.