about summary refs log tree commit diff
path: root/pkg/lang/vm/exec.go
AgeCommit message (Expand)Author
2022-06-26Change arguments order in VM to match expectationMel
2022-06-02OpMod and OpSetLocal for prime testMel
2022-06-01Implement proper object typesMel
2022-06-01Always return from native functions implicitlyMel
2022-06-01Shift call base only after new call base pushedMel
2022-05-31Call args fix in tests and better error messageMel
2022-05-31Add untyped Objects (for now)Mel
2022-05-30Specify arg count on VM FunctionsMel
2022-05-29Types, Methods and basic Core LibMel
2022-05-29VM Native FunctionsMel
2022-05-28Extract stack package and hide behind interfaceMel
2022-05-28Harden VM MemMel
2022-05-27Function envs and value escapingMel
2022-05-27VM ARCMel
2022-05-20Call and return from functionsMel
2022-05-20Continuous VM stack implementationMel
2022-05-20More VM operations needed for FibMel
2022-05-20Access methods for VM data and stop Array copyingMel
2022-05-18Handle errors gracefully in VMMel
2022-05-18Fix type assertions for add and sub execMel
2022-05-17Lang VM PrototypeMel
/ .highlight .ne { color: #DDD } /* Name.Exception */ .highlight .nf { color: #FF0 } /* Name.Function */ .highlight .nl { color: #DDD } /* Name.Label */ .highlight .nn { color: #DDD } /* Name.Namespace */ .highlight .nx { color: #DDD } /* Name.Other */ .highlight .py { color: #DDD } /* Name.Property */ .highlight .nt { color: #DDD } /* Name.Tag */ .highlight .nv { color: #EEDD82 } /* Name.Variable */ .highlight .ow { color: #F00 } /* Operator.Word */ .highlight .pm { color: #DDD } /* Punctuation.Marker */ .highlight .w { color: #DDD } /* Text.Whitespace */ .highlight .mb { color: #F0F } /* Literal.Number.Bin */ .highlight .mf { color: #F0F } /* Literal.Number.Float */ .highlight .mh { color: #F0F } /* Literal.Number.Hex */ .highlight .mi { color: #F0F } /* Literal.Number.Integer */ .highlight .mo { color: #F0F } /* Literal.Number.Oct */ .highlight .sa { color: #87CEEB } /* Literal.String.Affix */ .highlight .sb { color: #87CEEB } /* Literal.String.Backtick */ .highlight .sc { color: #87CEEB } /* Literal.String.Char */ .highlight .dl { color: #87CEEB } /* Literal.String.Delimiter */ .highlight .sd { color: #87CEEB } /* Literal.String.Doc */ .highlight .s2 { color: #87CEEB } /* Literal.String.Double */ .highlight .se { color: #87CEEB } /* Literal.String.Escape */ .highlight .sh { color: #87CEEB } /* Literal.String.Heredoc */ .highlight .si { color: #87CEEB } /* Literal.String.Interpol */ .highlight .sx { color: #87CEEB } /* Literal.String.Other */ .highlight .sr { color: #87CEEB } /* Literal.String.Regex */ .highlight .s1 { color: #87CEEB } /* Literal.String.Single */ .highlight .ss { color: #87CEEB } /* Literal.String.Symbol */ .highlight .bp { color: #DDD } /* Name.Builtin.Pseudo */ .highlight .fm { color: #FF0 } /* Name.Function.Magic */ .highlight .vc { color: #EEDD82 } /* Name.Variable.Class */ .highlight .vg { color: #EEDD82 } /* Name.Variable.Global */ .highlight .vi { color: #EEDD82 } /* Name.Variable.Instance */ .highlight .vm { color: #EEDD82 } /* Name.Variable.Magic */ .highlight .il { color: #F0F } /* Literal.Number.Integer.Long */
package vm

import (
	"jinx/pkg/lang/vm/mem"
	"jinx/pkg/lang/vm/value"
)

func (vm *VM) popAndDrop() (value.Value, error) {
	v, err := vm.stack.Pop()
	if err != nil {
		return value.Value{}, err
	}
	v.Drop(vm.memory)
	return v, nil
}

func (vm *VM) popCallAndDrop() (int, error) {
	envPtr := vm.stack.CurrentCallEnv()
	vm.memory.Release(envPtr)

	for !vm.stack.ReachedBaseOfCall() {
		_, err := vm.popAndDrop()
		if err != nil {
			return 0, err
		}
	}

	return vm.stack.PopCall()
}

func (vm *VM) getMemCell(ptr mem.Ptr, kind mem.CellKind, allowNil bool) (mem.CellData, error) {
	if ptr.IsNull() {
		return nil, ErrNullPtrDereference{At: ptr}
	}

	if !vm.memory.Is(ptr, kind) {
		return nil, ErrUnexpectedMemCell{Ptr: ptr, Expected: mem.CellKindEnv, Got: vm.memory.Kind(ptr)}
	}

	cell, err := vm.memory.Get(ptr)
	if err != nil {
		return nil, err
	}

	if cell == nil {
		if allowNil {
			return nil, nil
		}
		return nil, ErrMemNilCell{Ptr: ptr}
	}

	ok := false
	switch kind {
	case mem.CellKindString:
		_, ok = cell.(value.StringCell)
	case mem.CellKindArray:
		_, ok = cell.(value.ArrayCell)
	case mem.CellKindEnv:
		_, ok = cell.(value.EnvCell)
	case mem.CellKindOutlet:
		_, ok = cell.(value.OutletCell)
	case mem.CellKindType:
		_, ok = cell.(value.TypeCell)
	case mem.CellKindObject:
		_, ok = cell.(value.ObjectCell)
	}

	if !ok {
		return nil, ErrCorruptedMemCell{Ptr: ptr}
	}

	return cell, nil
}