Exemplo n.º 1
0
        public void CompileFile(TextFile file)
        {
            var raw      = Scanner.Scanner.ScanFile(file);
            var prepared = TokenPreparation.Apply(raw);
            var block    = new BlockLineGrouping().Group(prepared);
            var ast      = Parser.Parser.ParseBlock(block, InjectedContext);
            //var cppFileName = GetTempFileName(Path.GetFileNameWithoutExtension(file.Filename), extension: "cpp");
            var cppFileName = Path.ChangeExtension(file.Filename, extension: "cpp") ?? "test.cpp"; // use this for debugging cpp output

            using (var writer = File.CreateText(cppFileName)) {
                CppGenerator.Generate(writer, ast);
            }
            RunCppCompiler(cppFileName, Path.ChangeExtension(file.Filename, extension: "exe"));
        }
Exemplo n.º 2
0
        public void Filter(IEnumerable <dynamic> inputTokens, IEnumerable <Token> expectedTokens)
        {
            var input = inputTokens.Select(
                t => {
                var s = t as string;
                if (s != null)
                {
                    return(new TokenData {
                        Type = Token.IdentifierLiteral,
                        Range = new TextFileRange {
                            File = new TextFile {
                                Content = s
                            }, End = new TextPosition {
                                Index = s.Length
                            }
                        },
                        Data = new IdentifierLiteral {
                            Content = s
                        }
                    });
                }
                var c = t as char?;
                if (c != null)
                {
                    return(new TokenData {
                        Type = Token.OperatorLiteral,
                        Range = new TextFileRange {
                            File = new TextFile {
                                Content = "" + c
                            }, End = new TextPosition {
                                Index = 1
                            }
                        },
                        Data = new IdentifierLiteral {
                            Content = "" + c
                        }
                    });
                }
                return(new TokenData {
                    Type = t
                });
            });

            var prepared = TokenPreparation.Apply(input).ToArray();

            Assert.AreEqual(expectedTokens, prepared.Select(t => t.Type));
        }
Exemplo n.º 3
0
        public void IdentifierSeparators(dynamic[] inputTokens, IEnumerable <TestNeighor> neighbors)
        {
            var inputIdentifier = inputTokens.Select(
                t => {
                var s = t as string;
                if (s != null)
                {
                    return(new TokenData {
                        Type = Token.IdentifierLiteral,
                        Range = new TextFileRange {
                            File = new TextFile {
                                Content = s
                            }, End = new TextPosition {
                                Index = s.Length
                            }
                        },
                        Data = new IdentifierLiteral {
                            Content = s
                        }
                    });
                }
                return(new TokenData {
                    Type = t
                });
            });

            var preparedIdentifier = TokenPreparation.Apply(inputIdentifier).ToArray();

            Assert.AreEqual(
                neighbors,
                preparedIdentifier.Where(token => token.Type == Token.IdentifierLiteral)
                .Select(t => (TestNeighor)((t.Data as IdentifierLiteral)?.NeighborSeparator ?? SeparatorNeighbor.None))
                .ToList());

            var inputOperator = inputTokens.Select(
                t => {
                var s = t as string;
                if (s != null)
                {
                    return(new TokenData {
                        Type = Token.OperatorLiteral,
                        Range = new TextFileRange {
                            File = new TextFile {
                                Content = s
                            }, End = new TextPosition {
                                Index = s.Length
                            }
                        },
                        Data = new IdentifierLiteral {
                            Content = s
                        }
                    });
                }
                return(new TokenData {
                    Type = t
                });
            });

            var preparedOperator = TokenPreparation.Apply(inputOperator).ToArray();

            Assert.AreEqual(
                neighbors,
                preparedOperator.Where(token => token.Type == Token.OperatorLiteral)
                .Select(t => (TestNeighor)((t.Data as IdentifierLiteral)?.NeighborSeparator ?? SeparatorNeighbor.None))
                .ToList());
        }