From 908d8f01d5e964971ad76909c0ec31468ee93d2d Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Tue, 16 Apr 2019 00:12:37 +0200 Subject: Play around with a vector type 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]` --- source/machine.d | 3 ++- source/primitives/conditional.d | 3 ++- source/primitives/core.d | 38 +++++++++++++++++++++++++++++--------- source/state/definition.d | 8 +++++--- source/state/stack.d | 6 +++--- source/state/variable.d | 3 ++- 6 files changed, 43 insertions(+), 18 deletions(-) diff --git a/source/machine.d b/source/machine.d index 6dadfd6..9565c65 100644 --- a/source/machine.d +++ b/source/machine.d @@ -59,7 +59,8 @@ Stack!Token evaluate(Token token) { return token.visit!( (int ) => Stack!Token(token), (bool ) => Stack!Token(token), - (string word) => definition.get(word) + (string word) => definition.get(word), + (DList!int ) => Stack!Token(token) ); } catch (Exception ex) { diff --git a/source/primitives/conditional.d b/source/primitives/conditional.d index fd101bd..62a9fc5 100644 --- a/source/primitives/conditional.d +++ b/source/primitives/conditional.d @@ -19,7 +19,8 @@ bool handle(Token token) { return token.visit!( (int ) => capture(token), (bool ) => capture(token), - (string word) => handle(word) + (string word) => handle(word), + (DList!int ) => capture(token) ); } diff --git a/source/primitives/core.d b/source/primitives/core.d index 46ed890..7a13606 100644 --- a/source/primitives/core.d +++ b/source/primitives/core.d @@ -2,6 +2,8 @@ module primitives.core; import std.stdio; import std.variant; +import std.conv : to; +import std.algorithm : map; import state.stack; @@ -9,7 +11,8 @@ bool handle(Token token) { return token.visit!( (int ) => false, (bool ) => false, - (string word) => handle(word) + (string word) => handle(word), + (DList!int ) => false ); } @@ -25,7 +28,8 @@ bool handle(string word) { case "pop" : unary_op_stack_pop; break; case "dup" : unary_op_stack_dup; break; case "swp" : binary_op_stack_swp; break; - case "ovr" : binary_op_stack_ovr; break; + case "ovr" : binary_op_stack_ovr; break; + case ":" : binary_op_vector_cons; break; case "rot" : ternary_op_stack_rot; break; case "true" : nullary_op_value_bool(true); break; case "false" : nullary_op_value_bool(false); break; @@ -43,16 +47,18 @@ bool handle(string word) { void binary_op_add() { int b = stack.pop.get!int; - int a = stack.pop.get!int; - - stack.push(a + b); + stack.pop.tryVisit!( + (int a) => stack.push(a + b), + (DList!int v) => stack.push(v[].map!(x => x + b).to!(DList!int)) + ); } void binary_op_multiply() { int b = stack.pop.get!int; - int a = stack.pop.get!int; - - stack.push(a * b); + stack.pop.tryVisit!( + (int a) => stack.push(a * b), + (DList!int v) => stack.push(v[].map!(x => x * b).to!(DList!int)) + ); } void binary_op_divide() { @@ -77,8 +83,22 @@ void binary_op_modulo() { } } +void binary_op_vector_cons() { + int b = stack.pop.get!int; + + stack.pop.tryVisit!( + (int a) => stack.push(DList!int(a, b)), + (DList!int v) => stack.push(v ~ b) + ); +} + void unary_op_io_print() { - writeln(stack.top); + stack.top.visit!( + (int x) => writeln(x), + (bool b) => writeln(b), + (string word) => writeln(word), + (DList!int v) => writeln(v[]) + ); } void unary_op_stack_pop() { 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 ); } -- cgit v1.2.3