Ejemplo n.º 1
0
        public static void Test()
        {
            Assembly a;
            var      bs = BinarySerializerWithString.Create();

            using (var s = Streams.OpenReadable("Assembly.bin"))
            {
                a = bs.Read <Assembly>(s);
            }

            var vc = new VariableContext <int>();

            foreach (var m in a.Modules)
            {
                foreach (var f in m.Functions)
                {
                    vc.Replace(f.Name, f);
                }
            }

            var c = new Calculation(a);

            Trace.Assert(c.Character.GetUpgradeExperience(1, 2) == 2);

            TestBasic(vc);

            TestInteractive(vc);
        }
Ejemplo n.º 2
0
        public static void TestInteractive(VariableContext <int> vc)
        {
            while (true)
            {
                Console.Write("> ");
                var OutputStart = "> ".Length;
                var Line        = "";
                while (true)
                {
                    var TempLine = Console.ReadLine();
                    if (TempLine.EndsWith(@"\"))
                    {
                        Line += TempLine.Substring(0, TempLine.Length - 1);
                    }
                    else
                    {
                        Line += TempLine;
                        break;
                    }
                }
                if (Line == "exit")
                {
                    break;
                }
                try
                {
                    {
                        var m = rAssignment.Match(Line);
                        if (m.Success)
                        {
                            var Identifier = m.Result("${Identifier}");
                            var Expr       = m.Result("${Expr}");
                            OutputStart += Line.Length - Expr.Length;

                            var v = Evaluate(vc, Expr);
                            vc.Replace(Identifier, v);
                            Console.WriteLine(ToString(v));
                            continue;
                        }
                    }
                    {
                        var m = rFunctionDefinition.Match(Line);
                        if (m.Success)
                        {
                            var Signature = m.Result("${Signature}");
                            var Expr      = m.Result("${Expr}");
                            var p         = new VariableProviderCombiner <int>(vc, new ExpressionRuntimeProvider <int>());
                            var rs        = ExpressionParser.ParseSignature(Signature);
                            OutputStart += Line.Length - Expr.Length;
                            var rb = ExpressionParser.ParseBody(p, rs.Declaration, Expr);
                            var r  = new ExpressionParserResult
                            {
                                Definition = new FunctionDef
                                {
                                    Name        = rs.Declaration.Name,
                                    Parameters  = rs.Declaration.Parameters,
                                    ReturnValue = rs.Declaration.ReturnValue,
                                    Body        = rb.Body
                                },
                                Positions = rb.Positions,
                                TypeDict  = rb.TypeDict
                            };
                            vc.Replace(r.Definition.Name, r.Definition);
                            Console.WriteLine(rs.Declaration.Name);
                            continue;
                        }
                    }
                    {
                        var m = rDelete.Match(Line);
                        if (m.Success)
                        {
                            var Identifier = m.Result("${Identifier}");
                            var Overloads  = vc.GetOverloads(Identifier);
                            foreach (var o in Overloads)
                            {
                                vc.TryRemove(Identifier, o.ParameterTypes);
                                String t;
                                if (o.ParameterTypes == null)
                                {
                                    t = o.ReturnType.ToString();
                                }
                                else
                                {
                                    t = String.Join("->", o.ParameterTypes.Concat(new PrimitiveType[] { o.ReturnType }).Select(p => p.ToString()).ToArray());
                                }
                                Console.WriteLine(String.Format("{0}:{1} deleted.", Identifier, t));
                            }
                            continue;
                        }
                    }
                    {
                        var o = Evaluate(vc, Line);
                        Console.WriteLine(ToString(o));
                    }
                }
                catch (InvalidSyntaxException ex)
                {
                    if (ex.Range.OnSome)
                    {
                        var r = ex.Range.Value.Range;
                        if (r.OnSome)
                        {
                            var Start = r.Value.Start.CharIndex;
                            var End   = r.Value.End.CharIndex;
                            var s     = new String(' ', OutputStart + Start) + new String('~', End - Start);
                            Console.WriteLine(s);
                        }
                    }
                    Console.WriteLine(ex.Message);
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ExceptionInfo.GetExceptionInfo(ex));
                }
            }
        }