Forbid to redeclare variables, builtins, functions.

This commit is contained in:
Anton Medvedev
2023-08-22 22:30:07 +02:00
parent 3fad31af59
commit d237c8ab4e
3 changed files with 35 additions and 1 deletions
+12
View File
@@ -875,6 +875,18 @@ func (v *visitor) PointerNode(node *ast.PointerNode) (reflect.Type, info) {
}
func (v *visitor) VariableDeclaratorNode(node *ast.VariableDeclaratorNode) (reflect.Type, info) {
if _, ok := v.config.Types[node.Name]; ok {
return v.error(node, "cannot redeclare %v", node.Name)
}
if _, ok := v.config.Functions[node.Name]; ok {
return v.error(node, "cannot redeclare function %v", node.Name)
}
if _, ok := v.config.Builtins[node.Name]; ok {
return v.error(node, "cannot redeclare builtin %v", node.Name)
}
if _, ok := v.lookupVariable(node.Name); ok {
return v.error(node, "cannot redeclare variable %v", node.Name)
}
vtype, vinfo := v.visit(node.Value)
v.scopes = append(v.scopes, scope{node.Name, vtype, vinfo})
t, i := v.visit(node.Expr)
+20
View File
@@ -507,6 +507,26 @@ repeat("0", 1/0)
cannot use float64 as argument (type int) to call repeat (1:14)
| repeat("0", 1/0)
| .............^
let map = 42; map
cannot redeclare builtin map (1:5)
| let map = 42; map
| ....^
let len = 42; len
cannot redeclare builtin len (1:5)
| let len = 42; len
| ....^
let Float = 42; Float
cannot redeclare Float (1:5)
| let Float = 42; Float
| ....^
let foo = 1; let foo = 2; foo
cannot redeclare variable foo (1:18)
| let foo = 1; let foo = 2; foo
| .................^
`
func TestCheck_error(t *testing.T) {
+3 -1
View File
@@ -193,11 +193,13 @@ func (p *parser) parseVariableDeclaration() Node {
value := p.parseExpression(0)
p.expect(Operator, ";")
node := p.parseExpression(0)
return &VariableDeclaratorNode{
let := &VariableDeclaratorNode{
Name: variableName.Value,
Value: value,
Expr: node,
}
let.SetLocation(variableName.Location)
return let
}
func (p *parser) parseConditional(node Node) Node {