add cppcheck and test targets; some code size reductions

llvm
Clyne 2 years ago
parent 970bbb473c
commit 967775313d

@ -9,10 +9,10 @@ LIBFILE := libalee/libalee.a
all: alee all: alee
msp430: CXX := msp430-elf32-g++ msp430: CXX := msp430-elf32-g++
msp430: AR := msp430-elf32-ar msp430: AR := msp430-elf32-gcc-ar
msp430: CXXFLAGS += -Os -mmcu=msp430g2553 -ffunction-sections -fdata-sections msp430: CXXFLAGS += -Os -mmcu=msp430g2553 -ffunction-sections -fdata-sections
msp430: CXXFLAGS += -DMEMDICTSIZE=200 msp430: CXXFLAGS += -DMEMDICTSIZE=200 -flto
msp430: LDFLAGS += -L/opt/msp430-elf32/include -Wl,-gc-sections msp430: LDFLAGS += -L/opt/msp430-elf32/include -Tmsp430g2553.ld -Wl,-gc-sections
msp430: clean-lib alee-msp430 msp430: clean-lib alee-msp430
small: CXXFLAGS += -Os small: CXXFLAGS += -Os
@ -27,8 +27,15 @@ alee: $(LIBFILE)
alee-msp430: $(LIBFILE) alee-msp430: $(LIBFILE)
alee-standalone: $(LIBFILE) alee-standalone: $(LIBFILE)
cppcheck:
cppcheck --enable=warning,style,information --disable=missingInclude \
libalee alee*.cpp *dict.hpp
test: standalone
echo "\nbye\n" | ./alee-standalone test/tester.fr test/core.fr
$(LIBFILE): $(OBJFILES) $(LIBFILE): $(OBJFILES)
$(AR) cr $@ $(OBJFILES) $(AR) crs $@ $(OBJFILES)
core.fth.h: alee.dat core.fth.h: alee.dat
xxd -i $< > $@ xxd -i $< > $@
@ -44,5 +51,5 @@ clean: clean-lib
clean-lib: clean-lib:
rm -f $(LIBFILE) $(OBJFILES) rm -f $(LIBFILE) $(OBJFILES)
.PHONY: all msp430 small fast standalone clean clean-lib .PHONY: all clean clean-lib cppcheck fast msp430 small standalone test

@ -19,6 +19,7 @@
#include "alee.hpp" #include "alee.hpp"
#include "splitmemdict.hpp" #include "splitmemdict.hpp"
#include <cctype>
#include <msp430.h> #include <msp430.h>
#include "core.fth.h" #include "core.fth.h"
@ -95,16 +96,14 @@ int main()
static void readchar(State& state) static void readchar(State& state)
{ {
auto len = state.dict.read(Dictionary::Input); auto idx = state.dict.read(Dictionary::Input);
Addr addr = Dictionary::Input + sizeof(Cell) + Addr addr = Dictionary::Input + sizeof(Cell) + idx;
Dictionary::InputCells - len - 1;
for (Cell i = 0; i < len; ++i, ++addr)
state.dict.writebyte(addr, state.dict.readbyte(addr + 1));
while (!(IFG2 & UCA0RXIFG)); while (!(IFG2 & UCA0RXIFG));
state.dict.writebyte(addr, UCA0RXBUF); auto c = UCA0RXBUF;
state.dict.write(Dictionary::Input, len + 1); if (isupper(c))
c += 32;
state.dict.writebyte(addr, c ? c : ' ');
} }
void serput(int c) void serput(int c)

@ -95,38 +95,33 @@ Addr Dictionary::getexec(Addr addr) noexcept
Word Dictionary::input() noexcept Word Dictionary::input() noexcept
{ {
auto src = read(Dictionary::Source); const auto src = read(Dictionary::Source);
auto end = read(Dictionary::SourceLen); const auto end = read(Dictionary::SourceLen);
auto idx = read(Dictionary::Input); auto idx = read(Dictionary::Input);
Addr wordstart = src + idx; Word word {
Addr wordend = wordstart; static_cast<Addr>(src + idx),
static_cast<Addr>(src + idx)
};
while (idx < end) { while (idx < end) {
auto ch = readbyte(wordend); auto ch = readbyte(word.end);
if (ch == '\0')
break;
if (isspace(ch)) { if (isspace(ch)) {
if (wordstart != wordend) { if (word.size() > 0)
writebyte(Dictionary::Input, idx + 1); break;
return {wordstart, wordend};
}
++wordstart; ++word.start;
} else if (ch == '\0') {
break;
} }
++wordend; ++word.end;
++idx; ++idx;
} }
if (wordstart != wordend) {
writebyte(Dictionary::Input, idx + 1); writebyte(Dictionary::Input, idx + 1);
return {wordstart, wordend}; return word;
}
return {};
} }
bool Dictionary::equal(Word word, const char *str, unsigned len) const noexcept bool Dictionary::equal(Word word, const char *str, unsigned len) const noexcept

@ -19,7 +19,7 @@
#include "corewords.hpp" #include "corewords.hpp"
#include "parser.hpp" #include "parser.hpp"
#include <charconv> #include <cctype>
#include <cstring> #include <cstring>
int Parser::parse(State& state, const char *str) int Parser::parse(State& state, const char *str)
@ -79,18 +79,35 @@ int Parser::parseWord(State& state, Word word)
int Parser::parseNumber(State& state, Word word) int Parser::parseNumber(State& state, Word word)
{ {
char buf[MaxCellNumberChars + 1]; const auto base = state.dict.read(Dictionary::Base);
unsigned i; DoubleCell result = 0;
for (i = 0; i < std::min(MaxCellNumberChars, word.size()); ++i) auto i = word.start;
buf[i] = state.dict.readbyte(word.start + i); bool inv;
buf[i] = '\0'; char c;
auto base = state.dict.read(0); c = state.dict.readbyte(i);
DoubleCell dl; if (inv = c == '-'; inv)
auto [ptr, ec] = std::from_chars(buf, buf + i, dl, base); c = state.dict.readbyte(++i);
Cell l = static_cast<Cell>(dl);
do {
if (ec == std::errc() && ptr == buf + i) { if (isdigit(c)) {
result *= base;
result += c - '0';
} else if (isalpha(c) && base > 10) {
result *= base;
result += 10 + (c > 'a' ? c - 'a' : c - 'A');
} else {
return UnknownWord;
}
if (++i < word.end)
c = state.dict.readbyte(i);
} while (i < word.end);
if (inv)
result *= -1;
Cell value = static_cast<Cell>(result);
if (state.compiling()) { if (state.compiling()) {
auto ins = CoreWords::findi("_lit"); auto ins = CoreWords::findi("_lit");
@ -98,15 +115,12 @@ int Parser::parseNumber(State& state, Word word)
// state.dict.add(ins | ((l + 1) << 8)); // state.dict.add(ins | ((l + 1) << 8));
//} else { //} else {
state.dict.add(ins); state.dict.add(ins);
state.dict.add(l); state.dict.add(value);
//} //}
} else { } else {
state.push(l); state.push(value);
} }
return 0; return 0;
} else {
return UnknownWord;
}
} }

Loading…
Cancel
Save