コード例 #1
0
ファイル: Program.cs プロジェクト: LukaHorvat/Hype
        static void Main(string[] args)
        {
            if (args.Length != 0)
            {
                using (StreamReader reader = new StreamReader(args[0]))
                {
                    var parser = new Parser();
                    var output = parser.BuildExpressionTree(parser.Tokenize(parser.Split(reader.ReadToEnd())), 0);

                    var interpreter = new Interpreter(output);
                    interpreter.LoadLibrary(StandardLibrary.Load);
                    var timer = Stopwatch.StartNew();
                    interpreter.Run();
                    timer.Stop();
                    Console.WriteLine("Time spent: " + timer.ElapsedMilliseconds);
                }
            }
            else
            {
                using (StreamReader reader = new StreamReader("samples/sandbox.hy"))
                {
                    var parser = new Parser();
                    var output = parser.BuildExpressionTree(parser.Tokenize(parser.Split(reader.ReadToEnd())), 0);

                    var interpreter = new Interpreter(output);
                    interpreter.LoadLibrary(StandardLibrary.Load);
                    var timer = Stopwatch.StartNew();
                    interpreter.Run();
                    timer.Stop();
                    Console.WriteLine("Time spent: " + timer.ElapsedMilliseconds);
                }
            }
            Console.WriteLine("Done");
            Console.ReadKey();
        }
コード例 #2
0
ファイル: UnitTest1.cs プロジェクト: LukaHorvat/Hype
        public void Fact()
        {
            using (StreamReader reader = new StreamReader(SamplesPath + "fact.hy"))
            {
                var parser = new Parser();
                var output = parser.BuildExpressionTree(parser.Tokenize(parser.Split(reader.ReadToEnd())), 0);

                var interpreter = new Interpreter(output);
                interpreter.LoadLibrary(StandardLibrary.Load);
                interpreter.Run();

                Assert.IsInstanceOfType(interpreter.CurrentScopeNode.LookupNoRef("acc"), typeof(Number));
                Assert.AreEqual(2004310016, (interpreter.CurrentScopeNode.LookupNoRef("acc") as Number).Num);
            }
        }
コード例 #3
0
ファイル: UnitTest1.cs プロジェクト: LukaHorvat/Hype
        public void Bool()
        {
            using (StreamReader reader = new StreamReader(SamplesPath + "bool.hy"))
            {
                var parser = new Parser();
                var output = parser.BuildExpressionTree(parser.Tokenize(parser.Split(reader.ReadToEnd())), 0);

                var interpreter = new Interpreter(output);
                interpreter.LoadLibrary(StandardLibrary.Load);
                interpreter.Run();

                Assert.IsInstanceOfType(interpreter.CurrentScopeNode.LookupNoRef("a"), typeof(Hype.SL.Global.Boolean));
                Assert.IsInstanceOfType(interpreter.CurrentScopeNode.LookupNoRef("b"), typeof(Number));
                Assert.IsInstanceOfType(interpreter.CurrentScopeNode.LookupNoRef("c"), typeof(Hype.SL.Global.Boolean));
                Assert.IsInstanceOfType(interpreter.CurrentScopeNode.LookupNoRef("d"), typeof(Hype.SL.Global.Boolean));
                Assert.AreEqual(false, (interpreter.CurrentScopeNode.LookupNoRef("a") as Hype.SL.Global.Boolean).Bool);
                Assert.AreEqual(6, (interpreter.CurrentScopeNode.LookupNoRef("b") as Number).Num);
                Assert.AreEqual(true, (interpreter.CurrentScopeNode.LookupNoRef("c") as Hype.SL.Global.Boolean).Bool);
                Assert.AreEqual(true, (interpreter.CurrentScopeNode.LookupNoRef("d") as Hype.SL.Global.Boolean).Bool);
            }
        }
コード例 #4
0
ファイル: UnitTest1.cs プロジェクト: LukaHorvat/Hype
        public void Strings()
        {
            using (StreamReader reader = new StreamReader(SamplesPath + "strings.hy"))
            {
                var parser = new Parser();
                var output = parser.BuildExpressionTree(parser.Tokenize(parser.Split(reader.ReadToEnd())), 0);

                var interpreter = new Interpreter(output);
                interpreter.LoadLibrary(StandardLibrary.Load);
                interpreter.Run();

                Assert.IsInstanceOfType(interpreter.CurrentScopeNode.LookupNoRef("a"), typeof(Hype.SL.Global.String));
                Assert.IsInstanceOfType(interpreter.CurrentScopeNode.LookupNoRef("b"), typeof(Hype.SL.Global.String));
                Assert.AreEqual("testing", (interpreter.CurrentScopeNode.LookupNoRef("a") as Hype.SL.Global.String).Str);
                Assert.AreEqual("strings", (interpreter.CurrentScopeNode.LookupNoRef("b") as Hype.SL.Global.String).Str);
            }
        }
