1 % ---------------------------------------------------------------------------- 2 % File: dotsville_i.pro 3 % Type: Prolog source 4 % Line: Implementation of an interpreter for Dotspeak 5 % Date: November 20, 2020 6 % ---------------------------------------------------------------------------- 7 8 %----------------------------------------------------------------------------- 9 % libraries 10 :- consult('io.pro'). 11 :- consult('dotsville_lls.pro'). 12 13 % -------------------------------------------------------------------------- 14 % DCG for the Dotspeak language 15 16 17 % CFG 1-10 18 % sentence, question, command 19 sentence --> command,[.]. 20 sentence --> question,[?]. 21 question --> existentialquestion. 22 question --> countquestion. 23 question --> contentquestion. 24 command --> displaycommand. 25 command --> addcommand. 26 command --> deletecommand. 27 command --> movecommand. 28 command --> [stop],{abort}. 29 30 existentialquestion --> [is],[there],nsdot,[not],[on],[the],[table],{is_there_a_dot_not_on_the_table}. 31 existentialquestion --> [is],[there],nsdot,[on],[the],[table],{is_there_a_dot}. 32 existentialquestion --> [is],[there],nsdot,{is_there_a_dot}. 33 existentialquestion --> [is],[there],csdot(Color),[on],[the],[table],{is_there_a_colored_dot_on_the_table(Color)}. 34 existentialquestion --> [is],[there],csdot(Color),{is_there_a_colored_dot(Color)}. 35 existentialquestion --> [is],[there],csdot,[to],[the],[left],[of],csdot. 36 existentialquestion --> [is],[there],csdot,[to],[the],[right],[of],csdot. 37 existentialquestion --> [is],[there],csdot,[not],[on],[the],[table]. 38 39 countquestion --> countdots,[in],[column],columnid(Column),{how_many_dots_in_column(Column)}. 40 countquestion --> countdots,[in],[the],[world],{how_many_dots_in_the_world}. 41 countquestion --> countdots,[on],[the],[table]. 42 countquestion --> countdots,[off],[the],[table]. 43 countquestion --> countcolordots,[in],[column],columnid. 44 countquestion --> countcolordots,[in],[the],[world]. 45 countquestion --> countspaces,[in],[column],columnid. 46 countquestion --> countspaces,[in],[the],[world]. 47 48 contentquestion --> [is],[column],columnid(Column),[empty],{is_column_empty(Column)}. 49 contentquestion --> [is],[column],columnid(Column),[full],{is_column_full(Column)}. 50 contentquestion --> [is],[the],[world],[empty]. 51 contentquestion --> [is],[the],[world],[full]. 52 53 displaycommand --> [display],[the],[world],{display_world}. 54 displaycommand --> [list],[the],[dots],{list_the_dots}. 55 displaycommand --> [list],[the],color,[dots]. 56 displaycommand --> [list],[the],[dots],[in],[column],columnid. 57 displaycommand --> [list],[the],[dots],[on],[the],[table]. 58 displaycommand --> [list],[the],[dots],[off],[the],[table]. 59 60 addcommand --> [add],csdot(Color),[to],[column],columnid(Column),{add_dot_to_column(Column,Color)}. 61 addcommand --> [add],csdot(Color),{add_one_colored_dot(Color)}. 62 addcommand --> [add],nsdot,{sprinkle_one_dot}. 63 addcommand --> [add],nsdot,[to],[column],columnid. 64 addcommand --> [fill],[column],columnid. 65 addcommand --> [fill],[the],[world],{fill_world}. 66 addcommand --> [sprinkle],xdigit,[dots],[onto],[the],[world]. 67 68 69 deletecommand --> [remove],nsdot,[from],[column],columnid(Column),{remove_dot_from_column(Column)}. 70 deletecommand --> [remove],nsdot. 71 deletecommand --> [clear],[the],[world],{clear_world}. 72 73 74 movecommand --> [move],nsdot,[from],[column],columnid,[to],[column],columnid. 75 76 77 countdots --> [how],[many],[dots]. 78 countcolordots --> [how],[many],color,[dots]. 79 countspaces --> [how],[many],[spaces],[available]. 80 81 82 nsdot --> [a],[dot]. 83 csdot(Color) --> detcolor(Color),[dot]. 84 detcolor(orange) --> [an],[orange]. 85 detcolor(Color) --> [a],nonorange(Color). 86 87 88 nonorange(blue) --> [blue]. 89 nonorange(yellow) --> [yellow]. 90 nonorange(red) --> [red]. 91 nonorange(green) --> [green]. 92 nonorange(purple) --> [purple]. 93 94 color --> [orange]. 95 color --> [blue]. 96 color --> [yellow]. 97 color --> [red]. 98 color --> [green]. 99 color --> [purple]. 100 101 102 xdigit --> [two]. 103 xdigit --> [2]. 104 xdigit --> [three]. 105 xdigit --> [3]. 106 xdigit --> [four]. 107 xdigit --> [4]. 108 xdigit --> [five]. 109 xdigit --> [5]. 110 xdigit --> [six]. 111 xdigit --> [6]. 112 xdigit --> [seven]. 113 xdigit --> [7]. 114 xdigit --> [eight]. 115 xdigit --> [8]. 116 xdigit --> [nine]. 117 xdigit --> [9]. 118 119 columnid(1) --> [1]. 120 columnid(2) --> [2]. 121 columnid(3) --> [3]. 122 columnid(4) --> [4]. 123 columnid(5) --> [5]. 124 columnid(6) --> [6]. 125 126 %-----------------Interpreter/Semantic Support ------------------------ 127 % Interpreter 128 interpreter :- 129 read_sentence(S), 130 sentence(S,[]), 131 interpreter. 132 interpreter :- 133 write('Error ...'),nl, 134 interpreter. 135 136 % Conditional existance commands 137 138 139 is_there_a_dot :- % is_there_a_dot 140 exists_dot_of_color_on_table(_), 141 write('Yes.'),nl. 142 is_there_a_dot :- 143 write('No.'),nl. 144 145 146 147 is_there_not_a_dot :- %is_there_not_a_dot 148 not(exists_dot_of_color_on_table(_)), 149 write('Yes.'),nl. 150 is_there_not_a_dot :- 151 write('No.'),nl. 152 153 154 155 is_there_a_dot_to_the_left(Color1,Color2) :- % is_there_a_dot_to_the_left 156 exists_dot_left_of(Color1, Color2), 157 write('Yes.'),nl. 158 159 is_there_a_dot_to_the_left(_,_) :- 160 write('No.'),nl. 161 162 163 164 is_there_a_dot_to_the_right(Color1,Color2) :- % is_there_a_dot_to_the_right 165 exists_dot_right_of(Color1, Color2), 166 write('Yes.'),nl. 167 168 is_there_a_dot_to_the_right(_,_) :- 169 write('No.'),nl. 170 171 172 is_column_empty(Column) :- % is_column_empty 173 is_empty_column(Column), 174 write('Yes.'),nl. 175 176 is_column_empty(_) :- % is_column_full (list) 177 write('No.'),nl. 178 179 180 is_column_full(Column) :- % is_column_full 181 is_full_column(Column), 182 write('Yes.'),nl. 183 is_column_full(_) :- 184 write('No.'),nl. 185 186 187 is_color_right_of_color(Color1, Color2) :- % is_color_right_of_color(Color1, Color2) 188 exists_dot_right_of(Color1,Color2), write('Yes.'), nl. 189 is_color_right_of_color(_, _) :- 190 write('No.'), nl. 191 192 193 is_color_left_of_color(Color1, Color2) :- % is_color_left_of_color(Color1, Color2) 194 exists_dot_left_of(Color1,Color2), write('Yes.'), nl. 195 is_color_left_of_color(_, _) :- 196 write('No.'), nl. 197 198 199 %-------------------------- Functions ---------------------- 200 201 % list_the_dots 202 list_the_dots :- list_dots_in_column(1), 203 list_dots_in_column(2), 204 list_dots_in_column(3), 205 list_dots_in_column(4), 206 list_dots_in_column(5), 207 list_dots_in_column(6). 208 209 210 remove_random_dot :- % remove_random_dot 211 dot(cell(Row,Column),_), Row >= 1, remove_dot_from_column(Column). 212 remove_random_dot :- 213 write('No dots to remove.'), nl. 214 215 216 add_one_colored_dot(Color) :-% add_one_colored_dot(Color) 217 available_spaces(S), 218 pick(S,space(Row,Column)), 219 add_dot(cell(Row,Column),color(Color)). 220 221 add_one_colored_dot(_) :- 222 write('### Could not add the dot.'),nl. 223 224 225 add_one_colored_dot_to_column(Color, Column) :- not(dot(cell(1, Column), color(_))), 226 add_dot(cell(1, Column), color(Color)). 227 add_one_colored_dot_to_column(Color, Column) :- not(dot(cell(2, Column), color(_))), 228 add_dot(cell(2, Column), color(Color)). 229 add_one_colored_dot_to_column(Color, Column) :- not(dot(cell(3, Column), color(_))), 230 add_dot(cell(3, Column), color(Color)). 231 add_one_colored_dot_to_column(Color, Column) :- not(dot(cell(4, Column), color(_))), 232 add_dot(cell(4, Column), color(Color)). 233 add_one_colored_dot_to_column(_, _) :- write('### Could not add the dot.'), nl. 234 235 236 count_dots_of_color_in_world(Color, Count) :- % count_dots_of_color_in_world 237 count_dots_of_color_in_column(Color,1,Count1), 238 count_dots_of_color_in_column(Color,2,Count2), 239 count_dots_of_color_in_column(Color,3,Count3), 240 count_dots_of_color_in_column(Color,4,Count4), 241 count_dots_of_color_in_column(Color,5,Count5), 242 count_dots_of_color_in_column(Color,6,Count6), 243 Count is Count1 + Count2 + Count3 + Count4 + Count5 + Count6.