Beispiel #1
0
 public static object DefineConstant(Symbol sym, object value, string doc)
 {
     EraseCompilerValue(sym);
     sym.ConstantValue = value;
     sym.Documentation = doc;
     return sym;
 }
Beispiel #2
0
        public static Cons GetMethodSyntax(MethodInfo method, Symbol context)
        {
            var name = context;
            if (name == null)
            {
                var attrs = method.GetCustomAttributes(typeof(LispAttribute), false);
                if (attrs.Length != 0)
                {
                    name = CreateDocSymbol(((LispAttribute)attrs[0]).Names[0]);
                }
                else {
                    name = CreateDocSymbol(method.Name.LispName());
                }
            }

            var buf = new Vector();
            if (!method.IsStatic)
            {
                buf.Add(CreateDocSymbol("object"));
            }
            foreach (var arg in method.GetParameters())
            {
                bool hasParamArray = arg.IsDefined(typeof(ParamArrayAttribute), false);
                if (hasParamArray)
                {
                    buf.Add(Symbols.Rest);
                }
                buf.Add(CreateDocSymbol(arg.Name.LispName()));
            }
            return MakeListStar(name, AsList(buf));
        }
Beispiel #3
0
 public static Type SetFindType(Symbol name, Type type)
 {
     if (Keywordp(name))
     {
         throw new LispException("Type name cannot be a keyword: {0}", name);
     }
     Types[name] = type;
     return type;
 }
Beispiel #4
0
 public static object GetType(Symbol name)
 {
     var type = FindType(name);
     if (type == null)
     {
         throw new LispException("Undefined type name: {0}", name);
     }
     return type;
 }
Beispiel #5
0
 public static object DefineCompilerMacro(Symbol sym, LambdaClosure value, string doc)
 {
     if (!Functionp(sym.Value))
     {
         ThrowError("Cannot define compiler macro for non-function {0}", sym);
     }
     sym.CompilerMacroValue = value;
     sym.CompilerDocumentation = doc;
     return sym;
 }
Beispiel #6
0
 public static Prototype SetFindType(Symbol name, Prototype type)
 {
     if (Keywordp(name))
     {
         throw new LispException("Type name cannot be a keyword");
     }
     Types[name] = type;
     type.ClassName = name;
     return type;
 }
Beispiel #7
0
 public static Array AsArray(IEnumerable seq, Symbol type)
 {
     var a = AsArray(seq);
     var t = (Type)GetType(type);
     Array b = Array.CreateInstance(t, a.Length);
     for (var i = 0; i < a.Length; ++i)
     {
         b.SetValue(a[i], i);
     }
     return b;
 }
        public static object GetDynamic(Symbol sym)
        {
            // used by compiler generated code
            for (SpecialVariables entry = CurrentThreadContext.SpecialStack; entry != null; entry = entry.Link)
            {
                if (entry.Sym == sym)
                {
                    return entry.Value;
                }
            }

            return sym.CheckedValue;
        }
Beispiel #9
0
 public static void EraseVariable(Symbol sym)
 {
     sym.Value = null;
     sym.Documentation = null;
     sym.Usage = SymbolUsage.None;
 }
Beispiel #10
0
 public static void EraseCompilerValue(Symbol sym)
 {
     sym.CompilerMacroValue = null;
     sym.CompilerDocumentation = null;
     sym.CompilerUsage = SymbolUsage.None;
 }
Beispiel #11
0
 public static object DefineSymbolMacro(Symbol sym, SymbolMacro value, string doc)
 {
     EraseVariable(sym);
     sym.SymbolMacroValue = value;
     sym.CompilerDocumentation = doc;
     return sym;
 }
Beispiel #12
0
        public object TryGetValue(Symbol sym)
        {
            for (Frame frame = this; frame != null; frame = frame.Link)
            {
                if (frame.Names != null)
                {
                    int index = frame.Names.IndexOf(sym);
                    if (index != -1)
                    {
                        return frame.Values[index];
                    }
                }
            }

            return null;
        }
 public bool FindLocal(Symbol sym, ScopeFlags reason)
 {
     int depth;
     ScopeEntry entry;
     return FindLocal(sym, reason, out depth, out entry);
 }
Beispiel #14
0
 public static object DefineMacro(Symbol sym, LambdaClosure value, string doc)
 {
     EraseVariable(sym);
     sym.MacroValue = value;
     sym.CompilerDocumentation = doc;
     return sym;
 }
