mirror of
https://github.com/hanzoai/expr.git
synced 2026-09-24 16:58:58 +00:00
Forbid to redeclare variables, builtins, functions.
This commit is contained in:
@@ -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)
|
||||
|
||||
@@ -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
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user