Engine 0.0.6
Game engine in lua
Loading...
Searching...
No Matches
main.lua
Go to the documentation of this file.
1--! @file src/lib/repl/main.lua
2--! @short Read Eval Print Loop
3--! @brief an interpreter to debuging the game via stdio.
4--! @par Extended Backus-Naur Form
5--! @startebnf
6--! line = exit | frame_skip | [frame_skip], variable, ["=", value (* assignment *)];
7--! frame_skip = [digit], "!" ;
8--! digit = { ? 0 - 9 ? }- ;
9--! exit = "?" ;
10--! @endebnf
11local zeebo_module = require('src/lib/engine/module')
12local engine_game = require('src/lib/engine/game')
13local engine_math = require('src/lib/engine/math')
14local engine_color = require('src/lib/object/color')
15local engine_http = require('src/lib/engine/http')
16local engine_encoder = require('src/lib/engine/encoder')
17local engine_draw_fps = require('src/lib/draw/fps')
18local engine_draw_poly = require('src/lib/draw/poly')
19local protocol_curl = require('src/lib/protocol/http_curl')
20local library_csv = require('src/third_party/csv/rodrigodornelles')
21local library_json = require('src/third_party/json/rxi')
22local application_default = require('src/lib/object/application')
23local game = require('src/lib/object/game')
24local std = require('src/lib/object/std')
25
26local function line_skip_frames(line_src)
27 local frames, line = line_src:match('(%d+)!(.*)')
28 if frames and line then
29 return frames, line
30 end
31 frames, line = line_src:match('(!)(.*)')
32 if frames and line then
33 return 1, line
34 end
35 return 0, line_src
36end
37
38local function line_assignment(line)
39 local variable, assignment = line:match('(.*)=(.*)')
40 if variable and assignment then
41 return variable, assignment
42 end
43 return line, ''
44end
45
46local function evaluate(var, assign, std, game, application)
47 local script = ''
48
49 if assign and #assign > 0 then
50 script = 'return function(std, game, application)\n'..var..'=('..assign..')\n return ('..var..')\nend'
51 elseif var and #var > 0 then
52 script = 'return function(std, game, application)\nreturn ('..var..')\nend'
53 end
54
55 if script and #script > 0 then
56 local ok, output = pcall(function()
57 local func, err = load(script)
58 if func then
59 local result = func()
60 return result(std, game, application)
61 else
62 error(err)
63 end
64 end)
65
66 return ok, tostring(output)
67 end
68
69 return true, ''
70end
71
72local function main()
73 local frames = 0
74 local variable = ''
75 local assignment = ''
76 local started = false
78
79 -- init the game
85 :package('math', engine_math.clib)
86 :package('random', engine_math.clib_random)
90 :run()
91
92 while true do
93 local index = 1
94 local ok, line = pcall(io.read)
95
96 if not ok or line == nil or line == '?' then
97 break
98 end
99
100 frames, line = line_skip_frames(line)
101 variable, assignment = line_assignment(line)
102 frames = tonumber(frames)
103
104 local ok, output = evaluate(variable, assignment, std, game, application)
105 if ok then
106 print(output)
107 else
108 io.stderr:write(output)
109 print('\n')
110 end
111
112 if not started and frames > 0 and application.callbacks.init then
113 application.callbacks.init(std, game)
114 started = true
115 end
116
117 while index <= frames do
118 if application.callbacks.loop then
119 application.callbacks.loop(std, game)
120 end
121 if application.callbacks.draw then
122 application.callbacks.draw(std, game)
123 end
124 index = index + 1
125 end
126 end
127end
128
129if not package.loaded['modulename'] then
130 main()
131end
local function file(self, file)
local function require(std, game, application)
local function loadgame(game_file)
safe load game
local function package(self, module_name, module, custom)
local json
Definition rxi.lua:2
local function exit(self)
local function error(self, handler_func)
local function draw(std, game)
local function http(std, game)
local function run(args)
local function init(args)
local ok
Definition main.lua:40
local library_csv
Definition main.lua:13
local engine_draw_fps
Definition main.lua:11
local library_json
Definition main.lua:14
local application
Definition main.lua:16
local engine_game
Definition main.lua:5
local game
Definition main.lua:17
local std
Definition main.lua:18
local engine_color
Definition main.lua:7
local engine_math
Definition main.lua:4
local engine_draw_poly
Definition main.lua:12
local engine_http
Definition main.lua:6
local zeebo_module
Definition main.lua:1
local engine_encoder
Definition main.lua:2
function love load(args)
local application_default
Definition main.lua:22
local function line_skip_frames(line_src)
local protocol_curl
Definition main.lua:19
local function evaluate(var, assign, std, game, application)
local function main()
local function line_assignment(line)
local function line(x1, y1, x2, y2)
local function color(c)
local math
Definition draw.lua:1
local function loop(std, game, application, dt)
local function fps(self, show, x, y)