~madcapjake/rhi

0792f7336178eef6a4e3071e3fcb61c881bfda58 — Jake Russo 1 year, 6 months ago 5ef217a v0.1.0
Fix REPL crash
4 files changed, 32 insertions(+), 26 deletions(-)

A .gitignore
M internal/cli/cli.go
M internal/generator/visitor.go
M internal/vm/vm.go
A .gitignore => .gitignore +1 -0
@@ 0,0 1,1 @@
cmd/rhumb/__debug_bin

M internal/cli/cli.go => internal/cli/cli.go +1 -1
@@ 57,8 57,8 @@ func interpret(ctx context.Context, chars antlr.CharStream) {
	// p.BuildParseTrees = true

	visitor := ctx.Value(VisitorCK).(*generator.RhumbVisitor)
	vm := visitor.GetVM().ResetCurrentChunk()
	visitor.Visit(p.Sequence())
	vm := visitor.GetVM()
	if ctx.Value(DisassembleCK).(bool) {
		vm.Disassemble()
	}

M internal/generator/visitor.go => internal/generator/visitor.go +10 -10
@@ 99,7 99,7 @@ func (v *RhumbVisitor) VisitMutableLabel(ctx *P.MutableLabelContext) interface{}
func (v *RhumbVisitor) VisitLabelLiteral(ctx *P.LabelLiteralContext) interface{} {
	var (
		text       string       = ctx.GetText()
		lits       vm.WordArray = v.vm.Routine.ReviveLits(&v.vm)
		lits       vm.WordArray = v.vm.CurrentChunk.ReviveLits(&v.vm)
		lblIdx     uint64
		lblFindErr error
		ra         vm.RuneArray


@@ 110,7 110,7 @@ func (v *RhumbVisitor) VisitLabelLiteral(ctx *P.LabelLiteralContext) interface{}
		ra = vm.NewRuneArray(&v.vm, word.FromAddress(0), []rune(text)...)
		lblIdx = ra.Id()
	}
	v.vm.WriteCodeToCurrentRoutine(
	v.vm.WriteCodeToCurrentChunk(
		ctx.GetStart().GetLine(),
		word.FromAddress(lblIdx),
		vm.NewLocalRequest,


@@ 126,7 126,7 @@ func (v *RhumbVisitor) VisitAssignment(ctx *P.AssignmentContext) interface{} {
		ra                    vm.RuneArray
		lblIdx, opIdx         uint64
		lblFindErr, opFindErr error
		lits                  vm.WordArray = v.vm.Routine.ReviveLits(&v.vm)
		lits                  vm.WordArray = v.vm.CurrentChunk.ReviveLits(&v.vm)
	)
	if addr := ctx.GetAddress(); addr != nil {
		text = addr.GetText()


@@ 139,7 139,7 @@ func (v *RhumbVisitor) VisitAssignment(ctx *P.AssignmentContext) interface{} {
			lblIdx = ra.Id()
		}

		v.vm.WriteCodeToCurrentRoutine(
		v.vm.WriteCodeToCurrentChunk(
			addr.GetLine(),
			word.FromAddress(lblIdx),
			vm.NewLocalRequest,


@@ 155,7 155,7 @@ func (v *RhumbVisitor) VisitAssignment(ctx *P.AssignmentContext) interface{} {
			ra = vm.NewRuneArray(&v.vm, word.FromAddress(0), []rune(text)...)
			lblIdx = ra.Id()
		}
		v.vm.WriteCodeToCurrentRoutine(
		v.vm.WriteCodeToCurrentChunk(
			addrRef.GetStart().GetLine(),
			word.FromAddress(lblIdx),
			vm.NewLocalRequest,


@@ 171,7 171,7 @@ func (v *RhumbVisitor) VisitAssignment(ctx *P.AssignmentContext) interface{} {
		ra = vm.NewRuneArray(&v.vm, word.FromAddress(0), []rune(op.GetText())...)
		opIdx = ra.Id()
	}
	v.vm.WriteCodeToCurrentRoutine(
	v.vm.WriteCodeToCurrentChunk(
		op.GetStart().GetLine(),
		word.FromAddress(opIdx),
		vm.NewOuterRequest,


@@ 192,7 192,7 @@ func (v *RhumbVisitor) VisitIntegerLiteral(ctx *P.IntegerLiteralContext) interfa
		return RhumbReturn{nil, fmt.Errorf("unable to parse int")}
	}
	viLogger.Println("VALUE:", val)
	v.vm.WriteCodeToCurrentRoutine(
	v.vm.WriteCodeToCurrentChunk(
		ctx.GetStart().GetLine(),
		word.FromInt(uint32(val)),
		vm.NewValueLiteral,


@@ 256,7 256,7 @@ func (v *RhumbVisitor) VisitMultiplicative(ctx *P.MultiplicativeContext) interfa
		exprs[i].Accept(v)
	}

	v.vm.WriteCodeToCurrentRoutine(
	v.vm.WriteCodeToCurrentChunk(
		mulOp.GetStart().GetLine(),
		word.FromAddress(ra.Id()),
		vm.NewOuterRequest, // FIXME: re-implement as NewInnerRequest


@@ 280,7 280,7 @@ func (v *RhumbVisitor) VisitAdditive(ctx *P.AdditiveContext) interface{} {
		exprs[i].Accept(v)
	}

	v.vm.WriteCodeToCurrentRoutine(
	v.vm.WriteCodeToCurrentChunk(
		addOp.GetStart().GetLine(),
		word.FromAddress(ra.Id()),
		vm.NewOuterRequest, // FIXME: re-implement as NewInnerRequest


@@ 337,7 337,7 @@ func (v *RhumbVisitor) VisitPower(ctx *P.PowerContext) interface{} {
	for i := range exprs {
		exprs[i].Accept(v)
	}
	v.vm.WriteCodeToCurrentRoutine(
	v.vm.WriteCodeToCurrentChunk(
		powOp.GetStart().GetLine(),
		word.FromAddress(ra.Id()),
		vm.NewOuterRequest, // FIXME: re-implement as NewInnerRequest

M internal/vm/vm.go => internal/vm/vm.go +20 -15
@@ 18,11 18,11 @@ func init() {
}

type VirtualMachine struct {
	heap    []word.Word
	free    []bool
	stack   []word.Word
	scope   []map[string]uint64
	Routine Chunk
	heap         []word.Word
	free         []bool
	stack        []word.Word
	scope        []map[string]uint64
	CurrentChunk Chunk
}

var DEBUG_WIDTH int = 10


@@ 100,7 100,12 @@ func NewVirtualMachine() *VirtualMachine {
	vm.scope = append(vm.scope, make(map[string]uint64))

	// TODO: convert to actual routine
	vm.Routine = NewChunk(vm)
	vm.ResetCurrentChunk()
	return vm
}

func (vm *VirtualMachine) ResetCurrentChunk() *VirtualMachine {
	vm.CurrentChunk = NewChunk(vm)
	return vm
}



@@ 119,7 124,7 @@ func (vm *VirtualMachine) ReAllocate(ws ...word.Word) (uint64, error) {
		next = 0
		if obj.free[first] {
			if size == 1 {
				vmLogger.Println("adding word to index:", first)
				vmLogger.Println("moving word to index:", first)
				obj.heap[first] = ws[0]
				obj.free[first] = false
				obj.DebugHeap()


@@ 132,7 137,7 @@ func (vm *VirtualMachine) ReAllocate(ws ...word.Word) (uint64, error) {
						next++
						break
					} else if next == last {
						vmLogger.Println("appending words to index:", first)
						vmLogger.Println("moving words to index:", first)
						obj.allocInPlace(first, last, ws...)
						obj.DebugHeap()
						*vm = obj


@@ 146,7 151,7 @@ func (vm *VirtualMachine) ReAllocate(ws ...word.Word) (uint64, error) {
	}
	// no available chunk in existing memory locations.
	first = len(obj.heap)
	vmLogger.Println("appending words to end:", first)
	vmLogger.Println("moving words to end:", first)
	obj = appendRhumb(obj, ws)
	*vm = obj
	return uint64(first), nil


@@ 246,22 251,22 @@ func (vm VirtualMachine) allocInPlace(x, y int, ws ...word.Word) {
// 	}
// }

func (vm *VirtualMachine) WriteCodeToCurrentRoutine(
func (vm *VirtualMachine) WriteCodeToCurrentChunk(
	line int,
	lit word.Word,
	codeFactory func(i uint64) []Code,
) {
	id, _ := vm.Routine.AddLiteral(vm, lit)
	id, _ := vm.CurrentChunk.AddLiteral(vm, lit)
	codes := codeFactory(id)
	vm.Routine.WriteCode(vm, line, codes)
	vm.CurrentChunk.WriteCode(vm, line, codes)
}

func (vm *VirtualMachine) Disassemble() {
	vm.Routine.Disassemble(vm)
	vm.CurrentChunk.Disassemble(vm)
}

func (vm *VirtualMachine) Execute(lastValueFlag bool) {
	vm.Routine.Execute(vm)
	vm.CurrentChunk.Execute(vm)
	if lastValueFlag {
		if len(vm.stack) == 0 {
			fmt.Println("()")


@@ 366,7 371,7 @@ func (vm *VirtualMachine) SubmitUnderRequest(label word.Word) {
// Used for traversing primitives and compilations
func (vm *VirtualMachine) SubmitOuterRequest(label word.Word) {
	// FIXME: locate text
	lits := vm.Routine.ReviveLits(vm)
	lits := vm.CurrentChunk.ReviveLits(vm)
	addr, err := lits.IndexOf(vm, label)
	if err != nil {
		panic("unable to find word for outer request")