BlitzMax to Monkey conversion

BlitzMax Forums/BlitzMax Programming/BlitzMax to Monkey conversion

Tachyon(Posted 2011) [#1]
Has anyone come up with an automated way to convert BlitzMax code to Monkey? Obviously, I wouldn't expect conversion to magically translate a Bmax program into fully functional Monkey code, but if it could do 80% of the conversion (and possibly add REMarks to sections that can't be directly converted) it would be very helpful.


ima747(Posted 2011) [#2]
Better off in the monkey forum me thinks... I think you're more looking for a bmax importer for monkey (logical progression as new could be backwards compatible), than a monkey exporter for bmax (reversed, the old doesn't know the new exists since it was created first...).

Haven't delved much into monkey yet, but what I do like to do when porting similar languages is just shove the old source into the new system and then debug the code until it compiles... it's a little tedious but tends to be the fastest way to get stuff working without risk of missing things.


Nigel Brown(Posted 2011) [#3]
Debatable which forum this should be in? @Tachyon why not write something yourself, here is a start.

SuperStrict
'
For Local path:String = EachIn AppArgs[1..]
	'
	Select FileType(path)

		Case 0
			' file does not exsist"
			
		Case 1
			' found single file
			processfile( path )
			
		Case 2
			' found a driectory
			Local dir:Int = ReadDir(path)
			If dir
				'
				Repeat
					Local t:String = NextFile( dir )
					If t="" Exit
					If t="." Or t=".." Continue
					'DebugLog "path = " + path + "\" + t
					processfile( path + "\" + t )
				Forever
			
				CloseDir(dir)
			EndIf

		Default
			DebugLog "filetype = unknown filetype"
	End Select
Next
End

Function processfile( filename:String )
	'	
	Local infile:TStream = ReadFile( filename )
	Assert infile,"failed to load blitzmax file" + filename

	Local outfile:TStream = WriteFile( StripExt(filename) + ".monkey" )
	Assert outfile,"unable to open monkey file" + filename

	WriteLine(outfile,"Import mojo~n")
	WriteLine(outfile,"Function Main ()")
	WriteLine(outfile,"~tNew Game")
	WriteLine(outfile,"End~n")
	WriteLine(outfile,"Class Game Extends App")
	WriteLine(outfile,"~tMethod OnCreate ()~n")
	WriteLine(outfile,"~t~t' Setup code goes here...")
	WriteLine(outfile,"~t~tSetUpdateRate 60")
	WriteLine(outfile,"~tEnd~n")
	WriteLine(outfile,"~tMethod OnUpdate ()")
	WriteLine(outfile,"~t~t' Game update code goes here...")
	WriteLine(outfile,"~tEnd~n")
	WriteLine(outfile,"~tMethod OnRender ()")
	WriteLine(outfile,"~t~t' Game rendering code goes here...")
	WriteLine(outfile,"~tEnd")
	WriteLine(outfile,"End~n")

	Repeat
		'
		Local line:String = ReadLine(infile)
		
		line = line.Replace( "Type","Class" )
		line = line.Replace( ":+", "+=" )
		line = line.Replace( ":-", "-=" )
		line = line.Replace( ":/", "/=" )
	
		WriteLine(outfile, line)
	
	Until Eof(infile)

	CloseFile( outfile )
	CloseFile( infile )
	
End Function


Last edited 2011


Czar Flavius(Posted 2011) [#4]
Nit picking but things such as whether files were loaded shouldn't be asserts as they are problems you want to detect in release mode too!


degac(Posted 2011) [#5]
Well, Monkey programs have a different structure compared to the Blitzmax's one.
1. you need a function Main() enter point
2. your application (in 99% of cases) extends the class App (used by all the platforms)
3. your data need to be placed in a folder called APP_NAME.data
4. your media MUST to be specific and user data file must be .txt files
5. Some methods/functions present in BlitzMax have no equivalent under Monkey or you need to implements itself (ie: SortList, CurrentDate...)

I dont' think it so easy to translate a program...


DrDeath(Posted 2011) [#6]
I dont' think it so easy to translate a program...

Well, a fully fledged translator is probably unrealistic, but a tutorial or at least some documentation ("Differences between BlitzMax and Monkey" or something) would be a great starting point.


degac(Posted 2011) [#7]

but a tutorial or at least some documentation ("Differences between BlitzMax and Monkey" or something) would be a great starting point.



Well, the Monkey forum on this site covers some features of the new language, probably is not specific about 'differences' but it has useful informations.

But I agree a thread about differences between BlitzMax and Monkey should be less 'dispersive' (than reading every single posts) and useful.

In any case these are some of the main differences (not in a particular order)

1. you NEED a Main:int() function
2. quite sure you need to extends the App class (it's implemented in the 'core' of Monkey)
3. The App class has 3 methods: OnCreate(), OnUpdate() and OnRender(): things should be done in the same methods


About the language

1 - the language is CASE SENSITIVE (ie: myClass <> MyClass)
2 - there are (at the moment) only this 'type of data' INT, FLOAT, STRING, BOOL
3 - Type are called Class (as in other languages)
4 - method New supports parameters (so you can do something like

Local mytype:=New MyClass(value1,value2)


Monkey supports a 'short' definition (mytype:= is equivalent to mytype:MyClass)

Overloading is supported, so you can have different methods
method SetValue:int(par1:int)
method SetValue:int(par1:float)
method SetValue:int(par1:String)


the 'closing' of a data block can be replaced by 'End': this could mean End Function, End Method, End Class, End If. Handy

Array:

In BlizMax you can use slice to increase an array, while in monkey there's a specific method

Local arr:int[]
arr=arr[..10]

'---- monkey

Local arr:int[]
arr=arr.Resize(10)


List (and Maps) can have a 'specific' type to handle
'BlitzMax
Local list:tlist=New Tlist
list.AddLast(mytype)
list.AddLast(another_type)
'--- monkey
Local list:=New List<mytype>
list.AddLast(mytype)
list.AddLast(another_type) '<--- should generate an error


The methods for list and maps are similar to the BlitzMax's one: AddLast(),AddFirst(), Foreach... [it misses the Sort method on list at the moment]

Monkey supports 'stack' (while in BlitzMax you need to build your own)

I'm sure there is more!

Last edited 2011


Kryzon(Posted 2011) [#8]
Thanks for the information Degac.
I agree that having a "Differences between Monkey and BMax" thread filled with information will be a real time-saver for people porting projects and starting to play with Monkey.

EDIT: Especially if it's one supported by the author. He's the one that knows the most of it!

Last edited 2011


Wiebo(Posted 2011) [#9]
There is such a thread on the monkey website forums.


Paul "Taiphoz"(Posted 2011) [#10]
what the hell is monkey ?


Gabriel(Posted 2011) [#11]
what the hell is monkey ?

Now see, this is why there should be a prominent notice on the front page of this site. I mean, I know Mark accepts that he's not a marketing man, but cross-selling is pretty fundamental.

Anyway, the answer to the question is:

http://www.monkeycoder.co.nz/


Kryzon(Posted 2011) [#12]
I tried submitting a News entry about it (seeing as there was none), but it wasn't published.
I took it as they want to keep monkey in a 'silent release' for now, until it is polished by bugtracking and feature requests from the first users and brought to a more down-to-earth, what-the-average-consumer-expects status.
[/speculation]


dawlane(Posted 2011) [#13]
@Yavin: I take it you haven't been keeping tabs on what was going on in general discussion when mark did a Little Survey 9 months ago.


Paul "Taiphoz"(Posted 2011) [#14]
Nah iv not really been around for years, I got max when it came out but didnt really do anything with it had no spare time, I check the forums every few months.

I have now check this monkey stuff thats bloody insane. the way the code works is a bit mental tho struggling to get my head around the programme flow.


dawlane(Posted 2011) [#15]
struggling to get my head around the programme flow.

It's best to think of the App (or to be more accurate the Extended) Class as a cut down program loop so there's really no need to write your own (it reminds me of how Borlands CBuilder did things) and to think of the "Main()" function as the programs entry point.
You could possibly do something like so.
Create other Class modules that your program requires e.g. sprites routines etc.
Add any data field members to the extended App Class and any additional methods/functions as required.
In the "OnCreate()" method load any media and set up initial application state
In the "OnUpdate()" method call the application logic routines.
In the "OnRender()" method call the drawing routines.


Paul "Taiphoz"(Posted 2011) [#16]
yeah starting to get how it flow's basically taking my normal update() and render() and making them part of a class instead of being out in the main programme. still sounds odd but I am starting to get it I think.