Exemple #1
0
 protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
 {
     foreach (BaseNode node in ChildID)
     {
         yield return(node.Execute());
     }
 }
Exemple #2
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            Variable functionTemplate = new Variable();

            functionTemplate.TypeName = ChildID[0].GetCode(); //function_name or procedure_name
            string functionName = "@function_" + functionTemplate.TypeName;

            Mediator.Instance.ExecutingNameSpace.RegisterObject(functionName, functionTemplate);
            BaseNode      parameterList = Child["parameter_list"];
            List <string> paramIndex    = new List <string>();

            foreach (BaseNode fpSection in parameterList.ChildID)
            {
                BaseNode parameterNames = fpSection.Child["parameter_name"];
                BaseNode parameterTypes = fpSection.Child["parameter_type"];
                string   typeName       = parameterTypes.GetCode();
                foreach (BaseNode parameterName in parameterNames.ChildID)
                {
                    string idName = parameterName.GetCode();
                    paramIndex.Add(idName);
                    functionTemplate.RegisterTypeName(idName, typeName);
                }
            }
            if (Child.ContainsKey("return_type"))
            {
                functionTemplate.RegisterTypeName("@return", Child["return_type"].GetCode());
            }
            functionTemplate.RegisterObject("@index", paramIndex.ToArray());
            functionTemplate.RegisterObject("@address", Child["procedure_body"]);
            yield break;
        }
Exemple #3
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            BaseNode ids = Child["var_names"];

            string[] idNames  = ids.ChildID.Select(node => node.GetCode()).ToArray();
            string   typeName = "";

            if (Child.ContainsKey("var_type"))
            {
                typeName = Child["var_type"].GetCode();
            }
            Return initValue = null; // null means no init_value

            if (Child.ContainsKey("init_value"))
            {
                initValue = new Return();
                yield return(Child["init_value"].Execute(initValue));
            }
            foreach (string idName in idNames)
            {
                if (typeName != "")
                {
                    Mediator.Instance.ExecutingNameSpace.RegisterTypeName(idName, typeName);
                    if (!(initValue is null))
                    {
                        Mediator.Instance.ExecutingNameSpace.Reassign(idName, initValue.Object);
                    }
                }
                else if (!(initValue is null))
                {
                    Mediator.Instance.ExecutingNameSpace.RegisterObject(idName, initValue.Object);
                }
Exemple #4
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            BaseNode thingsToWrite = ChildID[0];
            string   text          = "";

            foreach (BaseNode writeExpr in thingsToWrite.ChildID)
            {
                if (writeExpr.Type == "STRING")
                {
                    string result = writeExpr.GetCode();
                    text += result.Substring(1, result.Length - 2);
                }
                else //writeExpr is expression
                {
                    Return rhs = new Return();
                    yield return(writeExpr.Execute(rhs));

                    text += rhs.Object.ToString();
                }
            }
            Variable writeVar = Variable.Root.Get("$OUTPUT$") as Variable;

            writeVar.RegisterObject(writeVar.ChildCount(), text);
            yield break;
        }
Exemple #5
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            BaseNode          leftValue  = ChildID[0];
            BaseNode          rightValue = ChildID[1];
            VariableReference reference  = new VariableReference();

            reference.Offset = rightValue.GetCode();
            if (leftValue.Type == "ID")
            {
                string idName = leftValue.GetCode();
                if (!Mediator.Instance.ExecutingNameSpace.CanFind(idName))
                {
                    throw new Exception("Not declared variable: " + idName);
                }
                reference.Base = Mediator.Instance.ExecutingNameSpace.Get(idName) as Variable;
            }
            else
            {
                Return lhs = new Return();
                yield return(leftValue.Execute(lhs));

                reference.Base = lhs.Reference.Dereference() as Variable;
            }
            me.Return(reference);
        }
Exemple #6
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            // Return lhs = new Return();
            Return rhs = new Return();

            yield return(Child["r_value"].Execute(rhs));

            // yield return Child["l_value"].Execute(lhs);
            if (Child["l_value"].Type == "ID")
            {
                string idName = Child["l_value"].GetCode();
                if (!Mediator.Instance.ExecutingNameSpace.CanFind(idName))
                {
                    throw new Exception("Not declared variable: " + idName);
                }
                Mediator.Instance.ExecutingNameSpace.Reassign(idName, rhs.Object);
            }
            else
            {
                Return lhs = new Return();
                yield return(Child["l_value"].Execute(lhs));

                VariableReference reference = lhs.Reference;
                reference.Base.Reassign(reference.Offset, rhs.Object);
            }
        }
