fix instuction pointer increment
This commit is contained in:
22
elm.go
22
elm.go
@@ -77,6 +77,7 @@ func (m* Machine) Execute() error {
|
|||||||
|
|
||||||
m.stack[m.sp] = 0
|
m.stack[m.sp] = 0
|
||||||
m.sp--
|
m.sp--
|
||||||
|
m.ip++
|
||||||
case DUP:
|
case DUP:
|
||||||
if (m.sp - instr.Operand <= 0) {
|
if (m.sp - instr.Operand <= 0) {
|
||||||
return errors.New("Stack Underflow")
|
return errors.New("Stack Underflow")
|
||||||
@@ -92,6 +93,7 @@ func (m* Machine) Execute() error {
|
|||||||
|
|
||||||
m.stack[m.sp] = m.stack[m.sp - 1 - instr.Operand]
|
m.stack[m.sp] = m.stack[m.sp - 1 - instr.Operand]
|
||||||
m.sp++
|
m.sp++
|
||||||
|
m.ip++
|
||||||
case ADD:
|
case ADD:
|
||||||
if (m.sp < 2) {
|
if (m.sp < 2) {
|
||||||
return errors.New("Stack size is less than required to execute binary operation")
|
return errors.New("Stack size is less than required to execute binary operation")
|
||||||
@@ -270,23 +272,7 @@ func (m* Machine) InterpretProgramFromFile(filename string) error {
|
|||||||
func main() {
|
func main() {
|
||||||
m := Constructor()
|
m := Constructor()
|
||||||
|
|
||||||
// m.Push(Inst{Operation: PUSH, Operand: 1})
|
|
||||||
// m.Push(Inst{Operation: PUSH, Operand: 2})
|
|
||||||
// m.Push(Inst{Operation: ADD})
|
|
||||||
// m.Push(Inst{Operation: PUSH, Operand: 2})
|
|
||||||
// m.Push(Inst{Operation: SUB})
|
|
||||||
// m.Push(Inst{Operation: PUSH, Operand: 6})
|
|
||||||
// m.Push(Inst{Operation: MUL})
|
|
||||||
// m.Push(Inst{Operation: PUSH, Operand: 2})
|
|
||||||
// m.Push(Inst{Operation: DIV})
|
|
||||||
// m.Push(Inst{Operation: HALT})
|
|
||||||
|
|
||||||
// m.Push(Inst{Operation: PUSH, Operand: 1})
|
|
||||||
// m.Push(Inst{Operation: JMPIF, Operand: 3})
|
|
||||||
// m.Push(Inst{Operation: HALT})
|
|
||||||
// m.Push(Inst{Operation: PUSH, Operand: 1})
|
|
||||||
// m.Push(Inst{Operation: PUSH, Operand: 2})
|
|
||||||
// m.Push(Inst{Operation: HALT})
|
|
||||||
err := m.InterpretProgramFromFile("program.lil")
|
err := m.InterpretProgramFromFile("program.lil")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "[ERR] %s\n", err);
|
fmt.Fprintf(os.Stderr, "[ERR] %s\n", err);
|
||||||
@@ -294,7 +280,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
stackCounter := 0
|
stackCounter := 0
|
||||||
for !m.isHalted {
|
for !m.isHalted && stackCounter < 100 {
|
||||||
err = m.Execute()
|
err = m.Execute()
|
||||||
m.Print()
|
m.Print()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user