Example #1
0
        public static void RestartSymbols()
        {
            // these packages do not use lisp package
            KeywordPackage = MakePackage3("keyword", reserved: true);
            TempPackage = MakePackage3("temp", reserved: true);
            LispPackage = MakePackage3("lisp", reserved: true);
            LispDocPackage = MakePackage3("example", reserved: true);

            // these packages do use lisp package
            MakePackage3("terminal", useLisp: true);
            UserPackage = MakePackage3("user", useLisp: true);

            Symbols.Create();

            // standard set of variables
            Symbols.AssemblyPath.ReadonlyValue = null;
            Symbols.CommandLineArguments.ReadonlyValue = UserArguments;
            Symbols.CommandLineScriptName.ReadonlyValue = ScriptName;
            Symbols.DebugMode.ConstantValue = DebugMode;
            Symbols.E.ConstantValue = Math.E;
            Symbols.EnableWarnings.VariableValue = true;
            Symbols.Exception.ReadonlyValue = null;
            Symbols.Features.VariableValue = null;
            Symbols.HelpHook.VariableValue = null;
            Symbols.I.ConstantValue = Complex.ImaginaryOne;
            Symbols.InfoColor.Value = "gray";
            Symbols.InteractiveMode.ConstantValue = Repl;
            Symbols.It.VariableValue = null;
            Symbols.LazyImport.VariableValue = true;
            Symbols.LoadPath.ReadonlyValue = null;
            Symbols.LoadPrint.VariableValue = false;
            Symbols.LoadVerbose.VariableValue = false;
            Symbols.MissingValue.ConstantValue = MissingValue;
            Symbols.Modules.ReadonlyValue = null;
            Symbols.Package.VariableValue = LispPackage;
            Symbols.PackageNamePrefix.VariableValue = null;
            Symbols.PI.ConstantValue = Math.PI;
            Symbols.PlaybackDelay.VariableValue = 100;
            Symbols.PlaybackDelimiter.VariableValue = "<";
            Symbols.PrettyPrintHook.VariableValue = null;
            Symbols.PrintBase.VariableValue = 10;
            Symbols.PrintCompact.Value = true;
            Symbols.PrintEscape.VariableValue = true;
            Symbols.PrintForce.VariableValue = true;
            Symbols.PrintPrototypeWithBraces.VariableValue = false;
            Symbols.PrintShortSymbolNames.VariableValue = false;
            Symbols.PrintVectorWithBrackets.VariableValue = false;
            Symbols.ReadEval.VariableValue = null;
            Symbols.Readtable.VariableValue = GetStandardReadtable();
            Symbols.Self.ReadonlyValue = null;
            Symbols.ReplForceIt.VariableValue = false;
            Symbols.ReplListenerPort.VariableValue = 8080;
            Symbols.ScriptDirectory.ReadonlyValue = NormalizePath(HomeDirectory);
            Symbols.ScriptName.ReadonlyValue = null;
            Symbols.StdErr.VariableValue = null;
            Symbols.StdIn.VariableValue = null;
            Symbols.StdLog.VariableValue = null;
            Symbols.StdOut.VariableValue = null;
            Symbols.StdScr.VariableValue = null;
        }
Example #2
0
        public static void RestartVariables()
        {
            ReadDecimalNumbers = true;
            GentempCounter = 0;
            LispPackage = null;
            KeywordPackage = null;
            UserPackage = null;
            Documentation = new Dictionary<object, string>();
            Packages = new Dictionary<string, Package>();
            PackagesByType = new Dictionary<Type, Package>();
            Types = new Dictionary<Symbol, object>();

            InitRandom();

            DefaultReadtable = GetStandardReadtable();
        }
Example #3
0
        static Package MakePackage3(object name, bool reserved = false, bool useLisp = false)
        {
            var n = GetDesignatedString(name);
            if (n.IndexOf(PackageSymbolSeparator) != -1)
            {
                throw new LispException("Invalid package name: {0}", n);
            }

            var package = FindPackage(name);

            if (package == null)
            {
                Packages[n] = package = new Package(n);
            }

            package.Reserved = reserved;

            if (useLisp)
            {
                package.AddUsePackage(LispPackage);
            }

            return package;
        }
Example #4
0
        public Symbol(string name, Package package = null)
        {
            Name = name;
            if (Name[0] == '$')
            {
                IsDynamic = true;
            }
            else if (Name.Length >= 3 && Name[0] == '*' && Name[Name.Length - 1] == '*')
            {
                IsDynamic = true;
            }
            else {
                IsDynamic = false;
            }

            PropList = null;
            Package = package;

            if (package == Runtime.KeywordPackage)
            {
                _value = this;
                Usage = SymbolUsage.Constant;
            }
            else {
                _value = null;
                Usage = SymbolUsage.None;
            }
        }
