Пример #1
0
        private void Print_sub(SNodeBase x, int depth)
        {
            Console.Write(new String(' ', depth));

            //if ((x.Type & Types.Atom) == Types.Atom)
            if (x is SNodeAtom)
            {
                //if ((x.Type & Types.String) == Types.String)
                if (x is SNodeAtom)
                {
                    Console.WriteLine(String.Format("{0}", x.ToString()));
                }
                else if ((x.Type & Types.Symbol) == Types.Symbol)
                {
                    Console.WriteLine(String.Format("symbol: {0}", x.ToString()));
                }
                else if ((x.Type & Types.Number) == Types.Number)
                {
                    Console.WriteLine(String.Format("number: {0}", x.ToString()));
                }
            }
            //else if ((x.Type & Types.List) == Types.List)
            else if (x is SNodeList)
            {
                Console.WriteLine("(");
                SNodeList list = x as SNodeList;
                foreach (SNodeBase y in list.Items)
                {
                    Print_sub(y, depth + 1);
                }
                Console.WriteLine(new String(' ', depth) + ")");
            }
        }
Пример #2
0
        private SNodeBase ParseList(CLex lexer)
        {
            SNodeList node = new SNodeList();

            node.Items = new List <SNodeBase>();
            lexer.GetToken();

            while ((lexer.CurToken.Type != TokenType.EOF) &&
                   !((lexer.CurToken.Type == TokenType.Symbol) &&
                     (lexer.CurToken.Value == ")")
                     )
                   )
            {
                //
                node.Items.Add(ParseNode(lexer));
            }

            // == )
            lexer.GetToken();

            return(node);
        }
Пример #3
0
        private SNodeBase ParseNode(CLex lexer)
        {
            if ((lexer.CurToken.Type == TokenType.Symbol) &&
                (lexer.CurToken.Value == "("))
            {
                //return ParseList(lexer);
                SNodeBase node = ParseList(lexer);

                if (node is SNodeList)
                {
                    SNodeList list = node as SNodeList;
                    //
                    if (list.Items.Count > 0)
                    {
                        SExpression sexpr = new SExpression();

                        if (list.Items[0] is SNodeAtom)
                        {
                            SNodeAtom value = list.Items[0] as SNodeAtom;
                            sexpr.Name = value.Value;
                        }

                        sexpr.Items = new List <SNodeBase>();
                        for (int index = 1; index < list.Items.Count; index++)
                        {
                            sexpr.Items.Add(list.Items[index]);
                        }

                        //
                        return(sexpr);
                    }
                    else
                    {
                        return(node);
                    }
                }
                else
                {
                    return(node);
                }
            }
            else
            {
                // string
                SNodeAtom node;

                if (lexer.CurToken.Type == TokenType.StringLiteral)
                {
                    string value = lexer.CurToken.Value;

                    if (value.StartsWith("\"") && value.EndsWith("\""))
                    {
                        value = value.Substring(1, value.Length - 2);
                        // descape " and \
                    }

                    node = new SNodeAtom(value);
                }
                else
                {
                    node = new SNodeAtom(lexer.CurToken.Value);
                }
                lexer.GetToken();
                return(node);
            }
        }