Skip to content

Commit 98ed856

Browse files
committed
Add names to actions
1 parent dc91630 commit 98ed856

File tree

3 files changed

+50
-39
lines changed

3 files changed

+50
-39
lines changed

javascripts/compiler.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ function compile(dictionary, actions) {
119119
}
120120

121121
function Action(action) {
122+
this.name = action._name; // expose name for easy debugging
123+
122124
this.execute = function (context) {
123125
return action(context);
124126
};

javascripts/forth.js

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,26 @@ function Forth() {
2525
this.message = word + " ? ";
2626
}
2727

28+
function namedFunction(name, func) {
29+
func._name = name;
30+
return func;
31+
}
32+
2833
// Convert token into an action that executes that token's behavior
2934
function tokenToAction(token) {
3035
var word = token.value;
3136
var definition = context.dictionary.lookup(word);
3237

3338
if (token.isStringLiteral) {
34-
return function (context) {
39+
return namedFunction("String: " + word, function (context) {
3540
return word;
36-
};
41+
});
3742
} else if (definition !== null) {
3843
return definition;
3944
} else if (isFinite(word)) {
40-
return function (context) {
45+
return namedFunction("Number: " + word, function (context) {
4146
context.stack.push(+word);
42-
};
47+
});
4348
} else {
4449
throw new MissingWordError(word);
4550
}
@@ -49,21 +54,25 @@ function Forth() {
4954
};
5055
}
5156

57+
function addToDictionary(name, definition) {
58+
context.dictionary.add(name, namedFunction(name, definition));
59+
}
60+
5261
// compile actions into definition and add definition to dictionary
5362
function compileAndAddToDictionary(name, actions) {
5463
var definition = compile(context.dictionary, actions);
55-
context.dictionary.add(name, definition);
64+
addToDictionary(name, definition);
5665
}
5766

5867
function createVariable(name) {
5968
var pointer = context.memory.addVariable(name);
60-
context.dictionary.add(name, function (context) {
69+
addToDictionary(name, function (context) {
6170
context.stack.push(pointer);
6271
});
6372
}
6473

6574
function createConstant(name, value) {
66-
context.dictionary.add(name, function (context) {
75+
addToDictionary(name, function (context) {
6776
context.stack.push(value);
6877
});
6978
}
@@ -169,7 +178,7 @@ function Forth() {
169178
}
170179

171180
// because readLines is async, addPredefinedWords is async too
172-
var promise = addPredefinedWords(context.dictionary, readLines);
181+
var promise = addPredefinedWords(addToDictionary, readLines);
173182

174183
// because addPredefinedWords is async, Forth is async
175184
return promise.then(function () {

javascripts/predefined.js

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
function addPredefinedWords(dictionary, readLines) {
1+
function addPredefinedWords(addToDictionary, readLines) {
22
function controlCode(code) {
33
return {
44
isControlCode: true,
@@ -10,147 +10,147 @@ function addPredefinedWords(dictionary, readLines) {
1010
":", ";", "if", "else", "then", "do", "loop",
1111
"+loop", "begin", "until", "variable", "constant", "key"
1212
].forEach(function (code) {
13-
dictionary.add(code, controlCode(code));
13+
addToDictionary(code, controlCode(code));
1414
});
1515

16-
dictionary.add(".", function (context) {
16+
addToDictionary(".", function (context) {
1717
return context.stack.pop() + " ";
1818
});
1919

20-
dictionary.add(".s", function (context) {
20+
addToDictionary(".s", function (context) {
2121
return "\n" + context.stack.print();
2222
});
2323

24-
dictionary.add("+", function (context) {
24+
addToDictionary("+", function (context) {
2525
context.stack.push(context.stack.pop() + context.stack.pop());
2626
});
2727

28-
dictionary.add("-", function (context) {
28+
addToDictionary("-", function (context) {
2929
var a = context.stack.pop(), b = context.stack.pop();
3030
context.stack.push(b - a);
3131
});
3232

33-
dictionary.add("*", function (context) {
33+
addToDictionary("*", function (context) {
3434
context.stack.push(context.stack.pop() * context.stack.pop());
3535
});
3636

37-
dictionary.add("/", function (context) {
37+
addToDictionary("/", function (context) {
3838
var a = context.stack.pop(), b = context.stack.pop();
3939
context.stack.push(Math.floor(b / a));
4040
});
4141

42-
dictionary.add("/mod", function (context) {
42+
addToDictionary("/mod", function (context) {
4343
var a = context.stack.pop(), b = context.stack.pop();
4444
context.stack.push(Math.floor(b % a));
4545
context.stack.push(Math.floor(b / a));
4646
});
4747

48-
dictionary.add("mod", function (context) {
48+
addToDictionary("mod", function (context) {
4949
var a = context.stack.pop(), b = context.stack.pop();
5050
context.stack.push(Math.floor(b % a));
5151
});
5252

53-
dictionary.add("=", function (context) {
53+
addToDictionary("=", function (context) {
5454
context.stack.push(context.stack.pop() === context.stack.pop() ? TRUE : FALSE);
5555
});
5656

57-
dictionary.add("<", function (context) {
57+
addToDictionary("<", function (context) {
5858
var a = context.stack.pop(), b = context.stack.pop();
5959
context.stack.push(b < a ? TRUE : FALSE);
6060
});
6161

62-
dictionary.add(">", function (context) {
62+
addToDictionary(">", function (context) {
6363
var a = context.stack.pop(), b = context.stack.pop();
6464
context.stack.push(b > a ? TRUE : FALSE);
6565
});
6666

67-
dictionary.add("and", function (context) {
67+
addToDictionary("and", function (context) {
6868
var a = context.stack.pop(), b = context.stack.pop();
6969
context.stack.push(b & a);
7070
});
7171

72-
dictionary.add("or", function (context) {
72+
addToDictionary("or", function (context) {
7373
var a = context.stack.pop(), b = context.stack.pop();
7474
context.stack.push(b | a);
7575
});
7676

77-
dictionary.add("invert", function (context) {
77+
addToDictionary("invert", function (context) {
7878
// invert is bitwise not
7979
context.stack.push(~context.stack.pop());
8080
});
8181

82-
dictionary.add("i", function (context) {
82+
addToDictionary("i", function (context) {
8383
context.stack.push(context.returnStack.peek(1));
8484
});
8585

86-
dictionary.add("j", function (context) {
86+
addToDictionary("j", function (context) {
8787
context.stack.push(context.returnStack.peek(2));
8888
});
8989

9090
// I don't understand the difference between i and r@
9191
// http://www.forth.com/starting-forth/sf5/sf5.html
92-
dictionary.add("r@", function (context) {
92+
addToDictionary("r@", function (context) {
9393
context.stack.push(context.returnStack.peek(1));
9494
});
9595

96-
dictionary.add(">r", function (context) {
96+
addToDictionary(">r", function (context) {
9797
context.returnStack.push(context.stack.pop());
9898
});
9999

100-
dictionary.add("r>", function (context) {
100+
addToDictionary("r>", function (context) {
101101
context.stack.push(context.returnStack.pop());
102102
});
103103

104-
dictionary.add("emit", function (context) {
104+
addToDictionary("emit", function (context) {
105105
return String.fromCharCode(context.stack.pop());
106106
});
107107

108-
dictionary.add("swap", function (context) {
108+
addToDictionary("swap", function (context) {
109109
var a = context.stack.pop(), b = context.stack.pop();
110110
context.stack.push(a);
111111
context.stack.push(b);
112112
});
113113

114-
dictionary.add("dup", function (context) {
114+
addToDictionary("dup", function (context) {
115115
var a = context.stack.pop();
116116
context.stack.push(a);
117117
context.stack.push(a);
118118
});
119119

120-
dictionary.add("over", function (context) {
120+
addToDictionary("over", function (context) {
121121
var a = context.stack.pop(), b = context.stack.pop();
122122
context.stack.push(b);
123123
context.stack.push(a);
124124
context.stack.push(b);
125125
});
126126

127-
dictionary.add("rot", function (context) {
127+
addToDictionary("rot", function (context) {
128128
var a = context.stack.pop(), b = context.stack.pop(), c = context.stack.pop();
129129
context.stack.push(b);
130130
context.stack.push(a);
131131
context.stack.push(c);
132132
});
133133

134-
dictionary.add("drop", function (context) {
134+
addToDictionary("drop", function (context) {
135135
context.stack.pop();
136136
});
137137

138-
dictionary.add("!", function (context) {
138+
addToDictionary("!", function (context) {
139139
var address = context.stack.pop();
140140
var value = context.stack.pop();
141141
context.memory.setValue(address, value);
142142
});
143143

144-
dictionary.add("@", function (context) {
144+
addToDictionary("@", function (context) {
145145
var address = context.stack.pop();
146146
context.stack.push(context.memory.getValue(address));
147147
});
148148

149-
dictionary.add("allot", function (context) {
149+
addToDictionary("allot", function (context) {
150150
context.memory.allot(context.stack.pop());
151151
});
152152

153-
dictionary.add("key", function (context) {
153+
addToDictionary("key", function (context) {
154154
context.waitingForKey = true;
155155

156156
// set callback for when key is pressed

0 commit comments

Comments
 (0)