Huge speed boost by getting rid of unnecessary match setup and teardown.

This commit is contained in:
Jonathan Turner 2009-06-29 13:23:12 +00:00
parent 88e0d15efd
commit 85f9d049eb
2 changed files with 163 additions and 196 deletions

View File

@ -3,8 +3,8 @@ cmake_minimum_required(VERSION 2.6)
project(chaioop) project(chaioop)
SET (CMAKE_BUILD_TYPE gdb) SET (CMAKE_BUILD_TYPE gdb)
SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb -O3") SET (CMAKE_C_FLAGS_GDB " -Wall -ggdb")
SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb -O3") SET (CMAKE_CXX_FLAGS_GDB " -Wall -ggdb")
include_directories(../langkit ../dispatchkit) include_directories(../langkit ../dispatchkit)

View File

@ -11,7 +11,7 @@ namespace langkit {
struct File_Position { struct File_Position {
int line; int line;
int column; int column;
std::string::iterator text_pos; // std::string::iterator text_pos;
File_Position(int file_line, int file_column) File_Position(int file_line, int file_column)
: line(file_line), column(file_column) { } : line(file_line), column(file_column) { }
@ -86,40 +86,12 @@ namespace langkit {
match_stack.clear(); match_stack.clear();
} }
bool Start_Parse() { void build_match(Token_Type::Type match_type, int match_start) {
TokenPtr t(new Token("", Token_Type::Internal_Match_Begin, filename)); //so we want to take everything to the right of this and make them children
TokenPtr t(new Token("", match_type, filename, match_stack[match_start]->start.line, match_stack[match_start]->start.column, line, col));
t->children.assign(match_stack.begin() + (match_start), match_stack.end());
match_stack.erase(match_stack.begin() + (match_start), match_stack.end());
match_stack.push_back(t); match_stack.push_back(t);
t->start.line = line;
t->start.column = col;
t->start.text_pos = input_pos;
return true;
}
void Fail_Parse() {
TokenPtr t = match_stack.back(); match_stack.pop_back();
while (t->identifier != Token_Type::Internal_Match_Begin) {
t = match_stack.back(); match_stack.pop_back();
}
line = t->start.line;
col = t->start.column;
input_pos = t->start.text_pos;
}
bool Finish_Parse(int id) {
for (int i = (int)match_stack.size() - 1; i >= 0; --i) {
if (match_stack[i]->identifier == Token_Type::Internal_Match_Begin) {
//so we want to take everything to the right of this and make them children
match_stack[i]->children.insert(match_stack[i]->children.begin(), match_stack.begin() + (i+1), match_stack.end());
//match_stack[i]->children.assign(match_stack.begin() + (i+1), match_stack.end());
match_stack.erase(match_stack.begin() + (i+1), match_stack.end());
match_stack[i]->identifier = id;
return true;
}
}
throw Parse_Error("Could not find internal begin parse marker", File_Position());
return false;
} }
bool SkipComment() { bool SkipComment() {
@ -195,12 +167,7 @@ namespace langkit {
int prev_line = line; int prev_line = line;
if (Int_()) { if (Int_()) {
std::string match(start, input_pos); std::string match(start, input_pos);
TokenPtr t(new Token(match, Token_Type::Int, filename, prev_col, prev_line, col, line)); TokenPtr t(new Token(match, Token_Type::Int, filename, prev_line, prev_col, line, col));
/*
t->start.column = prev_col;
t->start.line = prev_line;
t->end.column = col;
t->end.line = line;*/
match_stack.push_back(t); match_stack.push_back(t);
return true; return true;
} }
@ -235,12 +202,7 @@ namespace langkit {
int prev_line = line; int prev_line = line;
if (Id_()) { if (Id_()) {
std::string match(start, input_pos); std::string match(start, input_pos);
TokenPtr t(new Token(match, Token_Type::Id, filename, prev_col, prev_line, col, line)); TokenPtr t(new Token(match, Token_Type::Id, filename, prev_line, prev_col, line, col));
/*
t->start.column = prev_col;
t->start.line = prev_line;
t->end.column = col;
t->end.line = line; */
match_stack.push_back(t); match_stack.push_back(t);
return true; return true;
} }
@ -273,12 +235,7 @@ namespace langkit {
int prev_line = line; int prev_line = line;
if (Char_(c)) { if (Char_(c)) {
std::string match(start, input_pos); std::string match(start, input_pos);
TokenPtr t(new Token(match, Token_Type::Char, filename, prev_col, prev_line, col, line)); TokenPtr t(new Token(match, Token_Type::Char, filename, prev_line, prev_col, line, col));
/*
t->start.column = prev_col;
t->start.line = prev_line;
t->end.column = col;
t->end.line = line; */
match_stack.push_back(t); match_stack.push_back(t);
return true; return true;
} }
@ -320,12 +277,7 @@ namespace langkit {
int prev_line = line; int prev_line = line;
if (Str_(s)) { if (Str_(s)) {
std::string match(start, input_pos); std::string match(start, input_pos);
TokenPtr t(new Token(match, Token_Type::Str, filename, prev_col, prev_line, col, line)); TokenPtr t(new Token(match, Token_Type::Str, filename, prev_line, prev_col, line, col));
/*
t->start.column = prev_col;
t->start.line = prev_line;
t->end.column = col;
t->end.line = line; */
match_stack.push_back(t); match_stack.push_back(t);
return true; return true;
} }
@ -362,12 +314,7 @@ namespace langkit {
int prev_line = line; int prev_line = line;
if (Eol_()) { if (Eol_()) {
std::string match(start, input_pos); std::string match(start, input_pos);
TokenPtr t(new Token(match, Token_Type::Eol, filename, prev_col, prev_line, col, line)); TokenPtr t(new Token(match, Token_Type::Eol, filename, prev_line, prev_col, line, col));
/*
t->start.column = prev_col;
t->start.line = prev_line;
t->end.column = col;
t->end.line = line; */
match_stack.push_back(t); match_stack.push_back(t);
return true; return true;
} }
@ -378,46 +325,45 @@ namespace langkit {
} }
bool Arg_List() { bool Arg_List() {
bool retval; bool retval = false;
Start_Parse(); int prev_stack_top = match_stack.size();
retval = Expression(); if (Expression()) {
while (retval && Char(',')) { retval = true;
retval = Expression(); if (Char(',')) {
if (!retval) { do {
throw Parse_Error("Unexpected value in parameter list", File_Position(line, col)); if (!Expression()) {
throw Parse_Error("Unexpected value in parameter list", File_Position(line, col));
}
} while (retval && Char(','));
}
build_match(Token_Type::Arg_List, prev_stack_top);
}
return retval;
}
bool Fun_Or_Id() {
bool retval = false;
std::string::iterator prev_pos = input_pos;
unsigned int prev_stack_top = match_stack.size();
if (Id(true)) {
retval = true;
if (Char('(')) {
Arg_List();
if (!Char(')')) {
throw Parse_Error("Incomplete function call", File_Position(line, col));
}
build_match(Token_Type::Fun_Call, prev_stack_top);
} }
} }
if (retval) { return retval;
Finish_Parse(Token_Type::Arg_List);
return true;
}
else {
Fail_Parse();
return false;
}
}
bool Fun_Call() {
bool retval = false;
Start_Parse();
if (Id(true) && Char('(')) {
Arg_List();
retval = Char(')');
}
if (retval) {
Finish_Parse(Token_Type::Fun_Call);
return true;
}
else {
Fail_Parse();
return false;
}
} }
bool LHS() { bool LHS() {
@ -430,20 +376,26 @@ namespace langkit {
} }
bool Var_Decl() { bool Var_Decl() {
Start_Parse(); bool retval = false;
if (Str("var") && Id(true)) { int prev_stack_top = match_stack.size();
Finish_Parse(Token_Type::Var_Decl);
return true; if (Str("var")) {
} retval = true;
else {
Fail_Parse(); if (!Id(true)) {
return false; throw Parse_Error("Incomplete variable declaration", File_Position(line, col));
}
build_match(Token_Type::Var_Decl, prev_stack_top);
} }
return retval;
} }
bool Value() { bool Value() {
if (Fun_Call() || Int(true) || Id(true) || Negate() || Not()) { if (Fun_Or_Id() || Int(true) || Negate() || Not()) {
return true; return true;
} }
else { else {
@ -452,134 +404,149 @@ namespace langkit {
} }
bool Negate() { bool Negate() {
Start_Parse(); bool retval = false;
if (Char('-') && Additive()) { int prev_stack_top = match_stack.size();
Finish_Parse(Token_Type::Negate);
return true; if (Char('-')) {
} retval = true;
else {
Fail_Parse(); if (!Additive()) {
return false; throw Parse_Error("Incomplete negation expression", File_Position(line, col));
}
build_match(Token_Type::Negate, prev_stack_top);
} }
return retval;
} }
bool Not() { bool Not() {
Start_Parse(); bool retval = false;
if (Char('!') && Expression()) { int prev_stack_top = match_stack.size();
Finish_Parse(Token_Type::Not);
return true; if (Char('!')) {
} retval = true;
else {
Fail_Parse(); if (!Expression()) {
return false; throw Parse_Error("Incomplete '!' expression", File_Position(line, col));
}
build_match(Token_Type::Not, prev_stack_top);
} }
return retval;
} }
bool Comparison() { bool Comparison() {
bool retval; bool retval = false;
Start_Parse(); int prev_stack_top = match_stack.size();
if ( (retval = (Additive() && ((Str(">=", true) || Char('>', true) || Str("<=", true) || Char('<', true) || Str("==", true) || Str("!=", true))))) ) { if (Additive()) {
do { retval = true;
retval = Additive(); if (Str(">=", true) || Char('>', true) || Str("<=", true) || Char('<', true) || Str("==", true) || Str("!=", true)) {
} while (retval && ((Str(">=", true) || Char('>', true) || Str("<=", true) || Char('<', true) || Str("==", true) || Str("!=", true)))); do {
if (!Additive()) {
throw Parse_Error("Incomplete comparison expression", File_Position(line, col));
}
} while (retval && (Str(">=", true) || Char('>', true) || Str("<=", true) || Char('<', true) || Str("==", true) || Str("!=", true)));
build_match(Token_Type::Comparison, prev_stack_top);
}
} }
if (retval) { return retval;
Finish_Parse(Token_Type::Comparison);
return true;
}
else {
Fail_Parse();
return Additive();
}
} }
bool Additive() { bool Additive() {
bool retval; bool retval = false;
Start_Parse(); int prev_stack_top = match_stack.size();
if ( (retval = (Multiplicative() && (Char('+', true) || Char('-', true)))) ) { if (Multiplicative()) {
do { retval = true;
retval = Multiplicative(); if (Char('+', true) || Char('-', true)) {
} while (retval && (Char('+', true) || Char('-', true))); do {
if (!Multiplicative()) {
throw Parse_Error("Incomplete math expression", File_Position(line, col));
}
} while (retval && (Char('+', true) || Char('-', true)));
build_match(Token_Type::Additive, prev_stack_top);
}
} }
if (retval) { return retval;
Finish_Parse(Token_Type::Additive);
return true;
}
else {
Fail_Parse();
return Multiplicative();
}
} }
bool Multiplicative() { bool Multiplicative() {
bool retval; bool retval = false;
Start_Parse(); int prev_stack_top = match_stack.size();
if ( (retval = (Value() && (Char('*', true) || Char('/', true)))) ) { if (Value()) {
do { retval = true;
retval = Value(); if (Char('*', true) || Char('/', true)) {
} while (retval && (Char('*', true) || Char('/', true))); do {
if (!Value()) {
throw Parse_Error("Incomplete math expression", File_Position(line, col));
}
} while (retval && (Char('*', true) || Char('/', true)));
build_match(Token_Type::Multiplicative, prev_stack_top);
}
} }
if (retval) { return retval;
Finish_Parse(Token_Type::Multiplicative);
return true;
}
else {
Fail_Parse();
return Value();
}
} }
bool Expression() { bool Expression() {
bool retval; bool retval = false;
Start_Parse(); int prev_stack_top = match_stack.size();
if ( (retval = (Comparison() && (Str("&&", true) || Str("||", true)))) ) { if (Comparison()) {
do { retval = true;
retval = Comparison(); if (Str("&&", true) || Str("||", true)) {
} while (retval && (Str("&&", true) || Str("||", true))); do {
if (!Comparison()) {
throw Parse_Error("Incomplete expression", File_Position(line, col));
}
} while (retval && (Str("&&", true) || Str("||", true)));
build_match(Token_Type::Expression, prev_stack_top);
}
} }
if (retval) { return retval;
Finish_Parse(Token_Type::Expression);
return true;
}
else {
Fail_Parse();
return Comparison();
}
} }
bool Equation() { bool Equation() {
bool retval; bool retval = false;
Start_Parse();
if ( (retval = (Expression() && (Char('=', true)))) ) { int prev_stack_top = match_stack.size();
do {
retval = Expression(); if (Expression()) {
} while (retval && (Char('=', true))); retval = true;
if (Char('=', true)) {
do {
if (!Expression()) {
throw Parse_Error("Incomplete math expression", File_Position(line, col));
}
} while (retval && (Char('=', true)));
build_match(Token_Type::Equation, prev_stack_top);
}
} }
return retval;
if (retval) {
Finish_Parse(Token_Type::Equation);
return true;
}
else {
Fail_Parse();
return Expression();
}
} }
bool Statement() { bool Statement() {