Skip to content

Commit

Permalink
Merge pull request #313 from refaktor/allin
Browse files Browse the repository at this point in the history
improving eyr, rye functions, builtins, general stack
  • Loading branch information
refaktor authored Aug 18, 2024
2 parents ca5ae1d + 1e4e2c4 commit 2f0f4c2
Show file tree
Hide file tree
Showing 8 changed files with 221 additions and 73 deletions.
24 changes: 22 additions & 2 deletions env/env.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,10 @@ func (ps *ProgramState) Dump() string {
return ps.Ctx.DumpBare(*ps.Idx)
}

func (ps *ProgramState) ResetStack() {
ps.Stack = NewEyrStack()
}

func AddToProgramState(ps *ProgramState, ser TSeries, idx *Idxs) *ProgramState {
ps.Ser = ser
ps.Res = nil
Expand Down Expand Up @@ -466,7 +470,7 @@ func (s *EyrStack) Push(es *ProgramState, x Object) {
//// *s = append(*s, x)
if s.I+1 >= STACK_SIZE {
es.ErrorFlag = true
es.Res = NewError("stack overflow")
es.Res = NewError("stack overflow (maxed)")
return
}
s.D[s.I] = x
Expand All @@ -478,10 +482,26 @@ func (s *EyrStack) Push(es *ProgramState, x Object) {
func (s *EyrStack) Pop(es *ProgramState) Object {
if s.IsEmpty() {
es.ErrorFlag = true
es.Res = NewError("stack underflow")
es.Res = NewError("stack underflow (empty)")
return es.Res
}
s.I--
x := s.D[s.I]
return x
}

// Pop removes and returns the top element of stack.
func (s *EyrStack) Peek(es *ProgramState, offset int) Object {
if s.IsEmpty() {
es.ErrorFlag = true
es.Res = NewError("stack underflow (empty 2)")
return es.Res
}
if s.I-offset < 0 {
es.ErrorFlag = true
es.Res = NewError("stack underflow (offset)")
return es.Res
}
x := s.D[s.I-offset]
return x
}
5 changes: 5 additions & 0 deletions env/object.go
Original file line number Diff line number Diff line change
Expand Up @@ -1138,6 +1138,11 @@ func (i Comma) Dump(e Idxs) string {

type Void struct{}

func NewVoid() *Void {
o := Void{}
return &o
}

func (i Void) Type() Type {
return VoidType
}
Expand Down
16 changes: 10 additions & 6 deletions evaldo/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1111,6 +1111,15 @@ var builtins = map[string]*env.Builtin{
},
},

"_.": { // ***
Argsn: 1,
Doc: "Accepts argument and returns void, meant to be used as drop in Eyr, void is not a valid runtime value.",
Pure: true,
Fn: func(ps *env.ProgramState, arg0 env.Object, arg1 env.Object, arg2 env.Object, arg3 env.Object, arg4 env.Object) env.Object {
return *env.NewVoid()
},
},

"_+": { // **
Argsn: 2,
Doc: "Adds or joins two values together (Integers, Strings, Uri-s and Blocks)",
Expand Down Expand Up @@ -2364,12 +2373,7 @@ var builtins = map[string]*env.Builtin{
case env.Block:
ser := ps.Ser
ps.Ser = bloc.Series
switch ps.Dialect {
case env.Rye2Dialect:
EvalBlock(ps)
case env.EyrDialect:
Eyr_EvalBlock(ps, ps.Stack, false)
}
EvalBlock(ps)
ps.Ser = ser
return ps.Res
default:
Expand Down
Loading

0 comments on commit 2f0f4c2

Please sign in to comment.