public static Block Parse(String body) { var block = new Block(); var lexer = new Lexer(body); while (lexer.HasNext()) { // Conditional if (lexer.PeekToken() == "if") { lexer.SkipToken(); // Absorb if Expression condition = new Expression(lexer.ReadBlock("(", ")")); Block b = Block.Parse(lexer.ReadBlock("{", "}")); IfStatement statement = new IfStatement(condition, b); block.Statements.Add(statement); } else if(lexer.PeekToken(2) == "=") { var type = lexer.ReadToken(); var name = lexer.ReadToken(); lexer.SkipToken(); var value = lexer.ReadUntil(";"); var variableDefinition = new VariableDefinition(name, type, value); block.Statements.Add(variableDefinition); } else { string str = lexer.ReadUntil(";"); block.Statements.Add( Expression.Parse(str) ); lexer.SkipToken(); // Skip ; } } return block; }
private void ParseClassBody(String body) { Lexer lexer = new Lexer(body); while (lexer.HasNext()) { if (lexer.PeekToken(2) == ";") // Member definition without initial value { var member = CreateMember(lexer.ReadToken(), lexer.ReadToken()); lexer.SkipToken(); // ; Members.Add(member); } if (lexer.PeekToken(2) == "=") // Member definition with initial value { var member = CreateMember(lexer.ReadToken(), lexer.ReadToken(), lexer.ReadBlock("=", ";")); Members.Add(member); } if (lexer.PeekToken(2) == "(") // Function definition { Function function = Function.Parse( lexer.ReadToken(), // return type lexer.ReadToken(), // name lexer.ReadBlock("(", ")"), // arguments lexer.ReadBlock("{", "}")); // body Functions.Add(function); } } }
public void ParseArguments(String arguments) { if (arguments.Trim() == String.Empty) return; Lexer lexer = new Lexer(arguments); while(lexer.HasNext(3)) { Arguments.Add(new FunctionParameter( lexer.ReadToken(), lexer.ReadToken() )); lexer.SkipToken(); } Arguments.Add(new FunctionParameter(lexer.ReadToken(), lexer.ReadToken())); }
public void FindAllTypes() { Lexer typeLexer = new Lexer(File.ReadAllText(ProjectRoot + @"\TypeDefinitions.dsl")); while (typeLexer.HasNext()) { typeLexer.SkipToken(); // extern String metaType = typeLexer.ReadToken(); String name = typeLexer.ReadToken(); String defaultValue = "nullptr"; if (metaType == "struct") { typeLexer.SkipToken(); // = defaultValue = typeLexer.ReadToken(); if (defaultValue == "default") defaultValue = name + "()"; } typeLexer.SkipToken(); // ; Type type = new Type(name, defaultValue, (metaType == "struct") ? MetaType.ValueType : MetaType.ReferenceType, DeclarationType.Extern); ExternTypes.Add(type); } }