Exemplo n.º 1
0
        public static BombardoModule ExecuteFile(string filePath, bool catchExceptions = true)
        {
            BombardoModule module;

            if (modules_.TryGetValue(filePath, out module))
            {
                if (module.loading)
                {
                    throw new ArgumentException("reqursive requirement found!");
                }
                return(module);
            }

            module = new BombardoModule(filePath, bombardoLang_);
            modules_.Add(filePath, module);
            modulesStack_.Push(module);

            if (catchExceptions)
            {
                try { ExecuteFile(filePath, module.moduleContext); }
                catch (BombardoException exc) { Console.WriteLine(exc.ToString()); }
            }
            else
            {
                ExecuteFile(filePath, module.moduleContext);
            }

            modulesStack_.Pop();

            return(module);
        }
Exemplo n.º 2
0
        private static Atom Require(Atom args, Context context)
        {
            BombardoModule currentModule = modulesStack_.Peek();
            Atom           path          = args.atom;

            if (path.type != AtomType.String && path.type != AtomType.Symbol)
            {
                throw new ArgumentException("argument must be string or symbol!");
            }
            string file = FSUtils.LookupModuleFile(programPath_, currentModule.currentPath, AllNames.MODULES_FOLDER, (string)path.value);

            if (file == null)
            {
                throw new ArgumentException("file not found!");
            }

            //  Lifting exception up to first file
            BombardoModule module = ExecuteFile(file, false);
            Atom           result = module.GetModuleResult();

            Atom rest = args.next;

            if (rest != null && rest.IsPair())
            {
                Atom command = rest.atom;

                if (!command.IsSymbol())
                {
                    throw new ArgumentException(string.Format("Unexpected symbol '{0}'!", command));
                }

                switch ((string)command.value)
                {
                case AllNames.MODULE_REQUIRE_AS:
                    Atom name = rest.next?.atom;
                    if (name == null || !name.IsSymbol())
                    {
                        throw new ArgumentException(string.Format("Unexpected symbol '{0}'!", name));
                    }
                    context.Define((string)name.value, result);
                    break;

                case AllNames.MODULE_REQUIRE_IMPORT:
                    string[] nameList = CommonUtils.ListToStringArray(rest.next, "REQUIRE");
                    ContextUtils.ImportSymbols((Context)result.value, context, nameList);
                    break;

                case AllNames.MODULE_REQUIRE_IMPORT_ALL:
                    ContextUtils.ImportAllSymbols((Context)result.value, context);
                    break;

                default:
                    throw new ArgumentException(string.Format("Unexpected symbol '{0}'!", command));
                }
            }
            else
            {
                string name = Path.GetFileNameWithoutExtension((string)path.value);
                context.Define(name, result);
            }

            return(result);
        }