Ejemplo n.º 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) + ")");
            }
        }
Ejemplo n.º 2
0
        public bool LoadFromFile(string Filename)
        {
            bool result = false;

            string[] lines;

            lines = File.ReadAllLines(Filename);

            CLex lexer = new CLex();

            lexer.IgnoreWhitespace = true;
            lexer.IgnoreEol        = true;
            lexer.Initialise(Filename);

            SNodeBase node = null;

            node = ParseNode(lexer);

            //
            // Print(node);

            //
            if (node != null)
            {
                //if (node is SNodeList)
                //{
                //    SNodeList list = node as SNodeList;

                //    if (list.Items.Count > 1)
                //    {
                //        if (list.Items[0] is SNodeAtom)
                //        {
                //            SNodeAtom value = list.Items[0] as SNodeAtom;
                //            this.Name = value.Value;
                //        }

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

            if (node is SExpression)
            {
                SExpression sexpr = node as SExpression;

                this.Name  = sexpr.Name;
                this.Items = sexpr.Items;
            }

            //
            return(result);
        }
Ejemplo n.º 3
0
        //

        public static string GetString(SNodeBase baseNode)
        {
            if (baseNode is SExpression)
            {
                SExpression expr = baseNode as SExpression;
                if ((expr.Items != null) && (expr.Items.Count > 0) && (expr.Items[0] is SNodeAtom))
                {
                    return((expr.Items[0] as SNodeAtom).Value);
                }
                else
                {
                    return("");
                }
            }
            else
            {
                return("");  // error
            }
        }
Ejemplo n.º 4
0
 public void Print(SNodeBase x)
 {
     Print_sub(x, 0);
 }
Ejemplo n.º 5
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);
            }
        }