xronos /System.Xronos/Builtins/SpecialMacros.SyntaxQuoteReader.cs

Language C# Lines 223
MD5 Hash ea25f3fb7dbe087dd886367facee92d0 Estimated Cost $4,281 (why?)
Repository https://bitbucket.org/stefanrusek/xronos View Raw File
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* ****************************************************************************
 * 
 * Copyright (c) 2008 Stefan Rusek and Benjamin Pollack
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * 
 * ***************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xronos.Language;
using System.Text.RegularExpressions;

namespace System.Xronos.Builtins
{
    public sealed partial class SpecialMacros
    {
        static int c = 100;
        abstract class UnquoteBox : XronosObject 
        {
            public object Value { get; set; }
            public UnquoteBox(IPersistentMap meta) : base(meta) { }
        }
        class Unquote : UnquoteBox
        {
            public Unquote(IPersistentMap meta) : base(meta) { }

            public override XronosObject withMeta(IPersistentMap meta)
            {
                return new Unquote(meta) { Value = Value };
            }
        }
        class UnquoteSplicing : UnquoteBox
        {
            public UnquoteSplicing(IPersistentMap meta) : base(meta) { }

            public override XronosObject withMeta(IPersistentMap meta)
            {
                return new UnquoteSplicing(meta) { Value = Value };
            }
        }

        class UnquoteReader : ReaderBase
        {
            protected override object Read(Reader reader, char c)
            {
                reader.Advance();

                UnquoteBox box;
                if (reader.Peek() == '@')
                    box = new UnquoteSplicing(null) {Value = reader.Advance().InternalRead(false, null)};
                else
                    box = new Unquote(null) { Value = reader.InternalRead(false, null) };

                return box;
            }
        }

        internal class SyntaxQuoteReader : ReaderBase
        {
            object nullObject = new object();

            protected override object Read(Reader reader, char c)
            {
                reader.Advance();
                var form = Expand(new Dictionary<Symbol,Symbol>(), reader.InternalRead(false, null));

                return form;
            }

            private object Expand(Dictionary<Symbol,Symbol> gensyms, object form)
            {
                object ret = null;

                if (form is IPersistentCollection || form is ISequence)
                    ret = ExpandCollection(gensyms, form);
                else
                    ret = ExpandNonCollection(gensyms, form);

                var obj = ret as XronosObject;
                if (obj != null && obj.meta() != null)
                {
                    var newMeta = obj.meta().without(RT.Line);
                    if (newMeta.count() > 0)
                        ret = obj.withMeta(newMeta);
                }
                return ret;
            }

            private object ExpandCollection(Dictionary<Symbol, Symbol> gensyms, object form)
            {
                Symbol action;
                ISequence list;
                if (form is IPersistentList)
                {
                    action = Symbol.Intern("xronos", "list");
                    list = ((ISequential)form).seq();
                }
                else if (form is IPersistentMap)
                {
                    action = Symbol.Intern("xronos", "hashmap");
                    list = FlattenMap((IPersistentMap)form);
                }
                else if (form is IPersistentVector)
                {
                    action = Symbol.Intern("xronos", "vector");
                    list = ((ISequential)form).seq();
                }
                else if (form is IPersistentSet)
                {
                    action = Symbol.Intern("xronos", "hashset");
                    list = ((IPersistentCollection)form).seq();
                }
                else if (form is ISequence)
                {
                    return RT.Cons(Symbol.Intern("xronos", "concat"), ExpandSequence(gensyms, RT.seq(form)));
                }
                else
                    throw new NotSupportedException("Unknown Collection type");

                return RT.List(Symbol.Intern("xronos", "apply"), action, RT.Cons(Symbol.Intern("xronos", "concat"), ExpandSequence(gensyms, list)));
            }

            internal static ISequence FlattenMap(IPersistentMap form)
            {
                IPersistentVector ret = PersistentVector.Empty;
                for (ISequence it = form.seq(); it != null; it = it.rest())
                {
                    var entry = (IMapEntry)it.first();
                    ret = ret.cons(entry.key()).cons(entry.val());
                }
                return ((ISequential)ret).seq();
            }

            private ISequence ExpandSequence(Dictionary<Symbol, Symbol> gensyms, ISequence seq)
            {
                IPersistentVector ret = PersistentVector.Empty;
                for (ISequence it = seq; it != null; it = it.rest())
                {
                    object o = it.first();

                    var unq = o as UnquoteBox;
                    var wrapInList = !(o is UnquoteSplicing);
                    if (unq != null)
                    {
                        o = unq.Value;

                        var meta = unq.meta();
                        if (meta != null)
                            o = RT.List(Symbol.Intern("xronos", "with-meta"), o, RT.List(Quote, meta));
                    }
                    else
                        o = Expand(gensyms, o);

                    if (wrapInList)
                        o = RT.List(Symbol.Intern("xronos", "list"), o);

                    ret = ret.cons(o);
                }
                return ((ISequential)ret).seq();
            }

            private object ExpandNonCollection(Dictionary<Symbol, Symbol> gensyms, object form)
            {
                object ret;
                Type t = (form ?? nullObject).GetType();

                switch (t.FullName)
                {
                    case "System.Xronos.Language.Symbol":
                        {
                            var sym = (Symbol)form;
                            if (sym.Namespace == null && sym.Name.EndsWith("#"))
                                form = ExpandGenSym(gensyms, sym);
                        }
                        goto default;

                    case "System.Xronos.Language.Keyword":
                    case "System.Int16":
                    case "System.Int32":
                    case "System.Int64":
                    case "System.Single":
                    case "System.Double":
                    case "System.String":
                        ret = form;
                        break;

                    default:
                        ret = RT.List(Quote, form);
                        break;
                }
                return ret;
            }

            private object ExpandGenSym(Dictionary<Symbol,Symbol> gensyms, Symbol sym)
            {
                Symbol ret;
                if (!gensyms.TryGetValue(sym, out ret))
                    gensyms[sym] = ret = Symbol.Intern("__GENSYM_" + (c++) + "_" + sym.Name.Substring(0, sym.Name.Length - 1));

                return ret;
            }
        }
    }
}
Back to Top