Exemple #7
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            string   structureName     = ChildID[0].GetCode();
            BaseNode innerTypeNode     = ChildID[1];
            Variable prototypeVariable = new Variable();

            prototypeVariable.TypeName = structureName;
            if (innerTypeNode.Type == "record_components")
            {
                BaseNode componentList = innerTypeNode.ChildID[0];
                Mediator.Instance.ExecutingNameSpace.RegisterObject("@prototype_" + structureName, prototypeVariable);
                foreach (BaseNode component in componentList.ChildID)
                {
                    string componentName = component.ChildID[0].GetCode();
                    string componentType = component.ChildID[1].GetCode();
                    prototypeVariable.RegisterTypeName(componentName, componentType);
                }
            }
            else //innerTypeNode.Type == "array_type"
            {
                string elementTypeName = innerTypeNode.ChildID[0].GetCode();
                Mediator.Instance.ExecutingNameSpace.RegisterObject("@prototype_" + structureName, prototypeVariable);
                prototypeVariable.RegisterTypeName("@element", elementTypeName);
            }
            yield break;
        }
Exemple #8
0
 protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
 {
     if (Child.ContainsKey("declarations"))
     {
         yield return(Child["declarations"].Execute());
     }
     yield return(Child["process"].Execute());
 }
Exemple #9
0
 protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
 {
     // Will only break on ExitNode
     while (true)
     {
         yield return(Child["loop_statements"].Execute());
     }
 }
Exemple #10
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            // Step1: prepare new frame
            Variable oldNameSpace       = Mediator.Instance.ExecutingNameSpace;
            string   functionName       = ChildID[0].GetCode();
            Variable templateStructure  = Mediator.Instance.ExecutingNameSpace.Get("@function_" + functionName) as Variable;
            Variable newNameSpaceParent = templateStructure.Parent;
            Variable callStructure      = templateStructure.Clone() as Variable;
            BaseNode actualParams       = ChildID[1];

            string[] paramIndex = callStructure.CurrentLevelGet("@index") as string[];
            int      id         = 0;

            foreach (BaseNode actualParam in actualParams.ChildID)
            {
                Return rhs = new Return();
                yield return(actualParam.Execute(rhs));

                callStructure.Reassign(paramIndex[id++], rhs.Object);
            }

            // Step2: stash old frame, change name space
            Variable newNameSpace = callStructure;
            Variable stashed      = null;

            if (newNameSpaceParent.CurrentLevelContainsKey(functionName))
            {
                // Stash the old stuff
                stashed = newNameSpaceParent.CurrentLevelGet(functionName) as Variable;
                newNameSpaceParent.CurrentLevelEraseKey(functionName);
            }
            newNameSpaceParent.RegisterObject(functionName, callStructure);
            Mediator.Instance.ExecutingNameSpace = callStructure;

            // Step3: Execute the procedure
            BaseNode callNode = callStructure.CurrentLevelGet("@address") as BaseNode;

            yield return(callNode.Execute());

            object returnValue = Type == "function_call" ?
                                 callStructure.CurrentLevelGet("@return") : null;

            // Step4: Clean, recover the old frame and old name space
            newNameSpaceParent.CurrentLevelEraseKey(functionName);
            if (!(stashed is null))
            {
                newNameSpaceParent.RegisterObject(functionName, stashed);
            }
            Mediator.Instance.ExecutingNameSpace = oldNameSpace;

            if (Type == "function_call")
            {
                me.Return(returnValue);
            }
            yield break;
        }
Exemple #11
0
 protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
 {
     foreach (BaseNode node in ChildID)
     {
         if (!(node.Type == "while" || node.Type == "if" || node.Type == "if_else" ||
               node.Type == "loop" || node.Type == "for"))
         {
             yield return(NewPause(node));
         }
         yield return(node.Execute());
     }
 }
Exemple #12
0
 protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
 {
     if (Type == "TRUE")
     {
         me.Return(true);
     }
     if (Type == "FALSE")
     {
         me.Return(false);
     }
     yield break;
 }
