summaryrefslogtreecommitdiff
path: root/src/main.c
blob: 3557bc5e6b284a1e81f21a3300559df74c741b1b (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "main.h"
#include "state.h"
#include "lua_core.h"

inline static void printVersion() {
	if ( VERSION_DEV ) {
		TraceLog( LOG_INFO, "ReiLua %d.%d.%d-Dev", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
	}
	else {
		TraceLog( LOG_INFO, "ReiLua %d.%d.%d", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH );
	}

#ifdef LUA_VERSION
	TraceLog( LOG_INFO, "%s", LUA_VERSION );
#endif

#ifdef LUAJIT_VERSION
	TraceLog( LOG_INFO, "%s", LUAJIT_VERSION );
#endif
}

int main( int argn, const char **argc ) {
	char exePath[ STRING_LEN ] = { '\0' };
	bool interpret_mode = false;

	if ( 1 < argn ) {
		if ( strcmp( argc[1], "--version" ) == 0 || strcmp( argc[1], "-v" ) == 0 ) {
			printVersion();
			return 1;
		}
		else if ( strcmp( argc[1], "--help" ) == 0 || strcmp( argc[1], "-h" ) == 0 ) {
			printf( "Usage: ReiLua [Options] [Directory to main.lua or main]\nOptions:\n-h --help\tThis help\n-v --version\tShow ReiLua version\n-i --interpret\tInterpret mode [File name]\n" );
			return 1;
		}
		else if ( strcmp( argc[1], "--interpret" ) == 0 || strcmp( argc[1], "-i" ) == 0 ) {
			interpret_mode = true;

			if ( 2 < argn ) {
				sprintf( exePath, "%s/%s", GetWorkingDirectory(), argc[2] );
			}
		}
		else{
			sprintf( exePath, "%s/%s", GetWorkingDirectory(), argc[1] );
		}
	}
	else {
		sprintf( exePath, "%s/", GetWorkingDirectory() );
	}

	if ( interpret_mode ) {
		stateInitInterpret( argn, argc );

		lua_State *L = state->luaState;
		lua_pushcfunction( L, luaTraceback );
		int tracebackidx = lua_gettop( L );

		luaL_loadfile( L, exePath );

		if ( lua_pcall( L, 0, 0, tracebackidx ) != 0 ) {
			TraceLog( LOG_ERROR, "Lua error: %s", lua_tostring( L, -1 ) );
			return false;
		}
	}
	else {
		printVersion();
		stateInit( argn, argc, exePath );
		state->run = luaCallMain();

		while ( state->run ) {
			if ( WindowShouldClose() ) {
				state->run = false;
			}
			luaCallUpdate();
			luaCallDraw();
		}
		luaCallExit();
	}
	stateFree();

	return 0;
}