]> code.bitgloo.com Git - bitgloo/alee-forth.git/commitdiff
add cppcheck and test targets; some code size reductions
authorClyne Sullivan <clyne@bitgloo.com>
Thu, 9 Mar 2023 20:45:39 +0000 (15:45 -0500)
committerClyne Sullivan <clyne@bitgloo.com>
Thu, 9 Mar 2023 20:45:39 +0000 (15:45 -0500)
Makefile
alee-msp430.cpp
libalee/dictionary.cpp
libalee/parser.cpp

index 624863d1090ceb737c65b9a6e5e3eaec10a3642b..afe8614d59588b2666c66882bb50a91595b04617 100644 (file)
--- 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
 
index 86582a17630a1b6343e5a5bb5e3c08925fde8c81..e9fe19d5a8bfb9e7a8d106a2d962b575774803bb 100644 (file)
@@ -19,6 +19,7 @@
 #include "alee.hpp"
 #include "splitmemdict.hpp"
 
+#include <cctype>
 #include <msp430.h>
 
 #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)
index 48230c44e10ed7bc5acd4ec716c641bc0cc2b97c..29844b68b43468df4b2999095765e31ca029d858 100644 (file)
@@ -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<Addr>(src + idx),
+        static_cast<Addr>(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
index 44b5cec31d1a8af973294c98c1d408e68e5813e7..d709d45ad93ca01dffc716816b38f350c2ad64ab 100644 (file)
@@ -19,7 +19,7 @@
 #include "corewords.hpp"
 #include "parser.hpp"
 
-#include <charconv>
+#include <cctype>
 #include <cstring>
 
 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<Cell>(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<Cell>(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;
 }