Implemented complex expressions
This commit is contained in:
@ -122,60 +122,45 @@ func (f *Function) CompileInstruction(line token.List) error {
|
||||
|
||||
// ExpressionToRegister moves the result of an expression into the given register.
|
||||
func (f *Function) ExpressionToRegister(root *expression.Expression, register cpu.Register) error {
|
||||
operation := root.Token
|
||||
|
||||
if root.IsLeaf() {
|
||||
return f.TokenToRegister(root.Token, register)
|
||||
return f.TokenToRegister(operation, register)
|
||||
}
|
||||
|
||||
left := root.Children[0]
|
||||
right := root.Children[1]
|
||||
|
||||
f.ExpressionToRegister(left, register)
|
||||
err := f.ExpressionToRegister(left, register)
|
||||
|
||||
if right.IsLeaf() {
|
||||
value := right.Token.Text()
|
||||
n, err := strconv.Atoi(value)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
switch root.Token.Text() {
|
||||
case "+":
|
||||
f.Assembler.RegisterNumber(asm.ADD, register, n)
|
||||
|
||||
case "-":
|
||||
f.Assembler.RegisterNumber(asm.SUB, register, n)
|
||||
|
||||
case "*":
|
||||
f.Assembler.RegisterNumber(asm.MUL, register, n)
|
||||
|
||||
case "/":
|
||||
f.Assembler.RegisterNumber(asm.DIV, register, n)
|
||||
}
|
||||
|
||||
return nil
|
||||
} else {
|
||||
temporary, _ := f.CPU.FindFree(f.CPU.General)
|
||||
f.CPU.Use(temporary)
|
||||
f.ExpressionToRegister(right, temporary)
|
||||
f.CPU.Free(temporary)
|
||||
|
||||
switch root.Token.Text() {
|
||||
case "+":
|
||||
f.Assembler.RegisterRegister(asm.ADD, register, temporary)
|
||||
|
||||
case "-":
|
||||
f.Assembler.RegisterRegister(asm.SUB, register, temporary)
|
||||
|
||||
case "*":
|
||||
f.Assembler.RegisterRegister(asm.MUL, register, temporary)
|
||||
|
||||
case "/":
|
||||
f.Assembler.RegisterRegister(asm.DIV, register, temporary)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return errors.New(errors.NotImplemented, f.File, root.Token.Position)
|
||||
return f.Execute(operation, register, right)
|
||||
}
|
||||
|
||||
// Execute executes an operation on a register with a value operand.
|
||||
func (f *Function) Execute(operation token.Token, register cpu.Register, value *expression.Expression) error {
|
||||
if value.IsLeaf() {
|
||||
return f.Calculate(register, operation, value.Token)
|
||||
}
|
||||
|
||||
temporary, found := f.CPU.FindFree(f.CPU.General)
|
||||
|
||||
if !found {
|
||||
panic("no free registers")
|
||||
}
|
||||
|
||||
f.CPU.Use(temporary)
|
||||
defer f.CPU.Free(temporary)
|
||||
err := f.ExpressionToRegister(value, temporary)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return f.CalculateRegisterRegister(operation, register, temporary)
|
||||
}
|
||||
|
||||
// TokenToRegister moves a token into a register.
|
||||
@ -190,7 +175,7 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error {
|
||||
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, t.Position)
|
||||
}
|
||||
|
||||
f.Assembler.MoveRegisterRegister(register, variable.Register)
|
||||
f.Assembler.RegisterRegister(asm.MOVE, register, variable.Register)
|
||||
return nil
|
||||
|
||||
case token.Number:
|
||||
|
Reference in New Issue
Block a user