Пример #1
0
        private static Atom LoadEval(Atom args, Context context)
        {
            if (args == null)
            {
                Console.WriteLine("load-eval requires string parameter!!!");
                return(Atom.FALSE);
            }

            Atom atom = args.atom;

            if (atom.type != AtomType.String)
            {
                Console.WriteLine("load-eval requires string parameter!!!");
                return(Atom.FALSE);
            }

            string name = atom.value as string;

            if (string.IsNullOrEmpty(name))
            {
                Console.WriteLine("load-eval requires string parameter!!!");
                return(Atom.FALSE);
            }

            string path  = FSUtils.FindFile(name);
            string raw   = File.ReadAllText(path);
            var    nodes = BombardoLangClass.Parse(raw);

            foreach (var node in nodes)
            {
                try
                {
                    Evaluator.Evaluate(node, BombardoLangClass.Global);
                }
                catch (Exception exc)
                {
                    Console.WriteLine(exc.ToString());
                }
            }

            return(null);
        }
Пример #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);
        }