aboutsummaryrefslogtreecommitdiff
path: root/source/state
diff options
context:
space:
mode:
authorAdrian Kummerlaender2019-04-16 00:12:37 +0200
committerAdrian Kummerlaender2019-04-16 00:12:37 +0200
commit908d8f01d5e964971ad76909c0ec31468ee93d2d (patch)
treef1f9a84928b39aabc4792544951797b8a9eb1bfa /source/state
parent1b92af67088b5e57f9134703ae6115c3529fb352 (diff)
downloadslang-master.tar
slang-master.tar.gz
slang-master.tar.bz2
slang-master.tar.lz
slang-master.tar.xz
slang-master.tar.zst
slang-master.zip
Play around with a vector typeHEADmaster
Using _vectors_ as fundamental datatype could make for a really neat experience. Imagine e.g.: * fundamental arithmetic operations that apply to both vectors and scalars * implicit component-wise or vector-wise operations * scalar values as 1D vectors * higher order functions to manipulate those vectors * `map` function that applies a (quoted?) word to all vector elements and returns the result * efficient parallel operations * a rich library of vector manipulation functions * or even: matrices as a fundamental datatype? * problem: probably harder to conveniently enter via a 1-D repl * sound more and more like a RPN version of APL… Back to reality, here is what this prototype actually adds: * new `DList!int` based datatype alongside the existing int, string and bool types * basic support for printing such values in a readable fashion * new fundamental `:` word that constructs a vector of two elements * `1 2 :` yields `[1, 2]` * adapted `+` and `*` to support component-wise operations * `1 2 : 3 +` yields `[4, 5]`
Diffstat (limited to 'source/state')
-rw-r--r--source/state/definition.d8
-rw-r--r--source/state/stack.d6
-rw-r--r--source/state/variable.d3
3 files changed, 10 insertions, 7 deletions
diff --git a/source/state/definition.d b/source/state/definition.d
index 6f41275..b8748ce 100644
--- a/source/state/definition.d
+++ b/source/state/definition.d
@@ -16,7 +16,8 @@ bool handle(Token token) {
return token.visit!(
(int value) => handle(value),
(bool value) => handle(value),
- (string word ) => handle(word)
+ (string word ) => handle(word),
+ (DList!int v) => handle(v)
);
}
@@ -38,7 +39,8 @@ void register(DList!Token definition) {
definition.front.visit!(
(int value) => wordToBeDefined = "",
(bool value) => wordToBeDefined = "",
- (string name ) => wordToBeDefined = name
+ (string name ) => wordToBeDefined = name,
+ (DList!int v) => wordToBeDefined = ""
);
if ( wordToBeDefined == "" ) {
@@ -50,7 +52,7 @@ void register(DList!Token definition) {
}
template handle(T)
-if ( is(T == int) || is(T == bool) ) {
+if ( is(T == int) || is(T == bool) || is(T == DList!int) ) {
bool handle(T value) {
if ( definition.isNull ) {
return false;
diff --git a/source/state/stack.d b/source/state/stack.d
index 3c6230f..4d1f32f 100644
--- a/source/state/stack.d
+++ b/source/state/stack.d
@@ -3,9 +3,9 @@ module state.stack;
import std.conv;
import std.string;
import std.variant;
-import std.container : SList;
+import std.container : SList, DList;
-alias Token = Algebraic!(int, bool, string);
+alias Token = Algebraic!(int, bool, string, DList!int);
alias Stack = SList;
Stack!Token stack;
@@ -33,7 +33,7 @@ Token pop(ref Stack!Token stack) {
}
template push(T)
-if ( is(T == int) || is(T == bool) || is (T == string) ) {
+if ( is(T == int) || is(T == bool) || is (T == string) || is (T == DList!int) ) {
void push(ref Stack!Token stack, T value) {
stack.push(Token(value));
}
diff --git a/source/state/variable.d b/source/state/variable.d
index f77e207..978a2fd 100644
--- a/source/state/variable.d
+++ b/source/state/variable.d
@@ -16,7 +16,8 @@ bool handle(Token token) {
return token.visit!(
(int ) => false,
(bool ) => false,
- (string word) => handle(word)
+ (string word) => handle(word),
+ (DList!int ) => false
);
}