Skip to content

Commit

Permalink
Add base12 support.
Browse files Browse the repository at this point in the history
Start adding design doc
  • Loading branch information
cwesson committed Feb 3, 2023
1 parent f94b97f commit 15114e8
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 22 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Vertical Tabs in the file represent 4D+1 and reset X, Y, and Z to zero. Carriag
following a vertical tab are ignored. This is not part of the Funge-98 specification, but provides a simple way to
create 4-dimensional funges.

Trefunge frunges can be loaded as funge-lib formatted `.fl` files.
Trefunge funges can be loaded as funge-lib formatted `.fl` files.

Higher dimension funges can be loaded as BeQunge formatted `.beq` files. Loading a `.beq` file implies `-lNFUN`.

Expand All @@ -27,7 +27,7 @@ The split instruction `t` can use native threads rather than the tick mechanism
specification. This mode can be enabled with `-fthreads=native`.

### Auto-Dimensions
The dimensionality (up to 4D) is determined automatically based on the file contents. This can be overriden with
The dimensionality (up to 4D) is determined automatically based on the file contents. This can be overridden with
the `-std` argument. Funge-lib formatted files are 3D. Higher dimensions can be determined automatically from BeQunge
formatted files with or without the `Dimensions` directive.

Expand All @@ -37,8 +37,8 @@ example, in 4D mode, `?` will change the delta to one of 8 possible directions,
directions.

### C-Style Strings
C-style strings can be enabled with `-fstrings=c`. In this mode, backslashes in strings are not pused to the stack,
instead, it and the following character are interpretted as an escape sequence. This happens in one tick. In addition
C-style strings can be enabled with `-fstrings=c`. In this mode, backslashes in strings are not pushed to the stack,
instead, it and the following character are interpreted as an escape sequence. This happens in one tick. In addition
to special characters, this allows quotes and ANSI escape codes in strings.

### 64-bit
Expand Down Expand Up @@ -127,4 +127,9 @@ Instructions from dynamic Funges run with the same stack as the IP that called t
## Debugger
The Funge++ debugger, known as defunge, can be run on any Befunge program by specifying the `-g` command line argument.

See [Defunge](doc/defunge.md) for details.
See [Defunge](doc/defunge.md) for details.

## Contributing
Please submit bug reports and feature requests on [GitHub Issues](https://github.com/cwesson/funge-plus-plus/issues).

Pull requests are also welcome. For details of how Funge++ works, review the [software design](doc/design.md).
2 changes: 1 addition & 1 deletion doc/defunge.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ print the entire stack stack.
: Set the delta of the current IP to vector *v*.

**setpos *v***
: Set the postion of the current IP to vector *v*.
: Set the position of the current IP to vector *v*.

**universe**, **u**
: Print a list universe names.
Expand Down
31 changes: 31 additions & 0 deletions doc/design.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Funge++ Software Design

```mermaid
classDiagram
FungeMultiverse "1" -- "*" FungeUniverse
<<singleton>> FungeMultiverse
FungeUniverse "1" -- "*" FungeRunner
FungeUniverse "1" -- "1" Field
FungeRunner "1" -- "*" FungeState
FungeRunner "1" -- "1" InstructionPointer
FungeRunner "1" --> "1" StackStack
StackStack "1" --> "*" Stack
FungeState <|-- FungeStateNormal
FungeState <|-- FungeStateString
FungeStateNormal "1" -- "*" FungeStrategy
<<abstract>> FungeStrategy
FungeStrategy <|-- Unefunge93Strategy
FungeStrategy <|-- Unefunge98Strategy
FungeStrategy <|-- Befunge93Strategy
FungeStrategy <|-- Befunge98Strategy
FungeStrategy <|-- Trefunge98Strategy
FungeStrategy <|-- FingerprintStrategy
FingerprintStrategy "1" o-- "*" Fingerprint
<<abstract>> Fingerprint
FungeStrategy <|-- FishStrategy
FishStrategy <|-- StarFishStrategy
```
39 changes: 24 additions & 15 deletions src/fingerprint/FingerprintBASE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,18 @@
#include <iostream>

namespace Funge {

const char FingerprintBASE::digit_map[32] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K',
'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V', 'W', 'X',
'Y', 'Z'
};

const char FingerprintBASE::base12[12] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'X', 'E'
};


const char FingerprintBASE::base36[36] = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
Expand Down Expand Up @@ -56,6 +60,7 @@ FingerprintBASE::FingerprintBASE(FungeRunner& r) :
Fingerprint(r, {'B', 'H', 'I', 'N', 'O'}),
base_map()
{
base_map[12] = &base12[0];
base_map[36] = &base36[0];
base_map[58] = &base58[0];
base_map[64] = &base64[0];
Expand Down Expand Up @@ -111,17 +116,15 @@ void FingerprintBASE::printBase(stack_t num, unsigned int base, bool low) const{
if(next > 0){
printBase(next, base, false);
}
if(base <= sizeof(digit_map)){
auto found = base_map.find(base);
if(found != base_map.end()){
std::cout << found->second[digit];
}else if(base <= sizeof(digit_map)){
std::cout << digit_map[digit];
}else{
auto found = base_map.find(base);
if(found != base_map.end()){
std::cout << found->second[digit];
}else{
std::cout << digit;
if(!low){
std::cout << ",";
}
std::cout << digit;
if(!low){
std::cout << ",";
}
}
}
Expand All @@ -132,28 +135,34 @@ stack_t FingerprintBASE::readNum(unsigned int base) const{
stack_t num = 0;
if(base <= 1){
num = str.size();
}
const char* map = digit_map;
auto found = base_map.find(base);
if(found != base_map.end()){
map = found->second;
readBase(str, &num, base, map);
}else if(base <= sizeof(digit_map)){
readBase(str, &num, base);
readBase(str, &num, base, map);
}
return num;
}

bool FingerprintBASE::readBase(std::string str, stack_t* num, unsigned int base) const{
bool FingerprintBASE::readBase(std::string str, stack_t* num, unsigned int base, const char* map) const{
char digit = str[0];
bool found = false;
if(base <= 36){
std::transform(str.begin(), str.end(), str.begin(), ::toupper);
}
bool found = false;
for(size_t i = 0; i <= base; ++i){
if(digit_map[i] == digit){
if(map[i] == digit){
*num = (*num*base) + i;
found = true;
break;
}
}
if(found && str.size() > 1){
str.erase(0, 1);
readBase(str, num, base);
readBase(str, num, base, map);
}
return true;
}
Expand Down
3 changes: 2 additions & 1 deletion src/fingerprint/include/FingerprintBASE.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class FingerprintBASE : public Fingerprint {

private:
static const char digit_map[32];
static const char base12[12];
static const char base36[36];
static const char base58[58];
static const char base64[64];
Expand All @@ -30,7 +31,7 @@ class FingerprintBASE : public Fingerprint {
void printNum(stack_t num, unsigned int base) const;
void printBase(stack_t num, unsigned int base, bool low) const;
stack_t readNum(unsigned int base) const;
bool readBase(std::string str, stack_t* num, unsigned int base) const;
bool readBase(std::string str, stack_t* num, unsigned int base, const char* map) const;
};

}

0 comments on commit 15114e8

Please sign in to comment.