int main(int argc, char *argv[]){ /* Define a default expression so I can test it without GDB screwing about * with escape characters... */ char defaultExpression[] = "substr(`This is a test`, 1, 4)"; char *expression = NULL; Buffer *b = new_Buffer(256); char *result = NULL; int ii; Expr *e = NULL; /* Variable "scopes" */ Vars *globals = NULL; Vars *locals = NULL; Logging_setup(argv[0], LOG_TOSTDERR | LOG_TOFILE | LOG_LEVELTRACE, "EvalTest.log"); /* Add a few variables */ globals = new_Vars(NULL); locals = new_Vars(globals); Vars_let(globals, "pi", "3.1415", VAR_CONST | VAR_NOSHADOW); Vars_let(locals, "x", "1234", VAR_STD); Vars_let(locals, "t", "This is a test", VAR_STD); /* Build expression off command line if supplied */ if(argc > 1){ for(ii = 1; ii < argc; ++ii){ Buffer_appendString(b, argv[ii]); Buffer_appendString(b, " "); } expression = b->data; } else{ expression = defaultExpression; } Logging_debugf("Input string: \"%s\"", expression); e = new_Expr(expression, variables); result = Expr_evaluate(e); if(result != NULL){ Logging_infof("Expression result: \"%s\"", result); } else{ Logging_warn("Expression result was NULL"); } delete_Vars(globals); delete_Vars(locals); delete_Expr(e); delete_Buffer(b); exit(EXIT_SUCCESS); }
pStatement Statement::parse() { if (currentToken().type == TT_NAMESPACE) { nextToken(); _namespace = new_Namespace()->parse(); } else if(currentToken().type == TT_TYPEDEF) { } else if(currentToken().type == TT_STRUCT || currentToken().type == TT_CLASS) { isClass = true; if(currentToken().type == TT_STRUCT) { isStruct = true; } nextToken(); _namespace = new_Namespace(); _namespace->statementsBlock = new_StatementsBlock(); _namespace->id = currentToken().strVal; pType newType = pType(new Type(owner, false) ); pExprResult res = owner->new_ExprResult(); res->evalType = ERT_TYPE; res->type = newType; newType->definition = this; owner->parsingStatementsBlockStack.back()->vars[_namespace->id] = res; _namespace->parse(); if(currentToken().type != TT_SEMICOLON) { Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken(), "Expected semicolon"); } nextToken(); } else if (currentToken().type == TT_BRACE_OPEN) { this->sb = new_StatementsBlock()->parse(); } else if (currentToken().type == TT_BREAK || currentToken().type == TT_RETURN || currentToken().type == TT_ECHO || currentToken().type == TT_CONTINUE) { isSpecial = true; specialType = currentToken().type; nextToken(); if (specialType == TT_RETURN || specialType == TT_ECHO) { expr = new_Expr()->parse(); } if (currentToken().type != TT_SEMICOLON) { throwTokenExpected(TT_SEMICOLON); } nextToken(); } else if (currentToken().type == TT_IF) { isSpecial = true; specialType = currentToken().type; nextToken(); expr = new_Expr()->parse(); statement1 = new_Statement()->parse(); if (currentToken().type == TT_ELSE) { nextToken(); statement2 = new_Statement()->parse(); } } else if (currentToken().type == TT_WHILE) { localStatementsBlock = new_StatementsBlock(); owner->parsingStatementsBlockStack.push_back(localStatementsBlock); isSpecial = true; specialType = currentToken().type; if (nextToken().type != TT_PARENTHESIS_OPEN) { Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() ); } nextToken(); expr = new_Expr()->parse(); if (currentToken().type != TT_PARENTHESIS_CLOSE) { Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() ); } nextToken(); statement1 = new_Statement()->parse(); owner->parsingStatementsBlockStack.pop_back(); } else if (currentToken().type == TT_FOR) { localStatementsBlock = new_StatementsBlock(); owner->parsingStatementsBlockStack.push_back(localStatementsBlock); isSpecial = true; specialType = currentToken().type; if (nextToken().type != TT_PARENTHESIS_OPEN) { Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() ); } nextToken(); statement1 = new_Statement()->parse(); expr = new_Expr()->parse(); if (currentToken().type != TT_SEMICOLON) { Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() ); } nextToken(); expr2 = new_Expr()->parse(); if (currentToken().type != TT_PARENTHESIS_CLOSE) { Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() ); } nextToken(); statement2 = new_Statement()->parse(); owner->parsingStatementsBlockStack.pop_back(); } else { tryParse([this]() { tn = new_Typename()->parse(); pVarDeclarationAllowDefault vd = new_VarDeclarationAllowDefault()->parse(); if (owner->currentToken().type == TT_PARENTHESIS_OPEN) { if (!vd->canBeFunctionDeclaration() ) { Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() ); } nextToken(); this->function = pFunction(new Function(owner, vd->vd->id) ); this->function->args = new_FunctionArgs()->parse(); if (currentToken().type == TT_PARENTHESIS_CLOSE) { if (nextToken().type == ';') { this->function->statementsBlock = nullptr; } else { this->function->statementsBlock = new_StatementsBlock()->parse(); for (auto vd: this->function->args->vds) { this->function->statementsBlock->vars[vd->vd->id] = nullptr; } } } else { Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken(), "Unexpeted token"); } } else { if (currentToken().type == TT_COMMA || currentToken().type == TT_SEMICOLON) { vds.push_back(vd); } while (currentToken().type == TT_COMMA) { nextToken(); vds.push_back(new_VarDeclarationAllowDefault()->parse() ); } if (currentToken().type != TT_SEMICOLON) { Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() ); } nextToken(); } }, [this](exception &e) { vds.clear(); tn = nullptr; expr = new_Expr()->parse(); this->function = nullptr; if (currentToken().type != TT_SEMICOLON) { Parser::get()->parsingException(__FUNCTION__, __FILE__, __LINE__, currentToken() ); } nextToken(); }); } return pStatement(this); }