Improved variable definitions
This commit is contained in:
@ -137,19 +137,35 @@ func (f *Function) CompileVariableDefinition(expr *expression.Expression) error
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
// All expressions are returned to the memory pool.
|
||||
// To avoid losing variable values, we will detach it from the expression.
|
||||
expr.RemoveChild(value)
|
||||
|
||||
f.Variables[name] = &Variable{
|
||||
Name: name,
|
||||
Value: value,
|
||||
IsConst: true,
|
||||
Name: name,
|
||||
Value: value,
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *Function) identifierExists(name string) bool {
|
||||
_, exists := f.Variables[name]
|
||||
return exists
|
||||
}
|
||||
|
||||
// CompileFunctionCall compiles a function call.
|
||||
func (f *Function) CompileFunctionCall(expr *expression.Expression) error {
|
||||
funcName := expr.Children[0].Token.Text()
|
||||
@ -193,18 +209,7 @@ func (f *Function) TokenToRegister(t token.Token, register cpu.Register) error {
|
||||
return errors.New(&errors.UnknownIdentifier{Name: name}, f.File, t.Position)
|
||||
}
|
||||
|
||||
if !variable.IsConst {
|
||||
return errors.New(errors.NotImplemented, f.File, t.Position)
|
||||
}
|
||||
|
||||
n, err := strconv.Atoi(variable.Value.Token.Text())
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
f.Assembler.MoveRegisterNumber(register, uint64(n))
|
||||
return nil
|
||||
return f.ExpressionToRegister(variable.Value, register)
|
||||
|
||||
case token.Number:
|
||||
value := t.Text()
|
||||
|
Reference in New Issue
Block a user