From c6d2b259a4f253403929f6f1104395a6a71b1be6 Mon Sep 17 00:00:00 2001 From: Adrian Kummerlaender Date: Fri, 14 Apr 2017 23:21:51 +0200 Subject: Convert structure to _dub_ build system --- dub.sdl | 4 ++ repl.d | 57 ----------------------- source/app.d | 57 +++++++++++++++++++++++ source/base/definition.d | 62 ++++++++++++++++++++++++ source/base/stack.d | 48 +++++++++++++++++++ source/primitives/conditional.d | 77 ++++++++++++++++++++++++++++++ source/primitives/core.d | 101 ++++++++++++++++++++++++++++++++++++++++ source/primitives/eval.d | 53 +++++++++++++++++++++ src/definition.d | 62 ------------------------ src/primitives/conditional.d | 77 ------------------------------ src/primitives/core.d | 101 ---------------------------------------- src/primitives/eval.d | 53 --------------------- src/stack.d | 48 ------------------- 13 files changed, 402 insertions(+), 398 deletions(-) create mode 100644 dub.sdl delete mode 100644 repl.d create mode 100644 source/app.d create mode 100644 source/base/definition.d create mode 100644 source/base/stack.d create mode 100644 source/primitives/conditional.d create mode 100644 source/primitives/core.d create mode 100644 source/primitives/eval.d delete mode 100644 src/definition.d delete mode 100644 src/primitives/conditional.d delete mode 100644 src/primitives/core.d delete mode 100644 src/primitives/eval.d delete mode 100644 src/stack.d diff --git a/dub.sdl b/dub.sdl new file mode 100644 index 0000000..0273ce9 --- /dev/null +++ b/dub.sdl @@ -0,0 +1,4 @@ +name "slang" +description "Experimental Forth-like stack language implemented in D" +authors "Adrian Kummerländer" +license "MIT" diff --git a/repl.d b/repl.d deleted file mode 100644 index 3f2aea4..0000000 --- a/repl.d +++ /dev/null @@ -1,57 +0,0 @@ -import std.stdio; -import std.string; -import std.variant; - -import std.container.util : make; - -import src.stack; - -import definition = src.definition; -import primitives = src.primitives.eval; - -Stack!Token resolve(Token token) { - try { - if ( primitives.evaluate(token) ) { - return primitives.result; - } else { - return token.visit!( - (int value) => Stack!Token(Token(value)), - (bool value) => Stack!Token(Token(value)), - (string word ) => definition.get(word) - ); - } - } - catch (Exception ex) { - writeln("Error: ", ex.msg); - return Stack!Token(); - } -} - -void process(string value) { - auto buffer = make!(Stack!Token)(toToken(value)); - - do { - Token current = buffer.pop; - - if ( !definition.handle(current) ) { - Stack!Token resolved = resolve(current); - - if ( !resolved.empty ) { - if ( resolved.front == current ) { - stack.push(current); - } else { - buffer.insertFront(resolved[]); - } - } - } - } - while ( !buffer.empty ); -} - -void main() { - while ( !stdin.eof ) { - foreach ( token; stdin.readln.split ) { - process(token); - } - } -} diff --git a/source/app.d b/source/app.d new file mode 100644 index 0000000..7d25f7f --- /dev/null +++ b/source/app.d @@ -0,0 +1,57 @@ +import std.stdio; +import std.string; +import std.variant; + +import std.container.util : make; + +import base.stack; + +import definition = base.definition; +import primitives = primitives.eval; + +Stack!Token resolve(Token token) { + try { + if ( primitives.evaluate(token) ) { + return primitives.result; + } else { + return token.visit!( + (int value) => Stack!Token(Token(value)), + (bool value) => Stack!Token(Token(value)), + (string word ) => definition.get(word) + ); + } + } + catch (Exception ex) { + writeln("Error: ", ex.msg); + return Stack!Token(); + } +} + +void process(string value) { + auto buffer = make!(Stack!Token)(toToken(value)); + + do { + Token current = buffer.pop; + + if ( !definition.handle(current) ) { + Stack!Token resolved = resolve(current); + + if ( !resolved.empty ) { + if ( resolved.front == current ) { + stack.push(current); + } else { + buffer.insertFront(resolved[]); + } + } + } + } + while ( !buffer.empty ); +} + +void main() { + while ( !stdin.eof ) { + foreach ( token; stdin.readln.split ) { + process(token); + } + } +} diff --git a/source/base/definition.d b/source/base/definition.d new file mode 100644 index 0000000..cf64f3e --- /dev/null +++ b/source/base/definition.d @@ -0,0 +1,62 @@ +module base.definition; + +import std.string; +import std.variant; +import std.typecons; + +import std.container : DList; + +import base.stack; + +alias Words = Stack!Token[string]; + +Nullable!(DList!Token) definition; +Words words; + +void start() { + definition = DList!Token(); +} + +void end() { + string wordToBeDefined; + + definition.front.visit!( + (int value) => wordToBeDefined = "", + (bool value) => wordToBeDefined = "", + (string name ) => wordToBeDefined = name + ); + + if ( wordToBeDefined == "" ) { + throw new Exception("words may not be numeric or boolean"); + } + + definition.removeFront; + words[wordToBeDefined] = Stack!Token(definition[]); + definition.nullify; +} + +bool handle(Token token) { + if ( definition.isNull ) { + return false; + } else { + if ( token.type == typeid(string) ) { + if ( *token.peek!string == ";" ) { + end; + } else { + definition.insertBack(token); + } + } else { + definition.insertBack(token); + } + + return true; + } +} + +Stack!Token get(string word) { + if ( word in words ) { + return words[word].dup; + } else { + return Stack!Token(Token(word)); + } +} diff --git a/source/base/stack.d b/source/base/stack.d new file mode 100644 index 0000000..253bac0 --- /dev/null +++ b/source/base/stack.d @@ -0,0 +1,48 @@ +module base.stack; + +import std.conv; +import std.string; +import std.variant; +import std.container : SList; + +import definition = base.definition; + +alias Token = Algebraic!(int, bool, string); +alias Stack = SList; + +Stack!Token stack; + +Token toToken(string value) { + if ( value.isNumeric ) { + return Token(parse!int(value)); + } else { + return Token(value); + } +} + +Token top(ref Stack!Token stack) { + if ( stack.empty ) { + throw new Exception("stack is empty"); + } else { + return stack.front; + } +} + +Token pop(ref Stack!Token stack) { + Token token = stack.top; + stack.removeFront; + return token; +} + +void push(ref Stack!Token stack, Token token) { + if ( !definition.handle(token) ) { + stack.insertFront(token); + } +} + +template push(T) +if ( is(T == int) || is(T == bool) || is (T == string) ) { + void push(ref Stack!Token stack, T value) { + stack.push(Token(value)); + } +} diff --git a/source/primitives/conditional.d b/source/primitives/conditional.d new file mode 100644 index 0000000..678f516 --- /dev/null +++ b/source/primitives/conditional.d @@ -0,0 +1,77 @@ +module primitives.conditional; + +import std.variant; +import std.typecons; +import std.container : DList; + +import base.stack; + +Nullable!(DList!Token) buffer; +bool concluded = true; +bool drop_mode = false; + +void capture(Token token) { + if ( !drop_mode ) { + buffer.insertBack(token); + } +} + +bool drop(Token token) { + if ( concluded && buffer.isNull ) { + return false; + } + + if ( token.type == typeid(string) ) { + switch ( *token.peek!string ) { + case "if" : eval_if; break; + case "then" : eval_then; break; + case "else" : eval_else; break; + default : capture(token); break; + } + } else { + capture(token); + } + + return true; +} + +void eval_if() { + if ( concluded ) { + buffer = DList!Token(); + drop_mode = !stack.pop.get!bool; + concluded = false; + } else { + throw new Exception("conditionals may not be nested directly"); + } +} + +void eval_then() { + if ( concluded ) { + throw new Exception("`then` without preceding `if`"); + } else { + drop_mode = !drop_mode; + } +} + +void eval_else() { + if ( concluded ) { + throw new Exception("`else` without preceding `if`"); + } else { + drop_mode = false; + concluded = true; + } +} + +bool dischargeable() { + return concluded && !buffer.isNull; +} + +Stack!Token discharge() { + if ( concluded ) { + Stack!Token result = buffer[]; + buffer.nullify; + return result; + } else { + throw new Exception("unconcluded conditional may not be discharged"); + } +} diff --git a/source/primitives/core.d b/source/primitives/core.d new file mode 100644 index 0000000..45bbeac --- /dev/null +++ b/source/primitives/core.d @@ -0,0 +1,101 @@ +module primitives.core; + +import std.stdio; + +import base.stack; +import definition = base.definition; + +Token[string] variables; + +void definition_start() { + definition.start; +} + +void binary_op_variable_bind() { + string name = stack.pop.get!string; + Token value = stack.pop; + variables[name] = value; +} + +void unary_op_variable_resolve() { + string name = stack.pop.get!string; + + if ( name in variables ) { + stack.push(variables[name]); + } +} + +void binary_op_add() { + int b = stack.pop.get!int; + int a = stack.pop.get!int; + + stack.push(a + b); +} + +void binary_op_multiply() { + int b = stack.pop.get!int; + int a = stack.pop.get!int; + + stack.push(a * b); +} + +void binary_op_divide() { + int b = stack.pop.get!int; + int a = stack.pop.get!int; + + if ( b == 0 ) { + throw new Exception("division by 0 undefined"); + } else { + stack.push(a / b); + } +} + +void binary_op_modulo() { + int b = stack.pop.get!int; + int a = stack.pop.get!int; + + if ( b == 0 ) { + throw new Exception("modulo 0 undefined"); + } else { + stack.push(a % b); + } +} + +void unary_op_io_print() { + writeln(stack.top); +} + +void unary_op_stack_pop() { + stack.pop; +} + +void unary_op_stack_dup() { + stack.push(stack.top); +} + +void binary_op_stack_swp() { + auto b = stack.pop; + auto a = stack.pop; + + stack.push(b); + stack.push(a); + +} + +void binary_cond_lt() { + int b = stack.pop.get!int; + int a = stack.pop.get!int; + + stack.push(a < b); +} + +void binary_cond_eq() { + auto b = stack.pop; + auto a = stack.pop; + + stack.push(a == b); +} + +void integral_value_bool(bool value) { + stack.push(Token(value)); +} diff --git a/source/primitives/eval.d b/source/primitives/eval.d new file mode 100644 index 0000000..0ddd65a --- /dev/null +++ b/source/primitives/eval.d @@ -0,0 +1,53 @@ +module primitives.eval; + +import std.variant; + +import base.stack; +import primitives.core; +import conditional = primitives.conditional; + +bool evaluate_primitive(string word) { + switch ( word ) { + case "§" : definition_start; break; + case "$" : binary_op_variable_bind; break; + case "@" : unary_op_variable_resolve; break; + case "if" : conditional.eval_if; break; + case "then" : conditional.eval_then; break; + case "else" : conditional.eval_else; break; + case "+" : binary_op_add; break; + case "*" : binary_op_multiply; break; + case "/" : binary_op_divide; break; + case "%" : binary_op_modulo; break; + case "." : unary_op_io_print; break; + case "pop" : unary_op_stack_pop; break; + case "dup" : unary_op_stack_dup; break; + case "swp" : binary_op_stack_swp; break; + case "true" : integral_value_bool(true); break; + case "false" : integral_value_bool(false); break; + case "<" : binary_cond_lt; break; + case "=" : binary_cond_eq; break; + default : return false; + } + + return true; +} + +bool evaluate(Token token) { + if ( conditional.drop(token) ) { + return true; + } else { + return token.visit!( + (int value) => false, + (bool value) => false, + (string word ) => evaluate_primitive(word) + ); + } +} + +Stack!Token result() { + if ( conditional.dischargeable ) { + return conditional.discharge; + } else { + return Stack!Token(); + } +} diff --git a/src/definition.d b/src/definition.d deleted file mode 100644 index 9c3606e..0000000 --- a/src/definition.d +++ /dev/null @@ -1,62 +0,0 @@ -module src.definition; - -import std.string; -import std.variant; -import std.typecons; - -import std.container : DList; - -import src.stack; - -alias Words = Stack!Token[string]; - -Nullable!(DList!Token) definition; -Words words; - -void start() { - definition = DList!Token(); -} - -void end() { - string wordToBeDefined; - - definition.front.visit!( - (int value) => wordToBeDefined = "", - (bool value) => wordToBeDefined = "", - (string name ) => wordToBeDefined = name - ); - - if ( wordToBeDefined == "" ) { - throw new Exception("words may not be numeric or boolean"); - } - - definition.removeFront; - words[wordToBeDefined] = Stack!Token(definition[]); - definition.nullify; -} - -bool handle(Token token) { - if ( definition.isNull ) { - return false; - } else { - if ( token.type == typeid(string) ) { - if ( *token.peek!string == ";" ) { - end; - } else { - definition.insertBack(token); - } - } else { - definition.insertBack(token); - } - - return true; - } -} - -Stack!Token get(string word) { - if ( word in words ) { - return words[word].dup; - } else { - return Stack!Token(Token(word)); - } -} diff --git a/src/primitives/conditional.d b/src/primitives/conditional.d deleted file mode 100644 index ee89ea5..0000000 --- a/src/primitives/conditional.d +++ /dev/null @@ -1,77 +0,0 @@ -module src.primitives.conditional; - -import std.variant; -import std.typecons; -import std.container : DList; - -import src.stack; - -Nullable!(DList!Token) buffer; -bool concluded = true; -bool drop_mode = false; - -void capture(Token token) { - if ( !drop_mode ) { - buffer.insertBack(token); - } -} - -bool drop(Token token) { - if ( concluded && buffer.isNull ) { - return false; - } - - if ( token.type == typeid(string) ) { - switch ( *token.peek!string ) { - case "if" : eval_if; break; - case "then" : eval_then; break; - case "else" : eval_else; break; - default : capture(token); break; - } - } else { - capture(token); - } - - return true; -} - -void eval_if() { - if ( concluded ) { - buffer = DList!Token(); - drop_mode = !stack.pop.get!bool; - concluded = false; - } else { - throw new Exception("conditionals may not be nested directly"); - } -} - -void eval_then() { - if ( concluded ) { - throw new Exception("`then` without preceding `if`"); - } else { - drop_mode = !drop_mode; - } -} - -void eval_else() { - if ( concluded ) { - throw new Exception("`else` without preceding `if`"); - } else { - drop_mode = false; - concluded = true; - } -} - -bool dischargeable() { - return concluded && !buffer.isNull; -} - -Stack!Token discharge() { - if ( concluded ) { - Stack!Token result = buffer[]; - buffer.nullify; - return result; - } else { - throw new Exception("unconcluded conditional may not be discharged"); - } -} diff --git a/src/primitives/core.d b/src/primitives/core.d deleted file mode 100644 index 5b3d43d..0000000 --- a/src/primitives/core.d +++ /dev/null @@ -1,101 +0,0 @@ -module src.primitives.core; - -import std.stdio; - -import src.stack; -import src.definition; - -Token[string] variables; - -void definition_start() { - src.definition.start; -} - -void binary_op_variable_bind() { - string name = stack.pop.get!string; - Token value = stack.pop; - variables[name] = value; -} - -void unary_op_variable_resolve() { - string name = stack.pop.get!string; - - if ( name in variables ) { - stack.push(variables[name]); - } -} - -void binary_op_add() { - int b = stack.pop.get!int; - int a = stack.pop.get!int; - - stack.push(a + b); -} - -void binary_op_multiply() { - int b = stack.pop.get!int; - int a = stack.pop.get!int; - - stack.push(a * b); -} - -void binary_op_divide() { - int b = stack.pop.get!int; - int a = stack.pop.get!int; - - if ( b == 0 ) { - throw new Exception("division by 0 undefined"); - } else { - stack.push(a / b); - } -} - -void binary_op_modulo() { - int b = stack.pop.get!int; - int a = stack.pop.get!int; - - if ( b == 0 ) { - throw new Exception("modulo 0 undefined"); - } else { - stack.push(a % b); - } -} - -void unary_op_io_print() { - writeln(stack.top); -} - -void unary_op_stack_pop() { - stack.pop; -} - -void unary_op_stack_dup() { - stack.push(stack.top); -} - -void binary_op_stack_swp() { - auto b = stack.pop; - auto a = stack.pop; - - stack.push(b); - stack.push(a); - -} - -void binary_cond_lt() { - int b = stack.pop.get!int; - int a = stack.pop.get!int; - - stack.push(a < b); -} - -void binary_cond_eq() { - auto b = stack.pop; - auto a = stack.pop; - - stack.push(a == b); -} - -void integral_value_bool(bool value) { - stack.push(Token(value)); -} diff --git a/src/primitives/eval.d b/src/primitives/eval.d deleted file mode 100644 index 08d8fb9..0000000 --- a/src/primitives/eval.d +++ /dev/null @@ -1,53 +0,0 @@ -module src.primitives.eval; - -import std.variant; - -import src.stack; -import src.primitives.core; -import conditional = src.primitives.conditional; - -bool evaluate_primitive(string word) { - switch ( word ) { - case "§" : definition_start; break; - case "$" : binary_op_variable_bind; break; - case "@" : unary_op_variable_resolve; break; - case "if" : conditional.eval_if; break; - case "then" : conditional.eval_then; break; - case "else" : conditional.eval_else; break; - case "+" : binary_op_add; break; - case "*" : binary_op_multiply; break; - case "/" : binary_op_divide; break; - case "%" : binary_op_modulo; break; - case "." : unary_op_io_print; break; - case "pop" : unary_op_stack_pop; break; - case "dup" : unary_op_stack_dup; break; - case "swp" : binary_op_stack_swp; break; - case "true" : integral_value_bool(true); break; - case "false" : integral_value_bool(false); break; - case "<" : binary_cond_lt; break; - case "=" : binary_cond_eq; break; - default : return false; - } - - return true; -} - -bool evaluate(Token token) { - if ( conditional.drop(token) ) { - return true; - } else { - return token.visit!( - (int value) => false, - (bool value) => false, - (string word ) => evaluate_primitive(word) - ); - } -} - -Stack!Token result() { - if ( conditional.dischargeable ) { - return conditional.discharge; - } else { - return Stack!Token(); - } -} diff --git a/src/stack.d b/src/stack.d deleted file mode 100644 index 678f3e8..0000000 --- a/src/stack.d +++ /dev/null @@ -1,48 +0,0 @@ -module src.stack; - -import std.conv; -import std.string; -import std.variant; -import std.container : SList; - -static import src.definition; - -alias Token = Algebraic!(int, bool, string); -alias Stack = SList; - -Stack!Token stack; - -Token toToken(string value) { - if ( value.isNumeric ) { - return Token(parse!int(value)); - } else { - return Token(value); - } -} - -Token top(ref Stack!Token stack) { - if ( stack.empty ) { - throw new Exception("stack is empty"); - } else { - return stack.front; - } -} - -Token pop(ref Stack!Token stack) { - Token token = stack.top; - stack.removeFront; - return token; -} - -void push(ref Stack!Token stack, Token token) { - if ( !src.definition.handle(token) ) { - stack.insertFront(token); - } -} - -template push(T) -if ( is(T == int) || is(T == bool) || is (T == string) ) { - void push(ref Stack!Token stack, T value) { - stack.push(Token(value)); - } -} -- cgit v1.2.3