Skip to content

Commit

Permalink
Moved to README.adoc
Browse files Browse the repository at this point in the history
Made lua 1-indexing compile time configurable
Strip whitespace from function expressions
  • Loading branch information
Jonas Minnberg committed Oct 16, 2020
1 parent 1fe4cf5 commit a074c0c
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 159 deletions.
30 changes: 27 additions & 3 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ See this http://apone.org/bass/part1.html[TUTORIAL] for a tutorial / introductio

=== Example

[source,asm]
[source,ca65]
----
VRAM_LO = $9f00
VRAM_HI = $9f01
!macro SetVRAM(a) {
.lo = a&0xff;
.hi = a>>8;
.lo = <a;
.hi = >a;
lda #.lo
sta VRAM_LO
!if .lo != .hi { lda #.hi }
Expand All @@ -48,6 +48,30 @@ $ lda start,x
text:
!byte "Text to copy", 0
end:
; --- Generate sine table with and without LUA
!define mysin(x, amp, size) {
(sin(x * Math.Pi * 2 / size) * 0.5 + 0.5) * amp
}
sin:
!fill 100 { mysin(i, 255, 100) }
%{ -- LUA code
function make_sin(amp, size)
res = {}
for i=0,size-1,1 do
res[i+1] = (math.sin(i * math.pi * 2/ size) * 0.5 + 0.5) * amp
end
return res
end
}%
sin2:
!fill make_sin(255, 100)
!assert make_sin(5, 5)[1] == round(mysin(1, 5,5))
!assert make_sin(5, 5)[3] == round(mysin(3, 5,5))
----

See http://apone.org/bass/example.asm.html[example.asm] for a full example.
Expand Down
147 changes: 0 additions & 147 deletions README.md

This file was deleted.

1 change: 1 addition & 0 deletions src/functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ void initFunctions(Assembler& a)
a.registerFunction("len",
[](std::vector<uint8_t> const& v) { return v.size(); });
a.registerFunction("round", [](double a) { return std::round(a); });
a.registerFunction("trunc", [](double a) { return std::trunc(a); });

a.registerFunction("compare",
[](std::vector<uint8_t> const& v0,
Expand Down
29 changes: 24 additions & 5 deletions src/meta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ void initMeta(Assembler& assem)

resetTranslate();

static auto is_space = [](char const c) {
return c == ' ' || c == '\t' || c == '\n' || c == '\r';
};

static auto strip_space = [](std::string_view sv) -> std::string_view {
while (is_space(sv.front())) {
sv.remove_prefix(1);
}
while (is_space(sv.back())) {
sv.remove_suffix(1);
}
return sv;
};


assem.registerMeta("test", [&](auto const& text, auto const& blocks) {
auto args = assem.evaluateList(text);
std::string testName;
Expand Down Expand Up @@ -128,7 +143,8 @@ void initMeta(Assembler& assem)
assem.registerMeta("define", [&](auto const& text, auto const& blocks) {
Check(blocks.size() == 1, "Expected block");
auto def = assem.evaluateDefinition(text);
assem.addDefine(def.name, def.args, blocks[0].line, blocks[0].contents);
auto contents = strip_space(blocks[0].contents);
assem.addDefine(def.name, def.args, blocks[0].line, contents);
});

assem.registerMeta("ascii", [&](auto const&, auto const&) {
Expand Down Expand Up @@ -233,9 +249,10 @@ void initMeta(Assembler& assem)
uint8_t d = (*vec)[i];
syms.erase("i");
syms.set("i", any_num(i));
for (auto const& b : blocks) {
for (Assembler::Block const& b : blocks) {
auto contents = strip_space(b.contents);
assem.getSymbols().at<Number>("v") = d;
auto res = assem.evaluateExpression(b.contents, b.line);
auto res = assem.evaluateExpression(contents, b.line);
d = number<uint8_t>(res);
}
mach.writeByte(d);
Expand All @@ -247,8 +264,9 @@ void initMeta(Assembler& assem)
syms.erase("i");
syms.set("i", any_num(i));
for (auto const& b : blocks) {
auto contents = strip_space(b.contents);
assem.getSymbols().at<Number>("v") = d;
auto res = assem.evaluateExpression(b.contents, b.line);
auto res = assem.evaluateExpression(contents, b.line);
d = number<wchar_t>(res);
}
mach.writeByte(char_translate.at(d));
Expand All @@ -261,8 +279,9 @@ void initMeta(Assembler& assem)
syms.set("i", any_num(i));
uint8_t d = 0;
for (auto const& b : blocks) {
auto contents = strip_space(b.contents);
assem.getSymbols().at<Number>("v") = d;
auto res = assem.evaluateExpression(b.contents, b.line);
auto res = assem.evaluateExpression(contents, b.line);
d = number<uint8_t>(res);
}
mach.writeByte(d);
Expand Down
10 changes: 6 additions & 4 deletions src/script.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,11 @@ std::any Scripting::to_any(sol::object const& obj)
}
if (isVec) {
auto index = key.as<size_t>();
if(index > 0) {
vec.resize(index);
vec[index-1] = val.as<uint8_t>();
if (index >= StartIndex) {
if (vec.size() < index + 1 - StartIndex) {
vec.resize(index + 1 - StartIndex);
}
vec[index-StartIndex] = val.as<uint8_t>();
}
} else {
auto s = key.as<std::string>();
Expand All @@ -132,7 +134,7 @@ sol::object Scripting::to_object(std::any const& a)
// TODO: Can we sol make this 'value' conversion?
// return sol::make_object(lua, *av);
sol::table t = lua.create_table();
size_t i = 1;
size_t i = StartIndex;
for (auto v : *av) {
t[i++] = v;
}
Expand Down
2 changes: 2 additions & 0 deletions src/script.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ class Scripting
sol::object to_object(std::any const& a);
std::any to_any(sol::object const& obj);

static constexpr int StartIndex = 1;

private:
std::unique_ptr<sol::state> luap;
sol::state& lua;
Expand Down

0 comments on commit a074c0c

Please sign in to comment.