Exemple #13
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            if (Child.ContainsKey("return_value"))
            {
                Return rhs = new Return();
                yield return(Child["return_value"].Execute(rhs));

                Mediator.Instance.ExecutingNameSpace.Reassign("@return", rhs.Object);
            }
            yield return(NewInterrupt("return", null));

            yield break; // Will never execute to there
        }
Exemple #14
0
        public IEnumerable <Interruption> Execute(Return returnGetter = null)
        {
            Return.ReturnSetter returnSetter = new Return.ReturnSetter();
            foreach (IEnumerable <Interruption> col in InnerExecute(returnSetter))
            {
                foreach (Interruption ir in col)
                {
                    if (ir.Type == "exit")
                    {
                        // For valid exit, a for/while/loop can be met outside
                        if (Type == "for" || Type == "while" || Type == "loop")
                        {
                            yield break;
                        }
                        else if (Type == "body") // Cannot find a for/while/loop
                        {
                            throw new Exception("Invalid exit encountered");
                        }
                        else // Pass to parent's executor
                        {
                            yield return(ir);

                            yield break;
                        }
                    }
                    else if (ir.Type == "return")
                    {
                        // Return to upper level of body node
                        if (Type == "body")
                        {
                            yield break;
                        }
                        else // Pass to parent's executor
                        {
                            yield return(ir);

                            yield break;
                        }
                    }
                    else
                    {
                        yield return(ir);
                    }
                }
            }
            if (!(returnGetter is null))
            {
                returnSetter.SetReturnGetter(returnGetter);
            }
            yield break;
        }
Exemple #15
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            Return calc = new Return();

            yield return(NewPause(Child["elsif_condition"]));

            yield return(Child["elsif_condition"].Execute(calc));

            if (calc.Bool)
            {
                yield return(Child["elsif_true_part"].Execute());
            }
            me.Return(calc.Bool);
        }
Exemple #16
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            string code = GetCode();

            if (code.Contains('.'))
            {
                me.Return(float.Parse(code));
            }
            else
            {
                me.Return(int.Parse(code));
            }
            yield break;
        }
Exemple #17
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            Return result   = new Return();
            bool   firstTry = true;

            foreach (BaseNode elseif in ChildID)
            {
                if (firstTry || result.Bool == false)
                {
                    firstTry = false;
                    yield return(elseif.Execute(result));
                }
            }
            me.Return(result.Bool);
        }
Exemple #18
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            Return calc = new Return();

            while (true)
            {
                yield return(NewPause(Child["while_condition"]));

                yield return(Child["while_condition"].Execute(calc));

                if (!calc.Bool)
                {
                    break;
                }
                yield return(Child["loop_statements"].Execute());
            }
        }
Exemple #19
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            string   typeName   = ChildID[0].GetCode();
            Variable structure  = Mediator.Instance.ExecutingNameSpace.GetPrototypeOf(typeName).Clone() as Variable;
            BaseNode compValues = ChildID[1];

            foreach (BaseNode compValuePair in compValues.ChildID)
            {
                string idName = compValuePair.ChildID[0].GetCode();
                Return rhs    = new Return();
                yield return(compValuePair.ChildID[1].Execute(rhs));

                structure.Reassign(idName, rhs.Object);
            }
            me.Return(structure);
            yield break;
        }
Exemple #20
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            string idName = GetCode();

            if (!Mediator.Instance.ExecutingNameSpace.CanFind(idName))
            {
                throw new Exception("Not decleared variable: " + idName);
            }
            object returnObject = Mediator.Instance.ExecutingNameSpace.Get(idName);

            // Need clone here!!
            if (returnObject is ICloneable)
            {
                returnObject = (returnObject as ICloneable).Clone();
            }
            me.Return(returnObject);
            yield break;
        }
Exemple #21
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            BaseNode thingsToRead = ChildID[0];

            foreach (BaseNode readExpr in thingsToRead.ChildID)
            {
                string idStr = readExpr.GetCode();
                string hint  = "";
                while (true)
                {
                    string inputValue = Mediator.Instance.GetUserInput(hint + "Please input " + idStr);
                    if (inputValue == "")
                    {
                        continue;
                    }
                    VariableReference reference = null;
                    if (readExpr.Type != "ID")
                    {
                        Return lhs = new Return();
                        yield return(readExpr.Execute(lhs));

                        reference = lhs.Reference;
                    }
                    try
                    {
                        if (reference is null)
                        {
                            Mediator.Instance.ExecutingNameSpace.Reassign(idStr, inputValue);
                        }
                        else
                        {
                            reference.Base.Reassign(reference.Offset, inputValue);
                        }
                    }
                    catch
                    {
                        hint = "Format error! ";
                        continue;
                    }
                    break;
                }
            }
            yield break;
        }
