示例#1
0
        public void Import(string libPath)
        {
#if DEBUG
            ColorConsoleMethods.WriteLineColor($"Importing: \"{libPath}\"", ConsoleColor.Magenta);
#endif
            InterpreterState state = this;
            SysLibrary[]     libs  = SysLibrary.GetAllLibraries();
            if (libs.Any(l => l.GetName() == libPath))
            {
                libs.First(l => l.GetName() == libPath).Import(ref state);
                return;
            }
            if (!libPath.EndsWith(".7slib"))
            {
                libPath += ".7slib";
            }
            if (!File.Exists(libPath))
            {
                throw new InterpreterException($"Could not find library \"{libPath}\"");
            }
            _7sLibrary     lib   = _7sLibManager.Load(libPath);
            UserFunction[] funcs = new Interpreter().GetFuncsFromCode(lib.Content, ref state);
            foreach (UserFunction f in funcs)
            {
                UserFuncs.Add(f);
            }
        }
示例#2
0
        internal UserFunction[] GetFuncsFromCode(string code, ref InterpreterState state)
        {
            // Convert code to tokens
            LexerResult <TokenType> result = lexer.Tokenize(code);

            // Error out if lexer fails
            if (result.IsError)
            {
                throw new InterpreterException($"Parsing Error: {result.Error}");
            }

            Queue <Token <TokenType> > tokens = new Queue <Token <TokenType> >(result.Tokens);

            RootNode node = BuildTree <RootNode>(tokens, ref state, true);
            List <FunctionDefinitionNode> funcs = new List <FunctionDefinitionNode>();

            GetFuncsFromTree(node, ref funcs);

            return(funcs.Select(f => new UserFunction(f.Name, f.Args, f.Children)).ToArray());
        }
示例#3
0
 public void Run(ref InterpreterState state, params object[] args)
 {
     if (args.Length != argNames.Length)
     {
         throw new InterpreterException($"Function {Name} expects {argNames.Length} args, but got {args.Length}");
     }
     state.InsideFunction = true;
     state.PushScope();
     state.FuncParams = Enumerable.Range(0, argNames.Length)
                        .Select(i => Tuple.Create(argNames[i], args[i]))
                        .ToDictionary(t => t.Item1, t => t.Item2);
     foreach (Node n in code)
     {
         state.Location = n.linePosition;
         n.Run(ref state);
         if (state.ExitFunc)                 // If return is used
         {
             break;
         }
     }
     state.PopScope();
     state.InsideFunction = false;
     state.FuncParams     = null;
 }
示例#4
0
        private T BuildTree <T>(Queue <Token <TokenType> > tokens, ref InterpreterState state, bool getNodesOnly = false) where T : BlockNode, new()
        {
            T root = new T();             // Create a new node

            while (tokens.Count > 0)
            {
                TokenList expr = new TokenList();
                // If we hit EOS we are done
                Token <TokenType> token = tokens.Dequeue();
                if (token == null || token.IsEOS)
                {
#if DEBUG
                    WriteLineColor("EOS", Cyan);
#endif
                    break;
                }
                // While i is in bounds of tokens AND we have not hit an "expression ending" (; or {)
                while (tokens.Count() > 0 && !token.IsEnding() && !token.IsEOS)
                {
                    expr.Add(token);
                    token = tokens.Dequeue();
                }
                expr.Add(token);
                // If expression starts with }, remove it
                if (expr.Count > 0 && expr[0].TokenID == RBRACE)
                {
                    expr = expr.Skip(1).ToList();
                }
                // Dont run empty expression
                if (expr.Count == 0)
                {
                    continue;
                }
#if DEBUG
                expr.TokenDump();
#endif
                LexerPosition exprPos = expr[0].Position.Adjust();
                state.Location = exprPos;
                bool built = false;
                // Loop over every expression type
                foreach (ExpressionType type in Enum.GetValues(typeof(ExpressionType)).Cast <ExpressionType>())
                {
                    if (type.Matches(expr))
                    {
                        Node node = type.GetNode(expr, exprPos, ref state);
                        if (type.IsBlock())
                        {
                            BlockNode tmp = (BlockNode)node;
                            Queue <Token <TokenType> > block = GetBlock(ref tokens);
                            BuildTree <RootNode>(block, ref state, getNodesOnly).Children.ForEach(c => tmp.Add(c));
                            node = tmp;
                        }
                        if (type.WillRun() || getNodesOnly)
                        {
                            root.Add(node);
                        }
                        if (!getNodesOnly)
                        {
                            if (node is FunctionDefinitionNode funcDefNode)
                            {
                                state.UserFuncs.Add(new UserFunction(
                                                        funcDefNode.Name,
                                                        funcDefNode.Args,
                                                        funcDefNode.Children
                                                        ));
                            }
                            if (node is ImportNode impNode)
                            {
                                state.Import(impNode.Library);
                            }
                        }
                        built = true;
                        break;
                    }
                }
                if (!built)                 // Unknown Expression
                {
                    throw new InterpreterException($"Unknown expression at {exprPos}");
                }
            }

            return(root);
        }