Beispiel #15
0
        public static void Create()
        {
            And = MakeSymbol("and");
            Append = MakeSymbol("append");
            Apply = MakeSymbol("apply");
            Args = MakeSymbol("__args__");
            Array = MakeSymbol("array");
            AssemblyPath = MakeSymbol("$assembly-path");
            AsVector = MakeSymbol("as-vector");
            Base = MakeSymbol(":base");
            BitAnd = MakeSymbol("bit-and");
            BitNot = MakeSymbol("bit-not");
            BitOr = MakeSymbol("bit-or");
            BitShiftLeft = MakeSymbol("bit-shift-left");
            BitShiftRight = MakeSymbol("bit-shift-right");
            BitXor = MakeSymbol("bit-xor");
            Body = MakeSymbol("&body");
            Bool = MakeSymbol("bool");
            BuiltinConstructor = MakeSymbol("builtin-constructor");
            BuiltinFunction = MakeSymbol("builtin-function");
            Case = MakeSymbol("case");
            Catch = MakeSymbol("catch");
            CommandLineArguments = MakeSymbol("$command-line-arguments");
            CommandLineScriptName = MakeSymbol("$command-line-script-name");
            CompilerMacro = MakeSymbol("compiler-macro");
            CompileTimeBranch = MakeSymbol("compile-time-branch");
            Compiling = MakeSymbol("compiling");
            Constant = MakeSymbol("constant");
            CreateDelayedExpression = MakeSymbol("system:create-delayed-expression");
            CreateTask = MakeSymbol("system:create-task");
            DebugMode = MakeSymbol("$debug-mode");
            Declare = MakeSymbol("declare");
            Def = MakeSymbol("def");
            DefConstant = MakeSymbol("defconstant");
            DefineCompilerMacro = MakeSymbol("define-compiler-macro");
            DefineSymbolMacro = MakeSymbol("define-symbol-macro");
            DefMacro = MakeSymbol("defmacro");
            DefMethod = MakeSymbol("defmethod");
            DefMulti = MakeSymbol("defmulti");
            Defun = MakeSymbol("defun");
            Do = MakeSymbol("do");
            Documentation = MakeSymbol("documentation");
            Dot = MakeSymbol(".");
            Dynamic = MakeSymbol("dynamic");
            E = MakeSymbol("math:E");
            EnableExternalDocumentation = MakeSymbol("$enable-external-documentation");
            EnableWarnings = MakeSymbol("$enable-warnings");
            Environment = MakeSymbol("&environment");
            Equality = MakeSymbol("=");
            Escape = MakeSymbol(":escape");
            Eval = MakeSymbol("eval");
            Exception = MakeSymbol("$exception");
            False = MakeSymbol("false");
            Features = MakeSymbol("$features");
            Finally = MakeSymbol("finally");
            Force = MakeSymbol("force");
            Funcall = MakeSymbol("funcall");
            Function = MakeSymbol("function");
            FunctionExitLabel = MakeSymbol("%function-exit");
            FunctionKeyword = MakeSymbol(":function");
            FutureVar = MakeSymbol("future");
            GenericFunction = MakeSymbol("generic-function");
            GetArgumentOrDefault = MakeSymbol("get-argument-or-default");
            GetAttr = MakeSymbol("attr");
            GetElt = MakeSymbol("elt");
            Goto = MakeSymbol("goto");
            HashElif = MakeSymbol("#elif");
            HashElse = MakeSymbol("#else");
            HashEndif = MakeSymbol("#endif");
            HelpHook = MakeSymbol("$help-hook");
            HiddenVar = MakeSymbol("hidden-var");
            I = MakeSymbol("math:I");
            If = MakeSymbol("if");
            IfLet = MakeSymbol("if-let");
            Ignore = MakeSymbol("ignore");
            ImportedConstructor = MakeSymbol("imported-constructor");
            ImportedFunction = MakeSymbol("imported-function");
            InfoColor = MakeSymbol("$info-color");
            InitialValue = MakeSymbol(":initial-value");
            InteractiveMode = MakeSymbol("$interactive-mode");
            It = MakeSymbol("it");
            Key = MakeSymbol("&key");
            Label = MakeSymbol("label");
            Lambda = MakeSymbol("lambda");
            LambdaList = MakeSymbol(@"__lambdas__");
            LazyImport = MakeSymbol("$lazy-import");
            LazyVar = MakeSymbol("lazy");
            Left = MakeSymbol(":left");
            Let = MakeSymbol("let");
            LetFun = MakeSymbol("letfun");
            LetMacro = MakeSymbol("letmacro");
            LetSymbolMacro = MakeSymbol("let-symbol-macro");
            List = MakeSymbol("list");
            ListStar = MakeSymbol("list*");
            LoadPath = MakeSymbol("$load-path");
            LoadPrint = MakeSymbol("$load-print");
            LoadPrintKeyword = MakeSymbol(":print");
            LoadVerbose = MakeSymbol("$load-verbose");
            LoadVerboseKeyword = MakeSymbol(":verbose");
            Macro = MakeSymbol("macro");
            MacroexpandHook = MakeSymbol("$macroexpand-hook");
            Macroexpand1 = MakeSymbol("macroexpand-1");
            MacroKeyword = MakeSymbol(":macro");
            Main = MakeSymbol("user:main");
            Math = MakeSymbol("math");
            MaxElements = MakeSymbol(":max-elements");
            MergingDo = MakeSymbol("merging-do");
            Method = MakeSymbol("method");
            MethodKeyword = MakeSymbol(":method");
            MissingValue = MakeSymbol("missing-value");
            Modules = MakeSymbol("$modules");
            New = MakeSymbol("new");
            Not = MakeSymbol("not");
            Nth = MakeSymbol("nth");
            Null = MakeSymbol("null");
            NullableDot = MakeSymbol("?");
            Optional = MakeSymbol("&optional");
            OptionalKeyword = MakeSymbol(":optional");
            Or = MakeSymbol("or");
            PI = MakeSymbol("math:PI");
            Package = MakeSymbol("$package");
            PackageNamePrefix = MakeSymbol("$package-name-prefix");
            Padding = MakeSymbol(":padding");
            Params = MakeSymbol("&params");
            PlaybackDelay = MakeSymbol("$playback-delay");
            PlaybackDelimiter = MakeSymbol("$playback-delimiter");
            Pow = MakeSymbol("math:pow");
            Pretty = MakeSymbol(":pretty");
            PrettyPrintHook = MakeSymbol("$pprint-hook");
            PrintBase = MakeSymbol("$print-base");
            PrintCompact = MakeSymbol("$print-compact");
            PrintEscape = MakeSymbol("$print-escape");
            PrintForce = MakeSymbol("$print-force");
            PrintPrototypeWithBraces = MakeSymbol("$print-prototype-with-braces");
            PrintShortSymbolNames = MakeSymbol("$print-short-symbol-names");
            PrintVectorWithBrackets = MakeSymbol("$print-vector-with-brackets");
            Prog = MakeSymbol("prog");
            QuasiQuote = MakeSymbol("quasi-quote");
            Quote = MakeSymbol("quote");
            RawParams = MakeSymbol("&rawparams");
            ReadEval = MakeSymbol("$read-eval");
            ReadonlyVariable = MakeSymbol("readonly-variable");
            Readtable = MakeSymbol("$readtable");
            Recur = MakeSymbol("recur");
            RecursionLabel = MakeSymbol("%recursion-label");
            ReplForceIt = MakeSymbol("$repl-force-it");
            ReplListenerPort = MakeSymbol("$repl-listener-port");
            Rest = MakeSymbol("&rest");
            Return = MakeSymbol("return");
            ReturnFrom = MakeSymbol("return-from");
            ReturnFromLoad = MakeSymbol("system:return-from-load");
            Returns = MakeSymbol("&returns");
            Right = MakeSymbol(":right");
            ScriptDirectory = MakeSymbol("$script-directory");
            ScriptName = MakeSymbol("$script-name");
            Self = MakeSymbol("self");
            Set = MakeSymbol("set");
            SetAttr = MakeSymbol("set-attr");
            SetElt = MakeSymbol("set-elt");
            Setf = MakeSymbol("setf");
            Setq = MakeSymbol("setq");
            SpecialConstant = MakeSymbol("special-constant");
            SpecialForm = MakeSymbol("special-form");
            SpecialReadonlyVariable = MakeSymbol("special-readonly-variable");
            SpecialVariable = MakeSymbol("special-variable");
            StdErr = MakeSymbol("$stderr");
            StdIn = MakeSymbol("$stdin");
            StdLog = MakeSymbol("$stdlog");
            StdOut = MakeSymbol("$stdout");
            StdScr = MakeSymbol("$stdscr");
            Str = MakeSymbol("string");
            Stream = MakeSymbol(":stream");
            StructurallyEqual = MakeSymbol("structurally-equal");
            SymbolMacro = MakeSymbol("symbol-macro");
            Target = MakeSymbol("__target__");
            Temp = MakeSymbol(@"__temp__");
            Throw = MakeSymbol("throw");
            Tilde = MakeSymbol("~");
            Tracing = MakeSymbol("$tracing");
            True = MakeSymbol("true");
            Try = MakeSymbol("try");
            Undefined = MakeSymbol("undefined");
            Underscore = MakeSymbol("_");
            Unquote = MakeSymbol("system:unquote");
            UnquoteSplicing = MakeSymbol("system:unquote-splicing");
            Values = MakeSymbol("values");
            Var = MakeSymbol("var");
            Variable = MakeSymbol("variable");
            Vector = MakeSymbol("&vector");
            Verbose = MakeSymbol("$verbose");
            Whole = MakeSymbol("&whole");
            Width = MakeSymbol(":width");
            kwForce = MakeSymbol(":force");

            bqAppend = MakeSymbol("bq:append");
            bqList = MakeSymbol("bq:list");
            bqQuote = MakeSymbol("bq:quote");
            bqForce = MakeSymbol("bq:force");

            NumberedVariables = new Symbol[]
            {
                MakeSymbol(@"\0"),
                MakeSymbol(@"\1"),
                MakeSymbol(@"\2"),
                MakeSymbol(@"\3"),
                MakeSymbol(@"\4"),
                MakeSymbol(@"\5"),
                MakeSymbol(@"\6"),
                MakeSymbol(@"\7"),
                MakeSymbol(@"\8"),
                MakeSymbol(@"\9")
            };

            DynamicVariables = new Symbol[]
            {
                MakeSymbol("$0"),
                MakeSymbol("$1"),
                MakeSymbol("$2"),
                MakeSymbol("$3"),
                MakeSymbol("$4"),
                MakeSymbol("$5"),
                MakeSymbol("$6"),
                MakeSymbol("$7"),
                MakeSymbol("$8"),
                MakeSymbol("$9")
            };

            ReservedVariables = new Symbol[]
            {
                MakeSymbol("{0}"),
                MakeSymbol("{1}"),
                MakeSymbol("{2}"),
                MakeSymbol("{3}"),
                MakeSymbol("{4}"),
                MakeSymbol("{5}"),
                MakeSymbol("{6}"),
                MakeSymbol("{7}"),
                MakeSymbol("{8}"),
                MakeSymbol("{9}")
            };

            ShortLambdaVariables = new Symbol[]
            {
                MakeSymbol("%"),
                MakeSymbol("%1"),
                MakeSymbol("%2"),
                MakeSymbol("%3"),
                MakeSymbol("%4"),
                MakeSymbol("%5"),
                MakeSymbol("%6"),
                MakeSymbol("%7"),
                MakeSymbol("%8"),
                MakeSymbol("%9")
            };
        }
