Example #1
0
        public IActionCode ChangeCondition(Type conditionType)
        {
            var contextTemp       = emit.Locals.Generate().GetRef();
            var DONOT_CHANGE_MODE = emit.Labels.Generate().GetRef();

            emit
            .Local(contextTemp.Def, emit.Types.Object)
            .Stloc(contextTemp)
            .Ldloc(contextTemp)
            .Brfalse(DONOT_CHANGE_MODE)
            .Do(ldCursor)
            .Ldloc(contextTemp)
            .Stfld((ScanCursor c) => c.RootContext)
            ;

            var condition = FindConditionByType(conditionType);

            if (condition == null)
            {
                throw new InvalidOperationException("Internal Error: Condition not found.");
            }

            emit
            .Do(ldCursor)
            .LdMethodDelegate(declaringType, ConditionMethods.GetMethodName(condition.Index), typeof(Scan1Delegate))
            .Stfld((ScanCursor c) => c.CurrentMode)

            .Label(DONOT_CHANGE_MODE.Def)
            .Nop()
            ;

            return(this);
        }
        private ClassSyntax BuildMethod_Scan1(ClassSyntax context)
        {
            logging.Write(
                new LogEntry
            {
                Severity = Severity.Verbose,
                Message  = string.Format("Started compiling Scan1 modes for {0} language", languageName.LanguageName)
            });

            foreach (var condition in data.Grammar.Conditions)
            {
                ITdfaData dfa = condition.Joint.The <ITdfaData>();
                var       dfaSerialization = new DfaSerialization(dfa);
                var       generator        = new ScannerGenerator(dfaSerialization);

                var methodName = ConditionMethods.GetMethodName(condition.Index);
                var args       = context
                                 .Method()
                                 .Static
                                 .Returning(context.Types.Int32)
                                 .Named(methodName)
                                 .BeginArgs();

                var emit = args
                           .Argument(
                    context.Types.Import(typeof(ScanCursor)),
                    args.Args.Generate("cursor"))              // input
                           .EndArgs()
                           .NoInlining
                           .NoOptimization
                           .BeginBody();

                generator.Build(emit);

                context = emit.EndBody();
            }

            logging.Write(
                new LogEntry
            {
                Severity = Severity.Verbose,
                Message  = string.Format("Done compiling Scan1 modes for {0} language", languageName.LanguageName)
            });
            return(context);
        }
        private ClassSyntax Build_Ctor(ClassSyntax context)
        {
            var emit = context
                       .Method()
                       .Public.Instance
                       .Returning(context.Types.Void)
                       .Named(".ctor")
                       .BeginArgs()
                       .Do(args =>
            {
                var type = context.Types.Import(typeof(CilGrammarSource));
                var arg  = args.Args.Generate();
                args.Argument(type, arg);
                return(args);
            })
                       .EndArgs()

                       .BeginBody();

            // Call base constructor:
            // this:
            emit = emit
                   .Ldarg(0) // this
                   .Ldarg(1) // LanguageName
                   .Call(emit.Methods.Import(typeof(LanguageBase).GetConstructor(new[] { typeof(CilGrammarSource) })))
            ;

            emit
            .Ldarg(0)
            .Ldc_I4(data.IsDeterministic ? 1 : 0)
            .Stfld(LanguageBase.Fields.isDeterministic);

            return(emit
                   // Init grammar
                   .Ldarg(0)
                   .Call(emit.Methods.Method(
                             _ => _
                             .StartSignature
                             .Returning(emit.Types.Import(typeof(Grammar)))
                             .DecaringType(declaringTypeRef)
                             .Named(CreateGrammarMethodName)
                             .BeginArgs()
                             .EndArgs()
                             ))
                   .Stfld(LanguageBase.Fields.grammar)

                   // Init state->token table
                   .Ldarg(0)
                   .Call(emit.Methods.Method(
                             _ => _
                             .StartSignature
                             .Returning(emit.Types.Import(typeof(int[])))
                             .DecaringType(declaringTypeRef)
                             .Named(CreateStateToSymbolMethodName)
                             .BeginArgs()
                             .EndArgs()
                             ))
                   .Stfld(LanguageBase.Fields.stateToSymbol)

                   // Init parser action conflicts table
                   .Ldarg(0)
                   .Call(emit.Methods.Method(
                             _ => _
                             .StartSignature
                             .Returning(emit.Types.Import(typeof(int[])))
                             .DecaringType(declaringTypeRef)
                             .Named(CreateParserActionConflictsMethodName)
                             .BeginArgs()
                             .EndArgs()
                             ))
                   .Stfld(LanguageBase.Fields.parserConflictActions)

                   // Init token complexity table
                   .Ldarg(0)
                   .Call(emit.Methods.Method(
                             _ => _
                             .StartSignature
                             .Returning(emit.Types.Import(typeof(int[])))
                             .DecaringType(declaringTypeRef)
                             .Named(CreateTokenComplexityTableMethodName)
                             .BeginArgs()
                             .EndArgs()
                             ))
                   .Stfld(LanguageBase.Fields.tokenComplexity)

                   // Init grammarAction field
                   .Ldarg(0)
                   .LdMethodDelegate(
                       declaringTypeRef,
                       RuleActionMethodName,
                       typeof(ProductionActionDelegate))
                   .Stfld(LanguageBase.Fields.grammarAction)

                   // Init grammarAction field
                   .Ldarg(0)
                   .LdMethodDelegate(
                       declaringTypeRef,
                       MergeActionMethodName,
                       typeof(MergeDelegate))
                   .Stfld(LanguageBase.Fields.merge)


                   // Init tokenIdentities
                   .Ldarg(0)
                   .Call(emit
                         .Methods.Method(
                             _ => _
                             .StartSignature
                             .Returning(emit.Types.Import(LanguageBase.Fields.tokenKeyToId.FieldType))
                             .DecaringType(declaringTypeRef)
                             .Named(CreateTokenKeyToIdMethodName)
                             .BeginArgs()
                             .EndArgs()))
                   .Stfld(LanguageBase.Fields.tokenKeyToId)


                   // Init scan field
                   .Ldarg(0)
                   .LdMethodDelegate(
                       declaringTypeRef,
                       ConditionMethods.GetMethodName(0),
                       typeof(Scan1Delegate))
                   .Stfld(LanguageBase.Fields.scan1)

                   // Init termFactory field
                   .Ldarg(0)
                   .LdMethodDelegate(
                       declaringTypeRef,
                       TermFactoryMethodName,
                       typeof(ScanActionDelegate))
                   .Stfld(LanguageBase.Fields.termFactory)

                   // Init getParserAction field
                   .Ldarg(0)
                   .LdMethodDelegate(
                       declaringTypeRef,
                       GetParserActionMethodName,
                       typeof(TransitionDelegate))
                   .Stfld(LanguageBase.Fields.getParserAction)

                   // Init defaul context factory
                   .Ldarg(0)
                   .LdMethodDelegate(
                       declaringTypeRef,
                       CreateDefaultContextMethodName,
                       typeof(Func <object>))
                   .Stfld(LanguageBase.Fields.createDefaultContext)

                   .Ret()
                   .EndBody());
        }