Implemented definitions
This commit is contained in:
parent
aba9cf2412
commit
ef7deb30b7
6 changed files with 70 additions and 14 deletions
|
@ -25,25 +25,32 @@ func (f *Function) CompileVariableDefinition(expr *expression.Expression) error
|
|||
return errors.New(&errors.UnusedVariable{Name: name}, f.File, expr.Children[0].Token.Position)
|
||||
}
|
||||
|
||||
reg, exists := f.cpu.FindFree(f.cpu.General)
|
||||
value := expr.Children[1]
|
||||
|
||||
if !exists {
|
||||
panic("no free registers")
|
||||
}
|
||||
err := value.EachLeaf(func(leaf *expression.Expression) error {
|
||||
if leaf.Token.Kind == token.Identifier && !f.identifierExists(leaf.Token.Text()) {
|
||||
return errors.New(&errors.UnknownIdentifier{Name: leaf.Token.Text()}, f.File, leaf.Token.Position)
|
||||
}
|
||||
|
||||
err := f.ExpressionToRegister(expr.Children[1], reg)
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f.addVariable(&Variable{
|
||||
Name: name,
|
||||
Register: reg,
|
||||
Alive: uses,
|
||||
})
|
||||
if uses == 1 {
|
||||
expr.RemoveChild(value)
|
||||
|
||||
return nil
|
||||
f.definitions[name] = &Definition{
|
||||
Name: name,
|
||||
Value: value,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return f.storeVariableInRegister(name, value, uses)
|
||||
}
|
||||
|
||||
func (f *Function) addVariable(variable *Variable) {
|
||||
|
@ -75,6 +82,24 @@ func (f *Function) useVariable(variable *Variable) {
|
|||
}
|
||||
}
|
||||
|
||||
func (f *Function) storeVariableInRegister(name string, value *expression.Expression, uses int) error {
|
||||
reg, exists := f.cpu.FindFree(f.cpu.General)
|
||||
|
||||
if !exists {
|
||||
panic("no free registers")
|
||||
}
|
||||
|
||||
err := f.ExpressionToRegister(value, reg)
|
||||
|
||||
f.addVariable(&Variable{
|
||||
Name: name,
|
||||
Register: reg,
|
||||
Alive: uses,
|
||||
})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func countIdentifier(tokens token.List, name string) int {
|
||||
count := 0
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue