Example #1
0
        public override InterpreterResult Interpret(InterpreterEnvironment Env)
        {
            InterpreterVariable variable = null;

            if (Variable.IsLast)
            {
                if (Env.CurrentContext.LastVariable == null)
                {
                    throw new InterpreterException("Invalid use of " + Variable.T.Value, Variable.T);
                }
                variable = Env.CurrentContext.LastVariable;
            }
            else if (Env.CurrentContext.VariableExists(Variable.Name))
            {
                variable = Env.CurrentContext.GetVariable(Variable.Name);
            }
            else
            {
                variable = new InterpreterVariable(Variable.Name, InterpreterVariableType.Undefined);
                Env.CurrentContext.AddVariable(variable);
            }
            InterpreterResult result = Value.Interpret(Env);

            variable.Type  = result.Type;
            variable.Value = result.Value;
            return(new InterpreterResult()
            {
                Value = variable.Value, Type = result.Type
            });
        }
Example #2
0
        public override InterpreterResult Interpret(InterpreterEnvironment Env)
        {
            InterpreterVariable variable = null;

            if (Variable.IsLast)
            {
                if (Env.CurrentContext.LastVariable != null)
                {
                    variable = Env.CurrentContext.LastVariable;
                }
                else
                {
                    throw new InterpreterException("Invalid use of " + Variable.T.Value, Variable.T);
                }
            }
            else if (Env.CurrentContext.VariableExists(Variable.Name))
            {
                variable = Env.CurrentContext.GetVariable(Variable.Name);
            }
            else
            {
                variable = new InterpreterVariable(Variable.Name, InterpreterVariableType.String);
                Env.CurrentContext.AddVariable(variable);
            }
            if (variable.Type != InterpreterVariableType.String)
            {
                variable.Type = InterpreterVariableType.String;
            }
            variable.Value = Console.ReadLine();
            return(new InterpreterResult()
            {
                Type = InterpreterVariableType.Null
            });
        }
        public override InterpreterResult Interpret(InterpreterEnvironment Env)
        {
            InterpreterResult lhsResult = LHS.Interpret(Env);
            InterpreterResult rhsResult = RHS.Interpret(Env);

            if (lhsResult.Type == InterpreterVariableType.Undefined || rhsResult.Type == InterpreterVariableType.Undefined ||
                lhsResult.Value == null || rhsResult.Value == null)
            {
                throw new InterpreterException("Unable to divide mysterious", T);
            }
            decimal?lhs = InterpreterVariable.GetNumericValueFor(lhsResult.Value);
            decimal?rhs = InterpreterVariable.GetNumericValueFor(rhsResult.Value);

            if (!lhs.HasValue || !rhs.HasValue)
            {
                throw new InterpreterException("Unable to divide mysterious", T);
            }
            if (rhs.Value == 0)
            {
                throw new InterpreterException("Divide by zero error", T);
            }
            return(new InterpreterResult()
            {
                Type = InterpreterVariableType.Numeric, Value = lhs.Value / rhs.Value
            });
        }
Example #4
0
 public override InterpreterResult Interpret(InterpreterEnvironment Env)
 {
     if (IsLast)
     {
         if (Env.CurrentContext.LastVariable != null)
         {
             InterpreterVariable v = Env.CurrentContext.LastVariable;
             return(new InterpreterResult()
             {
                 Type = v.Type, Value = v.Value
             });
         }
         throw new InterpreterException("Invalid use of " + T.Value, T);
     }
     if (Env.CurrentContext.VariableExists(Name))
     {
         InterpreterVariable v = Env.CurrentContext.GetVariable(Name);
         return(new InterpreterResult()
         {
             Value = v.Value, Type = v.Type
         });
     }
     return(new InterpreterResult()
     {
         Type = InterpreterVariableType.Undefined
     });
 }
        public override InterpreterResult Interpret(InterpreterEnvironment Env)
        {
            object value = null;

            if (ToSay is VariableParseNode)
            {
                VariableParseNode   variableNode = ToSay as VariableParseNode;
                InterpreterVariable variable     = null;
                if (variableNode.IsLast)
                {
                    if (Env.CurrentContext.LastVariable != null)
                    {
                        variable = Env.CurrentContext.LastVariable;
                    }
                    else
                    {
                        throw new InterpreterException("Invalid use of " + variableNode.T.Value, variableNode.T);
                    }
                }
                else if (Env.CurrentContext.VariableExists(variableNode.Name))
                {
                    variable = Env.CurrentContext.GetVariable(variableNode.Name);
                }
                else
                {
                    Console.WriteLine("mysterious");
                    return(new InterpreterResult());
                }
                if (variable.Type == InterpreterVariableType.Null)
                {
                    Console.WriteLine("null");
                    return(new InterpreterResult());
                }
                if (variable.Type == InterpreterVariableType.Undefined)
                {
                    Console.WriteLine("mysterious");
                    return(new InterpreterResult());
                }
                value = variable.Value;
            }
            else
            {
                InterpreterResult result = ToSay.Interpret(Env);
                if (result.Type == InterpreterVariableType.Null)
                {
                    Console.WriteLine("null");
                }
                else if (result.Type == InterpreterVariableType.Undefined)
                {
                    Console.WriteLine("mysterious");
                }
                else
                {
                    value = result.Value;
                }
            }
            Console.WriteLine(value);
            return(new InterpreterResult());
        }
        public override InterpreterResult Interpret(InterpreterEnvironment Env)
        {
            InterpreterVariable variable = null;

            if (Variable.IsLast)
            {
                if (Env.CurrentContext.LastVariable == null)
                {
                    throw new InterpreterException("Invalid use of " + Variable.T.Value, Variable.T);
                }
                variable = Env.CurrentContext.LastVariable;
            }
            else if (Env.CurrentContext.VariableExists(Variable.Name))
            {
                variable = Env.CurrentContext.GetVariable(Variable.Name);
            }
            else
            {
                variable = new InterpreterVariable(Variable.Name, InterpreterVariableType.Undefined);
                Env.CurrentContext.AddVariable(variable);
            }
            variable.Type = InterpreterVariableType.String;
            string           value = "";
            List <ParseNode> words = Words.GetNodes();

            foreach (ParseNode node in words)
            {
                WordParseNode word = node as WordParseNode;
                value += word.Text + " ";
            }
            if (value.Length > 0)
            {
                value = value.Substring(0, value.Length - 1);
            }
            variable.Value = value;
            return(new InterpreterResult());
        }
