Skip to content

Commit

Permalink
add tests and update documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrk24 committed Nov 27, 2023
1 parent 764c3aa commit 74c3251
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 5 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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.
Expand Down
8 changes: 4 additions & 4 deletions src/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,15 +404,15 @@ void thread::tick() {
break;
}

int24_t top = m_stack.back();
size_t index = static_cast<size_t>(m_stack.back()) & 0x00ff'ffffU;
m_stack.pop_back();

if (m_flags.warnings && (top < INT24_C(0) || m_stack.size() < static_cast<size_t>(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<size_t>(top) - 1;
size_t i = m_stack.size() - index - 1;
m_stack.push_back(m_stack[i]);

break;
Expand Down
3 changes: 3 additions & 0 deletions tests/cli/arithmetic/add.trg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
?
? p
+ @ .
15 changes: 15 additions & 0 deletions tests/cli/arithmetic/index.sh
Original file line number Diff line number Diff line change
@@ -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"
3 changes: 3 additions & 0 deletions tests/cli/arithmetic/sdiv.trg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
?
? !
: @ .
3 changes: 3 additions & 0 deletions tests/cli/arithmetic/sub.trg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
?
? !
- @ .
3 changes: 3 additions & 0 deletions tests/cli/arithmetic/udiv.trg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
?
? p
d @ .
3 changes: 3 additions & 0 deletions tests/cli/cat/index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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)"
2 changes: 2 additions & 0 deletions tests/cli/cat/uint.trg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
?
p @
1 change: 1 addition & 0 deletions tests/cli/print1/index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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")"
3 changes: 3 additions & 0 deletions tests/cli/print1/uint.trg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
'
1 @
p . .
1 change: 1 addition & 0 deletions tests/compiler/arithmetic

0 comments on commit 74c3251

Please sign in to comment.