Beispiel #16
0
 public static object AsVector(IEnumerable seq, Symbol type)
 {
     var t1 = (Type)GetType(type);
     var t2 = GenericListType.MakeGenericType(t1);
     var c = t2.GetConstructors();
     var x = c[2].Invoke(new object[] { AsArray(seq, type) });
     return x;
 }
 public ScopeEntry(AnalysisScope scope, Symbol key, object value, ScopeFlags flags)
 {
     Scope = scope;
     Key = key;
     Value = value;
     Flags = flags;
 }
 bool LexicalSymEqual(Symbol sym1, Symbol sym2)
 {
     if (sym1 == sym2)
     {
         return true;
     }
     return false;
 }
 public void PrintWarning(string context, string error, Symbol sym)
 {
     if (context == null)
     {
         Runtime.PrintWarning(error, " ", sym.Name);
     }
     else {
         Runtime.PrintWarning(error, " ", sym.Name, " in ", context);
     }
 }
 public ScopeEntry FindLocal(Symbol name)
 {
     int depth;
     ScopeEntry entry;
     if (FindLocal(name, 0, out depth, out entry))
     {
         return entry;
     }
     else {
         return null;
     }
 }
        public bool FindLocal(Symbol sym, ScopeFlags reason, out int depth, out ScopeEntry entry)
        {
            bool noCapturedNativeParametersBeyondThisPoint = false;

            depth = 0;
            entry = null;

            for (AnalysisScope sc = this; sc != null; sc = sc.Parent)
            {
                ScopeEntry item;

                if (sc.IsBlockScope && sym == Symbols.Tilde)
                {
                    if (sc.Tilde == null)
                    {
                        sc.Tilde = sc.DefineNativeLocal(Symbols.Tilde, ScopeFlags.All);
                    }
                }

                for (var i = sc.Variables.Count - 1; i >= 0; --i)
                {
                    item = sc.Variables[i];

                    // Looking for exact match
                    if (LexicalSymEqual(item.Key, sym))
                    {
                        entry = item;
                        item.Flags |= reason;

                        if (item.Index != -1 || item.MacroValue != null || item.SymbolMacroValue != null)
                        {
                        }
                        else if (reason != 0 && noCapturedNativeParametersBeyondThisPoint && !LexicalSymEqual(sym, Symbols.Tilde))
                        {
                            // Linq.Expression closures do not support native variables defined
                            // outside the LambdaExpression. Whenever we encounter such a variable
                            // it is added to the free variables and also added as frame variable
                            // to keep the compiler happy.
                            // The recompile that comes later uses the list of free variables to
                            // choose the correct implementation scheme.
                            sc.FreeVariables.Add(sym);
                            sc.ChangeNativeToFrameLocal(item);
                        }

                        return true;
                    }
                }

                if (sc.IsBlockScope && sym == Symbols.Tilde)
                {
                    // boundary for ~ variable which is tightly coupled to its DO block.
                    break;
                }

                if (sc.IsLambda)
                {
                    // boundary for native variables in closures.
                    noCapturedNativeParametersBeyondThisPoint = true;
                }

                if (sc.Names != null)
                {
                    ++depth;
                }

            }

            depth = 0;

            return false;
        }