Example #7
0
        public override InterpreterResult Interpret(InterpreterEnvironment Env)
        {
            InterpreterResult lhsResult = LHS.Interpret(Env);
            InterpreterResult rhsResult = RHS.Interpret(Env);

            InterpreterResult trueResult = new InterpreterResult()
            {
                Value = true, Type = InterpreterVariableType.Boolean
            };
            InterpreterResult falseResult = new InterpreterResult()
            {
                Value = false, Type = InterpreterVariableType.Boolean
            };

            if (lhsResult.Value == null && rhsResult.Value == null)
            {
                return(trueResult);
            }

            if (lhsResult.Type == InterpreterVariableType.NaN && rhsResult.Type == InterpreterVariableType.NaN)
            {
                return(trueResult);
            }

            if (lhsResult.Type == InterpreterVariableType.Null && rhsResult.Type == InterpreterVariableType.Null)
            {
                return(trueResult);
            }

            if (lhsResult.Type == InterpreterVariableType.Undefined && rhsResult.Type == InterpreterVariableType.Undefined)
            {
                return(trueResult);
            }

            if (lhsResult.Type == InterpreterVariableType.Numeric && rhsResult.Type == InterpreterVariableType.Numeric)
            {
                if ((lhsResult.Value as decimal?).Value == (rhsResult.Value as decimal?).Value)
                {
                    return(trueResult);
                }
                return(falseResult);
            }

            if (lhsResult.Type == InterpreterVariableType.String && lhsResult.Type == InterpreterVariableType.String)
            {
                if (lhsResult.Value as string == rhsResult.Value as string)
                {
                    return(trueResult);
                }
                return(falseResult);
            }

            decimal?lhsNumVal = InterpreterVariable.GetNumericValueFor(lhsResult.Value);
            decimal?rhsNumVal = InterpreterVariable.GetNumericValueFor(rhsResult.Value);

            if (lhsNumVal.HasValue && rhsNumVal.HasValue && lhsNumVal.Value == rhsNumVal.Value)
            {
                return(trueResult);
            }
            return(falseResult);
        }
        public override InterpreterResult Interpret(InterpreterEnvironment Env)
        {
            InterpreterVariable variable = null;

            if (Variable.IsLast)
            {
                if (Env.CurrentContext.LastVariable == null)
                {
                    throw new InterpreterException(Variable.Name + " is mysterious", T);
                }
                variable = Env.CurrentContext.LastVariable;
            }
            else if (!Env.CurrentContext.VariableExists(Variable.Name))
            {
                throw new InterpreterException(Variable.Name + " is mysterious", T);
            }
            else
            {
                variable = Env.CurrentContext.GetVariable(Variable.Name);
            }

            if (variable.Type == InterpreterVariableType.Undefined)
            {
                throw new InterpreterException(Variable.Name + " is mysterious", T);
            }

            if (Up)
            {
                if (variable.Type == InterpreterVariableType.Boolean)
                {
                    if ((variable.Value as bool?).Value)
                    {
                        variable.Type  = InterpreterVariableType.Numeric;
                        variable.Value = 2;
                    }
                    else
                    {
                        variable.Type  = InterpreterVariableType.Numeric;
                        variable.Value = 1;
                    }
                }
                else if (variable.Type == InterpreterVariableType.Null)
                {
                    variable.Type  = InterpreterVariableType.Numeric;
                    variable.Value = 1;
                }
                else if (variable.Type == InterpreterVariableType.Object || variable.Type == InterpreterVariableType.String)
                {
                    variable.Type  = InterpreterVariableType.Numeric;
                    variable.Value = double.NaN;
                }
                else
                {
                    variable.Value = (variable.Value as decimal?).Value + 1;
                }
            }
            else
            {
                if (variable.Type == InterpreterVariableType.Boolean)
                {
                    if ((variable.Value as bool?).Value)
                    {
                        variable.Type  = InterpreterVariableType.Numeric;
                        variable.Value = 0;
                    }
                    else
                    {
                        variable.Type  = InterpreterVariableType.Numeric;
                        variable.Value = -1;
                    }
                }
                else if (variable.Type == InterpreterVariableType.Null)
                {
                    variable.Type  = InterpreterVariableType.Numeric;
                    variable.Value = -1;
                }
                else if (variable.Type == InterpreterVariableType.Object || variable.Type == InterpreterVariableType.String)
                {
                    variable.Type  = InterpreterVariableType.Numeric;
                    variable.Value = double.NaN;
                }
                else
                {
                    variable.Value = (variable.Value as decimal?).Value - 1;
                }
            }
            return(new InterpreterResult()
            {
                Type = variable.Type, Value = variable.Value
            });
        }