Exemple #22
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            string idName = Child["loop_variable"].GetCode();
            int    initValue, endValue, stepValue;
            Return init = new Return();

            yield return(NewPause(Child["loop_init_value"]));

            yield return(Child["loop_init_value"].Execute(init));

            initValue = init.Integer;
            Return end = new Return();

            yield return(Child["loop_end_value"].Execute(end));

            endValue = end.Integer;
            if (Child.ContainsKey("loop_step_size"))
            {
                Return step = new Return();
                yield return(Child["loop_step_size"].Execute(step));

                stepValue = step.Integer;
            }
            else
            {
                stepValue = 1;
            }
            Mediator.Instance.ExecutingNameSpace.Reassign(idName, init.Integer);
            yield return(NewPause(Child["loop_end_value"]));

            while (ContinueLoop((int)Mediator.Instance.ExecutingNameSpace.Get(idName),
                                endValue, stepValue))
            {
                yield return(Child["loop_statements"].Execute());

                if (Child.ContainsKey("loop_step_size"))
                {
                    yield return(NewPause(Child["loop_step_size"]));
                }
                Mediator.Instance.ExecutingNameSpace.Reassign(idName,
                                                              (int)Mediator.Instance.ExecutingNameSpace.Get(idName) + stepValue);
                yield return(NewPause(Child["loop_end_value"]));
            }
        }
Exemple #23
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            string   typeName    = ChildID[0].GetCode();
            Variable structure   = Mediator.Instance.ExecutingNameSpace.GetPrototypeOf(typeName).Clone() as Variable;
            BaseNode arrayValues = ChildID[1];
            int      index       = 0;

            foreach (BaseNode arrayValuePair in arrayValues.ChildID)
            {
                int    repeatTimes;
                Return lhs = new Return();
                Return rhs = new Return();
                if (arrayValuePair.Type == "array_value_pair")
                {
                    yield return(arrayValuePair.ChildID[0].Execute(lhs));

                    repeatTimes = lhs.Integer;
                    yield return(arrayValuePair.ChildID[1].Execute(rhs));
                }
                else // arrayValuePair is expression
                {
                    repeatTimes = 1;
                    yield return(arrayValuePair.Execute(rhs));
                }
                object elementValue = rhs.Object;
                for (int i = 0; i < repeatTimes; ++i)
                {
                    object elementObject = structure.CurrentLevelGet("@element");
                    structure.RegisterObject(index, elementObject);
                    if (elementValue is ICloneable)
                    {
                        elementValue = (elementValue as ICloneable).Clone();
                    }
                    structure.Reassign(index, elementValue);
                    ++index;
                }
            }
            me.Return(structure);
            yield break;
        }
Exemple #24
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            Return lhs = new Return();

            yield return(ChildID[0].Execute(lhs));

            switch (Type)
            {
            case "brackets":
                me.Return(lhs.Object);
                break;

            case "positive":
                if (lhs.IsReal())
                {
                    me.Return(lhs.Real);
                }
                else if (lhs.IsInteger())
                {
                    me.Return(lhs.Integer);
                }
                break;

            case "negative":
                if (lhs.IsReal())
                {
                    me.Return(-lhs.Real);
                }
                else if (lhs.IsInteger())
                {
                    me.Return(-lhs.Integer);
                }
                break;

            case "logical_not":
                me.Return(!lhs.Bool);
                break;
            }
        }
Exemple #25
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            Return calc = new Return();

            yield return(NewPause(Child["if_condition"]));

            yield return(Child["if_condition"].Execute(calc));

            if (calc.Bool)
            {
                yield return(Child["if_true_part"].Execute());
            }
            else
            {
                if (Child.ContainsKey("elsif_part"))
                {
                    yield return(Child["elsif_part"].Execute(calc));
                }
                if (!calc.Bool && Type == "if_else")
                {
                    yield return(Child["else_part"].Execute());
                }
            }
        }