Example #5
0
 public void RemoveUsePackage(Package package)
 {
     if (UseList.Contains(package))
     {
         UseList.Remove(package);
     }
 }
Example #6
0
 public static void AddPackageByType(Type type, Package package)
 {
     PackagesByType[type] = package;
 }
Example #7
0
 public static void VerifyNoMissingSymbols(Package package)
 {
     if (package.ImportedType != null && !package.ImportMissingDone)
     {
         package.ImportMissingDone = true;
         var names = package.ImportedType.GetMembers(ImportBindingFlags).Select(x => x.Name).Distinct().ToArray();
         foreach (var name in names)
         {
             ImportMissingSymbol(name, package);
         }
     }
 }
Example #8
0
 public void AddUsePackage(Package package)
 {
     if (!UseList.Contains(package))
     {
         UseList.Insert(0, package);
     }
 }
Example #9
0
        public static bool ImportMissingSymbol2(string name, Package package)
        {
            var members = package.ImportedType.GetMember(name, ImportBindingFlags).Select(x => ResolveGenericMethod(x)).ToArray();

            if (members.Length == 0)
            {
                return false;
            }

            var importable = !package.RestrictedImport || members[0].GetCustomAttributes(typeof(LispAttribute), true).Length != 0;

            if (!importable)
            {
                return false;
            }

            var field = members[0] as FieldInfo;
            var ucName = members[0].Name.LispName().ToUpper();
            var lcName = members[0].Name.LispName();

            if (field != null)
            {
                if (field.IsLiteral || (field.IsStatic && field.IsInitOnly))
                {
                    var sym = package.FindOrCreate(ucName, excludeUseList: true, export: true);
                    sym.ConstantValue = field.GetValue(null);
                }
                return true;
            }

            if (members[0] is EventInfo)
            {
                var sym = package.FindOrCreate(lcName, excludeUseList: true, export: true);
                sym.ConstantValue = members[0];
                return true;
            }

            if (members[0] is ConstructorInfo)
            {
                var builtin = new ImportedConstructor(members.Cast<ConstructorInfo>().ToArray());
                var sym = package.FindOrCreate("new", excludeUseList: true, export: true);
                sym.FunctionValue = builtin;
                package.ImportedConstructor = builtin;
                return true;
            }

            if (members[0] is MethodInfo)
            {
                //if ( !name.StartsWith( "get_" ) && !name.StartsWith( "set_" ) )
                {
                    var sym = package.FindOrCreate(lcName, excludeUseList: true, export: true);
                    var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, members.Cast<MethodInfo>().ToArray(), false);
                    sym.FunctionValue = builtin;
                }

                return true;
            }

            if (members[0] is PropertyInfo)
            {
                var properties = members.Cast<PropertyInfo>().ToArray();
                var getters = properties.Select(x => x.GetGetMethod()).Where(x => x != null).ToArray();
                var setters = properties.Select(x => x.GetSetMethod()).Where(x => x != null).ToArray();

                if (getters.Length != 0)
                {
                    var sym = package.FindOrCreate(lcName, excludeUseList: true, export: true);
                    var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, getters, false);
                    sym.FunctionValue = builtin;
                }

                if (setters.Length != 0)
                {
                    // create getter symbol for setf/setq
                    package.FindOrCreate(lcName, excludeUseList: true, export: true);
                    // use set-xxx
                    var sym = package.FindOrCreate("set-" + lcName, excludeUseList: true, export: true);
                    var builtin = new ImportedFunction(members[0].Name, members[0].DeclaringType, setters, false);
                    sym.FunctionValue = builtin;
                }

                return true;
            }

            return false;
        }
Example #10
0
        public static void ImportMissingSymbol(string key, Package package)
        {
            var name = key.LispToPascalCaseName();

            if (key == "new")
            {
                name = ".ctor";
            }

            if (ImportMissingSymbol2(name, package))
            {
                return;
            }

            if (key.StartsWith("set-"))
            {
                // choice between setter and ordinary method
                var name2 = "set_" + name.Substring(3);
                if (ImportMissingSymbol2(name2, package))
                {
                    return;
                }
            }

            var name3 = key.Replace("-", "_");

            ImportMissingSymbol2(name3, package);
        }