示例#5
0
 public static void Init(ref InterpreterState state)
 {
     state.Variables.Push(new Dictionary <string, object>());
     state.evaluator.PreEvaluateFunction += state.Evaluator_PreEvaluateFunction;
     state.evaluator.PreEvaluateVariable += state.Evaluator_PreEvaluateVariable;
     state.evaluator.OptionInlineNamespacesEvaluationActive = false;
     state.evaluator.OptionNumberParsingDecimalSeparator    = ".";
     state.evaluator.Assemblies.Clear();
     state.evaluator.Variables.Clear();
     state.evaluator.Variables.Add("PI", Math.PI);
     state.evaluator.Variables.Add("E", Math.E);
     state.evaluator.Variables.Add("BLACK", (int)ConsoleColor.Black);
     state.evaluator.Variables.Add("BLUE", (int)ConsoleColor.Blue);
     state.evaluator.Variables.Add("CYAN", (int)ConsoleColor.Cyan);
     state.evaluator.Variables.Add("DARK_BLUE", (int)ConsoleColor.DarkBlue);
     state.evaluator.Variables.Add("DARK_CYAN", (int)ConsoleColor.DarkCyan);
     state.evaluator.Variables.Add("DARK_GRAY", (int)ConsoleColor.DarkGray);
     state.evaluator.Variables.Add("DARK_GREEN", (int)ConsoleColor.DarkGreen);
     state.evaluator.Variables.Add("DARK_MAGENTA", (int)ConsoleColor.DarkMagenta);
     state.evaluator.Variables.Add("DARK_RED", (int)ConsoleColor.DarkRed);
     state.evaluator.Variables.Add("DARK_YELLOW", (int)ConsoleColor.DarkYellow);
     state.evaluator.Variables.Add("GRAY", (int)ConsoleColor.Gray);
     state.evaluator.Variables.Add("GREEN", (int)ConsoleColor.Green);
     state.evaluator.Variables.Add("MAGENTA", (int)ConsoleColor.Magenta);
     state.evaluator.Variables.Add("RED", (int)ConsoleColor.Red);
     state.evaluator.Variables.Add("WHITE", (int)ConsoleColor.White);
     state.evaluator.Variables.Add("YELLOW", (int)ConsoleColor.Yellow);
     state.Functions.Add(new _7sFunction("write", new Dictionary <int, Delegate>()
     {
         { 1, new Action <object>(SysFunctions.Write) }
     }));
     state.Functions.Add(new _7sFunction("writeraw", new Dictionary <int, Delegate>()
     {
         { 1, new Action <object>(SysFunctions.WriteRaw) }
     }));
     state.Functions.Add(new _7sFunction("read", new Dictionary <int, Delegate>()
     {
         { 0, new Func <string>(SysFunctions.Read) }
     }));
     state.Functions.Add(new _7sFunction("getLoopIndex", new Dictionary <int, Delegate>()
     {
         { 0, new Func <int>(state.GetFirstLoopIndex) },
         { 1, new Func <int, int>(state.GetLoopIndex) }
     }));
     state.Functions.Add(new _7sFunction("sin", new Dictionary <int, Delegate>()
     {
         { 1, new Func <double, double>(SysFunctions.Sin) }
     }));
     state.Functions.Add(new _7sFunction("cos", new Dictionary <int, Delegate>()
     {
         { 1, new Func <double, double>(SysFunctions.Cos) }
     }));
     state.Functions.Add(new _7sFunction("tan", new Dictionary <int, Delegate>()
     {
         { 1, new Func <double, double>(SysFunctions.Tan) }
     }));
     state.Functions.Add(new _7sFunction("asin", new Dictionary <int, Delegate>()
     {
         { 1, new Func <double, double>(SysFunctions.Asin) }
     }));
     state.Functions.Add(new _7sFunction("acos", new Dictionary <int, Delegate>()
     {
         { 1, new Func <double, double>(SysFunctions.Acos) }
     }));
     state.Functions.Add(new _7sFunction("atan", new Dictionary <int, Delegate>()
     {
         { 1, new Func <double, double>(SysFunctions.Atan) }
     }));
     state.Functions.Add(new _7sFunction("atan2", new Dictionary <int, Delegate>()
     {
         { 2, new Func <double, double, double>(SysFunctions.Atan2) }
     }));
     state.Functions.Add(new _7sFunction("deg2rad", new Dictionary <int, Delegate>()
     {
         { 1, new Func <double, double>(SysFunctions.Deg2Rad) }
     }));
     state.Functions.Add(new _7sFunction("rad2deg", new Dictionary <int, Delegate>()
     {
         { 1, new Func <double, double>(SysFunctions.Rad2Deg) }
     }));
     state.Functions.Add(new _7sFunction("sleep", new Dictionary <int, Delegate>()
     {
         { 1, new Action <int>(SysFunctions.Sleep) }
     }));
     state.Functions.Add(new _7sFunction("len", new Dictionary <int, Delegate>()
     {
         { 1, new Func <object, int>(SysFunctions.Len) }
     }));
     state.Functions.Add(new _7sFunction("chars", new Dictionary <int, Delegate>()
     {
         { 1, new Func <string, object[]>(SysFunctions.Chars) }
     }));
     state.Functions.Add(new _7sFunction("toString", new Dictionary <int, Delegate>()
     {
         { 1, new Func <object, string>(SysFunctions._7sToString) }
     }));
     state.Functions.Add(new _7sFunction("arrayAdd", new Dictionary <int, Delegate>()
     {
         { 2, new Func <object[], object, object[]>(SysFunctions.ArrayAdd) }
     }));
     state.Functions.Add(new _7sFunction("arrayRemove", new Dictionary <int, Delegate>()
     {
         { 2, new Func <object[], int, object[]>(SysFunctions.ArrayRemove) }
     }));
     state.Functions.Add(new _7sFunction("fgColor", new Dictionary <int, Delegate>()
     {
         { 1, new Action <int>(SysFunctions.FgColor) }
     }));
     state.Functions.Add(new _7sFunction("bgColor", new Dictionary <int, Delegate>()
     {
         { 1, new Action <int>(SysFunctions.BgColor) }
     }));
     state.Functions.Add(new _7sFunction("resetColor", new Dictionary <int, Delegate>()
     {
         { 0, new Action(SysFunctions.ResetColor) }
     }));
     state.Functions.Add(new _7sFunction("sqrt", new Dictionary <int, Delegate>()
     {
         { 1, new Func <double, double>(SysFunctions.Sqrt) }
     }));
     state.Functions.Add(new _7sFunction("pow", new Dictionary <int, Delegate>()
     {
         { 2, new Func <double, double, double>(SysFunctions.Pow) }
     }));
     state.Functions.Add(new _7sFunction("double", new Dictionary <int, Delegate>()
     {
         { 1, new Func <object, double>(SysFunctions.Double) }
     }));
 }