+/EE312
software design
git clone https://git.julia.blue/+/EE312
5dbb718
master
1#include "parse.h" 2#include <iostream> 3 4Scope global (nullptr ); 5Scope * currentScope = & global ; 6std::vector < Scope *> callStack ; 7bool ret = false; 8int retval ; 9 10int * lookupVar (std::string s ) { 11Scope * temp = currentScope ; 12if (!temp -> vars .count (s )) { 13temp = temp -> outer ; 14if (!temp || !temp -> vars .count (s )) { 15return nullptr ; 16 } 17 } 18return & temp -> vars [s ]; 19} 20 21Statement * lookupFunc (std::string s ) { 22Scope * temp = currentScope ; 23if (!temp -> funcs .count (s )) { 24temp = temp -> outer ; 25if (!temp || !temp -> funcs .count (s )) { 26return nullptr ; 27 } 28 } 29return temp -> funcs [s ]; 30} 31 32bool strcmp (const char * a ,const char * b ) { 33while (* a && * b ) { 34if (* a != * b ) { 35return false; 36 } 37++ a ,++ b ; 38 } 39if (* a || * b ) { 40return false; 41 } 42return true; 43} 44 45int atoi (const char * a ) { 46int temp = 0 ; 47while (* a ) { 48temp *=10 ; 49temp += * a ++ - '0' ; 50 } 51return temp ; 52} 53 54Expression ::Expression () { 55lhs = rhs = nullptr ; 56tok = current ; 57advance (); 58if (strcmp (tok .data ,"call" )) { 59rhs = new Expression (current ); 60advance (); 61advance (); 62Expression * e = this ; 63while (!strcmp (current .data ,"sgra" )) { 64e -> lhs = new Expression (tok ); 65e = e -> lhs ; 66e -> rhs = new Expression (); 67 } 68advance (); 69return ; 70 } 71if (tok .type != OPERATOR ) { 72return ; 73 } 74if (strcmp (tok .data ,"~" )|| strcmp (tok .data ,"!" )) { 75lhs = new Expression (); 76return ; 77 } 78lhs = new Expression (); 79rhs = new Expression (); 80} 81 82Expression ::Expression (Token t ) { 83lhs = rhs = nullptr ; 84tok = t ; 85} 86 87Expression ::~Expression () { 88if (lhs != nullptr ) { 89delete lhs ; 90 } 91if (rhs != nullptr ) { 92delete rhs ; 93 } 94} 95 96int Expression ::evaluate () { 97if (tok .type == NUMBER ) { 98return atoi (tok .data ); 99 } 100if (tok .type == NAME ) { 101if (strcmp (tok .data ,"call" )) { 102Statement * s = lookupFunc (rhs -> tok .data ); 103if (!s ) { 104 std::cout <<"\nError: undeclared function\n" ; 105return 0 ; 106 } 107s -> local = new Scope (& global ); 108Expression * e = lhs ; 109for (int i = 0 ;e ;++ i ,e = e -> lhs ) { 110s -> local -> vars [s -> args [i ].data ]= e -> rhs -> evaluate (); 111 } 112callStack .push_back (currentScope ); 113currentScope = s -> local ; 114for (unsigned int i = 0 ; !ret && i < s -> contents .size ();++ i ) { 115s -> contents [i ]-> execute (); 116 } 117if (!ret ) { 118retval = 0 ; 119 } 120ret = false; 121if (!callStack .empty ()) { 122currentScope = callStack [callStack .size ()- 1 ]; 123callStack .pop_back (); 124 }else { 125currentScope = currentScope -> outer ; 126 } 127delete s -> local ; 128s -> local = nullptr ; 129return retval ; 130 } 131int * temp = lookupVar (tok .data ); 132if (!temp ) { 133// error undeclared identifier 134return 0 ; 135 } 136return * temp ; 137 } 138if (strcmp (tok .data ,"+" )) { 139return lhs -> evaluate ()+ rhs -> evaluate (); 140 } 141if (strcmp (tok .data ,"-" )) { 142return lhs -> evaluate ()- rhs -> evaluate (); 143 } 144if (strcmp (tok .data ,"*" )) { 145return lhs -> evaluate ()* rhs -> evaluate (); 146 } 147if (strcmp (tok .data ,"/" )) { 148return lhs -> evaluate () /rhs -> evaluate (); 149 } 150if (strcmp (tok .data ,"%" )) { 151return lhs -> evaluate () %rhs -> evaluate (); 152 } 153if (strcmp (tok .data ,"&&" )) { 154int noShortCircuit = lhs -> evaluate (); 155return rhs -> evaluate ()&& noShortCircuit ; 156 } 157if (strcmp (tok .data ,"||" )) { 158int noShortCircuit = lhs -> evaluate (); 159return rhs -> evaluate ()|| noShortCircuit ; 160 } 161if (strcmp (tok .data ,">" )) { 162return lhs -> evaluate ()> rhs -> evaluate (); 163 } 164if (strcmp (tok .data ,"<" )) { 165return lhs -> evaluate ()< rhs -> evaluate (); 166 } 167if (strcmp (tok .data ,"==" )) { 168return lhs -> evaluate ()== rhs -> evaluate (); 169 } 170if (strcmp (tok .data ,"!=" )) { 171return lhs -> evaluate ()!= rhs -> evaluate (); 172 } 173if (strcmp (tok .data ,"<=" )) { 174return lhs -> evaluate () <=rhs -> evaluate (); 175 } 176if (strcmp (tok .data ,">=" )) { 177return lhs -> evaluate () >=rhs -> evaluate (); 178 } 179if (strcmp (tok .data ,"~" )) { 180return - lhs -> evaluate (); 181 } 182if (strcmp (tok .data ,"!" )) { 183return !lhs -> evaluate (); 184 } 185 std::cout <<"expected expression, got: " <<current .data << std::endl ; 186exit (1 ); 187} 188 189void Expression ::debugPrint () { 190 std::cout <<tok .data <<' ' ; 191if (lhs ) { 192lhs -> debugPrint (); 193 } 194if (rhs ) { 195rhs -> debugPrint (); 196 } 197} 198 199void Statement ::parseText () { 200type = TEXT ; 201advance (); 202tok = current ; 203advance (); 204} 205 206void Statement ::parseOutput () { 207type = OUTPUT ; 208advance (); 209exp = new Expression (); 210} 211 212void Statement ::parseVar () { 213type = VAR ; 214advance (); 215tok = current ; 216advance (); 217exp = new Expression (); 218} 219 220void Statement ::parseSet () { 221type = SET ; 222advance (); 223tok = current ; 224advance (); 225exp = new Expression (); 226} 227 228void Statement ::parseDo () { 229type = DO ; 230advance (); 231exp = new Expression (); 232while (!strcmp (current .data ,"od" )) {contents .push_back (new Statement ()); } 233advance (); 234} 235 236void Statement ::parseIf () { 237type = IF ; 238advance (); 239exp = new Expression (); 240while (!(strcmp (current .data ,"fi" )|| strcmp (current .data ,"else" ))) { 241contents .push_back (new Statement ()); 242 } 243if (strcmp (current .data ,"else" )) { 244advance (); 245while (!strcmp (current .data ,"fi" )) { 246elseContents .push_back (new Statement ()); 247 } 248 } 249advance (); 250} 251 252void Statement ::parseFunc () { 253type = FUNC ; 254advance (); 255tok = current ; 256advance (); 257advance (); 258while (!strcmp (current .data ,"smarap" )) { 259args .push_back (current ); 260advance (); 261 } 262advance (); 263while (!strcmp (current .data ,"nufed" )) { 264contents .push_back (new Statement ()); 265 } 266advance (); 267} 268 269void Statement ::parseReturn () { 270type = RETURN ; 271advance (); 272exp = new Expression (); 273} 274 275Statement ::Statement () { 276exp = nullptr ; 277local = nullptr ; 278if (strcmp (current .data ,"text" )) { 279parseText (); 280 }else if (strcmp (current .data ,"output" )) { 281parseOutput (); 282 }else if (strcmp (current .data ,"var" )) { 283parseVar (); 284 }else if (strcmp (current .data ,"set" )) { 285parseSet (); 286 }else if (strcmp (current .data ,"do" )) { 287parseDo (); 288 }else if (strcmp (current .data ,"if" )) { 289parseIf (); 290 }else if (strcmp (current .data ,"defun" )) { 291parseFunc (); 292 }else if (strcmp (current .data ,"return" )) { 293parseReturn (); 294 }else { 295 std::cout <<"Expected keyword, got: " <<current .data << std::endl ; 296exit (1 ); 297 } 298} 299 300Statement ::~Statement () { 301if (exp ) { 302delete exp ; 303 } 304if (local ) { 305delete local ; 306 } 307for (unsigned int i = 0 ;i < contents .size ();++ i ) {delete contents [i ]; } 308for (unsigned int i = 0 ;i < elseContents .size ();++ i ) { 309delete elseContents [i ]; 310 } 311} 312 313static void printString (const char * s ) { 314while (* s ) { 315if (* s == '\\' ) { 316if (*++ s == 'n' ) { 317 std::cout <<'\n' ; 318 }else if (* s == 't' ) { 319 std::cout <<'\t' ; 320 }else if (* s == '\\' ) { 321 std::cout <<'\\' ; 322 }else { 323// escaped something weird 324 } 325++ s ; 326 }else { 327 std::cout <<* s ++ ; 328 } 329 } 330} 331 332void Statement ::execute () { 333if (type == TEXT ) { 334printString (tok .data ); 335 }else if (type == OUTPUT ) { 336 std::cout <<exp -> evaluate (); 337 }else if (type == VAR ) { 338if (currentScope -> vars .count (tok .data )) { 339 std::cout <<"variable " <<tok .data 340 <<" incorrectly re-initialized" << std::endl ; 341 } 342currentScope -> vars [tok .data ]= exp -> evaluate (); 343 }else if (type == SET ) { 344int * var = lookupVar (tok .data ); 345if (!var ) { 346 std::cout <<"variable " <<tok .data <<" not declared" 347 << std::endl ; 348currentScope -> vars [tok .data ]= exp -> evaluate (); 349 }else { 350* var = exp -> evaluate (); 351 } 352 }else if (type == DO ) { 353while (exp -> evaluate ()&& !ret ) { 354for (unsigned int i = 0 ;i < contents .size ()&& !ret ;++ i ) { 355contents [i ]-> execute (); 356 } 357 } 358 }else if (type == IF ) { 359if (exp -> evaluate ()) { 360for (unsigned int i = 0 ;i < contents .size ()&& !ret ;++ i ) { 361contents [i ]-> execute (); 362 } 363 }else { 364for (unsigned int i = 0 ;i < elseContents .size ()&& !ret ;++ i ) { 365elseContents [i ]-> execute (); 366 } 367 } 368 }else if (type == RETURN ) { 369retval = exp -> evaluate (); 370ret = true; 371 }else if (type == FUNC ) { 372// Execution is handled by "call" expression 373currentScope -> funcs [tok .data ]= this ; 374 } 375} 376 377void Statement ::debugPrint () { 378if (type == TEXT ) { 379 std::cout <<"text: " <<tok .data ; 380 }else if (type == OUTPUT ) { 381 std::cout <<"output: " ; 382exp -> debugPrint (); 383 }else if (type == VAR ) { 384 std::cout <<"var: " <<tok .data <<" = " ; 385exp -> debugPrint (); 386 }else if (type == SET ) { 387 std::cout <<"set: " <<tok .data <<" = " ; 388exp -> debugPrint (); 389 }else if (type == DO ) { 390 std::cout <<"while: " ; 391exp -> debugPrint (); 392 std::cout << std::endl ; 393for (unsigned int i = 0 ;i < contents .size ();++ i ) { 394contents [i ]-> debugPrint (); 395 } 396 }else if (type == IF ) { 397 std::cout <<"if: " ; 398exp -> debugPrint (); 399 std::cout <<"then: " << std::endl ; 400for (unsigned int i = 0 ;i < contents .size ();++ i ) { 401contents [i ]-> debugPrint (); 402 } 403 std::cout <<"else: " << std::endl ; 404for (unsigned int i = 0 ;i < elseContents .size ();++ i ) { 405elseContents [i ]-> debugPrint (); 406 } 407 }else if (type == FUNC ) { 408 std::cout <<"func: " ; 409for (unsigned int i = 0 ;i < args .size ();++ i ) { 410 std::cout <<args [i ].data <<" " ; 411 } 412 std::cout << std::endl ; 413for (unsigned int i = 0 ;i < contents .size ();++ i ) { 414contents [i ]-> debugPrint (); 415 } 416 }else if (type == RETURN ) { 417 std::cout <<"return: " ; 418exp -> debugPrint (); 419 } 420 std::cout <<"\tDone\n" ; 421} 422