Beispiel #22
0
 public static void WarnWhenShadowing(Symbol sym)
 {
     var package = CurrentPackage();
     if (sym.Package != package && sym.Package != UserPackage)
     {
         PrintWarning("defining symbol ", sym.Name, " shadows ", sym.LongName);
     }
 }
 public void DefineMacro(Symbol sym, object macro, ScopeFlags flags)
 {
     Variables.Add(new ScopeEntry(this, sym, macro, flags));
 }
        public ParameterExpression DefineNativeLocal(Symbol sym, ScopeFlags flags, Type type = null)
        {
            var parameter = Expression.Parameter(type ?? typeof(object), sym.Name);
            Variables.Add(new ScopeEntry(this, sym, parameter, flags));

            return parameter;
        }
        public int DefineFrameLocal(Symbol sym, ScopeFlags flags)
        {
            if (Names == null)
            {
                Names = new List<Symbol>();
            }

            UsesFramedVariables = true;
            Names.Add(sym);
            Variables.Add(new ScopeEntry(this, sym, Names.Count - 1, flags));

            return Names.Count - 1;
        }
Beispiel #26
0
 Cons ISyntax.GetSyntax(Symbol context)
 {
     var v = new Vector();
     foreach (var m in Members)
     {
         v.Add(Runtime.GetMethodSyntax(m, context));
     }
     return Runtime.AsList(Runtime.SeqBase.Distinct(v, Runtime.StructurallyEqualApply));
 }
 public static object TryGetLexicalSymbol(Symbol sym)
 {
     // used by compiler generated code
     return CurrentThreadContext.Frame.TryGetValue(sym);
 }
 public bool FindDuplicate(Symbol name)
 {
     var entry = FindLocal(name);
     return entry != null && entry.Scope == this;
 }
Beispiel #29
0
 public static object ConvertToEnumerableObject(IEnumerable seq, Symbol type)
 {
     var t = (Type)GetType(type);
     var m2 = CastMethod.MakeGenericMethod(t);
     var seq2 = m2.Invoke(null, new object[] { ToIter(seq) });
     return seq2;
 }
 public static object DefDynamicConst(Symbol sym, object value)
 {
     // used by compiler generated code
     CurrentThreadContext.SpecialStack = new SpecialVariables(sym, true, value, CurrentThreadContext.SpecialStack);
     return value;
 }