예제 #1
0
        public static Atom TableImportAll(Atom args, Context context)
        {
            Atom dict = (Atom)args?.value;

            Context dictionary = GetDictionary(dict);

            ContextUtils.ImportAllSymbols(dictionary, context);

            return(null);
        }
예제 #2
0
        public static Atom Evaluate(Atom atom, Context context)
        {
            if (atom == null)
            {
                return(null);
            }

            if (atom.IsSymbol())
            {
                string name = (string)atom.value;
                return(ContextUtils.Get(context, name));
            }

            if (atom.IsPair())
            {
                Atom head = Evaluate((Atom)atom.value, context);
                Atom args = atom.next;

                if (head == null || !head.IsProcedure())
                {
                    throw new BombardoException(
                              $"List head '{head?.ToString() ?? "null"}' is not a function or lambda!");
                }

                var proc = (Procedure)head.value;

                if (proc.EvalArgs)
                {
                    args = EvaluateList(args, context);
                }

                Atom result = null;
                try
                {
                    result = proc.Apply(args, context);
                }
                catch (BombardoException sexc)
                {
                    sexc.AddSection(atom, 0);
                    throw;
                }
                catch (Exception exc)
                {
                    throw new BombardoException(exc);
                }

                if (proc.EvalResult)
                {
                    result = Evaluate(result, context);
                }
                return(result);
            }

            return(atom);
        }
예제 #3
0
        public static Atom TableImport(Atom args, Context context)
        {
            Atom dict  = (Atom)args?.value;
            Atom names = (Atom)args?.next?.value;

            Context dictionary = GetDictionary(dict);

            string[] nameList = CommonUtils.ListToStringArray(names, "TABLE");
            ContextUtils.ImportSymbols(dictionary, context, nameList);

            return(null);
        }
예제 #4
0
        private static Atom Export(Atom args, Context context)
        {
            Atom symbol = args?.atom;

            if (symbol == null)
            {
                throw new ArgumentException("argument must be symbol!");
            }

            string name  = (string)symbol.value;
            Atom   value = context.Get(name);

            ContextUtils.Define(context, value, string.Format("{0}.{1}", AllNames.MODULE, name));

            return(null);
        }
예제 #5
0
        public static Atom SetFirst(Atom args, Context context)
        {
            Atom sym = (Atom)args?.value;
            Atom val = (Atom)args?.next?.value;

            val = Evaluator.Evaluate(val, context);

            if (!sym.IsSymbol())
            {
                throw new ArgumentException("Variable name must be symbol!");
            }

            ContextUtils.Set(context, val, (string)sym.value);

            return(val);
        }
예제 #6
0
        public static Atom Define(Atom args, Context context)
        {
            Atom sym = (Atom)args?.value;
            Atom val = (Atom)args?.next?.value;

            val = Evaluator.Evaluate(val, context);

            if (sym.type != AtomType.Symbol)
            {
                throw new ArgumentException("Definition name must be symbol!");
            }

            ContextUtils.Define(context, val, (string)sym.value);

            return(val);
        }
예제 #7
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);
        }