示例#1
0
        internal void LoadLibrary(string path)
        {
            _7sLibrary library = _7sLibManager.Load(path);

            if (library != null)
            {
                var res = lexer.Tokenize(library.Content);
                if (res == null || res.IsError)
                {
                    WriteLineColor("Error lexing library!", Red);
                    return;
                }
                var tokens = res.Tokens;
                for (int i = 0; i < tokens.Count; i++)
                {
                    TokenList expression = GetExpression(tokens, ref i);
                    if (IsFunctionDef(expression))
                    {
                        string   expr = GetExpressionToToken(expression, LBRACE);
                        string[] args = GetArgs(expr).Replace(" ", string.Empty).Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
                        int      end  = GetClosingBrace(tokens, i, out _, out string inside);
                        userFunctions.Add(expression[1].Value, new FunctionDefinition(inside, this, args));
                        i = end;
                    }
                }
            }
            else
            {
                WriteLineColor("Library could not be loaded!", Red);
                return;
            }
        }
示例#2
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);
            }
        }