Exemple #1
0
        /// <summary>
        /// Assign a value to a variable
        /// </summary>
        /// <param name="tree">Tree to be executed</param>
        private void Assign(KermitAST tree)
        {
            KermitAST lhs   = (KermitAST)tree.GetChild(0);
            KermitAST expr  = tree.GetChild(1) as KermitAST;
            KObject   value = Execute(expr);

            if (value != null && !value.IsVoid)
            {
                if (lhs.Type == KermitParser.DOT || lhs.Type == KermitParser.INDEX)
                {
                    FieldAssign(lhs, value);
                }
                else
                {
                    MemorySpace space = GetSpaceWithSymbol(lhs.Text) ?? _currentSpace;
                    KLocal      var   = space[lhs.Text];
                    if (var == null)
                    {
                        space[lhs.Text] = new KLocal(lhs.Text, value);
                    }
                    else
                    {
                        var.Value = value;
                    }
                }
            }
        }
Exemple #2
0
 /// <summary>
 /// Tries to cast the value
 /// </summary>
 /// <typeparam name="T">Type of the cast</typeparam>
 /// <param name="var">Input value</param>
 /// <param name="casted">Outputs the value casted</param>
 /// <returns>true or false if it can be casted</returns>
 protected bool TryCast <T>(KLocal var, out T casted)
 {
     if (var.Value.Value is T)
     {
         casted = (T)var.Value.Value;
         return(true);
     }
     casted = default(T);
     return(false);
 }
Exemple #3
0
 public override void Put(string id, KLocal value)
 {
     if (Contains(id) || !_parentSpace.Contains(id))
     {
         base.Put(id, value);
     }
     else
     {
         _parentSpace[id] = value;
     }
 }
Exemple #4
0
        /// <summary>
        /// Call a function
        /// </summary>
        /// <param name="tree">Tree to be executed</param>
        /// <returns>The result of the function</returns>
        private KObject Call(KermitAST tree)
        {
            string    fName    = tree.GetChild(0).Text;
            KFunction function = GetFunction(fName);

            if (function == null)
            {
                ThrowHelper.NameNotExists(fName, StackTrace);
                //Listener.Error($"Function name {fName} is not defined");
                return(null);
            }

            int argCount = tree.ChildCount - 1;

            if (!function.IsNative &&
                (function.Value.Arguments == null && argCount > 0 ||
                 function.Value.Arguments != null && argCount != function.Value.Arguments.Count))
            {
                ThrowHelper.TypeError($"Function {fName} takes {argCount} arguments", StackTrace);
                //Listener.Error($"Function {fName}: argument list mismatch");
                return(null);
            }

            List <KLocal> param     = new List <KLocal>(argCount);
            var           arguments = function.Value.Arguments.Values.GetEnumerator();

            for (int i = 0; i < argCount; ++i)
            {
                arguments.MoveNext();
                string    name         = function.IsNative ? "" : arguments.Current.Name;
                KermitAST argumentTree = (KermitAST)tree.GetChild(i + 1);
                KLocal    var;
                if (argumentTree.Type == KermitParser.REF)
                {
                    var = new KGlobal(name, argumentTree.GetChild(0).Text, _currentSpace);
                }
                else
                {
                    KObject argumentValue = Execute(argumentTree);
                    var = new KLocal(name, argumentValue);
                }
                param.Add(var);
            }

            return(CallFunction(function, param));
        }
Exemple #5
0
        public override KLocal Get(string id)
        {
            KLocal ret = base.Get(id) ?? _parentSpace[id];

            return(ret);
        }
Exemple #6
0
 /// <summary>
 /// Cast a variable o another type
 /// </summary>
 /// <typeparam name="T">Type of the cast</typeparam>
 /// <param name="var">Input value</param>
 /// <returns>The object casteds</returns>
 protected T Cast <T>(KLocal var)
 {
     return((T)var.Value.Value);
 }
Exemple #7
0
 /// <summary>
 /// Adds or updates a variable in the space
 /// </summary>
 /// <param name="id">The name of the variable</param>
 /// <param name="value">The new value</param>
 public virtual void Put(string id, KLocal value)
 {
     _members[id] = value;
 }