Skip to content

Instantly share code, notes, and snippets.

@nobuyukinyuu
Last active December 26, 2015 17:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nobuyukinyuu/7186162 to your computer and use it in GitHub Desktop.
Save nobuyukinyuu/7186162 to your computer and use it in GitHub Desktop.
example of Monkey's syntax and some language features
'Vars must be declared in Monkey, but Strict forces type declarations, too. Monkey's ALWAYS strongly-typed.
'In non-strict mode, vars, functions, and methods declared without a type are assumed to be Int.
Strict
'Importing crap
Import mojo
'Declares
Const MY_CONST:Int = 1 'You can use MY_CONST% as a shortcut for int type when Strict is on.
Global str:String = "Hello World!" 'You can use a$ as a shortcut for string type. Strings are immutable.
Global GlobalVar:MyObject = New MyObject() 'Monkey uses default constructors, even if one isn't explicitly defined.
Global A$, B#, C% 'You can make multiple declarations per line
'The entry point lies here.
Function Main:Int()
Print str 'Hello World!
If MY_CONST = 1 'Then is optional
Print("Const is 1")
End If 'You can use EndIf or simply End, here, if you prefer.
GlobalVar.Value = Rnd(32)
Print(GlobalVar.Value) 'Properties at work!
Local anArray:Int[] =[1, 2, 3] 'Arrays can be initialized like this
Local emptyArrayOf3:Int[3] 'Or like this
'Notice "Until". Syntactic sugar alternative to "To", it skips the last loop.
For Local i:Int = 0 Until emptyArrayOf3.Length Step 1 'Step 1 is optional
emptyArrayOf3[i] = i + 1 '1,2,3
Next
Local arrayOfArrays:Int[][] 'These are staggered and thus need to be initialized in a loop.
If str = "Hello World!" And MY_CONST = 1 'Logical operators are words, bitwise operators are symbols.
str = str[0 .. 5] 'String slicing
Print str 'Prints "Hello"
str = "Hello World!"
str = str[-6 .. -1]
Print str 'Prints "World"
End If
'Do loops
Repeat
Print("I will execute at least once.")
Until MY_CONST = 1
While MY_CONST <> 1
Print("I might not execute at all.")
Wend
Return 0 'Success
'You can end most blocks with just "End", if you like! I like being verbose
End Function
Class MyObject Implements Something, iPropertyValue
Field m_instanceVar:Float = 0.5 'You can use m_instanceVar# as a shortcut for float type.
Global I_am_static:Int = 42 'Declaring Globals in class scope means they're static.
Field MyList:= New List<Int> 'Generic containers must be initialized, but Late-binding is supported
Field MyStack:= New Stack<String>
Field MyMap:= New Map<String, String> 'Dictionary! Red-black trees etc
Method New(barf:Float = 0.5) 'Optional arguments are supported!
Self.m_instanceVar = barf 'Self refers to
End Method
'I'm an instance method!
Method DoSomething:Void()
m_instanceVar += Rnd() 'Assignment operators, yes. Rnd() without arguments returns 0-1.
End Method
'I'm a static method!
Function DoSomethingElse:Void()
MyObject.I_am_static = Int(Rnd(-99999, 99999)) 'Casting works by Specifying T(castvalue)
End Function
'Properties
'Get
Method Value:Float() Property
Return m_instanceVar
End Method
'Set
Method Value:Void(value:Float) Property
m_instanceVar = value
End Method
End Class
Class MyExtendedObject Extends MyObject
Method DoSomething:Void() 'Overriding method
Print "I'm about to do something!"
Super.DoSomething() 'Calls the parent's method
Print "I did something!"
End Method
'Abstracts
Method Spooky:Void() Abstract
Method Ghosts:Void() Abstract
End Class
'Basic interface
Interface Something
Method DoSomething:Void()
End Interface
'Interface enforcing properties
Interface iPropertyValue
Method Value:Float() Property
Method Value:Void(value:Float) Property
End Interface
'Generic class
Class MyGeneric<T>
Field myVal:T
Method New(t:T)
Self.myVal = t
End Method
Method WhatsMyVal:T()
Return myVal
End Method
End Class
#REM
For some reason, some people like big block comments.
For these people, there is the REM directive.
It probably breaks all of geshi,er, pygments' syntax highlighting, so I'm putting it at the end of this example.
#END
Function IAmATest:Void()
If False
'Did I just break the highlighting?
End If
End Function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment