From 967775313dbe04b40f5b70fcfdab02c573f3b57b Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 9 Mar 2023 15:45:39 -0500 Subject: [PATCH] add cppcheck and test targets; some code size reductions --- Makefile | 17 +++++++---- alee-msp430.cpp | 15 +++++----- libalee/dictionary.cpp | 35 ++++++++++------------- libalee/parser.cpp | 64 +++++++++++++++++++++++++----------------- 4 files changed, 73 insertions(+), 58 deletions(-) diff --git a/Makefile b/Makefile index 624863d..afe8614 100644 --- a/Makefile +++ b/Makefile @@ -9,10 +9,10 @@ LIBFILE := libalee/libalee.a all: alee 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 += -DMEMDICTSIZE=200 -msp430: LDFLAGS += -L/opt/msp430-elf32/include -Wl,-gc-sections +msp430: CXXFLAGS += -DMEMDICTSIZE=200 -flto +msp430: LDFLAGS += -L/opt/msp430-elf32/include -Tmsp430g2553.ld -Wl,-gc-sections msp430: clean-lib alee-msp430 small: CXXFLAGS += -Os @@ -27,8 +27,15 @@ alee: $(LIBFILE) alee-msp430: $(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) - $(AR) cr $@ $(OBJFILES) + $(AR) crs $@ $(OBJFILES) core.fth.h: alee.dat xxd -i $< > $@ @@ -44,5 +51,5 @@ clean: clean-lib clean-lib: rm -f $(LIBFILE) $(OBJFILES) -.PHONY: all msp430 small fast standalone clean clean-lib +.PHONY: all clean clean-lib cppcheck fast msp430 small standalone test diff --git a/alee-msp430.cpp b/alee-msp430.cpp index 86582a1..e9fe19d 100644 --- a/alee-msp430.cpp +++ b/alee-msp430.cpp @@ -19,6 +19,7 @@ #include "alee.hpp" #include "splitmemdict.hpp" +#include #include #include "core.fth.h" @@ -95,16 +96,14 @@ int main() static void readchar(State& state) { - auto len = state.dict.read(Dictionary::Input); - Addr addr = Dictionary::Input + sizeof(Cell) + - Dictionary::InputCells - len - 1; - - for (Cell i = 0; i < len; ++i, ++addr) - state.dict.writebyte(addr, state.dict.readbyte(addr + 1)); + auto idx = state.dict.read(Dictionary::Input); + Addr addr = Dictionary::Input + sizeof(Cell) + idx; while (!(IFG2 & UCA0RXIFG)); - state.dict.writebyte(addr, UCA0RXBUF); - state.dict.write(Dictionary::Input, len + 1); + auto c = UCA0RXBUF; + if (isupper(c)) + c += 32; + state.dict.writebyte(addr, c ? c : ' '); } void serput(int c) diff --git a/libalee/dictionary.cpp b/libalee/dictionary.cpp index 48230c4..29844b6 100644 --- a/libalee/dictionary.cpp +++ b/libalee/dictionary.cpp @@ -95,38 +95,33 @@ Addr Dictionary::getexec(Addr addr) noexcept Word Dictionary::input() noexcept { - auto src = read(Dictionary::Source); - auto end = read(Dictionary::SourceLen); + const auto src = read(Dictionary::Source); + const auto end = read(Dictionary::SourceLen); auto idx = read(Dictionary::Input); - Addr wordstart = src + idx; - Addr wordend = wordstart; + Word word { + static_cast(src + idx), + static_cast(src + idx) + }; while (idx < end) { - auto ch = readbyte(wordend); - - if (ch == '\0') - break; + auto ch = readbyte(word.end); if (isspace(ch)) { - if (wordstart != wordend) { - writebyte(Dictionary::Input, idx + 1); - return {wordstart, wordend}; - } + if (word.size() > 0) + break; - ++wordstart; + ++word.start; + } else if (ch == '\0') { + break; } - ++wordend; + ++word.end; ++idx; } - if (wordstart != wordend) { - writebyte(Dictionary::Input, idx + 1); - return {wordstart, wordend}; - } - - return {}; + writebyte(Dictionary::Input, idx + 1); + return word; } bool Dictionary::equal(Word word, const char *str, unsigned len) const noexcept diff --git a/libalee/parser.cpp b/libalee/parser.cpp index 44b5cec..d709d45 100644 --- a/libalee/parser.cpp +++ b/libalee/parser.cpp @@ -19,7 +19,7 @@ #include "corewords.hpp" #include "parser.hpp" -#include +#include #include int Parser::parse(State& state, const char *str) @@ -79,34 +79,48 @@ int Parser::parseWord(State& state, Word word) int Parser::parseNumber(State& state, Word word) { - char buf[MaxCellNumberChars + 1]; - unsigned i; - for (i = 0; i < std::min(MaxCellNumberChars, word.size()); ++i) - buf[i] = state.dict.readbyte(word.start + i); - buf[i] = '\0'; - - auto base = state.dict.read(0); - DoubleCell dl; - auto [ptr, ec] = std::from_chars(buf, buf + i, dl, base); - Cell l = static_cast(dl); - - if (ec == std::errc() && ptr == buf + i) { - if (state.compiling()) { - auto ins = CoreWords::findi("_lit"); - - //if (l >= 0 && l < 0xFF) { - // state.dict.add(ins | ((l + 1) << 8)); - //} else { - state.dict.add(ins); - state.dict.add(l); - //} + const auto base = state.dict.read(Dictionary::Base); + DoubleCell result = 0; + auto i = word.start; + bool inv; + char c; + + c = state.dict.readbyte(i); + if (inv = c == '-'; inv) + c = state.dict.readbyte(++i); + + do { + 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 { - state.push(l); + return UnknownWord; } - return 0; + if (++i < word.end) + c = state.dict.readbyte(i); + } while (i < word.end); + + if (inv) + result *= -1; + + Cell value = static_cast(result); + if (state.compiling()) { + auto ins = CoreWords::findi("_lit"); + + //if (l >= 0 && l < 0xFF) { + // state.dict.add(ins | ((l + 1) << 8)); + //} else { + state.dict.add(ins); + state.dict.add(value); + //} } else { - return UnknownWord; + state.push(value); } + + return 0; }