Jargon Examples

Hello World

   import Jargon;

   module Main;
      handler [main string[] args];
         [Jargon:print "Hello World\n"];
         return 0;
      end;
   end;

Generic Bubble Sort


   import Jargon;

   -- A module to perform Bubble Sort.
   -- Note that objects must implement a compare handler to use this.
   module BubbleSort;
      -- Perform the sort.
      handler [sort object[] list];
         int x, y;
         for x = 0 upto list'length - 2;
            for y = x + 1 upto list'length - 1;
               if [list[x]:compare list[y]] < 0;
                  [swap list, x, y];
               end;
            end;
         end;
      end;

      -- Swap two objects.
      handler [swap object[] list, int x, int y];
         object temp;
         temp = list[x];
         list[x] = list[y];
         list[y] = temp;
      end;
   end;


   -- Container for floats.
   module Float;
      float value;

      handler [set float v];
         value = v;
      end;

      handler [get];
         return value;
      end;

      handler [compare object o];
         return value - [o:get];
      end;

   end;

   module Main;
      handler [main string[] args];
         object[] list;
         int x;

         -- Read floats from the argument list.
         list = new Float[args'length];
         for x = 0 upto list'length - 1;
            [list[x]:set [Jargon:parseFloat args[x]]];
         end;

         [BubbleSort list];   -- Sort the floats.

         -- Display the sorted floats.
         for x = 0 upto list'length - 1;
            [Jargon:print [list[x]:get]];
            [Jargon:print "\n"];
         end;

         -- Clean up and exit.
         delete list;
         return 0;
      end;
   end;

Home / Programs / Jargon