Ejemplo n.º 1
0
        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 static ClassDefinition Parse(Lexer lexer)
 {
     var className = lexer.ReadToken();
     lexer.SkipToken(); // :
     var baseClassName = lexer.ReadToken();
     var body = lexer.ReadBlock("{", "}");
     return new ClassDefinition(className, baseClassName, body);
 }
        public CompilationUnit(DSLProject project, String sourceText)
        {
            Project = project;

            Lexer lexer = new Lexer(sourceText);
            HeaderIncludes.Add(new Include(project.Name + "Types"));

            ClassDefinition = ClassDefinition.Parse(lexer);
        }
Ejemplo n.º 5
0
        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()));
        }
Ejemplo n.º 6
0
        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);
            }
        }