void printTree(std::ostream& os, int depth =0){ os<<string(depth,' ')<<"NArray:values{"<<endl; for(int i=0; i<values.size(); i++){ values[i]->printTree(os,depth+1); } os<<string(depth,' ')<<"}"<<endl; }
void printTree(std::ostream& os, int depth =0){ os<<string(depth,' ')<<"NFunctionCall: id="<< id.name<<endl; os<<string(depth+1,' ')<<"arguments= {"<<endl; for(int i=0;i<arguments.size();i++){ arguments[i]->printTree(os,depth+2); } os<<string(depth+1,' ')<<"}"<<endl; }
NArray(std::string& s){ #ifdef DEBUG cout << "string param "<< s.size()<<endl; cout << "string param "<< s<<endl; #endif for(int i=0; i<s.size();i++){ values.push_back(new NChar(s[i])); } #ifdef DEBUG cout << "constrc string "<< values.size()<<endl; #endif }
TType* typeChk(Symtable& t,TType* expected = NULL){ bool err=false; #ifdef DEBUG cout << "values size " << values.size() << endl; #endif TType* elem = values[0]->typeChk(t); for(int i=1; i<values.size(); i++){ if(elem != values[i]->typeChk(t)) err=true; } if(err){ fprintf(stderr, "Array elements are not the same type.\n"); return NULL; }else{ #ifdef DEBUG cout << "cons array type " << elem->name << endl; #endif return new TArray(*elem,values.size()); } }
TType* typeChk(Symtable& t,TType* expected = NULL){ TType* tip; std::vector<TType*> args; for(int i=0;i<arguments.size();i++){ tip = arguments[i]->typeChk(t); if(tip == NULL){ cerr << "Type of argument " << i << " doesn't exist" << endl; }else{ args.push_back(tip); } } if(t.lookupFunc(id.name,args) == NULL){ cerr << "Function "<<id.name<<" was not declared"<< endl; return NULL; }else{ #ifdef DEBUG cerr << "Function call type "<<(t.lookupFunc(id.name,args)->type)->name<< endl; #endif return t.lookupFunc(id.name,args)->type; } }
CommandResult ExpressionInterpreter::interpretCommand(string userInput) { CommandResult result = CommandSuccess; string text; stringstream ss; ss << userInput; ExpressionList currentState; currentState.push_back(mpRoot); ExpressionList nextState; while (result == CommandSuccess && !ss.eof()) { ss >> text; transform(text.begin(), text.end(), text.begin(), ::tolower); ExpressionList::const_iterator iter = currentState.begin(); ExpressionList::const_iterator end = currentState.end(); for (; iter != end; ++iter) { Expression* expr = *iter; ExpressionList exprNextList = expr->getNextExpressionClosure(text); nextState.splice(nextState.end(), exprNextList); } if (nextState.size() > 0) { currentState = nextState; nextState.clear(); } else { mErrorText = "'" + text + "' not recognized."; result = CommandInvalid; } } //remove impossible expressions in the final state before checking for ambiguity nextState.clear(); ExpressionList::const_iterator iter = currentState.begin(); ExpressionList::const_iterator end = currentState.end(); for (; iter != end; ++iter) { Expression* expr = *iter; if (expr->isExecutable()) { nextState.push_back(expr); } else { ExpressionList children = expr->getNextExpressions(); bool flag = false; ExpressionList::const_iterator iter = children.begin(); ExpressionList::const_iterator end = children.end(); for (; iter != end; ++iter) { if ((*iter)->getName()[0] == '[') { flag = true; } } if (flag || children.size() == 0) { nextState.push_back(expr); } } } currentState = nextState; if (currentState.size() != 1) { mErrorText = "'" + text + "' ambiguous or incomplete."; result = CommandInvalid; } //run command if executable and non-ambiguous if (result == CommandSuccess) { Expression* expr = *(currentState.begin()); ExpressionList executables = expr->getClosureExecutables(false); if (executables.size() == 1) { ilmErrorTypes initResult = ilm_init(); if (ILM_SUCCESS != initResult) { mErrorText = ILM_ERROR_STRING(initResult); result = CommandExecutionFailed; } else { Expression* exec = executables.front(); exec->execute(); ilm_commitChanges(); ilm_destroy(); } } else if (executables.size() == 0) { mErrorText = "command is incomplete."; result = CommandIncomplete; } else { mErrorText = "command is ambiguous."; result = CommandIncomplete; } } return result; }