public void Parse() { var program = new SSExpression(null, null); var current = program; foreach (var tok in TokenList) { //if (lex.Type == TokenType.Char) //Console.WriteLine(lex.Type + " [" + lex.Value+"]"); switch (tok.Type) { case TokenType.LeftBracket: case TokenType.LeftCurlyBracket: case TokenType.LeftParentheses: var newNode = new SSExpression(tok: tok, parent: current); current.Children.Add(newNode); current = newNode; break; case TokenType.RightBracket: case TokenType.RightParenthese: case TokenType.RightCurlyBracket: current = current.Parent; break; default: //Console.WriteLine("adding {0} {1}".Fmt(tok.Type, tok.Value)); current.Children.Add(new SSExpression(tok, current)); break; } } Expressions = program.Children; }
//public static String[] Tokenize(String text) //{ // String[] tokens = text.Replace("(", " ( ").Replace(")", " ) ").Split(" \t\r\n".ToArray(), StringSplitOptions.RemoveEmptyEntries); // return tokens; //} public static void Print(this SSExpression exp, TextWriter writer) { var ct = 3; while (ct-- != 0 && exp.Parent != null) { exp = exp.Parent; } DoPrint(exp, writer, 5); }
public static void DoPrint(this SSExpression exp, TextWriter writer, int threshold) { if (threshold < 0) { return; } if (exp.Token != null) { if (exp.Token.Type == TokenType.LeftBracket || exp.Token.Type == TokenType.LeftCurlyBracket || exp.Token.Type == TokenType.LeftParentheses) { writer.WriteLine(exp.Token.Value); } else { writer.Write(exp.Token.Value + " "); } } exp.Children.ForEach(a => a.DoPrint(writer, --threshold)); }
public SSExpression(CodeToken tok, SSExpression parent) { this.Token = tok; this.Parent = parent; this.Children = new List <SSExpression>(6); }
public SSFunction(SSExpression body, CodeToken[] parameters, SSScope scope) { this.Body = body; this.Parameters = parameters; this.Scope = scope; }