Example #9
0
        public override InterpreterResult Interpret(InterpreterEnvironment Env)
        {
            InterpreterResult lhsResult = LHS.Interpret(Env);
            InterpreterResult rhsResult = RHS.Interpret(Env);

            //string concatenation
            if (lhsResult.Type == InterpreterVariableType.String || rhsResult.Type == InterpreterVariableType.String)
            {
                string lhsString = "";
                string rhsString = "";
                if (lhsResult.Type == InterpreterVariableType.Undefined)
                {
                    lhsString = "mysterious";
                }
                else if (lhsResult.Type == InterpreterVariableType.Null)
                {
                    rhsString = "null";
                }
                else if (lhsResult.Type == InterpreterVariableType.NaN)
                {
                    lhsString = "NaN";
                }
                else
                {
                    lhsString = lhsResult.Value.ToString();
                }

                if (rhsResult.Type == InterpreterVariableType.Undefined)
                {
                    rhsString = "mysterious";
                }
                else if (rhsResult.Type == InterpreterVariableType.Null)
                {
                    rhsString = "null";
                }
                else if (rhsResult.Type == InterpreterVariableType.NaN)
                {
                    rhsString = "NaN";
                }
                else
                {
                    rhsString = rhsResult.Value.ToString();
                }
                return(new InterpreterResult()
                {
                    Value = lhsString + rhsString, Type = InterpreterVariableType.String
                });
            }

            if (lhsResult.Type == InterpreterVariableType.Undefined || rhsResult.Type == InterpreterVariableType.Undefined ||
                lhsResult.Value == null || rhsResult.Value == null)
            {
                throw new InterpreterException("Unable to add mysterious", T);
            }
            decimal?lhs = InterpreterVariable.GetNumericValueFor(lhsResult.Value);
            decimal?rhs = InterpreterVariable.GetNumericValueFor(rhsResult.Value);

            if (!lhs.HasValue || !rhs.HasValue)
            {
                throw new InterpreterException("Unable to add mysterious", T);
            }
            return(new InterpreterResult()
            {
                Type = InterpreterVariableType.Numeric, Value = lhs.Value + rhs.Value
            });
        }
        public override InterpreterResult Interpret(InterpreterEnvironment Env)
        {
            InterpreterVariableType typeOfAssignment = InterpreterVariableType.Null;
            object value = null;

            if (Value is ParseNodeList)
            {
                typeOfAssignment = InterpreterVariableType.Numeric;
                string           strValue  = "";
                List <ParseNode> wordNodes = (Value as ParseNodeList).GetNodes();
                foreach (ParseNode node in wordNodes)
                {
                    WordParseNode wordNode = node as WordParseNode;

                    if (wordNode.Text == ".")
                    {
                        strValue += ".";
                        continue;
                    }

                    string word = "";

                    foreach (char c in wordNode.Text)
                    {
                        if (CharactersToIgnore.Contains(c))
                        {
                            continue;
                        }
                        word += c;
                    }
                    strValue += word.Length % 10;
                }
                decimal val = 0;
                if (!decimal.TryParse(strValue, out val))
                {
                    throw new InterpreterException("Invalid poetic number literl", T);
                }
                value = val;
            }
            else
            {
                InterpreterResult rhsResult = Value.Interpret(Env);
                typeOfAssignment = rhsResult.Type;
                value            = rhsResult.Value;
            }
            InterpreterVariable variable = null;

            if (Variable.IsLast)
            {
                if (Env.CurrentContext.LastVariable != null)
                {
                    variable = Env.CurrentContext.LastVariable;
                }
                else
                {
                    throw new InterpreterException("Invalid use of " + Variable.T.Value, Variable.T);
                }
            }
            else if (Env.CurrentContext.VariableExists(Variable.Name))
            {
                variable = Env.CurrentContext.GetVariable(Variable.Name);
            }
            else
            {
                variable = new InterpreterVariable(Variable.Name, typeOfAssignment);
                Env.CurrentContext.AddVariable(variable);
            }
            if (variable.Type != typeOfAssignment)
            {
                variable.Type = typeOfAssignment;
            }
            variable.Value = value;
            return(new InterpreterResult()
            {
                Type = InterpreterVariableType.Null
            });
        }