Commit 7a02fc73 authored by Jack's avatar Jack
Browse files

fixed /= operator in parser, fixed max and min optional values, added sqrt and band

parent bcf6dedd
......@@ -491,6 +491,7 @@ static int llex (LexState *ls, SemInfo *seminfo) {
case '/': {
next(ls);
if (check_next1(ls, '/')) return TK_IDIV;
else if (check_next1(ls, '=')) return TK_ASSDIV;
else return '/';
}
case '~': {
......@@ -514,11 +515,6 @@ static int llex (LexState *ls, SemInfo *seminfo) {
if (check_next1(ls, '=')) return TK_ASSMUL;
else return '*';
}
case '//': {
next(ls);
if (check_next1(ls, '=')) return TK_ASSDIV;
else return '//';
}
case '%': {
next(ls);
if (check_next1(ls, '=')) return TK_ASSMOD;
......
......@@ -100,7 +100,7 @@ void GameView::render()
machine.code().loadAPI();
retro8::io::LoaderP8 loader;
std::string path = !_path.empty() ? _path : "test.p8";
std::string path = !_path.empty() ? _path : "pico_physics.p8";
loader.load(path, machine);
manager->setFrameRate(machine.code().require60fps() ? 60 : 30);
......
......@@ -3,6 +3,7 @@
#include "machine.h"
#include "lua/lua.hpp"
#include <functional>
#include <iostream>
#include <fstream>
......@@ -414,12 +415,20 @@ namespace math
return 1;
}
#define FAIL_IF_NOT_NUMBER(i) do { if (!lua_isnumber(L, i)) { printf("Expected number but got %s\n", lua_typename(L, i)); assert(false); } } while (false)
int min(lua_State* L)
{
assert(lua_isnumber(L, 1));
assert(lua_isnumber(L, 2));
FAIL_IF_NOT_NUMBER(1);
real_t v1 = lua_tonumber(L, 1);
real_t v2 = 0;
if (lua_gettop(L) == 2)
{
FAIL_IF_NOT_NUMBER(2);
v2 = lua_tonumber(L, 2);
}
real_t v1 = lua_tonumber(L, 1), v2 = lua_tonumber(L, 2);
lua_pushnumber(L, std::min(v1, v2));
return 1;
......@@ -427,10 +436,16 @@ namespace math
int max(lua_State* L)
{
assert(lua_isnumber(L, 1));
assert(lua_isnumber(L, 2));
FAIL_IF_NOT_NUMBER(1);
real_t v1 = lua_tonumber(L, 1);
real_t v2 = 0;
if (lua_gettop(L) == 2)
{
FAIL_IF_NOT_NUMBER(2);
v2 = lua_tonumber(L, 2);
}
real_t v1 = lua_tonumber(L, 1), v2 = lua_tonumber(L, 2);
lua_pushnumber(L, std::max(v1, v2));
return 1;
......@@ -475,6 +490,35 @@ namespace math
return 1;
}
int sqrt(lua_State* L)
{
assert(lua_isnumber(L, 1));
real_t v = lua_tonumber(L, 1);
lua_pushnumber(L, sqrtf(v));
return 1;
}
}
namespace bitwise
{
using data_t = uint64_t;
template<typename F>
int bitwise(lua_State* L)
{
assert(lua_isnumber(L, 1));
assert(lua_isnumber(L, 2));
uint64_t a = lua_tonumber(L, 1);
uint64_t b = lua_tonumber(L, 2);
lua_pushnumber(L, F()(a,b));
return 1;
}
}
namespace sound
......@@ -590,6 +634,10 @@ void lua::registerFunctions(lua_State* L)
lua_register(L, "mid", math::mid);
lua_register(L, "abs", math::abs);
lua_register(L, "sgn", math::sgn);
lua_register(L, "sqrt", math::sqrt);
lua_register(L, "band", bitwise::bitwise<std::bit_and<uint64_t>>);
lua_register(L, "music", sound::music);
lua_register(L, "sfx", sound::music);
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment