コード例 #1
0
ファイル: State.cs プロジェクト: kayateia/climoo
        public State()
        {
            _stack = new Stack<Step>();
            _prevStep = null;

            _resultStack = new Stack<object>();

            _constScope = new ConstScope( null );
            _lookupScope = new LookupScope( _constScope );
            _rootScope = new StandardScope( _lookupScope );

            _baggage = new StandardScope();
        }
コード例 #2
0
ファイル: ConstScope.cs プロジェクト: kayateia/climoo
 public void ConstScope()
 {
     string program = @"
     a = testconst
     b = """"
     try:
     testconst = 5
     except exc:
     b = exc
     ";
     Runner r = new Runner();
     ConstScope scope = new ConstScope( r.state.scope );
     scope.setConstant( "testconst", 10 );
     r.pushScope( scope );
     runAndDump( "ConstScope", r, program, null );
 }
コード例 #3
0
ファイル: MathObject.cs プロジェクト: kayateia/climoo
 public static void RegisterObject( ConstScope scope )
 {
     var pt = new Passthrough( new MathObject() );
     pt.registerConst( scope, "math" );
 }
コード例 #4
0
ファイル: Passthrough.cs プロジェクト: kayateia/climoo
 public object callMethod( State state, string name, object[] args )
 {
     if( name == "test2" )
     return "test worked " + String.Join( ",", args.Select( x => x.ToStringI() ).ToArray() );
     else if( name == "dumpcontext" )
     {
     var cxt = state.securityContext;
     return "Current context: " + ( cxt == null ? "none" : cxt.name );
     }
     else if( name == "complex" )
     {
     int which = (int)args[0];
     if( which == 1 )
     {
         return new AsyncAction()
         {
             action = AsyncAction.Action.Call,
             function = (FValue)args[1],
             args = new object[] { "added\r\n" },
             frame = new StackTrace.StackFrame()
             {
                 line = 0,
                 col = 0,
                 unitName = "test",
                 funcName = "PtTest.{0}".FormatI( name )
             }
         };
     }
     else if( which == 2 )
     {
         return new AsyncAction[]
         {
             new AsyncAction()
             {
                 action = AsyncAction.Action.Variable,
                 name = "shouldexist",
                 value = "oh cool"
             },
             new AsyncAction()
             {
                 action = AsyncAction.Action.Callback,
                 callback = st =>
                     {
                         var rv = "{0}".FormatI( st.scope.get( "shouldexist" ) );
                         st.pushResult( rv );
                     }
             }
         };
     }
     else if( which == 3 )
     {
         var constscope = new ConstScope( state.scope );
         constscope.setConstant( "testconst", "bob" );
         return new AsyncAction[]
         {
             new AsyncAction()
             {
                 action = AsyncAction.Action.Code,
                 code = Compiler.Compile( "test", @"
     def innerfunc(x):
     return [x + 1, testconst]
     " )
             },
             new AsyncAction()
             {
                 action = AsyncAction.Action.Call,
                 name = "innerfunc",
                 args = new object[] { 5 },
                 frame = new StackTrace.StackFrame()
                 {
                     line = 0,
                     col = 0,
                     unitName = "test",
                     funcName = "PtTest.{0}".FormatI( name )
                 }
             },
             new AsyncAction()
             {
                 action = AsyncAction.Action.PushScope,
                 scope = constscope
             }
         };
     }
     else if( which == 4 )
     {
         return new AsyncAction[]
         {
             new AsyncAction()
             {
                 action = AsyncAction.Action.Code,
                 code = Compiler.Compile( "test", @"
     def innerfunc(pt):
     return pt.dumpcontext()
     " )
             },
             new AsyncAction()
             {
                 action = AsyncAction.Action.Call,
                 name = "innerfunc",
                 args = new object[] { this },
                 frame = new StackTrace.StackFrame()
             },
             new AsyncAction()
             {
                 action = AsyncAction.Action.PushSecurityContext,
                 securityContext = new TestSC( "Context 1" )
             }
         };
     }
     else if( which == 5 )
     {
         return new AsyncAction[]
         {
             new AsyncAction()
             {
                 action = AsyncAction.Action.Code,
                 code = Compiler.Compile( "test", @"
     def innerfunc(pt):
     return pt.dumpcontext()
     " )
             },
             new AsyncAction()
             {
                 action = AsyncAction.Action.Call,
                 name = "innerfunc",
                 args = new object[] { this },
                 frame = new StackTrace.StackFrame()
             }
         };
     }
     else /* if( which == 6 ) */
     {
         throw new ArgumentException( "No idea what you're talking about!" );
     }
     }
     else
     return null;
 }
コード例 #5
0
ファイル: StringObject.cs プロジェクト: kayateia/climoo
 /// <summary>
 /// Registers the string object in the const scope.
 /// </summary>
 /// <remarks>
 /// This could probably be factored out into a helper class.
 /// </remarks>
 public static void RegisterObject( ConstScope scope )
 {
     scope.setConstant( "string",
     new MetalObject()
     {
         indexLookup = (state, idx) =>
         {
             throw CoralException.GetArg( "Can't index the string object" );
         },
         memberLookup = (state, name) =>
         {
             state.pushResult( new LValue()
             {
                 read = st => {
                     if( name == "join" )
                     {
                         return new FValue( (st2,args) => MethodJoin( st2, args ) );
                     }
                     else
                     {
                         throw CoralException.GetArg( "Unknown method on string object" );
                     }
                 },
                 write = (st,val) => { throw CoralException.GetInvOp( "Can't write to the string object" ); }
             } );
         }
     }
     );
 }