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