예제 #1
0
파일: BasicNode.cs 프로젝트: t-stf/TsBasic
 public static ControlNode Make(EvalResultKind kind, BasicNode node)
 {
     return(new ControlNode
     {
         Target = node,
         Kind = kind
     });
 }
예제 #2
0
        public override BasicNode Eval(BasicEnvironment env)
        {
            BasicNode result = null;

            for (int i = 0; i < statements.Length; i++)
            {
                result = statements[i].Eval(env);
                if (result.IsError())
                {
                    return(result);
                }
            }
            return(result);
        }
예제 #3
0
 internal BasicNode ApplyFunc(BasicEnvironment env, FunctionProperty func)
 {
     env.InstructionCount++;
     if (!env.SaveContext())
     {
         return(env.LastError);
     }
     try
     {
         // create the formal parameters with their values
         int            nArgs      = func.argumentNames.Length;
         ConstantNode[] evaledArgs = new ConstantNode[nArgs];
         for (int i = 0; i < nArgs; i++)
         {
             string    argName = func.argumentNames[i];
             BasicNode evArg   = null;
             if (i < Arguments.Length)
             {
                 evArg = Arguments[i].Eval(env);
             }
             else
             {
                 evArg = CheckStringName(argName) ? ConstantNode.EmptyString : ConstantNode.Zero;
             }
             if (evArg.IsAbort())
             {
                 return(evArg);
             }
             var econs = evArg as ConstantNode;
             if (econs == null)
             {
                 env.RuntimeError();
             }
             evaledArgs[i] = econs;
         }
         var result = func.Apply(env, evaledArgs);
         if (result == null)
         {
             result = env.RuntimeError(ERuntimeErrors.NotImplemented, Name);
         }
         return(result);
     }
     finally
     {
         env.RestoreContext();
     }
 }
예제 #4
0
        public BasicNode SetValue(BasicEnvironment env, BasicNode value)
        {
            env.InstructionCount++;
            var prop = env.GetProperty(Name);

            if (prop == null)
            {
                if (Arguments.Length == 0)
                {
                    prop = new ConstantProperty();
                }
                else
                {
                    prop = IsStringName ? (Property) new ArrayProperty <StringConstantNode>(Arguments.Length)
                                                : new ArrayProperty <NumericConstantNode>(Arguments.Length);
                }
                prop.Name = Name;
                env.SetProperty(prop);
            }

            var consProp = prop as ConstantProperty;

            if (consProp != null)
            {
                if (Arguments.Length > 0)
                {
                    return(env.RuntimeError($"simple variable '{Name}' is not an array"));
                }
                var consValue = value as ConstantNode;
                if (consValue == null)
                {
                    return(env.RuntimeError());
                }
                consProp.Value = consValue;
                return(value);
            }
            var arrProp = prop as IArrayProperty;

            if (Arguments.Length == 0)
            {
                return(env.RuntimeError($"array '{Name}' should not be used like a simple variable"));
            }
            if (arrProp.DimCount != Arguments.Length)
            {
                return(env.RuntimeError(ERuntimeErrors.ArrayDimensionMismatch));
            }
            int[] indexes;
            var   result = BuildArrayIndexes(env, out indexes);

            if (result.IsError())
            {
                return(result);
            }
            if (IsStringName)
            {
                var sar  = arrProp as ArrayProperty <StringConstantNode>;
                var sval = value as StringConstantNode;
                if (sval == null)
                {
                    return(env.RuntimeError("type mismatch"));
                }
                if (!sar.SetValue(indexes, sval))
                {
                    return(env.RuntimeError(ERuntimeErrors.ArrayIndexOutOfRange, Name));
                }
                return(sval);
            }
            else
            {
                var sar  = arrProp as ArrayProperty <NumericConstantNode>;
                var sval = value as NumericConstantNode;
                if (sval == null)
                {
                    return(env.RuntimeError("type mismatch"));
                }
                if (!sar.SetValue(indexes, sval))
                {
                    return(env.RuntimeError(ERuntimeErrors.ArrayIndexOutOfRange, Name));
                }
                return(sval);
            }
        }
예제 #5
0
파일: BasicNode.cs 프로젝트: t-stf/TsBasic
        public static bool IsError(this BasicNode node)
        {
            var cnode = node as ControlNode;

            return(cnode?.Kind == EvalResultKind.Error);
        }
예제 #6
0
파일: BasicNode.cs 프로젝트: t-stf/TsBasic
        public static bool IsAbort(this BasicNode node)
        {
            var cnode = node as ControlNode;

            return(cnode?.Kind >= EvalResultKind.Abort);
        }