Example #1
0
        static void Main(string[] args)
        {
            // new Thread(() =>
            // {
            const string path          = "/home/chenjinsong/RiderProjects/SchemeSharp/SchemeSharp/example.txt";
            var          lexer         = new SexprLexer(CharStreams.fromTextReader(new StreamReader(path)));
            var          parser        = new SexprParser(new BufferedTokenStream(lexer));
            var          sexpr         = new SexprVisitor().VisitSexpr(parser.sexpr());
            var          ast           = Scheme.FromSexpr(sexpr);
            var          globalContext = new SchemeContext()
                                         .Define("write", new SchemeProcedure((value, context, consumer) =>
            {
                var(arg0, _) = ((SchemePair)value).Pair;
                Console.Write(arg0);
                consumer(context, Scheme.Null());
            }))
                                         .Define("call/cc", new SchemeProcedure((args, context, continuation) =>
            {
                var(arg0, _)              = ((SchemePair)args).Pair;
                var procedure             = (SchemeProcedure)arg0;
                var continuationProcedure = new SchemeProcedure((args, context2, cc) =>
                {
                    var arg0 = args.Item(0);
                    continuation(context, arg0);
                });
                procedure.Invoke(Scheme.Cons(continuationProcedure, Scheme.Null()), context, continuation);
            }))
                                         .Define("+", new SchemeProcedure((args, context, continuation) =>
            {
                continuation(context, args.Item(0).AsNumber() + args.Item(1).AsNumber());
            }))
                                         .Define("-", new SchemeProcedure((args, context, continuation) =>
            {
                continuation(context, args.Item(0).AsNumber() - args.Item(1).AsNumber());
            }))
                                         .Define("*", new SchemeProcedure((args, context, continuation) =>
            {
                continuation(context, args.Item(0).AsNumber() * args.Item(1).AsNumber());
            }));
            var interpreter = new Interpreter(globalContext);

            Scheme.ForEachInList(ast, statement =>
            {
                // interpreter.Evaluate(statement, globalContext, (context, value) =>
                // {
                //     globalContext = context;
                //     Console.WriteLine(value);
                // });
                interpreter.EvaluateGlobal(statement, Console.WriteLine);
            });
            Console.WriteLine("Program exited");

            // }, 1024*1024*512).Start();
        }
Example #2
0
    static void Main(string[] args)
    {
        var options = lfmt.FormatOptions.Parse(args);

        if (options == null)
        {
            return;
        }

        ICharStream charStream = null;

        switch (options.inputMode)
        {
        case "text":
            charStream = CharStreams.fromstring(options.input);
            break;

        case "file":
            charStream = CharStreams.fromPath(options.input);
            break;

        case "stdin":
            charStream = CharStreams.fromTextReader(Console.In);
            break;

        default:
            // show error messages
            return;
        }

        IFormatWriter writer = null;

        switch (options.outputMode)
        {
        case "stdout":
            writer = new StdoutWriter();
            break;

        case "file":
            writer = new FileWriter(options.output);
            break;

        default:
            // show error messages
            return;
        }

        lfmt.Formatter.Format(charStream, writer, options);

        charStream.Release(0);
        writer.Close();
    }
Example #3
0
        private TokenizationResult GetSentenceTokens(string sentence)
        {
            this.EntityLexer.SetInputStream(CharStreams.fromTextReader(new StringReader(sentence)));

            this.EntityLexer.RemoveErrorListeners();

            TokenizationResult tokenizationResult = new TokenizationResult();

            this.EntityLexer.AddErrorListener(new ErrorLexerListener(tokenizationResult, sentence));

            tokenizationResult.Tokens = this.EntityLexer.GetAllTokens();

            return(tokenizationResult);
        }
Example #4
0
        public static RawAffChart ParseFromPath(string path)
        {
            RawAffChart chart = new RawAffChart();

            using (StreamReader reader = new StreamReader(path))
            {
                // manually parse metadata
                int  metadataLinesCount          = 0;
                bool AudioOffsetSet              = false;
                bool TimingPointDensityFactorSet = false;
                while (true)
                {
                    string line = reader.ReadLine();
                    if (line == null)
                    {
                        break;
                    }
                    metadataLinesCount++;
                    if (line == "-")
                    {
                        break;
                    }
                    int pos = line.IndexOf(":");
                    if (pos < 0)
                    {
                        chart.warning.Add($"第 {metadataLinesCount} 行:{line} 元信息格式错误,此行会被忽略");
                        continue;
                    }
                    string key   = line.Substring(0, pos);
                    string value = line.Substring(pos + 1);
                    if (key == "AudioOffset")
                    {
                        if (AudioOffsetSet)
                        {
                            chart.warning.Add($"第 {metadataLinesCount} 行:AudioOffset 被重复设置为 {value},此行会被忽略");
                        }
                        else
                        {
                            int offset;
                            if (Int32.TryParse(value, out offset))
                            {
                                chart.AudioOffset = offset;
                                AudioOffsetSet    = true;
                            }
                            else
                            {
                                chart.warning.Add($"第 {metadataLinesCount} 行:AudioOffset 的值不是整数,此行会被忽略");
                            }
                        }
                    }
                    else if (key == "TimingPointDensityFactor")
                    {
                        if (TimingPointDensityFactorSet)
                        {
                            chart.warning.Add($"第 {metadataLinesCount} 行:TimingPointDensityFactor 被重复设置为 {value},此行会被忽略");
                        }
                        else
                        {
                            float factor;
                            if (float.TryParse(value, out factor))
                            {
                                if (factor > 0)
                                {
                                    chart.TimingPointDensityFactor = factor;
                                    TimingPointDensityFactorSet    = true;
                                }
                                else
                                {
                                    chart.warning.Add($"第 {metadataLinesCount} 行:TimingPointDensityFactor 的值不是正数,此行会被忽略");
                                }
                            }
                            else
                            {
                                chart.warning.Add($"第 {metadataLinesCount} 行:TimingPointDensityFactor 的值不是浮点数,此行会被忽略");
                            }
                        }
                    }
                    else
                    {
                        if (chart.additionalMetadata.ContainsKey(key))
                        {
                            chart.warning.Add($"第 {metadataLinesCount} 行:{key}被重复设置为{value},此行会被忽略");
                        }
                        else
                        {
                            chart.additionalMetadata.Add(key, value);
                        }
                    }
                }
                ICharStream            stream             = CharStreams.fromTextReader(reader);
                ArcaeaFileFormatLexer  lexer              = new ArcaeaFileFormatLexer(stream);
                AffErrorListener <int> lexerErrorListener = new AffErrorListener <int>(chart, metadataLinesCount);
                lexer.RemoveErrorListeners();
                lexer.AddErrorListener(lexerErrorListener);
                ITokenStream           tokenStream = new CommonTokenStream(lexer);
                ArcaeaFileFormatParser parser      = new ArcaeaFileFormatParser(tokenStream);
                parser.BuildParseTree = true;
                AffErrorListener <IToken> parserErrorListener = new AffErrorListener <IToken>(chart, metadataLinesCount);
                parser.RemoveErrorListeners();
                parser.AddErrorListener(parserErrorListener);
                IParseTree tree = parser.file();
                if (chart.error.Count == 0)
                {
                    ParseTreeWalker.Default.Walk(new AffTypeChecker(chart, metadataLinesCount), tree);
                }
            }
            foreach (string warning in chart.warning)
            {
                Debug.LogWarning(warning);
            }
            foreach (string error in chart.error)
            {
                Debug.LogError(error);
            }
            return(chart);
        }