diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/definition.d | 57 | ||||
-rw-r--r-- | src/primitives.d | 69 | ||||
-rw-r--r-- | src/stack.d | 38 |
3 files changed, 164 insertions, 0 deletions
diff --git a/src/definition.d b/src/definition.d new file mode 100644 index 0000000..dd4dddc --- /dev/null +++ b/src/definition.d @@ -0,0 +1,57 @@ +module src.definition; + +import std.string; +import std.variant; +import std.typecons; +import std.container : DList; + +import src.stack : Token; + +alias Words = DList!Token[string]; + +Nullable!(DList!Token) definition; +Words words; + +void start() { + definition = DList!Token(); +} + +void end() { + string wordToBeDefined; + + definition.front.visit!( + (int x ) => wordToBeDefined = "", + (string name) => wordToBeDefined = name + ); + + if ( wordToBeDefined == "" ) { + throw new Exception("words may not be numeric"); + } + + definition.removeFront; + words[wordToBeDefined] = definition; + definition.nullify; +} + +bool handle(int value) { + if ( definition.isNull ) { + return false; + } else { + definition.insertBack(Token(value)); + return true; + } +} + +bool handle(string word) { + if ( definition.isNull ) { + return false; + } else { + if ( word == ";" ) { + end(); + } else { + definition.insertBack(Token(word)); + } + + return true; + } +} diff --git a/src/primitives.d b/src/primitives.d new file mode 100644 index 0000000..0be6836 --- /dev/null +++ b/src/primitives.d @@ -0,0 +1,69 @@ +module src.primitives; + +import std.stdio; + +import src.stack; +import src.definition; + +int[string] variables; + +bool evaluate(string word) { + switch ( word ) { + case "ยง": + src.definition.start; + return true; + case "$": + string name = stack.pop.get!string; + int value = stack.pop.get!int; + + variables[name] = value; + return true; + case "@": + string name = stack.pop.get!string; + + if ( name in variables ) { + stack.push(variables[name]); + } + return true; + case "+": + int b = stack.pop.get!int; + int a = stack.pop.get!int; + + stack.push(a + b); + return true; + case "*": + int b = stack.pop.get!int; + int a = stack.pop.get!int; + + stack.push(a * b); + return true; + case "/": + 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); + } + return true; + case "%": + 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); + } + return true; + case ".": + stack.pop; + return true; + case "'": + writeln(stack.top); + return true; + default: + return false; + } +} diff --git a/src/stack.d b/src/stack.d new file mode 100644 index 0000000..756dafc --- /dev/null +++ b/src/stack.d @@ -0,0 +1,38 @@ +module src.stack; + +import std.variant; +import std.string; +import std.container : SList; + +static import src.definition; + +alias Token = Algebraic!(int, string); +alias Stack = SList; + +Stack!Token stack; + +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, int value) { + if ( !src.definition.handle(value) ) { + stack.insertFront(Token(value)); + } +} + +void push(ref Stack!Token stack, string word) { + if ( !src.definition.handle(word) ) { + stack.insertFront(Token(word)); + } +} |