Implemented infinite loops
This commit is contained in:
55
src/build/VariableDefinition.go
Normal file
55
src/build/VariableDefinition.go
Normal file
@ -0,0 +1,55 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"git.akyoto.dev/cli/q/src/build/expression"
|
||||
"git.akyoto.dev/cli/q/src/build/token"
|
||||
"git.akyoto.dev/cli/q/src/errors"
|
||||
)
|
||||
|
||||
// CompileVariableDefinition compiles a variable definition.
|
||||
func (f *Function) CompileVariableDefinition(expr *expression.Expression) error {
|
||||
if len(expr.Children) < 2 {
|
||||
return errors.New(errors.MissingAssignValue, f.File, expr.LastChild().Token.End())
|
||||
}
|
||||
|
||||
name := expr.Children[0].Token.Text()
|
||||
|
||||
if f.identifierExists(name) {
|
||||
return errors.New(&errors.VariableAlreadyExists{Name: name}, f.File, expr.Children[0].Token.Position)
|
||||
}
|
||||
|
||||
value := expr.Children[1]
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
reg, exists := f.CPU.FindFree()
|
||||
|
||||
if !exists {
|
||||
panic("no free registers")
|
||||
}
|
||||
|
||||
f.ExpressionToRegister(value, reg)
|
||||
f.CPU.Use(reg)
|
||||
|
||||
f.Variables[name] = &Variable{
|
||||
Name: name,
|
||||
Register: reg,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// isVariableDefinition returns true if the expression is a variable definition.
|
||||
func isVariableDefinition(expr *expression.Expression) bool {
|
||||
return expr.Token.Kind == token.Operator && expr.Token.Text() == ":="
|
||||
}
|
Reference in New Issue
Block a user