Exemple #26
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            Return lhs = new Return();
            Return rhs = new Return();

            switch (Type)
            {
            case "logical_or":     // short-circuit
                yield return(ChildID[0].Execute(lhs));

                if (lhs.Bool == true)
                {
                    me.Return(true);
                }
                else
                {
                    yield return(ChildID[1].Execute(rhs));

                    me.Return(rhs.Bool);
                }
                break;

            case "logical_and":
                yield return(ChildID[0].Execute(lhs));

                if (lhs.Bool == false)
                {
                    me.Return(false);
                }
                else
                {
                    yield return(ChildID[1].Execute(rhs));

                    me.Return(rhs.Bool);
                }
                break;

            default:
                break;
            }
            yield return(ChildID[0].Execute(lhs));

            yield return(ChildID[1].Execute(rhs));

            switch (Type)
            {
            case "add":
                if (lhs.IsReal() || rhs.IsReal())
                {
                    me.Return(lhs.Real + rhs.Real);
                }
                else
                {
                    me.Return(lhs.Integer + rhs.Integer);
                }
                break;

            case "subtract":
                if (lhs.IsReal() || rhs.IsReal())
                {
                    me.Return(lhs.Real - rhs.Real);
                }
                else
                {
                    me.Return(lhs.Integer - rhs.Integer);
                }
                break;

            case "multiply":
                if (lhs.IsReal() || rhs.IsReal())
                {
                    me.Return(lhs.Real * rhs.Real);
                }
                else
                {
                    me.Return(lhs.Integer * rhs.Integer);
                }
                break;

            case "divide":
                me.Return(lhs.Real / rhs.Real);
                break;

            case "mod":
                me.Return(lhs.Integer % rhs.Integer);
                break;

            case "div":
                me.Return(lhs.Integer / rhs.Integer);
                break;

            case "less_than":
                if (lhs.IsReal() || rhs.IsReal())
                {
                    me.Return(lhs.Real < rhs.Real);
                }
                else
                {
                    me.Return(lhs.Integer < rhs.Integer);
                }
                break;

            case "greater_than":
                if (lhs.IsReal() || rhs.IsReal())
                {
                    me.Return(lhs.Real > rhs.Real);
                }
                else
                {
                    me.Return(lhs.Integer > rhs.Integer);
                }
                break;

            case "less_equal":
                if (lhs.IsReal() || rhs.IsReal())
                {
                    me.Return(lhs.Real <= rhs.Real);
                }
                else
                {
                    me.Return(lhs.Integer <= rhs.Integer);
                }
                break;

            case "greater_equal":
                if (lhs.IsReal() || rhs.IsReal())
                {
                    me.Return(lhs.Real >= rhs.Real);
                }
                else
                {
                    me.Return(lhs.Integer >= rhs.Integer);
                }
                break;

            case "equal":
                if (lhs.IsReal() || rhs.IsReal())
                {
                    me.Return(lhs.Real == rhs.Real);
                }
                else if (lhs.IsInteger() && rhs.IsInteger())
                {
                    me.Return(lhs.Integer == rhs.Integer);
                }
                else if (lhs.IsBool() && rhs.IsBool())
                {
                    me.Return(lhs.Bool == rhs.Bool);
                }
                else
                {
                    me.Return(lhs.Structure == rhs.Structure);
                }
                break;

            case "not_equal":
                if (lhs.IsReal() || rhs.IsReal())
                {
                    me.Return(lhs.Real != rhs.Real);
                }
                else if (lhs.IsInteger() && rhs.IsInteger())
                {
                    me.Return(lhs.Integer != rhs.Integer);
                }
                else if (lhs.IsBool() && rhs.IsBool())
                {
                    me.Return(lhs.Bool != rhs.Bool);
                }
                else
                {
                    me.Return(lhs.Structure != rhs.Structure);
                }
                break;

            case "logical_and":
                me.Return(lhs.Bool && rhs.Bool);
                break;

            case "logical_or":
                me.Return(lhs.Bool || rhs.Bool);
                break;

            default:
                throw new NotSupportedException();
            }
        }
Exemple #27
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            yield return(NewInterrupt("exit", null));

            yield break;
        }
Exemple #28
0
 abstract protected IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me);
Exemple #29
0
        protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
        {
            yield return(NewInterrupt("Unsupported node type : " + Type, this));

            throw new NotImplementedException();
        }
Exemple #30
0
 protected override IEnumerable <IEnumerable <Interruption> > InnerExecute(Return.ReturnSetter me)
 {
     Mediator.Instance.ExecutingNameSpace = Variable.Root;
     yield return(ChildID[0].Execute());
 }