aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAdrian Kummerlaender2017-04-11 21:57:43 +0200
committerAdrian Kummerlaender2017-04-11 21:57:43 +0200
commit29626d3beaabdc638ed5cb52f2b252bb35e7ec0b (patch)
treecda3dc55ecb8358c9d40ecec0ada1e72a8aa4931
parent9fa3bcf8d3d8801ae01530afa3cff9a31e22a0a7 (diff)
downloadslang-29626d3beaabdc638ed5cb52f2b252bb35e7ec0b.tar
slang-29626d3beaabdc638ed5cb52f2b252bb35e7ec0b.tar.gz
slang-29626d3beaabdc638ed5cb52f2b252bb35e7ec0b.tar.bz2
slang-29626d3beaabdc638ed5cb52f2b252bb35e7ec0b.tar.lz
slang-29626d3beaabdc638ed5cb52f2b252bb35e7ec0b.tar.xz
slang-29626d3beaabdc638ed5cb52f2b252bb35e7ec0b.tar.zst
slang-29626d3beaabdc638ed5cb52f2b252bb35e7ec0b.zip
Play around with contractual programming
-rw-r--r--repl.d74
1 files changed, 44 insertions, 30 deletions
diff --git a/repl.d b/repl.d
index 7026b30..9117c28 100644
--- a/repl.d
+++ b/repl.d
@@ -10,32 +10,31 @@ SList!int stack;
DList!string[string] words;
Nullable!(DList!string) definition;
-void push(ref SList!int stack, int value) {
- if ( definition.isNull ) {
- stack.insertFront(value);
- } else {
- definition.insertBack(to!string(value));
- }
+void startDefinition()
+in { assert( definition.isNull); }
+out { assert(!definition.isNull); }
+body {
+ definition = DList!string();
}
-int pop(ref SList!int stack) {
- auto x = stack.front;
- stack.removeFront;
+void endDefinition()
+in { assert(!definition.isNull); }
+out { assert( definition.isNull); }
+body {
+ auto wordToBeDefined = definition.front;
- return x;
+ if ( wordToBeDefined.isNumeric ) {
+ throw new Exception("words may not be numeric");
+ } else {
+ definition.removeFront;
+ words[wordToBeDefined] = definition;
+ definition.nullify;
+ }
}
void append(ref Nullable!(DList!string) definition, string token) {
if ( token == ";" ) {
- auto wordToBeDefined = definition.front;
-
- if ( wordToBeDefined.isNumeric ) {
- throw new Exception("words may not be numeric");
- } else {
- definition.removeFront;
- words[wordToBeDefined] = definition;
- definition.nullify;
- }
+ endDefinition();
} else {
definition.insertBack(token);
}
@@ -44,7 +43,7 @@ void append(ref Nullable!(DList!string) definition, string token) {
void evaluate(string word) {
switch ( word ) {
case "ยง":
- definition = DList!string();
+ startDefinition();
break;
case "+":
auto a = stack.pop();
@@ -63,24 +62,39 @@ void evaluate(string word) {
break;
default:
foreach ( token; words[word] ) {
- if ( token.isNumeric ) {
- stack.push(parse!int(token));
- } else {
- evaluate(token);
- }
+ process(token);
}
}
}
+void process(string token) {
+ if ( token.isNumeric ) {
+ stack.push(parse!int(token));
+ } else {
+ evaluate(token);
+ }
+}
+
+void push(ref SList!int stack, int value) {
+ if ( definition.isNull ) {
+ stack.insertFront(value);
+ } else {
+ definition.append(to!string(value));
+ }
+}
+
+int pop(ref SList!int stack) {
+ auto x = stack.front;
+ stack.removeFront;
+
+ return x;
+}
+
void main() {
while ( !stdin.eof ) {
foreach ( token; stdin.readln.split ) {
if ( definition.isNull ) {
- if ( token.isNumeric ) {
- stack.push(parse!int(token));
- } else {
- evaluate(token);
- }
+ process(token);
} else {
definition.append(token);
}