Skip to content

Commit

Permalink
more documentation, mostly
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrk24 committed Sep 29, 2024
1 parent 064898f commit 0c4f640
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 3 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ All notable changes to this project will be documented in this file. See [Keep a

### Added

- The interpreter can now handle assembly syntax with the `-A` flag.
- The interpreter can now handle assembly syntax, with the `-A` flag in the command line and a checkbox in the online interpreter. Currently, interactive debugging of assembly is not supported in the web interpreter.

### Changed

Expand Down
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ Trilangle is a 2-D, stack-based programming language inspired by [Hexagony].
> - [Threading](#threading)
> - [Interpreter flags](#interpreter-flags)
> - [The disassembler](#the-disassembler)
> - [Assembly syntax](#assembly-syntax)
> - [C compiler](#c-compiler)
> - [Sample programs](#sample-programs)
> - [cat](#cat)
Expand Down Expand Up @@ -224,6 +225,36 @@ For example, when passing [the cat program below](#cat) with the flags `-Dn`, th
2.2: EXT
```

### Assembly syntax

In addition to producing this syntax, Trilangle is capable of interpreting this syntax. Currently, the output with `--hide-nops` is not guaranteed to be interpretable, as it may be missing jump targets. Each line can maximally consist of a label, an instruction, and a comment. The syntax can be described with the following extended Backus-Naur form:

```
program = line, {newline, line};
line = [label, [":"]], [multiple_whitespace, instruction], {whitespace}, [comment];
newline = ? U+000A END OF LINE ?;
tab = ? U+0009 CHARACTER TABULATION ?;
whitespace = " " | ? U+000D CARRIAGE RETURN ? | tab;
non_whitespace = ? Any single unicode character not in 'newline' or 'whitespace' ?;
multiple_whitespace = whitespace, {whitespace};
label = non_whitespace, {non_whitespace};
comment = ";", {non_whitespace | whitespace};
instruction = instruction_with_target | instruction_with_argument | plain_instruction;
instruction_with_target = ("BNG" | "TSP" | "JMP"), multiple_whitespace, label;
instruction_with_argument = ("PSI" | "PSC"), multiple_whitespace, number_literal;
plain_instruction = ? Any three-character instruction besides the five already covered ?;
number_literal = character_literal | decimal_literal | hex_literal;
character_literal = "'", (non_whitespace | tab), "'";
decimal_literal = "#", decimal_digit;
hex_literal = "0x", hex_digit, {hex_digit};
decimal_digit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9";
hex_digit = "a" | "b" | "c" | "d" | "e" | "f" | "A" | "B" | "C" | "D" | "E" | "F" | decimal_digit;
```

## C compiler

When using the `-c` flag, the input program will be translated into C code. The C code is not meant to be idiomatic or easy to read, as it is a literal translation of the input. Optimizers such as those used by clang and GCC tend to do a good job of improving the program, in some cases removing the heap allocation altogether; MSVC does not.
Expand Down
11 changes: 9 additions & 2 deletions src/assembly_scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -165,14 +165,21 @@ NONNULL_PTR(const std::vector<NONNULL_PTR(std::vector<instruction>)>) assembly_s
}
arg_value = static_cast<int24_t>(ul);
} else if (argument[0] == '#' && argument.size() == 2) {
arg_value = argument[1];
if (arg_value < (int24_t)'0' || arg_value > (int24_t)'9') {
arg_value = static_cast<int24_t>(argument[1] - '0');
if (arg_value < INT24_C(0) || arg_value > INT24_C(9)) {
invalid_literal(argument);
}
} else {
invalid_literal(argument);
}

if (opcode == instruction::operation::PSI) {
// PSI expects to be given the digit, not the actual value
auto p = arg_value.add_with_overflow('0');
assert(!p.first);
arg_value = p.second;
}

instruction::argument arg;
arg.number = arg_value;
add_instruction({ opcode, arg });
Expand Down
2 changes: 2 additions & 0 deletions tests/cli/invalid_flags/index.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ invalid_flags=(
-ae -aD
# --show-stack without --debug
-s
# --hide-nops without --disassemble
-n
)

for flag in "${invalid_flags[@]}"
Expand Down
1 change: 1 addition & 0 deletions wasm/in.civet
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ do
elements.urlButton.onclick = generateURL
elements.program.addEventListener 'input', hideUrl, { +passive }
elements.includeInput.addEventListener 'change', hideUrl, { +passive }
elements.assembly.addEventListener 'change', hideUrl, { +passive }
elements.stdin.addEventListener 'change', => hideUrl() if elements.includeInput.checked, { +passive }

// Allow the header itself, or noninteractable children (e.g. the select icon)
Expand Down

0 comments on commit 0c4f640

Please sign in to comment.