コード例 #5
0
ファイル: ListCache.cs プロジェクト: LukaHorvat/Hype
 public ListCache(Expression exp, Interpreter interpreter)
     : base(Void.Instance)
 {
     this.exp = exp;
     this.interpreter = interpreter;
 }
コード例 #6
0
ファイル: UnitTest1.cs プロジェクト: LukaHorvat/Hype
        public void Scopes()
        {
            using (StreamReader reader = new StreamReader(SamplesPath + "scopes.hy"))
            {
                var parser = new Parser();
                var output = parser.BuildExpressionTree(parser.Tokenize(parser.Split(reader.ReadToEnd())), 0);

                var interpreter = new Interpreter(output);
                interpreter.LoadLibrary(StandardLibrary.Load);

                var writter = new StringWriter();
                var temp = Console.Out;
                Console.SetOut(writter);
                interpreter.Run();
                Console.SetOut(temp);

                var str = writter.ToString();
                Assert.AreEqual(@"0
            1
            2
            3
            4
            Var: i, Type: BlankIdentifier
            ", str);
            }
        }
コード例 #7
0
ファイル: Expression.cs プロジェクト: LukaHorvat/Hype
        public Value Execute(Interpreter interpreter)
        {
            if (Nodes.Count == 0) return Void.Instance;

            Value lastValue = null;

            for (var execNode = Nodes[0]; execNode != null; execNode = execNode.Next)
            {
                if (execNode.Cache == null)
                {
                    GenerateLookupCache(execNode, interpreter);
                }

                var functionList = new List<List<LinkedListNode<Value>>>(numFixities);
                for (int i = 0; i < numFixities; ++i) functionList.Add(new List<LinkedListNode<Value>>());

                var functionStack = new Stack<LinkedListNode<Value>>();
                var currentExecution = new LinkedList<Value>();
                var exceptions = new Queue<FunctionCallException>();

                Value val;
                for (int i = 0; i < execNode.Cache.Count; ++i)
                {
                    //If the original lookup (on block creation) didn't find the identifier, the cache will be null. If it's
                    //still null after another lookup, set the value to a blank identifier.
                    var r = execNode.Cache[i] ?? interpreter.CurrentScopeNode.Lookup(execNode.InnerExpression[i].OriginalToken.Content);
                    if (r == null)
                    {
                        val = new BlankIdentifier(execNode.InnerExpression[i].OriginalToken.Content);
                    }
                    else
                    {
                        val = r.RefValue;
                    }
                    if (val != null)
                    {
                        if (val.Type <= ValueType.ReturnValue) return val;
                        if (val.Type <= ValueType.ProxyValue) val = (val as ProxyValue).Getter();
                        currentExecution.AddLast(val);
                        if (val is Functional)
                        {
                            var fg = (IFunctionGroup)val;
                            functionList[(int)fg.Fixity].Add(currentExecution.Last);
                        }
                        if (val is CodeBlock)
                        {
                            (val as CodeBlock).Expression.GenerateLookupCache(interpreter);
                        }
                    }
                }

                for (int j = functionList.Count - 1; j >= 0; --j) for (int k = functionList[j].Count - 1; k >= 0; --k) functionStack.Push(functionList[j][k]);

                while (functionStack.Count > 0)
                {
                    var funcNode = functionStack.Pop();

                    //In some cases, a function yet to be executed gets eaten by another function
                    //as an argument. In that case we just skip it
                    if (funcNode.List != currentExecution) continue;

                    //In some cases, an object with the type Function might not be an actual function
                    //but a blank identifier or something else. We skip those cases
                    if (!(funcNode.Value is Functional)) continue;

                    if (!(funcNode.Value is ICurryable))
                    {
                        funcNode.Value = new PartialApplication(funcNode.Value as Function);
                    }
                    var func = funcNode.Value as ICurryable;

                    Value res = null;
                    Side side;

                    if (func.Fixity != Fixity.Prefix && funcNode.Previous != null)
                    {
                        side = Side.Left;
                        try
                        {
                            res = func.Apply(funcNode.Previous.Value, Side.Left);
                            if (res == SignatureMismatchValue.Instance) continue;
            #if DEBUG
                            interpreter.Log.Add(LogEntry.Applied(funcNode.Previous.Value, func, res));
                            if (interpreter.HeavyDebug) interpreter.Log.Add(LogEntry.State(this.Copy<Expression>()));
            #endif
                        }
                        catch (FunctionCallException e)
                        {
                            exceptions.Enqueue(e);
            #if DEBUG
                            interpreter.Log.Add(LogEntry.Caught(e));
                            if (interpreter.HeavyDebug) interpreter.Log.Add(LogEntry.State(this.Copy<Expression>()));
            #endif
                            continue;
                        }
                    }
                    else
                    {
                        if (funcNode.Next == null)
                        {
                            side = Side.NoArgument;
                            res = (Value)func;
                        }
                        else
                        {
                            side = Side.Right;
                            try
                            {
                                res = func.Apply(funcNode.Next.Value, Side.Right);
                                if (res == SignatureMismatchValue.Instance) continue;
            #if DEBUG
                                interpreter.Log.Add(LogEntry.Applied(funcNode.Next.Value, func, res));
                                if (interpreter.HeavyDebug) interpreter.Log.Add(LogEntry.State(this.Copy<Expression>()));
            #endif
                            }
                            catch (FunctionCallException e)
                            {
                                exceptions.Enqueue(e);
            #if DEBUG
                                interpreter.Log.Add(LogEntry.Caught(e));
                                if (interpreter.HeavyDebug) interpreter.Log.Add(LogEntry.State(this.Copy<Expression>()));
            #endif
                                continue;
                            }
                        }
                    }
                    if (side == Side.Left)
                    {
                        currentExecution.Remove(funcNode.Previous);
                    }
                    else if (side == Side.Right)
                    {
                        currentExecution.Remove(funcNode.Next);
                    }
                    var node = currentExecution.AddAfter(funcNode, res);
                    currentExecution.Remove(funcNode);
                    if (res.Type <= ValueType.ReturnValue) return res;
                    if (res != func && res.Type.IsSubtypeOf(ValueType.Functional) && currentExecution.Count > 1) functionStack.Push(node);
                }

                if (currentExecution.Count > 1)
                {
                    if (exceptions.Count > 0) throw exceptions.Dequeue();
                    else throw new MultipleValuesLeft();
                }
                if (currentExecution.First.Value.Type == ValueType.FunctionGroup
                    && ((IFunctionGroup)currentExecution.First.Value).Fixity != Fixity.Prefix) return (Value)(currentExecution.First.Value as ICurryable).PrefixApplication;
                lastValue = currentExecution.First();
            }
            return lastValue;
        }
コード例 #8
0
ファイル: Expression.cs プロジェクト: LukaHorvat/Hype
        public void GenerateLookupCache(ExecutionNode node, Interpreter interpreter)
        {
            var items = node.InnerExpression;

            if (node.Cache == null)
            {
                node.Cache = new List<Reference>();
                for (int i = 0; i < items.Count; ++i) node.Cache.Add(null);
            }

            for (int i = 0; i < items.Count; ++i)
            {
                //Fixed references aren't meant to be modified.
                if (node.Cache[i] != null && node.Cache[i].Fixed) continue;

                var tok = items[i].OriginalToken;
                switch (tok.Type)
                {
                    case TokenType.Literal:
                        node.Cache[i] = new Reference(interpreter.ParseLiteral(tok.Content));
                        break;
                    case TokenType.Identifier:
                        node.Cache[i] = interpreter.CurrentScopeNode.Lookup(tok.Content);
                        break;
                    case TokenType.Group:
                        var exp = items[i] as Expression;
                        if (exp.OriginalToken.Content == "{")
                        {
                            var codeblock = new CodeBlock(exp);
                            node.Cache[i] = new Reference(codeblock);
                        }
                        else
                        {
                            exp.GenerateLookupCache(interpreter);
                            exp.FixAllReferences();
                            if (exp.OriginalToken.Content == "[")
                            {
                                node.Cache[i] = new ListCache(exp, interpreter);
                            }
                            else
                            {
                                node.Cache[i] = new ExpressionCache(exp, interpreter);
                            }
                        }
                        break;
                }

            }
        }
コード例 #9
0
ファイル: Expression.cs プロジェクト: LukaHorvat/Hype
        public void GenerateLookupCache(Interpreter interpreter)
        {
            if (Nodes.Count == 0) return;

            for (var execNode = Nodes[0]; execNode != null; execNode = execNode.Next)
            {
                GenerateLookupCache(execNode, interpreter);
            }
        }
コード例 #10
0
ファイル: CodeBlock.cs プロジェクト: LukaHorvat/Hype
 public Value Execute(Interpreter interpreter)
 {
     return Expression.Execute(interpreter);
 }
コード例 #11
0
ファイル: BuiltInFunctions.cs プロジェクト: LukaHorvat/Hype
 public BuiltInFunctions(Interpreter interpreter)
 {
     Interpreter = interpreter;
 }