Example #11
0
        public static void ImportIntoPackage(Package package, Type type)
        {
            AddPackageByType(type, package);

            var restrictedImport = type.GetCustomAttributes(typeof(RestrictedImportAttribute), true).Length != 0;

            package.ImportedType = type;
            package.RestrictedImport = restrictedImport;
            var sym = package.FindOrCreate("T", excludeUseList: true, export: true);
            sym.ConstantValue = type;
            sym.Documentation = string.Format("The .NET type <{0}> imported in this package.", type);

            if (!ToBool(Symbols.LazyImport.Value))
            {
                VerifyNoMissingSymbols(package);
            }
        }
Example #12
0
        public static void ImportExtensionMethodsIntoPackage(Package package, Type type)
        {
            VerifyNoMissingSymbols(package);

            var isbuiltin = type.Assembly == Assembly.GetExecutingAssembly();
            var extendedType = (Type)package.Dict["T"].CheckedValue;
            var restrictedImport = type.GetCustomAttributes(typeof(RestrictedImportAttribute), true).Length != 0;
            var names = type.GetMethods(ImportBindingFlags).Select(x => x.Name).Distinct().ToArray();

            foreach (var name in names)
            {
                var methods = type.GetMember(name, ImportBindingFlags)
                                  .Where(x => x is MethodInfo)
                                  .Select(x => (MethodInfo)ResolveGenericMethod((MethodInfo)x))
                                  .Where(x => ExtendsType(x, extendedType))
                                  .ToList();

                if (methods.Count == 0)
                {
                    continue;
                }

                var importable = !restrictedImport || methods[0].GetCustomAttributes(typeof(LispAttribute), true).Length != 0;

                if (!importable)
                {
                    continue;
                }

                var sym = package.FindOrCreate(name.LispName(), useMissing: true, excludeUseList: true, export: true);
                var builtin = sym.Value as ImportedFunction;

                if (builtin == null)
                {
                    sym.FunctionValue = builtin = new ImportedFunction(name, type);
                }

                if (isbuiltin)
                {
                    // Designed to go before other methods.
                    methods.AddRange(builtin.BuiltinExtensionMembers);
                    builtin.BuiltinExtensionMembers = methods.Distinct().ToArray();
                }
                else {
                    // todo: change order
                    // Goes after other methods as in c#.
                    methods.AddRange(builtin.ExternalExtensionMembers);
                    builtin.ExternalExtensionMembers = methods.Distinct().ToArray();
                }

            }
        }
Example #13
0
        public static bool ImportMembers(MemberInfo[] members, Package package)
        {
            if (members.Length == 0)
            {
                return(false);
            }

            var member     = members[0];
            var importable = !package.RestrictedImport || member.GetCustomAttributes(typeof(LispAttribute), true).Length != 0;

            if (!importable)
            {
                return(false);
            }

            var name   = member.Name;
            var ucName = name.LispName().ToUpper();
            var lcName = name.LispName();

            if (member is FieldInfo)
            {
                var field = member as FieldInfo;
                if (field.IsLiteral || (field.IsStatic && field.IsInitOnly))
                {
                    var sym = package.Create(ucName, ispublic: true);
                    sym.ConstantValue = field.GetValue(null);
                }
                return(true);
            }

            if (member is EventInfo)
            {
                var sym = package.Create(lcName, ispublic: true);
                sym.ConstantValue = member;
                return(true);
            }

            if (member is ConstructorInfo)
            {
                var builtin = new ImportedConstructor(members.Cast <ConstructorInfo>().ToArray());
                var sym     = package.Create("new", ispublic: true);
                sym.FunctionValue = builtin;
                return(true);
            }

            if (member is MethodInfo)
            {
                var sym     = package.Create(lcName, ispublic: true);
                var builtin = new ImportedFunction(name, member.DeclaringType, members.Cast <MethodInfo>().ToArray(), false);
                sym.FunctionValue = builtin;
                return(true);
            }

            if (member is PropertyInfo)
            {
                var properties = members.Cast <PropertyInfo>().ToArray();
                var getters    = properties.Select(x => x.GetGetMethod()).Where(x => x != null).ToArray();
                var setters    = properties.Select(x => x.GetSetMethod()).Where(x => x != null).ToArray();

                if (getters.Length != 0)
                {
                    var sym     = package.Create(lcName, ispublic: true);
                    var builtin = new ImportedFunction(name, member.DeclaringType, getters, false);
                    sym.FunctionValue = builtin;
                }

                if (setters.Length != 0)
                {
                    // create getter symbol for setf/setq
                    package.Create(lcName, ispublic: true);
                    // use set-xxx
                    var sym     = package.Create("set-" + lcName, ispublic: true);
                    var builtin = new ImportedFunction(name, member.DeclaringType, setters, false);
                    sym.FunctionValue = builtin;
                }

                return(true);
            }

            return(false);
        }