Added more tests
This commit is contained in:
parent
ff2a43e593
commit
2c6999040d
@ -25,6 +25,7 @@ func TestErrors(t *testing.T) {
|
|||||||
{"MissingBlockStart.q", errors.MissingBlockStart},
|
{"MissingBlockStart.q", errors.MissingBlockStart},
|
||||||
{"MissingGroupEnd.q", errors.MissingGroupEnd},
|
{"MissingGroupEnd.q", errors.MissingGroupEnd},
|
||||||
{"MissingGroupStart.q", errors.MissingGroupStart},
|
{"MissingGroupStart.q", errors.MissingGroupStart},
|
||||||
|
{"VariableAlreadyExists.q", &errors.VariableAlreadyExists{Name: "a"}},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
@ -131,13 +131,21 @@ func (f *Function) CompileInstruction(line token.List) error {
|
|||||||
return errors.New(errors.MissingAssignValue, f.File, expr.LastChild().Token.After())
|
return errors.New(errors.MissingAssignValue, f.File, expr.LastChild().Token.After())
|
||||||
}
|
}
|
||||||
|
|
||||||
name := expr.Children[0]
|
name := expr.Children[0].Token.Text()
|
||||||
|
_, exists := f.Variables[name]
|
||||||
|
|
||||||
|
if exists {
|
||||||
|
return errors.New(&errors.VariableAlreadyExists{Name: name}, f.File, expr.Children[0].Token.Position)
|
||||||
|
}
|
||||||
|
|
||||||
value := expr.Children[1]
|
value := expr.Children[1]
|
||||||
|
|
||||||
|
// All expressions are returned to the memory pool.
|
||||||
|
// To avoid losing variable values, we will remove it from the expression.
|
||||||
expr.RemoveChild(value)
|
expr.RemoveChild(value)
|
||||||
|
|
||||||
f.Variables[name.Token.Text()] = &Variable{
|
f.Variables[name] = &Variable{
|
||||||
Name: name.Token.Text(),
|
Name: name,
|
||||||
Value: value,
|
Value: value,
|
||||||
IsConst: true,
|
IsConst: true,
|
||||||
}
|
}
|
||||||
|
13
src/errors/VariableAlreadyExists.go
Normal file
13
src/errors/VariableAlreadyExists.go
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
package errors
|
||||||
|
|
||||||
|
import "fmt"
|
||||||
|
|
||||||
|
// VariableAlreadyExists is used when existing variables are used for new variable declarations.
|
||||||
|
type VariableAlreadyExists struct {
|
||||||
|
Name string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Error generates the string representation.
|
||||||
|
func (err *VariableAlreadyExists) Error() string {
|
||||||
|
return fmt.Sprintf("Variable '%s' already exists", err.Name)
|
||||||
|
}
|
4
tests/errors/VariableAlreadyExists.q
Normal file
4
tests/errors/VariableAlreadyExists.q
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
main() {
|
||||||
|
a := 1
|
||||||
|
a := 2
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user