Skip to content

Banner

Welcome to Monkey2!#

Monkey2 is an open-source, user-friendly, cross-platform programming language.

It is designed and developed by Mark Sibly, creator of the ‘Blitz’ range of languages (BlitzBasic, Blitz2D, Blitz3D, BlitzPlus, BlitzMax).

Monkey2 is a crowd funded project. If you like what you see, please consider contributing to development either via paypal donation or by becoming a Patreon supporter.

While staying true to the ‘basic’ style of the original blitz languages, Monkey2 offers some very powerful new features including:

  • Function overloading

    Functions with the same name can have different parameter types.

    1
    2
    3
    4
    5
    6
    7
    Function Add( a:Int, b:Int )
        Return a + b
    End
    
    Function Add( a:Float, b:Float )
        Return a + b
    End
    
  • 'First class' functions

    Functions (and methods) can be stored in variables and passed to/from other functions.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    Function Test1()
        Print "Test1!"
    End
    
    Function Test2()
        Print "Test2!"
    End
    
    Function Tester( test:Void() )
        test()
    End
    
    Function Main()
        Tester( Test1 )
        Tester( Test2 )
    End
    
  • Lambda functions

    Lambda functions allow you to create closures:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    Function Test( func:Void() )
        func()
    End
    
    Function Main()
        For Local i:=0 Until 10
            Test( Lambda()
                    Print i
                End )
        Next
    End
    
  • Templates / Generics

    Classes, interfaces, structs, methods and functions can have ‘type’ parameters:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    Struct Rect<T>
        Field x:T
        Field y:T
        Field width:T
        Field height:T
    End
    
    Function Main()
        Local r := New Rect<Float>
    End
    
  • Structs

    Structs are similar to classes in that they encapsulate member data, but differ in that they are passed around ‘by value’ instead of ‘by reference’.

    This allows structs to be efficiently created on the stack without any garbage collection overhead.

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    Struct S
        Field data:Int=10
    End
    
    Function Test( s:S )
        s.data = 100
    End
    
    Function Main()
        Local s := New S  ' Create a new S on the stack (very fast!)
        Test( s )         ' Test gets a copy of 's'.
        Print s.data      ' Print '10'
    End
    
  • Fibers

    Fibers provide support for ‘cooperative’ multithreading:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    Function Server( host:String,service:String )
        Local server:=Socket.Listen( host,service )
    
        Repeat
            Local client:=server.Accept()
    
            New Fiber( Lambda()
                        Local data:=client.Receive(...)
                    End )
        Forever
    End
    
  • Operator overloading

    Operator overloading allows you to override the meaning of the built-in language operators, making for more expressive code:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    Struct Vec2
        Field x:Float
        Field y:Float
    
        Method New( x:float,y:Float )
            Self.x=x
            Self.y=y
        End
    
        Operator+:Vec2( v:Vec2 )
            Return New Vec2( x+v.x,y+v.y )
        End
    
        Operator To:String()
            Return "Vec2("+x+","+y+")"
        End
    
    End
    
    Function Main()
        Local v0 := New Vec2( 10,20 )
        Local v1 := New Vec2( 30,40 )
    
        Print v0+v1
    End
    
  • Optional reflection features

    Monkey2 includes an optional reflection system that allows you to inspect and modify variables and values at runtime:

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    #Import "<reflection>"
    
    Class C
        Method Update( msg:String )
            Print "C.Update : msg="+msg
        End
    End
    
    Function Main()
        Local c:=New C
    
        Local type := Typeof( c )
        Print type
    
        Local decl:=type.GetDecl( "Update" )
        decl.Invoke( c,"Hello World!" )
    End
    
  • Class extensions

    Class extensions allow you to add extra methods and functions to existing classes.

  • Fully garbage collected

    Monkey2 provides a ‘mostly’ incremental garbage collector that efficiently collects garbage as it runs without any of those annoying ‘sweep’ spikes found in typical garbage collectors.