diff --git a/README.md b/README.md index f003bd5..efe41b6 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ Additionally, trailing `.`s on the end of the last line may be omitted. Trilangle ditches the hexagonal memory layout in favor of a stack. Technically, it's not a pure stack as it is possible to index into it, but the majority of operations treat it as such. -Each item on the stack is a 24-bit signed integer. +Each item on the stack is a 24-bit integer. Different instructions variously treat it as signed or unsigned. ## Instructions @@ -180,6 +180,8 @@ When `{` is approached from the northeast or southeast, that interpreter thread | b | e | b | | a | d | a | +If the value on top of a stack is negative, that entire stack is carried over. + ## Interpreter flags When run with the `-d` flag, the interpreter enters "debug mode." Before executing each instruction, it prints the location of the IP and the opcode that it is pointing at, and it waits for you to press enter before continuing. If the `-s` flag is also set, the interpreter will print the stack contents as well as the IP. diff --git a/src/thread.cpp b/src/thread.cpp index baddf44..0da0737 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -404,15 +404,15 @@ void thread::tick() { break; } - int24_t top = m_stack.back(); + size_t index = static_cast(m_stack.back()) & 0x00ff'ffffU; m_stack.pop_back(); - if (m_flags.warnings && (top < INT24_C(0) || m_stack.size() < static_cast(top) + 1)) UNLIKELY { + if (m_flags.warnings && m_stack.size() < index + 1) UNLIKELY { cerr << "Warning: Attempt to index out of stack bounds (size = " << m_stack.size() - << ", index = " << top << ")\n"; + << ", index = " << index << ")\n"; } - size_t i = m_stack.size() - static_cast(top) - 1; + size_t i = m_stack.size() - index - 1; m_stack.push_back(m_stack[i]); break; diff --git a/tests/cli/arithmetic/add.trg b/tests/cli/arithmetic/add.trg new file mode 100644 index 0000000..1e84998 --- /dev/null +++ b/tests/cli/arithmetic/add.trg @@ -0,0 +1,3 @@ + ? + ? p ++ @ . diff --git a/tests/cli/arithmetic/index.sh b/tests/cli/arithmetic/index.sh new file mode 100755 index 0000000..04cdc47 --- /dev/null +++ b/tests/cli/arithmetic/index.sh @@ -0,0 +1,15 @@ +#!/bin/bash + +set -eu + +folder=$(dirname "$0") + +five=$($TRILANGLE "${folder}/add.trg" <<<'3 2') +one=$($TRILANGLE "${folder}/sub.trg" <<<'3 2') +minustwo=$($TRILANGLE "${folder}/sdiv.trg" <<<'-5 2') +twopowtwentytwo=$($TRILANGLE "${folder}/udiv.trg" <<<"$((0x800000)) 2") + +test 5 = "$five" +test 1 = "$one" +test -2 = "$minustwo" +test $((0x400000)) = "$twopowtwentytwo" diff --git a/tests/cli/arithmetic/sdiv.trg b/tests/cli/arithmetic/sdiv.trg new file mode 100644 index 0000000..047a516 --- /dev/null +++ b/tests/cli/arithmetic/sdiv.trg @@ -0,0 +1,3 @@ + ? + ? ! +: @ . diff --git a/tests/cli/arithmetic/sub.trg b/tests/cli/arithmetic/sub.trg new file mode 100644 index 0000000..28d0e47 --- /dev/null +++ b/tests/cli/arithmetic/sub.trg @@ -0,0 +1,3 @@ + ? + ? ! +- @ . diff --git a/tests/cli/arithmetic/udiv.trg b/tests/cli/arithmetic/udiv.trg new file mode 100644 index 0000000..45997e6 --- /dev/null +++ b/tests/cli/arithmetic/udiv.trg @@ -0,0 +1,3 @@ + ? + ? p +d @ . diff --git a/tests/cli/cat/index.sh b/tests/cli/cat/index.sh index 733d2fa..2aa7cf3 100755 --- a/tests/cli/cat/index.sh +++ b/tests/cli/cat/index.sh @@ -13,3 +13,6 @@ folder=$(dirname "$0") output=$($TRILANGLE "${folder}/cat.trg" <<<"$text") # Okay so on Windows the newline characters are different. Isn't this fun? test "$text" = "${output//$'\r'/}" + +x=$((0x912345)) +test $x = "$($TRILANGLE "${folder}/uint.trg" <<<$x)" diff --git a/tests/cli/cat/uint.trg b/tests/cli/cat/uint.trg new file mode 100644 index 0000000..991aa60 --- /dev/null +++ b/tests/cli/cat/uint.trg @@ -0,0 +1,2 @@ + ? +p @ diff --git a/tests/cli/print1/index.sh b/tests/cli/print1/index.sh index 45b2997..ab92f24 100755 --- a/tests/cli/print1/index.sh +++ b/tests/cli/print1/index.sh @@ -6,3 +6,4 @@ folder=$(dirname "$0") test 1 = "$($TRILANGLE "${folder}/int.trg")" test 1 = "$($TRILANGLE -a "${folder}/char.trg")" +test 1 = "$($TRILANGLE "${folder}/uint.trg")" diff --git a/tests/cli/print1/uint.trg b/tests/cli/print1/uint.trg new file mode 100644 index 0000000..5048980 --- /dev/null +++ b/tests/cli/print1/uint.trg @@ -0,0 +1,3 @@ + ' + 1 @ +p . . diff --git a/tests/compiler/arithmetic b/tests/compiler/arithmetic new file mode 120000 index 0000000..4ffb446 --- /dev/null +++ b/tests/compiler/arithmetic @@ -0,0 +1 @@ +../cli/arithmetic/ \ No newline at end of file