예제 #1
0
        private Ast.DeclareStmt ParseDeclareStmt(TokenQueue q)
        {
            var stmt = new Ast.DeclareStmt {
                SourceToken = q.SourceToken
            };

            q.Take("declare");
            if (q.Peek() == "parameter")
            {
                q.Take();
                stmt.IsParameter = true;
            }
            ParseAssignmentStmtCore(q, stmt);
            return(stmt);
        }
예제 #2
0
        private void ExecuteDeclareStmt(Ast.DeclareStmt stmt, ScriptEnv env)
        {
            var name = stmt.VariableName.ToLower();
            var declarationExists = env.Vars.ContainsKey(name);

            if (stmt.IsParameter)
            {
                if (env.ParNames.Contains(name))
                {
                    throw new ScriptException($"Duplicate DECLARE for parameter \"{stmt.VariableName}\".");
                }
                else
                {
                    env.ParNames.Add(name);
                }

                if (declarationExists)
                {
                    // do nothing; the parameter value was specified by the caller
                }
                else if (stmt.InitialValue != null)
                {
                    // the caller did not specify a value, but there is an initial value in the DECLARE statement.
                    env.Vars[name] = EvaluateExpr(stmt.InitialValue, env);
                }
                else
                {
                    throw new ScriptException(
                              $"An argument value was not provided for parameter \"{stmt.VariableName}\".");
                }
            }
            else if (declarationExists)
            {
                throw new ScriptException($"Duplicate DECLARE for variable \"{stmt.VariableName}\".");
            }
            else
            {
                if (stmt.InitialValue != null)
                {
                    env.Vars[name] = EvaluateExpr(stmt.InitialValue, env);
                }
                else
                {
                    env.Vars[name] = 0L;
                }
            }
        }