Commits

Taku Miyakawa committed 0d932da

CORE.try instead of /fun.try (closing issue 501)

  • Participants
  • Parent commits b1fe562

Comments (0)

Files changed (15)

src/doc/language/core.rst

 An exception can also be thrown explicitly by a program,
 using ``throw`` function of an :dfn:`exception value`.
 
-An exception can be caught by ``try`` function of a function value.
-``try`` first calls the function.
+An exception can be caught by ``try`` function.
+``try`` first calls the function specified as the argument.
 If the function terminates normally without throwing an exception,
 ``try`` returns a list containing *true* and the result of the function.
 If the function throws an exception,
 ::
 
     :try_and_report = { (:fun)
-        $fun.try.switch(
+        try($fun).switch(
             [true :Result] {
                 print_line('SUCCESS: ' + Result)
             }

src/main/java/org/kink_lang/kink/internal/box/FunEnhancer.java

 import org.kink_lang.kink.Fun;
 import org.kink_lang.kink.Area;
 import org.kink_lang.kink.Program;
-import org.kink_lang.kink.ValueList;
 import org.kink_lang.kink.ModuleFactory;
 import org.kink_lang.kink.KinkException;
 import org.kink_lang.kink.Define;
         }
 
 
-        @Define( "try" )
-        public Value try_( @Receiver Fun body ) {
-            try {
-                Value bodyResult = body.call();
-                return Value.value( new ValueList( Value.value( true ) , bodyResult ) );
-            } catch ( KinkException exception ) {
-                Value exceptionVal = Value.value( exception );
-                return Value.value( new ValueList( Value.value( false ) , exceptionVal ) );
-            } catch ( Throwable th ) {
-                Value exceptionVal = Value.value( new KinkException( th ) );
-                return Value.value( new ValueList( Value.value( false ) , exceptionVal ) );
-            }
-        }
-
-
         @Define
         public Value with_no_trace( @Receiver Fun origFun ) {
             return Value.value( origFun.withoutTraces() );

src/main/java/org/kink_lang/kink/internal/module/Core.java

         }
 
 
+        @Define( "try" )
+        public Value try_( Fun body ) {
+            try {
+                Value bodyResult = body.call();
+                return Value.value( new ValueList( Value.value( true ) , bodyResult ) );
+            } catch ( KinkException exception ) {
+                Value exceptionVal = Value.value( exception );
+                return Value.value( new ValueList( Value.value( false ) , exceptionVal ) );
+            } catch ( Throwable th ) {
+                Value exceptionVal = Value.value( new KinkException( th ) );
+                return Value.value( new ValueList( Value.value( false ) , exceptionVal ) );
+            }
+        }
+
+
         @Define
         public static Value here() {
             List< Trace > traces = Trace.currentTraces();

src/main/kink/CHECK.kn

 ### RETURNS /exception
 ###     Exception thrown by the block.
 :check_thrown = { (:_block)
-    $_block.try.switch(
+    try($_block).switch(
         [false :Exception] { Exception }
         [true :Result] {
             throw(expand('exception not thrown, return value: #{ Result.show }'))

src/main/kink/GUARD.kn

 
 :guarding = { (:_block)
     :Scope_guard = Scope_proto.child('Actions' [])
-    { _block(Scope_guard) } .try.switch(
+    try { _block(Scope_guard) } .switch(
         [true :Result] {
             Scope_guard.:result = const_fun(Result)
             Scope_guard._after_success
             Scope_guard.result
         } {
             :Action = Scope_guard.Actions.pop
-            { Action.act_for_success } .try.switch(
+            try { Action.act_for_success } .switch(
                 [true :Result] {
                     Scope_guard._after_success
                 }
     '_after_failure' { > :Scope_guard
         { Scope_guard.Actions.any? } .loop {
             :Action = Scope_guard.Actions.pop
-            { Action.act_for_failure } .try.switch(
+            try { Action.act_for_failure } .switch(
                 [false :New_exception] {
                     Scope_guard.exception.addSuppressed(New_exception)
                 }

src/main/kink/REPL.kn

 # Executes the program
 :_execute = { (:Program :Env)
     :run_source = Program.compile.with_no_trace
-    :run_and_show = {
+    try {
         :Result = run_source(Env)
         Env.:last = const_fun(Result)
         :Records = switch(
             (any) { ['=> ' + Result.show] }
         )
         ['success' Records]
-    }
-    $run_and_show.try.switch(
+    } .switch(
         [true :Result] { Result }
         [false :Exception] {
             Env.:last_exception = const_fun(Exception)

src/main/kink/TEST.kn

             _pending_test_result(Self_test.pending_reason)
         } {
             :_test_block = Self_test.Maybe_test_block.first
-            {
+            try {
                 GUARD.guarding { (:Guard)
                     Self_test.Parent_virtual_group._register_ref_clobber_blocks_to_be_run_on_exit(Guard)
                     Self_test.Parent_virtual_group._register_after_blocks_to_be_run_on_exit(Guard)
                     Self_test.Parent_virtual_group._run_before_blocks
                     _test_block
                 }
-            } .try.switch(
+            } .switch(
                 [true any] { Success_test_result }
                 [false :Exception] { _failure_test_result(Exception) }
             )

src/main/kink/_internal/START.kn

 
 # Sart the program
 :start = { (:Args)
-    { _parse_and_do_command(Args) } .try.switch(
+    try { _parse_and_do_command(Args) } .switch(
         [true :Result] { Result }
         [false :Exception] {
             Exception.setStackTrace([])
         }
         (any) {
             :File_name = Args_after_opts.first
-            :Program = { path(File_name).program } .try.switch(
+            :Program = try { path(File_name).program } .switch(
                 [true :Result] { Result }
                 [false :Exception] { die(expand('kink: #{Exception.message}')) }
             )

src/test/bintest/stack_overflow_test.kn

 use('TEST.test')
 
 test('StackOverflowError is caught by try function') {
-    :Kink_process_builder = ProcessBuilder.new('bin/kink' '-e' '{ :foo.do { foo 1 } } .try')
+    :Kink_process_builder = ProcessBuilder.new('bin/kink' '-e' 'try { :foo.do { foo 1 } }')
     Kink_process_builder.redirectInput(ProcessBuilder.Redirect.INHERIT)
     Kink_process_builder.redirectOutput(ProcessBuilder.Redirect.INHERIT)
     Kink_process_builder.redirectError(ProcessBuilder.Redirect.INHERIT)

src/test/java/org/kink_lang/kink/internal/box/FunEnhancerTest.java

 import org.kink_lang.kink.Fun;
 import org.kink_lang.kink.Area;
 import org.kink_lang.kink.Program;
-import org.kink_lang.kink.ValueList;
 import org.kink_lang.kink.Trace;
 import org.kink_lang.kink.KinkException;
 import org.kink_lang.kink.StubFun;
 import org.kink_lang.kink.internal.Checker;
 import org.kink_lang.common.test.ClassMockery;
 
-import java.util.List;
 import java.util.Collections;
 import java.util.concurrent.Callable;
 import java.util.regex.Pattern;
     }
 
 
-    // Tests FUN.try {{{1
-
-
-    @Test
-    public void test_try_success() throws Throwable {
-        final Value bodyResult = Value.value( "result" );
-        mockery.checking( new Expectations() { {
-            oneOf( body ).run( Value.base() );
-            will( returnValue( bodyResult ) );
-        } } );
-        Value tryResult = Value.value( body ).call( new GetSite( "try" ) );
-        assertThat( tryResult.unbox()
-                , is( (Object) new ValueList( Value.value( true ) , bodyResult ) ) );
-    }
-
-
-    @Test
-    public void test_try_failure_by_kink_exception() throws Throwable {
-        final KinkException exception = new KinkException( "exception" );
-        mockery.checking( new Expectations() { {
-            oneOf( body ).run( Value.base() );
-            will( throwException( exception ) );
-        } } );
-        Value tryResult = Value.value( body ).call( new GetSite( "try" ) );
-        assertThat( tryResult.unbox()
-                , is( (Object) new ValueList( Value.value( false ) , Value.value( exception ) ) ) );
-    }
-
-
-    @Test
-    public void test_try_failure_by_npe() {
-        Fun caller = new Fun() {
-            @Override public Value run( Value receiver , Value ... args ) {
-                // Causes NPE in try_ method
-                return new FunEnhancer.FunMethods().try_( null );
-            }
-        };
-        List< Value > tryResult = ( (ValueList) caller.call().unbox() ).getElements();
-        assertThat( tryResult.size() , is( 2 ) );
-        assertThat( tryResult.get( 0 ).asBool( "" ) , is( false ) );
-        KinkException exception = (KinkException) tryResult.get( 1 ).unbox();
-        assertThat( exception.getCause() , is( NullPointerException.class ) );
-    }
-
-
-    // }}}
-
-
     private final Value foo = Value.value();
     private final Value bar = Value.value();
     private final Value owner = Value.value();

src/test/java/org/kink_lang/kink/internal/module/CoreTest.java

     }
 
 
+    // Tests try(FUN) {{{1
+
+
+    /** Mock function. */
+    private final Fun body = mockery.mock( StubFun.class , "body" );
+
+
+    @Test
+    public void test_try_success() throws Throwable {
+        final Value bodyResult = Value.value( "result" );
+        mockery.checking( new Expectations() { {
+            oneOf( body ).run( Value.base() );
+            will( returnValue( bodyResult ) );
+        } } );
+        Value tryResult = core.call( new GetSite( "try" ) , Value.value( body ) );
+        assertThat( tryResult.unbox()
+                , is( (Object) new ValueList( Value.value( true ) , bodyResult ) ) );
+    }
+
+
+    @Test
+    public void test_try_failure_by_kink_exception() throws Throwable {
+        final KinkException exception = new KinkException( "exception" );
+        mockery.checking( new Expectations() { {
+            oneOf( body ).run( Value.base() );
+            will( throwException( exception ) );
+        } } );
+        Value tryResult = core.call( new GetSite( "try" ) , Value.value( body ) );
+        assertThat( tryResult.unbox()
+                , is( (Object) new ValueList( Value.value( false ) , Value.value( exception ) ) ) );
+    }
+
+
+    @Test
+    public void test_try_failure_by_npe() throws Exception {
+        Fun caller = new Fun() {
+            @Override public Value run( Value receiver , Value ... args ) throws Exception {
+                // Causes NPE in try_ method
+                return new Core.CoreMethods().try_( null );
+            }
+        };
+        List< Value > tryResult = ( (ValueList) caller.call().unbox() ).getElements();
+        assertThat( tryResult.size() , is( 2 ) );
+        assertThat( tryResult.get( 0 ).asBool( "" ) , is( false ) );
+        KinkException exception = (KinkException) tryResult.get( 1 ).unbox();
+        assertThat( exception.getCause() , is( NullPointerException.class ) );
+    }
+
+
+    // }}}
+
     @Test
     public void test_here_return_area() {
         Program program = new Program( "here.kn" , "here" );
 
 }
 
-// vim: et sw=4 sts=4
+// vim: et sw=4 sts=4 fdm=marker

src/test/kink/CHECK_test.kn

     }
 
     test('fails when the block does not throw an exception') {
-        [false :Exception_by_check_thrown] = { check_thrown { 'NOP' } } .try
+        [false :Exception_by_check_thrown] = try { check_thrown { 'NOP' } }
         Exception_by_check_thrown.show.check { \0.any?('NOP') }
     }
 
     test('succeeds when the block throws an exception') {
-        [true :Actual_exception] = { check_thrown { Exception.throw } } .try
+        [true :Actual_exception] = try { check_thrown { Exception.throw } }
         Actual_exception.check(Exception)
     }
 }

src/test/kink/enhancertest/KinkException_test.kn

 
 test {
     :Exception = exception('tlon uqbar orbis-tertius')
-    { Exception.throw } .try.switch(
+    try { Exception.throw } .switch(
         [true :Result] { throw('Exception should be thrown') }
         [false :Thrown] { Thrown.check(Exception) }
     )

src/test/kink/enhancertest/Throwable_test.kn

 test {
     use('java.lang.NullPointerException')
     :Npe = NullPointerException.new
-    { Npe.throw } .try.switch(
+    try { Npe.throw } .switch(
         [true :Result] { throw('fail') }
         [false :Exception] {
             Exception.getCause.check(Npe)

src/test/kink/issue/issue_0074_test.kn

 # http://code.google.com/p/kink-lang/issues/detail?id=74
 
 test {
-    :Result = { 42 } .try.switch([true :Result] { Result })
+    :Result = try { 42 } .switch([true :Result] { Result })
     Result.check(42)
 }